Skip to content

Commit fa8d0b0

Browse files
committed
Fix tests errors
1 parent d3ad3f3 commit fa8d0b0

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

internal/api/apilibs/response.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ type Response[T any] struct {
1212
Success bool `json:"success"`
1313
Payload T `json:"payload,omitempty"`
1414
Error *errors.Error `json:"error,omitempty"`
15-
Warnings []error `json:"warnings,omitempty"`
15+
Warnings []string `json:"warnings,omitempty"`
1616
Timestamp time.Time `json:"timestamp"`
1717
ResponseTime string `json:"responseTime"`
1818
}
@@ -34,6 +34,14 @@ func getRequestStartTime(c *gin.Context) (time.Time, bool) {
3434
return start, ok
3535
}
3636

37+
func errorsToStrings(errs []error) []string {
38+
out := make([]string, 0, len(errs))
39+
for _, e := range errs {
40+
out = append(out, e.Error())
41+
}
42+
return out
43+
}
44+
3745
func ToResponse[T any](c *gin.Context, in ResponseInput[T]) {
3846
var statusCode int
3947
startTime, _ := getRequestStartTime(c)
@@ -55,7 +63,7 @@ func ToResponse[T any](c *gin.Context, in ResponseInput[T]) {
5563
Success: success,
5664
Payload: in.Payload,
5765
Error: in.Error,
58-
Warnings: in.Warnings,
66+
Warnings: errorsToStrings(in.Warnings),
5967
Timestamp: time.Now().UTC(),
6068
ResponseTime: time.Since(startTime).String(),
6169
})

internal/api/apilibs/response_test.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package apilibs
22

33
import (
44
"encoding/json"
5+
"net/http"
56
"net/http/httptest"
67
"testing"
78
"time"
@@ -279,7 +280,6 @@ func TestResponseTimestampAndResponseTime(t *testing.T) {
279280
func TestToResponse_WithoutRequestStartTime(t *testing.T) {
280281
w := httptest.NewRecorder()
281282
c, _ := gin.CreateTestContext(w)
282-
// No request_start_time set
283283

284284
ToResponse(c, ResponseInput[string]{
285285
StatusSuccess: 200,
@@ -294,8 +294,8 @@ func TestToResponse_WithoutRequestStartTime(t *testing.T) {
294294
t.Fatalf("unmarshal failed: %v", err)
295295
}
296296

297-
if got.Success {
298-
t.Fatal("expected Success false when no request_start_time")
297+
if !got.Success {
298+
t.Fatal("expected Success true even without request_start_time")
299299
}
300300
}
301301

@@ -360,6 +360,14 @@ func TestToResponse_StatusCodeVariations(t *testing.T) {
360360
t.Fatalf("expected status %d, got %d", tc.expectedStatusCode, w.Code)
361361
}
362362

363+
// 204 has no body by HTTP spec
364+
if tc.expectedStatusCode == http.StatusNoContent {
365+
if w.Body.Len() != 0 {
366+
t.Fatal("expected empty body for 204 No Content")
367+
}
368+
return
369+
}
370+
363371
var got Response[string]
364372
if err := json.Unmarshal(w.Body.Bytes(), &got); err != nil {
365373
t.Fatalf("unmarshal failed: %v", err)
@@ -369,5 +377,6 @@ func TestToResponse_StatusCodeVariations(t *testing.T) {
369377
t.Fatalf("expected Success %v, got %v", tc.expectedSuccess, got.Success)
370378
}
371379
})
380+
372381
}
373382
}

0 commit comments

Comments
 (0)