diff --git a/pkg/api/response/response.go b/pkg/api/response/response.go index 0b5a1e2f53c..a037c8ca60e 100644 --- a/pkg/api/response/response.go +++ b/pkg/api/response/response.go @@ -311,6 +311,8 @@ func Respond(status int, body any) *NormalResponse { b = t case string: b = []byte(t) + case nil: + break default: var err error if b, err = json.Marshal(body); err != nil { diff --git a/pkg/api/response/response_test.go b/pkg/api/response/response_test.go index 5d8d10fdfa9..ec0c318194e 100644 --- a/pkg/api/response/response_test.go +++ b/pkg/api/response/response_test.go @@ -125,3 +125,49 @@ func TestErrors(t *testing.T) { ) } } + +func TestRespond(t *testing.T) { + testCases := []struct { + name string + status int + body any + expected []byte + }{ + { + name: "with body of type []byte", + status: 200, + body: []byte("message body"), + expected: []byte("message body"), + }, + { + name: "with body of type string", + status: 400, + body: "message body", + expected: []byte("message body"), + }, + { + name: "with nil body", + status: 204, + body: nil, + expected: nil, + }, + { + name: "with body of type struct", + status: 200, + body: struct { + Name string + Value int + }{"name", 1}, + expected: []byte(`{"Name":"name","Value":1}`), + }, + } + + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + resp := Respond(tc.status, tc.body) + + require.Equal(t, tc.status, resp.status) + require.Equal(t, tc.expected, resp.body.Bytes()) + }) + } +} diff --git a/pkg/services/publicdashboards/api/api_test.go b/pkg/services/publicdashboards/api/api_test.go index 662b2cefaeb..1984e7de1da 100644 --- a/pkg/services/publicdashboards/api/api_test.go +++ b/pkg/services/publicdashboards/api/api_test.go @@ -269,10 +269,7 @@ func TestAPIDeletePublicDashboard(t *testing.T) { assert.Equal(t, test.ExpectedHttpResponse, response.Code) if test.ExpectedHttpResponse == http.StatusOK { - var jsonResp any - err := json.Unmarshal(response.Body.Bytes(), &jsonResp) - require.NoError(t, err) - assert.Equal(t, jsonResp, nil) + assert.Equal(t, []byte(nil), response.Body.Bytes()) } if !test.ShouldCallService {