|
5 | 5 | "compress/flate"
|
6 | 6 | "compress/gzip"
|
7 | 7 | "context"
|
| 8 | + "errors" |
8 | 9 | "io"
|
9 | 10 | "io/ioutil"
|
10 | 11 | "net/http"
|
@@ -82,6 +83,41 @@ func newCompressHTTPServer() *httptest.Server {
|
82 | 83 | w.WriteHeader(200)
|
83 | 84 | io.WriteString(w, "test it")
|
84 | 85 | })
|
| 86 | + r.GET("/identity", func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) { |
| 87 | + w.Header().Set("Content-Type", "text/plain") |
| 88 | + w.Header().Set("Content-Encoding", "identity") |
| 89 | + w.Header().Set("X-Content-Type-Options", "nosniff") |
| 90 | + w.Header().Set("Transfer-Encoding", "chunked") |
| 91 | + w.WriteHeader(200) |
| 92 | + io.WriteString(w, "test it") |
| 93 | + }) |
| 94 | + r.GET("/multiple", func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) { |
| 95 | + gw := gzip.NewWriter(w) |
| 96 | + defer gw.Close() |
| 97 | + |
| 98 | + fw, err := flate.NewWriter(gw, 9) |
| 99 | + if err != nil { |
| 100 | + io.WriteString(w, err.Error()) |
| 101 | + w.WriteHeader(http.StatusInternalServerError) |
| 102 | + return |
| 103 | + } |
| 104 | + defer fw.Close() |
| 105 | + |
| 106 | + w.Header().Set("Content-Type", "text/plain") |
| 107 | + w.Header().Set("Content-Encoding", "deflate, gzip") |
| 108 | + w.Header().Set("X-Content-Type-Options", "nosniff") |
| 109 | + w.Header().Set("Transfer-Encoding", "chunked") |
| 110 | + w.WriteHeader(200) |
| 111 | + io.WriteString(fw, "test it") |
| 112 | + }) |
| 113 | + r.GET("/unknown", func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) { |
| 114 | + w.Header().Set("Content-Type", "text/plain") |
| 115 | + w.Header().Set("Content-Encoding", "unknown") |
| 116 | + w.Header().Set("X-Content-Type-Options", "nosniff") |
| 117 | + w.Header().Set("Transfer-Encoding", "chunked") |
| 118 | + w.WriteHeader(200) |
| 119 | + io.WriteString(w, "test it") |
| 120 | + }) |
85 | 121 |
|
86 | 122 | return httptest.NewServer(r)
|
87 | 123 | }
|
@@ -217,6 +253,98 @@ func TestDeflateResponse(t *testing.T) {
|
217 | 253 | }
|
218 | 254 | }
|
219 | 255 |
|
| 256 | +func TestIdentityResponse(t *testing.T) { |
| 257 | + srv := newCompressHTTPServer() |
| 258 | + defer srv.Close() |
| 259 | + |
| 260 | + agent, err := NewAgent(WithBaseURL(srv.URL), WithDefaultTransport()) |
| 261 | + if err != nil { |
| 262 | + t.Fatalf("%+v", err) |
| 263 | + } |
| 264 | + |
| 265 | + req, err := agent.GET("/identity") |
| 266 | + if err != nil { |
| 267 | + t.Fatalf("%+v", err) |
| 268 | + } |
| 269 | + |
| 270 | + res, err := agent.Do(context.Background(), req) |
| 271 | + if err != nil { |
| 272 | + t.Fatalf("%+v", err) |
| 273 | + } |
| 274 | + |
| 275 | + if res.StatusCode != 200 { |
| 276 | + t.Fatalf("%#v", res) |
| 277 | + } |
| 278 | + defer res.Body.Close() |
| 279 | + |
| 280 | + body, err := ioutil.ReadAll(res.Body) |
| 281 | + if err != nil { |
| 282 | + t.Fatalf("%+v", err) |
| 283 | + } |
| 284 | + |
| 285 | + if bytes.Compare(body, []byte("test it")) != 0 { |
| 286 | + t.Fatalf("%s missmatch %s", body, "test it") |
| 287 | + } |
| 288 | +} |
| 289 | + |
| 290 | +func TestMultipleResponse(t *testing.T) { |
| 291 | + srv := newCompressHTTPServer() |
| 292 | + defer srv.Close() |
| 293 | + |
| 294 | + agent, err := NewAgent(WithBaseURL(srv.URL), WithDefaultTransport()) |
| 295 | + if err != nil { |
| 296 | + t.Fatalf("%+v", err) |
| 297 | + } |
| 298 | + |
| 299 | + req, err := agent.GET("/multiple") |
| 300 | + if err != nil { |
| 301 | + t.Fatalf("%+v", err) |
| 302 | + } |
| 303 | + |
| 304 | + res, err := agent.Do(context.Background(), req) |
| 305 | + if err != nil { |
| 306 | + t.Fatalf("%+v", err) |
| 307 | + } |
| 308 | + |
| 309 | + if res.StatusCode != 200 { |
| 310 | + t.Fatalf("%#v", res) |
| 311 | + } |
| 312 | + defer res.Body.Close() |
| 313 | + |
| 314 | + body, err := ioutil.ReadAll(res.Body) |
| 315 | + if err != nil { |
| 316 | + t.Fatalf("%+v", err) |
| 317 | + } |
| 318 | + |
| 319 | + if bytes.Compare(body, []byte("test it")) != 0 { |
| 320 | + t.Fatalf("%s missmatch %s", body, "test it") |
| 321 | + } |
| 322 | +} |
| 323 | + |
| 324 | +func TestUnknownResponse(t *testing.T) { |
| 325 | + srv := newCompressHTTPServer() |
| 326 | + defer srv.Close() |
| 327 | + |
| 328 | + agent, err := NewAgent(WithBaseURL(srv.URL), WithDefaultTransport()) |
| 329 | + if err != nil { |
| 330 | + t.Fatalf("%+v", err) |
| 331 | + } |
| 332 | + |
| 333 | + req, err := agent.GET("/unknown") |
| 334 | + if err != nil { |
| 335 | + t.Fatalf("%+v", err) |
| 336 | + } |
| 337 | + |
| 338 | + _, err = agent.Do(context.Background(), req) |
| 339 | + if err == nil { |
| 340 | + t.Fatalf("expected error but err is nil, %+v", err) |
| 341 | + } else { |
| 342 | + if !errors.Is(err, ErrUnknownContentEncoding) { |
| 343 | + t.Fatalf("%+v", err) |
| 344 | + } |
| 345 | + } |
| 346 | +} |
| 347 | + |
220 | 348 | func TestWithEcho(t *testing.T) {
|
221 | 349 | e := echo.New()
|
222 | 350 | e.GET("/", func(c echo.Context) error {
|
|
0 commit comments