(t *testing.T)
| 2506 | } |
| 2507 | |
| 2508 | func TestCompressHandler(t *testing.T) { |
| 2509 | t.Parallel() |
| 2510 | |
| 2511 | expectedBody := string(createFixedBody(2e4)) |
| 2512 | h := CompressHandler(func(ctx *RequestCtx) { |
| 2513 | ctx.WriteString(expectedBody) //nolint:errcheck |
| 2514 | }) |
| 2515 | |
| 2516 | var ctx RequestCtx |
| 2517 | var resp Response |
| 2518 | |
| 2519 | // verify uncompressed response |
| 2520 | h(&ctx) |
| 2521 | s := ctx.Response.String() |
| 2522 | br := bufio.NewReader(bytes.NewBufferString(s)) |
| 2523 | if err := resp.Read(br); err != nil { |
| 2524 | t.Fatalf("unexpected error: %v", err) |
| 2525 | } |
| 2526 | ce := resp.Header.ContentEncoding() |
| 2527 | if len(ce) != 0 { |
| 2528 | t.Fatalf("unexpected Content-Encoding: %q. Expecting %q", ce, "") |
| 2529 | } |
| 2530 | body := resp.Body() |
| 2531 | if string(body) != expectedBody { |
| 2532 | t.Fatalf("unexpected body %q. Expecting %q", body, expectedBody) |
| 2533 | } |
| 2534 | |
| 2535 | // verify gzip-compressed response |
| 2536 | ctx.Request.Reset() |
| 2537 | ctx.Response.Reset() |
| 2538 | ctx.Request.Header.Set("Accept-Encoding", "gzip, deflate, sdhc") |
| 2539 | |
| 2540 | h(&ctx) |
| 2541 | s = ctx.Response.String() |
| 2542 | br = bufio.NewReader(bytes.NewBufferString(s)) |
| 2543 | if err := resp.Read(br); err != nil { |
| 2544 | t.Fatalf("unexpected error: %v", err) |
| 2545 | } |
| 2546 | ce = resp.Header.ContentEncoding() |
| 2547 | if string(ce) != "gzip" { |
| 2548 | t.Fatalf("unexpected Content-Encoding: %q. Expecting %q", ce, "gzip") |
| 2549 | } |
| 2550 | body, err := resp.BodyGunzip() |
| 2551 | if err != nil { |
| 2552 | t.Fatalf("unexpected error: %v", err) |
| 2553 | } |
| 2554 | if string(body) != expectedBody { |
| 2555 | t.Fatalf("unexpected body %q. Expecting %q", body, expectedBody) |
| 2556 | } |
| 2557 | |
| 2558 | // an attempt to compress already compressed response |
| 2559 | ctx.Request.Reset() |
| 2560 | ctx.Response.Reset() |
| 2561 | ctx.Request.Header.Set("Accept-Encoding", "gzip, deflate, sdhc") |
| 2562 | hh := CompressHandler(h) |
| 2563 | hh(&ctx) |
| 2564 | s = ctx.Response.String() |
| 2565 | br = bufio.NewReader(bytes.NewBufferString(s)) |
nothing calls this directly
no test coverage detected
searching dependent graphs…