Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix nil panic in confighttp #5628

Merged
merged 7 commits into from
Jul 19, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix the review comments to use httptest package
Signed-off-by: Ziqi Zhao <[email protected]>
  • Loading branch information
fatsheep9146 committed Jul 18, 2022
commit 53a1265601b8d2f07b75f74dab1a90e66d2a69e9
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")
fatsheep9146 marked this conversation as resolved.
Show resolved Hide resolved

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