MCPcopy
hub / github.com/valyala/fasthttp / TestCompressHandlerVary

Function TestCompressHandlerVary

server_test.go:2605–2744  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

2603}
2604
2605func TestCompressHandlerVary(t *testing.T) {
2606 t.Parallel()
2607
2608 expectedBody := string(createFixedBody(2e4))
2609
2610 h := CompressHandlerBrotliLevel(func(ctx *RequestCtx) {
2611 ctx.WriteString(expectedBody) //nolint:errcheck
2612 }, CompressBrotliBestSpeed, CompressBestSpeed)
2613
2614 var ctx RequestCtx
2615 var resp Response
2616
2617 // verify uncompressed response
2618 h(&ctx)
2619 s := ctx.Response.String()
2620 br := bufio.NewReader(bytes.NewBufferString(s))
2621 if err := resp.Read(br); err != nil {
2622 t.Fatalf("unexpected error: %v", err)
2623 }
2624 ce := resp.Header.ContentEncoding()
2625 if len(ce) != 0 {
2626 t.Fatalf("unexpected Content-Encoding: %q. Expecting %q", ce, "")
2627 }
2628 vary := resp.Header.Peek("Vary")
2629 if len(vary) != 0 {
2630 t.Fatalf("unexpected Vary: %q. Expecting %q", vary, "")
2631 }
2632 body := resp.Body()
2633 if string(body) != expectedBody {
2634 t.Fatalf("unexpected body %q. Expecting %q", body, expectedBody)
2635 }
2636
2637 // verify gzip-compressed response
2638 ctx.Request.Reset()
2639 ctx.Response.Reset()
2640 ctx.Request.Header.Set("Accept-Encoding", "gzip, deflate, sdhc")
2641
2642 h(&ctx)
2643 s = ctx.Response.String()
2644 br = bufio.NewReader(bytes.NewBufferString(s))
2645 if err := resp.Read(br); err != nil {
2646 t.Fatalf("unexpected error: %v", err)
2647 }
2648 ce = resp.Header.ContentEncoding()
2649 if string(ce) != "gzip" {
2650 t.Fatalf("unexpected Content-Encoding: %q. Expecting %q", ce, "gzip")
2651 }
2652 vary = resp.Header.Peek("Vary")
2653 if string(vary) != "Accept-Encoding" {
2654 t.Fatalf("unexpected Vary: %q. Expecting %q", vary, "Accept-Encoding")
2655 }
2656 body, err := resp.BodyGunzip()
2657 if err != nil {
2658 t.Fatalf("unexpected error: %v", err)
2659 }
2660 if string(body) != expectedBody {
2661 t.Fatalf("unexpected body %q. Expecting %q", body, expectedBody)
2662 }

Callers

nothing calls this directly

Calls 15

WriteStringMethod · 0.95
ReadMethod · 0.95
BodyMethod · 0.95
BodyGunzipMethod · 0.95
BodyInflateMethod · 0.95
BodyUnbrotliMethod · 0.95
createFixedBodyFunction · 0.85
CompressHandlerFunction · 0.85
NewReaderMethod · 0.80
PeekMethod · 0.65
ResetMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…