Skip to content

Commit

Permalink
fix: metrics endpoints content type setting
Browse files Browse the repository at this point in the history
* fix: metrics endpoints content type setting (was ineffective thus always returning text/plain)

Signed-off-by: lvlcn-t <75443136+lvlcn-t@users.noreply.github.com>
  • Loading branch information
lvlcn-t committed Nov 17, 2024
1 parent dd455b1 commit eea9e17
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 11 deletions.
9 changes: 7 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
"-cover",
"-count=1",
"-timeout=240s",
"-v"
]
// "-test.short",
"-v",
],
"go.testEnvVars": {
// "LOG_LEVEL": "debug",
"LOG_FORMAT": "text",
}
}
2 changes: 1 addition & 1 deletion pkg/sparrow/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ func (s *Sparrow) handleCheckMetrics(w http.ResponseWriter, r *http.Request) {
return
}

w.Header().Add("Content-Type", "application/json")
enc := json.NewEncoder(w)
enc.SetIndent("", " ")

Expand All @@ -134,5 +135,4 @@ func (s *Sparrow) handleCheckMetrics(w http.ResponseWriter, r *http.Request) {
}
return
}
w.Header().Add("Content-Type", "application/json")
}
23 changes: 15 additions & 8 deletions pkg/sparrow/handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,12 +132,18 @@ func TestSparrow_handleCheckMetrics(t *testing.T) {
if tt.wantCode == http.StatusBadRequest {
r = chiRequest(httptest.NewRequest(http.MethodGet, "/v1/metrics/", bytes.NewBuffer([]byte{})), "")
}
r.Header.Add("Accept", "application/json")

s.handleCheckMetrics(w, r)
resp := w.Result() //nolint:bodyclose
resp := w.Result()
defer resp.Body.Close()
body, _ := io.ReadAll(resp.Body)

if tt.wantCode == http.StatusOK {
if w.Header().Get("Content-Type") != "application/json" {
t.Errorf("Sparrow.getCheckMetrics() = %v, want %v", w.Header().Get("Content-Type"), "application/json")
}

if tt.wantCode != resp.StatusCode {
t.Errorf("Sparrow.getCheckMetrics() = %v, want %v", resp.StatusCode, tt.wantCode)
}
Expand All @@ -155,13 +161,14 @@ func TestSparrow_handleCheckMetrics(t *testing.T) {
if reflect.DeepEqual(got, want) {
t.Errorf("Sparrow.getCheckMetrics() = %v, want %v", got, want)
}
} else {
if tt.wantCode != resp.StatusCode {
t.Errorf("Sparrow.getCheckMetrics() = %v, want %v", resp.StatusCode, tt.wantCode)
}
if !reflect.DeepEqual(body, tt.want) {
t.Errorf("Sparrow.getCheckMetrics() = %v, want %v", body, tt.want)
}
return
}

if tt.wantCode != resp.StatusCode {
t.Errorf("Sparrow.getCheckMetrics() = %v, want %v", resp.StatusCode, tt.wantCode)
}
if !reflect.DeepEqual(body, tt.want) {
t.Errorf("Sparrow.getCheckMetrics() = %v, want %v", body, tt.want)
}
})
}
Expand Down

0 comments on commit eea9e17

Please sign in to comment.