|
5 | 5 | "net/http" |
6 | 6 | "strings" |
7 | 7 | "testing" |
| 8 | + "time" |
8 | 9 |
|
9 | 10 | "github.com/gookit/goutil/dump" |
10 | 11 | "github.com/gookit/goutil/jsonutil" |
@@ -127,3 +128,186 @@ func TestHttpReq_MustSend(t *testing.T) { |
127 | 128 | assert.True(t, httpreq.IsOK(sc)) |
128 | 129 | assert.True(t, httpreq.IsSuccessful(sc)) |
129 | 130 | } |
| 131 | + |
| 132 | +func TestClient_NewWithTimeout(t *testing.T) { |
| 133 | + cli := httpreq.NewWithTimeout(5000) |
| 134 | + assert.NotNil(t, cli) |
| 135 | + assert.NotNil(t, cli.Doer()) |
| 136 | +} |
| 137 | + |
| 138 | +func TestClient_NewWithDoer(t *testing.T) { |
| 139 | + customClient := &http.Client{Timeout: 3000 * time.Millisecond} |
| 140 | + cli := httpreq.NewWithDoer(customClient) |
| 141 | + assert.NotNil(t, cli) |
| 142 | + assert.Eq(t, customClient, cli.Doer()) |
| 143 | +} |
| 144 | + |
| 145 | +func TestClient_SetClient(t *testing.T) { |
| 146 | + cli := httpreq.New() |
| 147 | + originalDoer := cli.Doer() |
| 148 | + |
| 149 | + newClient := &http.Client{Timeout: 2000 * time.Millisecond} |
| 150 | + ret := cli.SetClient(newClient) |
| 151 | + |
| 152 | + assert.Eq(t, newClient, cli.Doer()) |
| 153 | + assert.Eq(t, cli, ret) |
| 154 | + assert.NotEq(t, originalDoer, cli.Doer()) |
| 155 | +} |
| 156 | + |
| 157 | +func TestClient_SetTimeout(t *testing.T) { |
| 158 | + cli := httpreq.New() |
| 159 | + ret := cli.SetTimeout(5000) |
| 160 | + |
| 161 | + assert.NotNil(t, ret) |
| 162 | + assert.Eq(t, cli, ret) |
| 163 | + |
| 164 | + hc, ok := cli.Doer().(*http.Client) |
| 165 | + assert.True(t, ok) |
| 166 | + assert.Eq(t, 5000*time.Millisecond, hc.Timeout) |
| 167 | +} |
| 168 | + |
| 169 | +func TestClient_PostJSON(t *testing.T) { |
| 170 | + cli := httpreq.New(testSrvAddr) |
| 171 | + data := map[string]string{"name": "inhere", "city": "chengdu"} |
| 172 | + |
| 173 | + resp, err := cli.PostJSON("/post", data) |
| 174 | + assert.NoErr(t, err) |
| 175 | + assert.True(t, httpreq.IsOK(resp.StatusCode)) |
| 176 | + |
| 177 | + rr := testutil.ParseRespToReply(resp) |
| 178 | + assert.Eq(t, "POST", rr.Method) |
| 179 | + assert.StrContains(t, rr.Body, `"name":"inhere"`) |
| 180 | +} |
| 181 | + |
| 182 | +func TestClient_WithData(t *testing.T) { |
| 183 | + cli := httpreq.New(testSrvAddr) |
| 184 | + |
| 185 | + opt := cli.WithData("name=inhere&age=18") |
| 186 | + assert.NotNil(t, opt) |
| 187 | + assert.NotNil(t, opt.Data) |
| 188 | +} |
| 189 | + |
| 190 | +func TestClient_WithBody(t *testing.T) { |
| 191 | + cli := httpreq.New(testSrvAddr) |
| 192 | + body := strings.NewReader("test body") |
| 193 | + |
| 194 | + opt := cli.WithBody(body) |
| 195 | + assert.NotNil(t, opt) |
| 196 | + assert.NotNil(t, opt.Body) |
| 197 | +} |
| 198 | + |
| 199 | +func TestClient_BytesBody(t *testing.T) { |
| 200 | + cli := httpreq.New(testSrvAddr) |
| 201 | + |
| 202 | + opt := cli.BytesBody([]byte("bytes body")) |
| 203 | + assert.NotNil(t, opt) |
| 204 | + assert.NotNil(t, opt.Body) |
| 205 | +} |
| 206 | + |
| 207 | +func TestClient_StringBody(t *testing.T) { |
| 208 | + cli := httpreq.New(testSrvAddr) |
| 209 | + |
| 210 | + opt := cli.StringBody("string body") |
| 211 | + assert.NotNil(t, opt) |
| 212 | + assert.NotNil(t, opt.Body) |
| 213 | +} |
| 214 | + |
| 215 | +func TestClient_FormBody(t *testing.T) { |
| 216 | + cli := httpreq.New(testSrvAddr) |
| 217 | + |
| 218 | + opt := cli.FormBody(map[string]string{"name": "inhere"}) |
| 219 | + assert.NotNil(t, opt) |
| 220 | + assert.NotNil(t, opt.Body) |
| 221 | +} |
| 222 | + |
| 223 | +func TestClient_JSONBody(t *testing.T) { |
| 224 | + cli := httpreq.New(testSrvAddr) |
| 225 | + |
| 226 | + opt := cli.JSONBody(map[string]string{"name": "inhere"}) |
| 227 | + assert.NotNil(t, opt) |
| 228 | + assert.NotNil(t, opt.Body) |
| 229 | + assert.Eq(t, httpctype.JSON, opt.ContentType) |
| 230 | +} |
| 231 | + |
| 232 | +func TestClient_JSONBytesBody(t *testing.T) { |
| 233 | + cli := httpreq.New(testSrvAddr) |
| 234 | + |
| 235 | + opt := cli.JSONBytesBody([]byte(`{"name":"inhere"}`)) |
| 236 | + assert.NotNil(t, opt) |
| 237 | + assert.NotNil(t, opt.Body) |
| 238 | + assert.Eq(t, httpctype.JSON, opt.ContentType) |
| 239 | +} |
| 240 | + |
| 241 | +func TestClient_AnyBody(t *testing.T) { |
| 242 | + cli := httpreq.New(testSrvAddr) |
| 243 | + |
| 244 | + t.Run("string body", func(t *testing.T) { |
| 245 | + opt := cli.AnyBody("string data") |
| 246 | + assert.NotNil(t, opt.Body) |
| 247 | + }) |
| 248 | + |
| 249 | + t.Run("bytes body", func(t *testing.T) { |
| 250 | + opt := cli.AnyBody([]byte("bytes data")) |
| 251 | + assert.NotNil(t, opt.Body) |
| 252 | + }) |
| 253 | + |
| 254 | + t.Run("map body", func(t *testing.T) { |
| 255 | + opt := cli.AnyBody(map[string]string{"key": "value"}) |
| 256 | + assert.NotNil(t, opt.Body) |
| 257 | + }) |
| 258 | +} |
| 259 | + |
| 260 | +func TestClient_WithOption(t *testing.T) { |
| 261 | + cli := httpreq.New(testSrvAddr) |
| 262 | + |
| 263 | + opt := cli.WithOption(httpreq.WithJSONType) |
| 264 | + assert.NotNil(t, opt) |
| 265 | + assert.Eq(t, httpctype.JSON, opt.ContentType) |
| 266 | +} |
| 267 | + |
| 268 | +func TestClient_SendWithOpt(t *testing.T) { |
| 269 | + cli := httpreq.New(testSrvAddr) |
| 270 | + opt := httpreq.NewOpt().WithMethod(http.MethodGet) |
| 271 | + |
| 272 | + resp, err := cli.SendWithOpt("/get", opt) |
| 273 | + assert.NoErr(t, err) |
| 274 | + assert.True(t, httpreq.IsOK(resp.StatusCode)) |
| 275 | + |
| 276 | + rr := testutil.ParseRespToReply(resp) |
| 277 | + assert.Eq(t, "GET", rr.Method) |
| 278 | +} |
| 279 | + |
| 280 | +func TestClient_SendRequest(t *testing.T) { |
| 281 | + cli := httpreq.New(testSrvAddr) |
| 282 | + req, err := http.NewRequest(http.MethodGet, testSrvAddr+"/get", nil) |
| 283 | + assert.NoErr(t, err) |
| 284 | + |
| 285 | + opt := httpreq.NewOpt() |
| 286 | + resp, err := cli.SendRequest(req, opt) |
| 287 | + assert.NoErr(t, err) |
| 288 | + assert.True(t, httpreq.IsOK(resp.StatusCode)) |
| 289 | +} |
| 290 | + |
| 291 | +func TestClient_BaseURL(t *testing.T) { |
| 292 | + cli := httpreq.New() |
| 293 | + ret := cli.BaseURL("http://localhost:8080") |
| 294 | + |
| 295 | + assert.Eq(t, cli, ret) |
| 296 | +} |
| 297 | + |
| 298 | +func TestClient_DefaultMethod(t *testing.T) { |
| 299 | + cli := httpreq.New() |
| 300 | + |
| 301 | + ret := cli.DefaultMethod(http.MethodPost) |
| 302 | + assert.Eq(t, cli, ret) |
| 303 | + |
| 304 | + ret = cli.DefaultMethod("") |
| 305 | + assert.Eq(t, cli, ret) |
| 306 | +} |
| 307 | + |
| 308 | +func TestClient_ContentType(t *testing.T) { |
| 309 | + cli := httpreq.New() |
| 310 | + |
| 311 | + ret := cli.ContentType(httpctype.JSON) |
| 312 | + assert.Eq(t, cli, ret) |
| 313 | +} |
0 commit comments