Esempio n. 1
0
func TestWithCompressorDeflateHeader(t *testing.T) {
	finalCH := ctxhttp.Chain(func(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
		assert.Exactly(t, httputil.CompressDeflate, w.Header().Get(httputil.ContentEncoding))
		assert.Exactly(t, httputil.AcceptEncoding, w.Header().Get(httputil.Vary))
		return nil
	}, ctxmw.WithCompressor())

	w, r := testCompressReqRes()
	r.Header.Set(httputil.AcceptEncoding, "deflate")
	if err := finalCH.ServeHTTPContext(context.TODO(), w, r); err != nil {
		t.Fatal(err)
	}
}
Esempio n. 2
0
// BenchmarkWithCompressorGZIP_1024B-4	   20000	     81916 ns/op	    1330 B/op	       5 allocs/op
func BenchmarkWithCompressorGZIP_1024B(b *testing.B) {

	rawData := randSeq(1024)

	finalCH := ctxhttp.Chain(func(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
		return httputil.NewPrinter(w, r).WriteString(http.StatusOK, rawData)
	}, ctxmw.WithCompressor())

	w, r := testCompressReqRes()
	r.Header.Set(httputil.AcceptEncoding, httputil.CompressGZIP)

	ctx := context.TODO()
	b.ResetTimer()
	b.ReportAllocs()
	for i := 0; i < b.N; i++ {
		if err := finalCH.ServeHTTPContext(ctx, w, r); err != nil {
			b.Fatal(err)
		}
		w.Body.Reset()
	}
}
Esempio n. 3
0
func testWithCompressorConcrete(t *testing.T, header string, uncompressor func(io.Reader) string) {

	rawData := randSeq(1024)

	finalCH := ctxhttp.Chain(func(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
		return httputil.NewPrinter(w, r).WriteString(http.StatusOK, rawData)
	}, ctxmw.WithCompressor())

	w, r := testCompressReqRes()
	r.Header.Set(httputil.AcceptEncoding, header)
	if err := finalCH.ServeHTTPContext(context.TODO(), w, r); err != nil {
		t.Fatal(err)
	}
	assert.False(t, len(rawData) < len(w.Body.Bytes()))

	uncompressedBody := uncompressor(w.Body)

	assert.Exactly(t, rawData, uncompressedBody)
	assert.Exactly(t, header, w.Header().Get(httputil.ContentEncoding))
	assert.Exactly(t, httputil.AcceptEncoding, w.Header().Get(httputil.Vary))
	assert.Exactly(t, httputil.TextPlain, w.Header().Get(httputil.ContentType))

}