requests_test.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. package httpc
  2. import (
  3. "context"
  4. "net/http"
  5. "net/http/httptest"
  6. "testing"
  7. "github.com/stretchr/testify/assert"
  8. "github.com/zeromicro/go-zero/rest/httpx"
  9. "github.com/zeromicro/go-zero/rest/internal/header"
  10. "github.com/zeromicro/go-zero/rest/router"
  11. )
  12. func TestDoRequest(t *testing.T) {
  13. svr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  14. }))
  15. defer svr.Close()
  16. req, err := http.NewRequest(http.MethodGet, svr.URL, nil)
  17. assert.Nil(t, err)
  18. resp, err := DoRequest(req)
  19. assert.Nil(t, err)
  20. assert.Equal(t, http.StatusOK, resp.StatusCode)
  21. }
  22. func TestDoRequest_NotFound(t *testing.T) {
  23. svr := httptest.NewServer(http.NotFoundHandler())
  24. defer svr.Close()
  25. req, err := http.NewRequest(http.MethodPost, svr.URL, nil)
  26. assert.Nil(t, err)
  27. req.Header.Set(header.ContentType, header.JsonContentType)
  28. resp, err := DoRequest(req)
  29. assert.Nil(t, err)
  30. assert.Equal(t, http.StatusNotFound, resp.StatusCode)
  31. }
  32. func TestDoRequest_Moved(t *testing.T) {
  33. svr := httptest.NewServer(http.RedirectHandler("/foo", http.StatusMovedPermanently))
  34. defer svr.Close()
  35. req, err := http.NewRequest(http.MethodGet, svr.URL, nil)
  36. assert.Nil(t, err)
  37. _, err = DoRequest(req)
  38. // too many redirects
  39. assert.NotNil(t, err)
  40. }
  41. func TestDo(t *testing.T) {
  42. type Data struct {
  43. Key string `path:"key"`
  44. Value int `form:"value"`
  45. Header string `header:"X-Header"`
  46. Body string `json:"body"`
  47. }
  48. rt := router.NewRouter()
  49. err := rt.Handle(http.MethodPost, "/nodes/:key",
  50. http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  51. var req Data
  52. assert.Nil(t, httpx.Parse(r, &req))
  53. }))
  54. assert.Nil(t, err)
  55. svr := httptest.NewServer(http.HandlerFunc(rt.ServeHTTP))
  56. defer svr.Close()
  57. data := Data{
  58. Key: "foo",
  59. Value: 10,
  60. Header: "my-header",
  61. Body: "my body",
  62. }
  63. resp, err := Do(context.Background(), http.MethodPost, svr.URL+"/nodes/:key", data)
  64. assert.Nil(t, err)
  65. assert.Equal(t, http.StatusOK, resp.StatusCode)
  66. }
  67. func TestDo_Ptr(t *testing.T) {
  68. type Data struct {
  69. Key string `path:"key"`
  70. Value int `form:"value"`
  71. Header string `header:"X-Header"`
  72. Body string `json:"body"`
  73. }
  74. rt := router.NewRouter()
  75. err := rt.Handle(http.MethodPost, "/nodes/:key",
  76. http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  77. var req Data
  78. assert.Nil(t, httpx.Parse(r, &req))
  79. assert.Equal(t, "foo", req.Key)
  80. assert.Equal(t, 10, req.Value)
  81. assert.Equal(t, "my-header", req.Header)
  82. assert.Equal(t, "my body", req.Body)
  83. }))
  84. assert.Nil(t, err)
  85. svr := httptest.NewServer(http.HandlerFunc(rt.ServeHTTP))
  86. defer svr.Close()
  87. data := &Data{
  88. Key: "foo",
  89. Value: 10,
  90. Header: "my-header",
  91. Body: "my body",
  92. }
  93. resp, err := Do(context.Background(), http.MethodPost, svr.URL+"/nodes/:key", data)
  94. assert.Nil(t, err)
  95. assert.Equal(t, http.StatusOK, resp.StatusCode)
  96. }
  97. func TestDo_BadRequest(t *testing.T) {
  98. _, err := Do(context.Background(), http.MethodPost, ":/nodes/:key", nil)
  99. assert.NotNil(t, err)
  100. val1 := struct {
  101. Value string `json:"value,options=[a,b]"`
  102. }{
  103. Value: "c",
  104. }
  105. _, err = Do(context.Background(), http.MethodPost, "/nodes/:key", val1)
  106. assert.NotNil(t, err)
  107. val2 := struct {
  108. Value string `path:"val"`
  109. }{
  110. Value: "",
  111. }
  112. _, err = Do(context.Background(), http.MethodPost, "/nodes/:key", val2)
  113. assert.NotNil(t, err)
  114. val3 := struct {
  115. Value string `path:"key"`
  116. Body string `json:"body"`
  117. }{
  118. Value: "foo",
  119. }
  120. _, err = Do(context.Background(), http.MethodGet, "/nodes/:key", val3)
  121. assert.NotNil(t, err)
  122. _, err = Do(context.Background(), "\n", "rtmp://nodes", nil)
  123. assert.NotNil(t, err)
  124. val4 := struct {
  125. Value string `path:"val"`
  126. }{
  127. Value: "",
  128. }
  129. _, err = Do(context.Background(), http.MethodPost, "/nodes/:val", val4)
  130. assert.NotNil(t, err)
  131. val5 := struct {
  132. Value string `path:"val"`
  133. Another int `path:"foo"`
  134. }{
  135. Value: "1",
  136. Another: 2,
  137. }
  138. _, err = Do(context.Background(), http.MethodPost, "/nodes/:val", val5)
  139. assert.NotNil(t, err)
  140. }
  141. func TestDo_Json(t *testing.T) {
  142. type Data struct {
  143. Key string `path:"key"`
  144. Value int `form:"value"`
  145. Header string `header:"X-Header"`
  146. Body chan int `json:"body"`
  147. }
  148. rt := router.NewRouter()
  149. err := rt.Handle(http.MethodPost, "/nodes/:key",
  150. http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  151. var req Data
  152. assert.Nil(t, httpx.Parse(r, &req))
  153. }))
  154. assert.Nil(t, err)
  155. svr := httptest.NewServer(http.HandlerFunc(rt.ServeHTTP))
  156. defer svr.Close()
  157. data := Data{
  158. Key: "foo",
  159. Value: 10,
  160. Header: "my-header",
  161. Body: make(chan int),
  162. }
  163. _, err = Do(context.Background(), http.MethodPost, svr.URL+"/nodes/:key", data)
  164. assert.NotNil(t, err)
  165. }