requests_test.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. package httpx
  2. import (
  3. "net/http"
  4. "net/http/httptest"
  5. "strconv"
  6. "strings"
  7. "testing"
  8. "github.com/stretchr/testify/assert"
  9. )
  10. func TestParseForm(t *testing.T) {
  11. var v struct {
  12. Name string `form:"name"`
  13. Age int `form:"age"`
  14. Percent float64 `form:"percent,optional"`
  15. }
  16. r, err := http.NewRequest(http.MethodGet, "http://hello.com/a?name=hello&age=18&percent=3.4", nil)
  17. assert.Nil(t, err)
  18. assert.Nil(t, Parse(r, &v))
  19. assert.Equal(t, "hello", v.Name)
  20. assert.Equal(t, 18, v.Age)
  21. assert.Equal(t, 3.4, v.Percent)
  22. }
  23. func TestParseHeader(t *testing.T) {
  24. m := ParseHeader("key=value;")
  25. assert.EqualValues(t, map[string]string{
  26. "key": "value",
  27. }, m)
  28. }
  29. func TestParseFormOutOfRange(t *testing.T) {
  30. var v struct {
  31. Age int `form:"age,range=[10:20)"`
  32. }
  33. tests := []struct {
  34. url string
  35. pass bool
  36. }{
  37. {
  38. url: "http://hello.com/a?age=5",
  39. pass: false,
  40. },
  41. {
  42. url: "http://hello.com/a?age=10",
  43. pass: true,
  44. },
  45. {
  46. url: "http://hello.com/a?age=15",
  47. pass: true,
  48. },
  49. {
  50. url: "http://hello.com/a?age=20",
  51. pass: false,
  52. },
  53. {
  54. url: "http://hello.com/a?age=28",
  55. pass: false,
  56. },
  57. }
  58. for _, test := range tests {
  59. r, err := http.NewRequest(http.MethodGet, test.url, nil)
  60. assert.Nil(t, err)
  61. err = Parse(r, &v)
  62. if test.pass {
  63. assert.Nil(t, err)
  64. } else {
  65. assert.NotNil(t, err)
  66. }
  67. }
  68. }
  69. func TestParseMultipartForm(t *testing.T) {
  70. var v struct {
  71. Name string `form:"name"`
  72. Age int `form:"age"`
  73. }
  74. body := strings.Replace(`----------------------------220477612388154780019383
  75. Content-Disposition: form-data; name="name"
  76. kevin
  77. ----------------------------220477612388154780019383
  78. Content-Disposition: form-data; name="age"
  79. 18
  80. ----------------------------220477612388154780019383--`, "\n", "\r\n", -1)
  81. r := httptest.NewRequest(http.MethodPost, "http://localhost:3333/", strings.NewReader(body))
  82. r.Header.Set(ContentType, "multipart/form-data; boundary=--------------------------220477612388154780019383")
  83. assert.Nil(t, Parse(r, &v))
  84. assert.Equal(t, "kevin", v.Name)
  85. assert.Equal(t, 18, v.Age)
  86. }
  87. func TestParseMultipartFormWrongBoundary(t *testing.T) {
  88. var v struct {
  89. Name string `form:"name"`
  90. Age int `form:"age"`
  91. }
  92. body := strings.Replace(`----------------------------22047761238815478001938
  93. Content-Disposition: form-data; name="name"
  94. kevin
  95. ----------------------------22047761238815478001938
  96. Content-Disposition: form-data; name="age"
  97. 18
  98. ----------------------------22047761238815478001938--`, "\n", "\r\n", -1)
  99. r := httptest.NewRequest(http.MethodPost, "http://localhost:3333/", strings.NewReader(body))
  100. r.Header.Set(ContentType, "multipart/form-data; boundary=--------------------------220477612388154780019383")
  101. assert.NotNil(t, Parse(r, &v))
  102. }
  103. func TestParseJsonBody(t *testing.T) {
  104. var v struct {
  105. Name string `json:"name"`
  106. Age int `json:"age"`
  107. }
  108. body := `{"name":"kevin", "age": 18}`
  109. r := httptest.NewRequest(http.MethodPost, "http://localhost:3333/", strings.NewReader(body))
  110. r.Header.Set(ContentType, ApplicationJson)
  111. assert.Nil(t, Parse(r, &v))
  112. assert.Equal(t, "kevin", v.Name)
  113. assert.Equal(t, 18, v.Age)
  114. }
  115. func TestParseRequired(t *testing.T) {
  116. v := struct {
  117. Name string `form:"name"`
  118. Percent float64 `form:"percent"`
  119. }{}
  120. r, err := http.NewRequest(http.MethodGet, "http://hello.com/a?name=hello", nil)
  121. assert.Nil(t, err)
  122. assert.NotNil(t, Parse(r, &v))
  123. }
  124. func TestParseOptions(t *testing.T) {
  125. v := struct {
  126. Position int8 `form:"pos,options=1|2"`
  127. }{}
  128. r, err := http.NewRequest(http.MethodGet, "http://hello.com/a?pos=4", nil)
  129. assert.Nil(t, err)
  130. assert.NotNil(t, Parse(r, &v))
  131. }
  132. func BenchmarkParseRaw(b *testing.B) {
  133. r, err := http.NewRequest(http.MethodGet, "http://hello.com/a?name=hello&age=18&percent=3.4", nil)
  134. if err != nil {
  135. b.Fatal(err)
  136. }
  137. for i := 0; i < b.N; i++ {
  138. v := struct {
  139. Name string `form:"name"`
  140. Age int `form:"age"`
  141. Percent float64 `form:"percent,optional"`
  142. }{}
  143. v.Name = r.FormValue("name")
  144. v.Age, err = strconv.Atoi(r.FormValue("age"))
  145. if err != nil {
  146. b.Fatal(err)
  147. }
  148. v.Percent, err = strconv.ParseFloat(r.FormValue("percent"), 64)
  149. if err != nil {
  150. b.Fatal(err)
  151. }
  152. }
  153. }
  154. func BenchmarkParseAuto(b *testing.B) {
  155. r, err := http.NewRequest(http.MethodGet, "http://hello.com/a?name=hello&age=18&percent=3.4", nil)
  156. if err != nil {
  157. b.Fatal(err)
  158. }
  159. for i := 0; i < b.N; i++ {
  160. v := struct {
  161. Name string `form:"name"`
  162. Age int `form:"age"`
  163. Percent float64 `form:"percent,optional"`
  164. }{}
  165. if err = Parse(r, &v); err != nil {
  166. b.Fatal(err)
  167. }
  168. }
  169. }