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
add tests to cover different errors
Signed-off-by: Ziqi Zhao <[email protected]>
  • Loading branch information
fatsheep9146 committed Jul 18, 2022
commit e5ec09d863b08c31e202f51917ec66a96a2ac9e2
57 changes: 57 additions & 0 deletions config/confighttp/compression_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@ import (
"compress/gzip"
"compress/zlib"
"fmt"
"io"
"io/ioutil"
"net"
"net/http"
"net/http/httptest"
"net/url"
"testing"
"time"

Expand Down Expand Up @@ -257,6 +259,61 @@ func TestHTTPContentCompressionRequestWithNilBody(t *testing.T) {
require.NoError(t, res.Body.Close(), "failed to close request body: %v", err)
}

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

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, nil)
require.NoError(t, err, "failed to create request to test handler")

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

type closeFailBody struct {
*bytes.Buffer
}

func (*closeFailBody) Close() error {
return fmt.Errorf("close failed")
}

func TestHTTPContentCompressionRequestBodyCloseError(t *testing.T) {
testBody := []byte("blank")
body := &closeFailBody{
Buffer: bytes.NewBuffer(testBody),
}

server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(200)
}))
defer server.Close()

url, _ := url.Parse(server.URL)
req := &http.Request{
Method: "GET",
URL: url,
Body: body,
}

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

func compressGzip(body []byte) (*bytes.Buffer, error) {
var buf bytes.Buffer

Expand Down