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

Function TestCompressHandler

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

Source from the content-addressed store, hash-verified

2506}
2507
2508func 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))

Callers

nothing calls this directly

Calls 12

WriteStringMethod · 0.95
ReadMethod · 0.95
BodyMethod · 0.95
BodyGunzipMethod · 0.95
BodyInflateMethod · 0.95
createFixedBodyFunction · 0.85
CompressHandlerFunction · 0.85
NewReaderMethod · 0.80
ResetMethod · 0.65
StringMethod · 0.45
ContentEncodingMethod · 0.45
SetMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…