@@ -2,6 +2,7 @@ package apilibs
22
33import (
44 "encoding/json"
5+ "net/http"
56 "net/http/httptest"
67 "testing"
78 "time"
@@ -279,7 +280,6 @@ func TestResponseTimestampAndResponseTime(t *testing.T) {
279280func 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