Skip to content

Commit

Permalink
Only set the body as binary when the Content-Type Header is a binary …
Browse files Browse the repository at this point in the history
…type.
  • Loading branch information
tateexon committed Jun 25, 2024
1 parent b7bafe8 commit ac2569a
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 3 deletions.
29 changes: 26 additions & 3 deletions internal/server/http/dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,25 @@ func writeLog(writer io.Writer, params handlers.LogFormatterParams, body string)
writer.Write(buf)
}

// isBinaryContent checks to see if the body is a common binary content type
func isBinaryContent(r *http.Request) bool {
contentType := r.Header.Get("Content-Type")
binaryContentTypes := []string{
"application/octet-stream",
"image/",
"audio/",
"video/",
"application/pdf",
}

for _, binaryType := range binaryContentTypes {
if strings.HasPrefix(contentType, binaryType) {
return true
}
}
return false
}

// CustomLoggingHandler provides a way to supply a custom log formatter
// while taking advantage of the mechanisms in this package
func CustomLoggingHandler(out io.Writer, h http.Handler, s *Server) http.Handler {
Expand All @@ -176,18 +195,22 @@ func CustomLoggingHandler(out io.Writer, h http.Handler, s *Server) http.Handler
return
}
params.Request.Body = io.NopCloser(bytes.NewReader(bodyBytes)) // Reset the body
base64Body := base64.StdEncoding.EncodeToString(bodyBytes)
body := string(bodyBytes)
// if content is binary, encode it to base64
if isBinaryContent(params.Request) {
body = base64.StdEncoding.EncodeToString(bodyBytes)
}
verbose := s.verbose

// record request, if error set verbose to true to log current request since
// it didn't make it into a full channel
err = recordRequest(params.Request, s, base64Body)
err = recordRequest(params.Request, s, body)
if err != nil {
verbose = true
}

if verbose {
writeLog(writer, params, base64Body)
writeLog(writer, params, body)
}
})
}
Expand Down
22 changes: 22 additions & 0 deletions internal/server/http/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,34 +219,55 @@ func TestBuildLogRequests(t *testing.T) {
testCases := map[string]struct {
method string
path string
contentType string
body string
expectedLog string
expectedStatus int
}{
"GET valid imposter request": {
method: "GET",
path: "/yamlTestDumpRequest",
contentType: "text/plain",
body: "Dumped",
expectedLog: "GET /yamlTestDumpRequest HTTP/1.1\" 200 17 Dumped\n",
expectedStatus: http.StatusOK,
},
"GET valid imposter binary request": {
method: "GET",
path: "/yamlTestDumpRequest",
contentType: "application/octet-stream",
body: "Dumped",
expectedLog: "GET /yamlTestDumpRequest HTTP/1.1\" 200 17 RHVtcGVk\n",
expectedStatus: http.StatusOK,
},
"GET valid imposter request no body": {
method: "GET",
path: "/yamlTestDumpRequest",
contentType: "text/plain",
body: "",
expectedLog: "GET /yamlTestDumpRequest HTTP/1.1\" 200 17\n",
expectedStatus: http.StatusOK,
},
"GET invalid imposter request": {
method: "GET",
path: "/doesnotexist",
contentType: "text/plain",
body: "Dumped",
expectedLog: "GET /doesnotexist HTTP/1.1\" 404 19 Dumped\n",
expectedStatus: http.StatusNotFound,
},
"GET invalid imposter binary request": {
method: "GET",
path: "/doesnotexist",
contentType: "video/mp4",
body: "Dumped",
expectedLog: "GET /doesnotexist HTTP/1.1\" 404 19 RHVtcGVk\n",
expectedStatus: http.StatusNotFound,
},
"GET invalid imposter request no body": {
method: "GET",
path: "/doesnotexist",
contentType: "text/plain",
body: "",
expectedLog: "GET /doesnotexist HTTP/1.1\" 404 19\n",
expectedStatus: http.StatusNotFound,
Expand All @@ -270,6 +291,7 @@ func TestBuildLogRequests(t *testing.T) {

w := httptest.NewRecorder()
req := httptest.NewRequest(tc.method, tc.path, strings.NewReader(tc.body))
req.Header.Set("Content-Type", tc.contentType)

server.httpServer.Handler.ServeHTTP(w, req)

Expand Down

0 comments on commit ac2569a

Please sign in to comment.