From 052c7d82f4ce2da95684fa63445b5777bc5047f3 Mon Sep 17 00:00:00 2001 From: Luca Comellini Date: Wed, 10 Jul 2024 14:04:51 -0700 Subject: [PATCH] Add linters that check for bugs (#310) --- .golangci.yml | 6 ++++++ client/nginx.go | 12 ++++++------ client/nginx_test.go | 18 ++++++++++-------- 3 files changed, 22 insertions(+), 14 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index bc1fa939..26a1c209 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -34,14 +34,19 @@ linters: - asasalint - asciicheck - bidichk + - contextcheck - dupword + - durationcheck - errcheck + - errchkjson - errname - errorlint - exportloopref - fatcontext - forcetypeassert - gocheckcompilerdirectives + - gochecksumtype + - gocritic - godot - gofmt - gofumpt @@ -54,6 +59,7 @@ linters: - intrange - makezero - misspell + - musttag - nilerr - noctx - nolintlint diff --git a/client/nginx.go b/client/nginx.go index 0d8cce0e..1c375c16 100644 --- a/client/nginx.go +++ b/client/nginx.go @@ -83,9 +83,9 @@ type StreamUpstreamServer struct { } type apiErrorResponse struct { - RequestID string `json:"request_id"` - Href string - Error apiError + RequestID string `json:"request_id"` + Href string `json:"href"` + Error apiError `json:"error"` } func (resp *apiErrorResponse) toString() string { @@ -94,9 +94,9 @@ func (resp *apiErrorResponse) toString() string { } type apiError struct { - Text string - Code string - Status int + Text string `json:"text"` + Code string `json:"code"` + Status int `json:"status"` } type internalError struct { diff --git a/client/nginx_test.go b/client/nginx_test.go index e2e8ab3b..1f533972 100644 --- a/client/nginx_test.go +++ b/client/nginx_test.go @@ -602,19 +602,20 @@ func TestClientWithHTTPClient(t *testing.T) { func TestGetStats_NoStreamEndpoint(t *testing.T) { t.Parallel() ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - if r.RequestURI == "/" { + switch { + case r.RequestURI == "/": _, err := w.Write([]byte(`[4, 5, 6, 7, 8, 9]`)) if err != nil { t.Fatalf("unexpected error: %v", err) } - } else if r.RequestURI == "/7/" { + case r.RequestURI == "/7/": _, err := w.Write([]byte(`["nginx","processes","connections","slabs","http","resolvers","ssl"]`)) if err != nil { t.Fatalf("unexpected error: %v", err) } - } else if strings.HasPrefix(r.RequestURI, "/7/stream") { + case strings.HasPrefix(r.RequestURI, "/7/stream"): t.Fatal("Stream endpoint should not be called since it does not exist.") - } else { + default: _, err := w.Write([]byte(`{}`)) if err != nil { t.Fatalf("unexpected error: %v", err) @@ -654,17 +655,18 @@ func TestGetStats_NoStreamEndpoint(t *testing.T) { func TestGetStats_SSL(t *testing.T) { t.Parallel() ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - if r.RequestURI == "/" { + switch { + case r.RequestURI == "/": _, err := w.Write([]byte(`[4, 5, 6, 7, 8, 9]`)) if err != nil { t.Fatalf("unexpected error: %v", err) } - } else if r.RequestURI == "/8/" { + case r.RequestURI == "/8/": _, err := w.Write([]byte(`["nginx","processes","connections","slabs","http","resolvers","ssl","workers"]`)) if err != nil { t.Fatalf("unexpected error: %v", err) } - } else if strings.HasPrefix(r.RequestURI, "/8/ssl") { + case strings.HasPrefix(r.RequestURI, "/8/ssl"): _, err := w.Write([]byte(`{ "handshakes" : 79572, "handshakes_failed" : 21025, @@ -684,7 +686,7 @@ func TestGetStats_SSL(t *testing.T) { if err != nil { t.Fatalf("unexpected error: %v", err) } - } else { + default: _, err := w.Write([]byte(`{}`)) if err != nil { t.Fatalf("unexpected error: %v", err)