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
fix unit test
Signed-off-by: Ziqi Zhao <[email protected]>
  • Loading branch information
fatsheep9146 committed Jul 19, 2022
commit b289f0ea76706a8c783bc26c32fb7b9b8a53719a
34 changes: 20 additions & 14 deletions config/confighttp/compression_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"compress/gzip"
"compress/zlib"
"fmt"
"io"
"io/ioutil"
"net"
"net/http"
Expand Down Expand Up @@ -259,27 +258,34 @@ func TestHTTPContentCompressionRequestWithNilBody(t *testing.T) {
require.NoError(t, res.Body.Close(), "failed to close request body: %v", err)
}

func TestHTTPContentCompressionCopyError(t *testing.T) {
testBody := bytes.NewBuffer([]byte("test"))
copyErrorCompressRoundTripper := &compressRoundTripper{
RoundTripper: http.DefaultTransport,
compressionType: "copyFailed",
writer: func(buf *bytes.Buffer) (io.WriteCloser, error) {
return nil, fmt.Errorf("copy failed")
},
}
type copyFailBody struct {
}

func (*copyFailBody) Read(p []byte) (n int, err error) {
return 0, fmt.Errorf("read failed")
}

func (*copyFailBody) Close() error {
return nil
}

func TestHTTPContentCompressionCopyError(t *testing.T) {
body := &copyFailBody{}
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(200)
}))
defer server.Close()

req, err := http.NewRequest("GET", server.URL, testBody)
require.NoError(t, err, "failed to create request to test handler")
url, _ := url.Parse(server.URL)
req := &http.Request{
Method: "GET",
URL: url,
Body: body,
}

client := http.Client{}
client.Transport = copyErrorCompressRoundTripper
_, err = client.Do(req)
client.Transport = newCompressRoundTripper(http.DefaultTransport, configcompression.Gzip)
_, err := client.Do(req)
require.Error(t, err)
}

Expand Down