github.com/blend/go-sdk@v1.20220411.3/web/gzip_middleware_test.go (about)

     1  /*
     2  
     3  Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file.
     5  
     6  */
     7  
     8  package web
     9  
    10  import (
    11  	"bytes"
    12  	"compress/gzip"
    13  	"io"
    14  	"testing"
    15  
    16  	"github.com/blend/go-sdk/assert"
    17  	"github.com/blend/go-sdk/r2"
    18  	"github.com/blend/go-sdk/webutil"
    19  )
    20  
    21  func TestGZipMiddlewarePlaintext(t *testing.T) {
    22  	assert := assert.New(t)
    23  
    24  	app := MustNew(
    25  		OptUse(GZip),
    26  	)
    27  	app.GET("/", ok)
    28  
    29  	req := MockGet(app, "/")
    30  	assert.Nil(req.Err)
    31  	resBody, _, err := req.Bytes()
    32  	assert.Nil(err)
    33  	assert.Equal("\"OK!\"\n", string(resBody))
    34  }
    35  
    36  func TestGZipMiddlewareCompressed(t *testing.T) {
    37  	assert := assert.New(t)
    38  
    39  	app := MustNew(
    40  		OptUse(GZip),
    41  	)
    42  	app.GET("/", ok)
    43  
    44  	req := MockGet(app, "/", r2.OptHeaderValue(webutil.HeaderAcceptEncoding, "gzip"))
    45  	assert.Nil(req.Err)
    46  	body, meta, err := req.Bytes()
    47  
    48  	assert.Equal("gzip", meta.Header.Get(webutil.HeaderContentEncoding))
    49  	assert.Equal(webutil.HeaderAcceptEncoding, meta.Header.Get(webutil.HeaderVary))
    50  	assert.Nil(err)
    51  
    52  	decompressor, err := gzip.NewReader(bytes.NewBuffer(body))
    53  	assert.Nil(err)
    54  	decompressed, err := io.ReadAll(decompressor)
    55  	assert.Nil(err)
    56  
    57  	assert.Equal("\"OK!\"\n", string(decompressed))
    58  }