github.com/jxskiss/gopkg/v2@v2.14.9-0.20240514120614-899f3e7952b4/easy/ezhttp/request_test.go (about) 1 package ezhttp 2 3 import ( 4 "net/http" 5 "net/http/httptest" 6 "net/http/httputil" 7 "strings" 8 "testing" 9 "time" 10 11 "github.com/stretchr/testify/assert" 12 "github.com/stretchr/testify/require" 13 ) 14 15 func testHttpHandler(w http.ResponseWriter, r *http.Request) { 16 dump, err := httputil.DumpRequest(r, true) 17 if err != nil { 18 panic(err) 19 } 20 w.Write(dump) 21 } 22 23 func TestDo(t *testing.T) { 24 s := httptest.NewServer(http.HandlerFunc(testHttpHandler)) 25 defer s.Close() 26 data := "test Do Request" 27 req, _ := http.NewRequest("POST", s.URL, strings.NewReader(data)) 28 29 var respText []byte 30 var status int 31 var err error 32 33 _, respText, status, err = Do(&Request{ 34 Req: req, 35 Timeout: time.Second, 36 DumpRequest: true, 37 DumpResponse: true, 38 }) 39 40 require.Nil(t, err) 41 assert.Equal(t, 200, status) 42 assert.Contains(t, string(respText), data) 43 } 44 45 func TestDiscardResponseBody(t *testing.T) { 46 s := httptest.NewServer(http.HandlerFunc(testHttpHandler)) 47 defer s.Close() 48 data := "test DiscardResponseBody" 49 50 _, respText, status, err := Do(&Request{ 51 Method: http.MethodGet, 52 URL: s.URL, 53 Body: data, 54 DiscardResponseBody: true, 55 }) 56 57 require.Nil(t, err) 58 assert.Equal(t, 200, status) 59 assert.Equal(t, "", string(respText)) 60 } 61 62 func TestMergeRequest(t *testing.T) { 63 s := httptest.NewServer(http.HandlerFunc(testHttpHandler)) 64 defer s.Close() 65 66 t.Run("build request", func(t *testing.T) { 67 reqURL := s.URL + "?k1=v1" 68 req := &Request{ 69 URL: reqURL, 70 Params: map[string]any{ 71 "k2": "v2", 72 "k3": 345, 73 }, 74 Headers: map[string]string{ 75 "Content-Type": "application/test", 76 "x-my-header-1": "test_header", 77 }, 78 } 79 80 _, respText, status, err := Do(req) 81 require.Nil(t, err) 82 assert.Equal(t, 200, status) 83 assert.Contains(t, string(respText), "?k1=v1&k2=v2&k3=345") 84 assert.Contains(t, string(respText), "Content-Type: application/test") 85 assert.Contains(t, string(respText), "X-My-Header-1: test_header") 86 }) 87 88 t.Run("prepared request", func(t *testing.T) { 89 reqURL := s.URL + "?k1=v1" 90 httpReq, _ := http.NewRequest("GET", reqURL, nil) 91 req := &Request{ 92 Req: httpReq, 93 Params: map[string]string{ 94 "k2": "v2", 95 "k3": "345", 96 }, 97 Headers: map[string]string{ 98 "Content-Type": "application/test", 99 "x-my-header-1": "test_header", 100 }, 101 } 102 103 _, respText, status, err := Do(req) 104 require.Nil(t, err) 105 assert.Equal(t, 200, status) 106 assert.Contains(t, string(respText), "?k1=v1&k2=v2&k3=345") 107 assert.Contains(t, string(respText), "Content-Type: application/test") 108 assert.Contains(t, string(respText), "X-My-Header-1: test_header") 109 }) 110 111 t.Run("with trailing ampersand", func(t *testing.T) { 112 reqURL := s.URL + "?k1=v1&" 113 httpReq, _ := http.NewRequest("GET", reqURL, nil) 114 req := &Request{ 115 Req: httpReq, 116 Params: map[string][]string{ 117 "k2": {"v2"}, 118 "k3": {"345"}, 119 }, 120 } 121 122 _, respText, status, err := Do(req) 123 require.Nil(t, err) 124 assert.Equal(t, 200, status) 125 assert.Contains(t, string(respText), "?k1=v1&k2=v2&k3=345") 126 }) 127 } 128 129 func TestRequestSetBasicAuth(t *testing.T) { 130 s := httptest.NewServer(http.HandlerFunc(testHttpHandler)) 131 defer s.Close() 132 133 t.Run("build request", func(t *testing.T) { 134 req := (&Request{ 135 URL: s.URL, 136 }).SetBasicAuth("user", "pass") 137 138 _, respText, status, err := Do(req) 139 require.Nil(t, err) 140 assert.Equal(t, 200, status) 141 assert.Contains(t, string(respText), "Authorization: ") 142 }) 143 144 t.Run("prepared request", func(t *testing.T) { 145 httpReq, _ := http.NewRequest("GET", s.URL, nil) 146 req := &Request{ 147 Req: httpReq, 148 } 149 req.SetBasicAuth("user", "pass") 150 151 _, respText, status, err := Do(req) 152 require.Nil(t, err) 153 assert.Equal(t, 200, status) 154 assert.Contains(t, string(respText), "Authorization: ") 155 }) 156 }