Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
aarnautu committed Feb 24, 2023
1 parent f38b2eb commit 4d0c873
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 4 deletions.
6 changes: 5 additions & 1 deletion server/handlers/compress.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"compress/gzip"
"fmt"
"io"
"log"
"net/http"
"strings"
"sync"
Expand Down Expand Up @@ -54,7 +55,10 @@ func CompressHandler(handler http.Handler) http.Handler {
defer func() {
gzipWriter.Close()
responseWriter.Header().Set("Content-Length", fmt.Sprint(len(b.Bytes())))
responseWriter.Write(b.Bytes())
_, err := responseWriter.Write(b.Bytes())
if err != nil {
log.Fatalf("Error writing the compressed response: %v", err)
}
}()

crw := &compressResponseWriter{Writer: gzipWriter, ResponseWriter: responseWriter}
Expand Down
5 changes: 4 additions & 1 deletion server/handlers/compress_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ const (
func executeRequest(w *httptest.ResponseRecorder, path string, acceptEncoding string) {
CompressHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
io.WriteString(w, requestBody)
_, err := io.WriteString(w, requestBody)
if err != nil {
log.Fatalf("Error writing the request body: %v", err)
}
})).ServeHTTP(w, &http.Request{
URL: &url.URL{Path: path},
Method: "GET",
Expand Down
4 changes: 3 additions & 1 deletion server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -3013,7 +3013,9 @@ func annotateSpan(ctx context.Context, decisionID string) {
func readPlainBody(r *http.Request) (io.ReadCloser, error) {
if strings.Contains(r.Header.Get("Content-Encoding"), "gzip") {
plainBody, err := gzip.NewReader(r.Body)
defer plainBody.Close()
if err != nil {
defer plainBody.Close()
}
return plainBody, err
}
return r.Body, nil
Expand Down
2 changes: 1 addition & 1 deletion server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1939,7 +1939,7 @@ allow_request { flag == true }
f.server.Handler.ServeHTTP(f.recorder, req)

expected := "true"
result := strings.TrimSuffix(string(f.recorder.Body.Bytes()), "\n")
result := strings.TrimSuffix(f.recorder.Body.String(), "\n")
if result != expected {
t.Fatalf("Expected %v but got: %v", expected, result)
}
Expand Down

0 comments on commit 4d0c873

Please sign in to comment.