Skip to content

Commit

Permalink
Merge 0a68fa8 into 856a767
Browse files Browse the repository at this point in the history
  • Loading branch information
mstoykov authored Dec 1, 2023
2 parents 856a767 + 0a68fa8 commit b738d6c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
6 changes: 3 additions & 3 deletions api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,15 @@ type wrappedResponseWriter struct {
status int
}

func (w wrappedResponseWriter) WriteHeader(status int) {
func (w *wrappedResponseWriter) WriteHeader(status int) {
w.status = status
w.ResponseWriter.WriteHeader(status)
w.ResponseWriter.WriteHeader(w.status)
}

// withLoggingHandler returns the middleware which logs response status for request.
func withLoggingHandler(l logrus.FieldLogger, next http.Handler) http.HandlerFunc {
return func(rw http.ResponseWriter, r *http.Request) {
wrapped := wrappedResponseWriter{ResponseWriter: rw, status: 200} // The default status code is 200 if it's not set
wrapped := &wrappedResponseWriter{ResponseWriter: rw, status: 200} // The default status code is 200 if it's not set
next.ServeHTTP(wrapped, r)

l.WithField("status", wrapped.status).Debugf("%s %s", r.Method, r.URL.Path)
Expand Down
10 changes: 9 additions & 1 deletion api/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,23 @@ import (
"go.k6.io/k6/lib/testutils"
)

func testHTTPHandler(rw http.ResponseWriter, r *http.Request) {
func testHTTPHandler(rw http.ResponseWriter, _ *http.Request) {
rw.Header().Add("Content-Type", "text/plain; charset=utf-8")
if _, err := fmt.Fprint(rw, "ok"); err != nil {
panic(err.Error())
}
}

func TestLogger(t *testing.T) {
t.Parallel()
for _, method := range []string{"GET", "POST", "PUT", "PATCH"} {
method := method
t.Run("method="+method, func(t *testing.T) {
t.Parallel()
for _, path := range []string{"/", "/test", "/test/path"} {
path := path
t.Run("path="+path, func(t *testing.T) {
t.Parallel()
rw := httptest.NewRecorder()
r := httptest.NewRequest(method, "http://example.com"+path, nil)

Expand All @@ -35,6 +40,7 @@ func TestLogger(t *testing.T) {
res := rw.Result()
assert.Equal(t, http.StatusOK, res.StatusCode)
assert.Equal(t, "text/plain; charset=utf-8", res.Header.Get("Content-Type"))
assert.NoError(t, res.Body.Close())

if !assert.Len(t, hook.Entries, 1) {
return
Expand All @@ -51,6 +57,7 @@ func TestLogger(t *testing.T) {
}

func TestPing(t *testing.T) {
t.Parallel()
logger := logrus.New()
logger.SetOutput(testutils.NewTestOutput(t))
mux := handlePing(logger)
Expand All @@ -62,4 +69,5 @@ func TestPing(t *testing.T) {
res := rw.Result()
assert.Equal(t, http.StatusOK, res.StatusCode)
assert.Equal(t, []byte{'o', 'k'}, rw.Body.Bytes())
assert.NoError(t, res.Body.Close())
}

0 comments on commit b738d6c

Please sign in to comment.