util_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. package server
  2. import (
  3. "bytes"
  4. "crypto/rand"
  5. "fmt"
  6. "github.com/stretchr/testify/require"
  7. "net/http"
  8. "strings"
  9. "testing"
  10. )
  11. func TestReadBoolParam(t *testing.T) {
  12. r, _ := http.NewRequest("GET", "https://ntfy.sh/mytopic?up=1&firebase=no", nil)
  13. up := readBoolParam(r, false, "x-up", "up")
  14. firebase := readBoolParam(r, true, "x-firebase", "firebase")
  15. require.Equal(t, true, up)
  16. require.Equal(t, false, firebase)
  17. r, _ = http.NewRequest("GET", "https://ntfy.sh/mytopic", nil)
  18. r.Header.Set("X-Up", "yes")
  19. r.Header.Set("X-Firebase", "0")
  20. up = readBoolParam(r, false, "x-up", "up")
  21. firebase = readBoolParam(r, true, "x-firebase", "firebase")
  22. require.Equal(t, true, up)
  23. require.Equal(t, false, firebase)
  24. r, _ = http.NewRequest("GET", "https://ntfy.sh/mytopic", nil)
  25. up = readBoolParam(r, false, "x-up", "up")
  26. firebase = readBoolParam(r, true, "x-up", "up")
  27. require.Equal(t, false, up)
  28. require.Equal(t, true, firebase)
  29. }
  30. func TestRenderHTTPRequest_ValidShort(t *testing.T) {
  31. r, _ := http.NewRequest("POST", "http://ntfy.sh/mytopic?p=2", strings.NewReader("some message"))
  32. r.Header.Set("Title", "A title")
  33. expected := `POST /mytopic?p=2 HTTP/1.1
  34. Title: A title
  35. some message`
  36. require.Equal(t, expected, renderHTTPRequest(r))
  37. }
  38. func TestRenderHTTPRequest_ValidLong(t *testing.T) {
  39. body := strings.Repeat("a", 5000)
  40. r, _ := http.NewRequest("POST", "http://ntfy.sh/mytopic?p=2", strings.NewReader(body))
  41. r.Header.Set("Accept", "*/*")
  42. expected := `POST /mytopic?p=2 HTTP/1.1
  43. Accept: */*
  44. ` + strings.Repeat("a", 4096) + " ... (peeked 4096 bytes)"
  45. require.Equal(t, expected, renderHTTPRequest(r))
  46. }
  47. func TestRenderHTTPRequest_InvalidShort(t *testing.T) {
  48. body := []byte{0xc3, 0x28}
  49. r, _ := http.NewRequest("GET", "http://ntfy.sh/mytopic/json?since=all", bytes.NewReader(body))
  50. r.Header.Set("Accept", "*/*")
  51. expected := `GET /mytopic/json?since=all HTTP/1.1
  52. Accept: */*
  53. (peeked bytes not UTF-8, 2 bytes, hex: c328)`
  54. require.Equal(t, expected, renderHTTPRequest(r))
  55. }
  56. func TestRenderHTTPRequest_InvalidLong(t *testing.T) {
  57. body := make([]byte, 5000)
  58. rand.Read(body)
  59. r, _ := http.NewRequest("GET", "http://ntfy.sh/mytopic/json?since=all", bytes.NewReader(body))
  60. r.Header.Set("Accept", "*/*")
  61. expected := `GET /mytopic/json?since=all HTTP/1.1
  62. Accept: */*
  63. (peeked bytes not UTF-8, peek limit of 4096 bytes reached, hex: ` + fmt.Sprintf("%x", body[:4096]) + ` ...)`
  64. require.Equal(t, expected, renderHTTPRequest(r))
  65. }
  66. func TestMaybeIgnoreSpecialHeader(t *testing.T) {
  67. require.Empty(t, maybeIgnoreSpecialHeader("priority", "u=1"))
  68. require.Empty(t, maybeIgnoreSpecialHeader("Priority", "u=1"))
  69. require.Empty(t, maybeIgnoreSpecialHeader("Priority", "u=1, i"))
  70. }
  71. func TestMaybeDecodeHeaders(t *testing.T) {
  72. r, _ := http.NewRequest("GET", "http://ntfy.sh/mytopic/json?since=all", nil)
  73. r.Header.Set("Priority", "u=1") // Cloudflare priority header
  74. r.Header.Set("X-Priority", "5") // ntfy priority header
  75. require.Equal(t, "5", readHeaderParam(r, "x-priority", "priority", "p"))
  76. }