requests_test.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. err = Parse(r, &v)
  19. assert.Nil(t, err)
  20. assert.Equal(t, "hello", v.Name)
  21. assert.Equal(t, 18, v.Age)
  22. assert.Equal(t, 3.4, v.Percent)
  23. }
  24. func TestParseFormOutOfRange(t *testing.T) {
  25. var v struct {
  26. Age int `form:"age,range=[10:20)"`
  27. }
  28. tests := []struct {
  29. url string
  30. pass bool
  31. }{
  32. {
  33. url: "http://hello.com/a?age=5",
  34. pass: false,
  35. },
  36. {
  37. url: "http://hello.com/a?age=10",
  38. pass: true,
  39. },
  40. {
  41. url: "http://hello.com/a?age=15",
  42. pass: true,
  43. },
  44. {
  45. url: "http://hello.com/a?age=20",
  46. pass: false,
  47. },
  48. {
  49. url: "http://hello.com/a?age=28",
  50. pass: false,
  51. },
  52. }
  53. for _, test := range tests {
  54. r, err := http.NewRequest(http.MethodGet, test.url, nil)
  55. assert.Nil(t, err)
  56. err = Parse(r, &v)
  57. if test.pass {
  58. assert.Nil(t, err)
  59. } else {
  60. assert.NotNil(t, err)
  61. }
  62. }
  63. }
  64. func TestParseMultipartForm(t *testing.T) {
  65. var v struct {
  66. Name string `form:"name"`
  67. Age int `form:"age"`
  68. }
  69. body := strings.Replace(`----------------------------220477612388154780019383
  70. Content-Disposition: form-data; name="name"
  71. kevin
  72. ----------------------------220477612388154780019383
  73. Content-Disposition: form-data; name="age"
  74. 18
  75. ----------------------------220477612388154780019383--`, "\n", "\r\n", -1)
  76. r := httptest.NewRequest(http.MethodPost, "http://localhost:3333/", strings.NewReader(body))
  77. r.Header.Set(ContentType, "multipart/form-data; boundary=--------------------------220477612388154780019383")
  78. err := Parse(r, &v)
  79. assert.Nil(t, err)
  80. assert.Equal(t, "kevin", v.Name)
  81. assert.Equal(t, 18, v.Age)
  82. }
  83. func TestParseRequired(t *testing.T) {
  84. v := struct {
  85. Name string `form:"name"`
  86. Percent float64 `form:"percent"`
  87. }{}
  88. r, err := http.NewRequest(http.MethodGet, "http://hello.com/a?name=hello", nil)
  89. assert.Nil(t, err)
  90. err = Parse(r, &v)
  91. assert.NotNil(t, err)
  92. }
  93. func TestParseOptions(t *testing.T) {
  94. v := struct {
  95. Position int8 `form:"pos,options=1|2"`
  96. }{}
  97. r, err := http.NewRequest(http.MethodGet, "http://hello.com/a?pos=4", nil)
  98. assert.Nil(t, err)
  99. err = Parse(r, &v)
  100. assert.NotNil(t, err)
  101. }
  102. func BenchmarkParseRaw(b *testing.B) {
  103. r, err := http.NewRequest(http.MethodGet, "http://hello.com/a?name=hello&age=18&percent=3.4", nil)
  104. if err != nil {
  105. b.Fatal(err)
  106. }
  107. for i := 0; i < b.N; i++ {
  108. v := struct {
  109. Name string `form:"name"`
  110. Age int `form:"age"`
  111. Percent float64 `form:"percent,optional"`
  112. }{}
  113. v.Name = r.FormValue("name")
  114. v.Age, err = strconv.Atoi(r.FormValue("age"))
  115. if err != nil {
  116. b.Fatal(err)
  117. }
  118. v.Percent, err = strconv.ParseFloat(r.FormValue("percent"), 64)
  119. if err != nil {
  120. b.Fatal(err)
  121. }
  122. }
  123. }
  124. func BenchmarkParseAuto(b *testing.B) {
  125. r, err := http.NewRequest(http.MethodGet, "http://hello.com/a?name=hello&age=18&percent=3.4", nil)
  126. if err != nil {
  127. b.Fatal(err)
  128. }
  129. for i := 0; i < b.N; i++ {
  130. v := struct {
  131. Name string `form:"name"`
  132. Age int `form:"age"`
  133. Percent float64 `form:"percent,optional"`
  134. }{}
  135. if err = Parse(r, &v); err != nil {
  136. b.Fatal(err)
  137. }
  138. }
  139. }