requests_test.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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/router"
  10. )
  11. func TestDoRequest(t *testing.T) {
  12. svr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  13. }))
  14. defer svr.Close()
  15. req, err := http.NewRequest(http.MethodGet, svr.URL, nil)
  16. assert.Nil(t, err)
  17. resp, err := DoRequest(req)
  18. assert.Nil(t, err)
  19. assert.Equal(t, http.StatusOK, resp.StatusCode)
  20. }
  21. func TestDoRequest_NotFound(t *testing.T) {
  22. svr := httptest.NewServer(http.NotFoundHandler())
  23. defer svr.Close()
  24. req, err := http.NewRequest(http.MethodPost, svr.URL, nil)
  25. assert.Nil(t, err)
  26. req.Header.Set("Content-Type", "application/json")
  27. resp, err := DoRequest(req)
  28. assert.Nil(t, err)
  29. assert.Equal(t, http.StatusNotFound, resp.StatusCode)
  30. }
  31. func TestDoRequest_Moved(t *testing.T) {
  32. svr := httptest.NewServer(http.RedirectHandler("/foo", http.StatusMovedPermanently))
  33. defer svr.Close()
  34. req, err := http.NewRequest(http.MethodGet, svr.URL, nil)
  35. assert.Nil(t, err)
  36. _, err = DoRequest(req)
  37. // too many redirects
  38. assert.NotNil(t, err)
  39. }
  40. func TestDo(t *testing.T) {
  41. type Data struct {
  42. Key string `path:"key"`
  43. Value int `form:"value"`
  44. Header string `header:"X-Header"`
  45. Body string `json:"body"`
  46. }
  47. rt := router.NewRouter()
  48. err := rt.Handle(http.MethodPost, "/nodes/:key",
  49. http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  50. var req Data
  51. assert.Nil(t, httpx.Parse(r, &req))
  52. }))
  53. assert.Nil(t, err)
  54. svr := httptest.NewServer(http.HandlerFunc(rt.ServeHTTP))
  55. defer svr.Close()
  56. data := Data{
  57. Key: "foo",
  58. Value: 10,
  59. Header: "my-header",
  60. Body: "my body",
  61. }
  62. resp, err := Do(context.Background(), http.MethodPost, svr.URL+"/nodes/:key", data)
  63. assert.Nil(t, err)
  64. assert.Equal(t, http.StatusOK, resp.StatusCode)
  65. }
  66. func TestDo_Ptr(t *testing.T) {
  67. type Data struct {
  68. Key string `path:"key"`
  69. Value int `form:"value"`
  70. Header string `header:"X-Header"`
  71. Body string `json:"body"`
  72. }
  73. rt := router.NewRouter()
  74. err := rt.Handle(http.MethodPost, "/nodes/:key",
  75. http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  76. var req Data
  77. assert.Nil(t, httpx.Parse(r, &req))
  78. assert.Equal(t, "foo", req.Key)
  79. assert.Equal(t, 10, req.Value)
  80. assert.Equal(t, "my-header", req.Header)
  81. assert.Equal(t, "my body", req.Body)
  82. }))
  83. assert.Nil(t, err)
  84. svr := httptest.NewServer(http.HandlerFunc(rt.ServeHTTP))
  85. defer svr.Close()
  86. data := &Data{
  87. Key: "foo",
  88. Value: 10,
  89. Header: "my-header",
  90. Body: "my body",
  91. }
  92. resp, err := Do(context.Background(), http.MethodPost, svr.URL+"/nodes/:key", data)
  93. assert.Nil(t, err)
  94. assert.Equal(t, http.StatusOK, resp.StatusCode)
  95. }
  96. func TestDo_BadRequest(t *testing.T) {
  97. _, err := Do(context.Background(), http.MethodPost, ":/nodes/:key", nil)
  98. assert.NotNil(t, err)
  99. val1 := struct {
  100. Value string `json:"value,options=[a,b]"`
  101. }{
  102. Value: "c",
  103. }
  104. _, err = Do(context.Background(), http.MethodPost, "/nodes/:key", val1)
  105. assert.NotNil(t, err)
  106. val2 := struct {
  107. Value string `path:"val"`
  108. }{
  109. Value: "",
  110. }
  111. _, err = Do(context.Background(), http.MethodPost, "/nodes/:key", val2)
  112. assert.NotNil(t, err)
  113. val3 := struct {
  114. Value string `path:"key"`
  115. Body string `json:"body"`
  116. }{
  117. Value: "foo",
  118. }
  119. _, err = Do(context.Background(), http.MethodGet, "/nodes/:key", val3)
  120. assert.NotNil(t, err)
  121. _, err = Do(context.Background(), "\n", "rtmp://nodes", nil)
  122. assert.NotNil(t, err)
  123. val4 := struct {
  124. Value string `path:"val"`
  125. }{
  126. Value: "",
  127. }
  128. _, err = Do(context.Background(), http.MethodPost, "/nodes/:val", val4)
  129. assert.NotNil(t, err)
  130. val5 := struct {
  131. Value string `path:"val"`
  132. Another int `path:"foo"`
  133. }{
  134. Value: "1",
  135. Another: 2,
  136. }
  137. _, err = Do(context.Background(), http.MethodPost, "/nodes/:val", val5)
  138. assert.NotNil(t, err)
  139. }