Skip to content

Commit

Permalink
fix the review comments to use httptest package
Browse files Browse the repository at this point in the history
Signed-off-by: Ziqi Zhao <[email protected]>
  • Loading branch information
fatsheep9146 committed Jul 14, 2022
1 parent 9f49c65 commit e857c29
Showing 1 changed file with 10 additions and 20 deletions.
30 changes: 10 additions & 20 deletions config/confighttp/compression_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"io/ioutil"
"net"
"net/http"
"net/http/httptest"
"testing"
"time"

Expand Down Expand Up @@ -233,38 +234,27 @@ func TestHTTPContentDecompressionHandler(t *testing.T) {
}
}

func TestHTTPContentCompressionNilRequestBody(t *testing.T) {
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
func TestHTTPContentCompressionRequestWithNilBody(t *testing.T) {
compressedGzipBody, _ := compressGzip([]byte{})
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(200)
})
body, err := ioutil.ReadAll(r.Body)
require.NoError(t, err, "failed to read request body: %v", err)
assert.EqualValues(t, compressedGzipBody.Bytes(), body)
}))
defer server.Close()

addr := testutil.GetAvailableLocalAddress(t)
ln, err := net.Listen("tcp", addr)
require.NoError(t, err, "failed to create listener: %v", err)
srv := &http.Server{
Handler: handler,
}
go func() {
_ = srv.Serve(ln)
}()
// Wait for the servers to start
<-time.After(10 * time.Millisecond)

serverURL := fmt.Sprintf("http://%s", ln.Addr().String())

req, err := http.NewRequest("GET", serverURL, nil)
req, err := http.NewRequest("GET", server.URL, nil)
require.NoError(t, err, "failed to create request to test handler")

client := http.Client{}
client.Transport = newCompressRoundTripper(http.DefaultTransport, configcompression.Gzip)

res, err := client.Do(req)
require.NoError(t, err)

_, err = ioutil.ReadAll(res.Body)
require.NoError(t, err)
require.NoError(t, res.Body.Close(), "failed to close request body: %v", err)
require.NoError(t, srv.Close())
}

func compressGzip(body []byte) (*bytes.Buffer, error) {
Expand Down

0 comments on commit e857c29

Please sign in to comment.