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 lint issues in api package #3485

Merged
merged 1 commit into from
Dec 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
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())
}