fix: replace nil with http.NoBody in various handler tests for clarity
This commit is contained in:
@@ -51,7 +51,7 @@ func TestDockerHandler_ListContainers_InvalidHostRejected(t *testing.T) {
|
||||
api := router.Group("/api/v1")
|
||||
h.RegisterRoutes(api)
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "/api/v1/docker/containers?host=tcp://127.0.0.1:2375", nil)
|
||||
req := httptest.NewRequest(http.MethodGet, "/api/v1/docker/containers?host=tcp://127.0.0.1:2375", http.NoBody)
|
||||
w := httptest.NewRecorder()
|
||||
router.ServeHTTP(w, req)
|
||||
|
||||
@@ -70,7 +70,7 @@ func TestDockerHandler_ListContainers_DockerUnavailableMappedTo503(t *testing.T)
|
||||
api := router.Group("/api/v1")
|
||||
h.RegisterRoutes(api)
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "/api/v1/docker/containers?host=local", nil)
|
||||
req := httptest.NewRequest(http.MethodGet, "/api/v1/docker/containers?host=local", http.NoBody)
|
||||
w := httptest.NewRecorder()
|
||||
router.ServeHTTP(w, req)
|
||||
|
||||
@@ -89,7 +89,7 @@ func TestDockerHandler_ListContainers_ServerIDResolvesToTCPHost(t *testing.T) {
|
||||
api := router.Group("/api/v1")
|
||||
h.RegisterRoutes(api)
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "/api/v1/docker/containers?server_id=abc-123", nil)
|
||||
req := httptest.NewRequest(http.MethodGet, "/api/v1/docker/containers?server_id=abc-123", http.NoBody)
|
||||
w := httptest.NewRecorder()
|
||||
router.ServeHTTP(w, req)
|
||||
|
||||
@@ -110,7 +110,7 @@ func TestDockerHandler_ListContainers_ServerIDNotFoundReturns404(t *testing.T) {
|
||||
api := router.Group("/api/v1")
|
||||
h.RegisterRoutes(api)
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "/api/v1/docker/containers?server_id=missing", nil)
|
||||
req := httptest.NewRequest(http.MethodGet, "/api/v1/docker/containers?server_id=missing", http.NoBody)
|
||||
w := httptest.NewRecorder()
|
||||
router.ServeHTTP(w, req)
|
||||
|
||||
@@ -147,7 +147,7 @@ func TestDockerHandler_ListContainers_Local(t *testing.T) {
|
||||
api := router.Group("/api/v1")
|
||||
h.RegisterRoutes(api)
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "/api/v1/docker/containers", nil)
|
||||
req := httptest.NewRequest(http.MethodGet, "/api/v1/docker/containers", http.NoBody)
|
||||
w := httptest.NewRecorder()
|
||||
router.ServeHTTP(w, req)
|
||||
|
||||
@@ -187,7 +187,7 @@ func TestDockerHandler_ListContainers_RemoteServerSuccess(t *testing.T) {
|
||||
api := router.Group("/api/v1")
|
||||
h.RegisterRoutes(api)
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "/api/v1/docker/containers?server_id=server-uuid-123", nil)
|
||||
req := httptest.NewRequest(http.MethodGet, "/api/v1/docker/containers?server_id=server-uuid-123", http.NoBody)
|
||||
w := httptest.NewRecorder()
|
||||
router.ServeHTTP(w, req)
|
||||
|
||||
@@ -212,7 +212,7 @@ func TestDockerHandler_ListContainers_RemoteServerNotFound(t *testing.T) {
|
||||
api := router.Group("/api/v1")
|
||||
h.RegisterRoutes(api)
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "/api/v1/docker/containers?server_id=nonexistent-uuid", nil)
|
||||
req := httptest.NewRequest(http.MethodGet, "/api/v1/docker/containers?server_id=nonexistent-uuid", http.NoBody)
|
||||
w := httptest.NewRecorder()
|
||||
router.ServeHTTP(w, req)
|
||||
|
||||
@@ -245,7 +245,7 @@ func TestDockerHandler_ListContainers_InvalidHost(t *testing.T) {
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
req := httptest.NewRequest(http.MethodGet, "/api/v1/docker/containers?"+tt.hostParam, nil)
|
||||
req := httptest.NewRequest(http.MethodGet, "/api/v1/docker/containers?"+tt.hostParam, http.NoBody)
|
||||
w := httptest.NewRecorder()
|
||||
router.ServeHTTP(w, req)
|
||||
|
||||
@@ -296,7 +296,7 @@ func TestDockerHandler_ListContainers_DockerUnavailable(t *testing.T) {
|
||||
api := router.Group("/api/v1")
|
||||
h.RegisterRoutes(api)
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "/api/v1/docker/containers?host=local", nil)
|
||||
req := httptest.NewRequest(http.MethodGet, "/api/v1/docker/containers?host=local", http.NoBody)
|
||||
w := httptest.NewRecorder()
|
||||
router.ServeHTTP(w, req)
|
||||
|
||||
@@ -347,7 +347,7 @@ func TestDockerHandler_ListContainers_GenericError(t *testing.T) {
|
||||
api := router.Group("/api/v1")
|
||||
h.RegisterRoutes(api)
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "/api/v1/docker/containers", nil)
|
||||
req := httptest.NewRequest(http.MethodGet, "/api/v1/docker/containers", http.NoBody)
|
||||
w := httptest.NewRecorder()
|
||||
router.ServeHTTP(w, req)
|
||||
|
||||
|
||||
@@ -49,7 +49,7 @@ func TestListProfiles(t *testing.T) {
|
||||
}
|
||||
db.Create(&profile2)
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "/security/headers/profiles", nil)
|
||||
req := httptest.NewRequest(http.MethodGet, "/security/headers/profiles", http.NoBody)
|
||||
w := httptest.NewRecorder()
|
||||
router.ServeHTTP(w, req)
|
||||
|
||||
@@ -70,7 +70,7 @@ func TestGetProfile_ByID(t *testing.T) {
|
||||
}
|
||||
db.Create(&profile)
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, fmt.Sprintf("/security/headers/profiles/%d", profile.ID), nil)
|
||||
req := httptest.NewRequest(http.MethodGet, fmt.Sprintf("/security/headers/profiles/%d", profile.ID), http.NoBody)
|
||||
w := httptest.NewRecorder()
|
||||
router.ServeHTTP(w, req)
|
||||
|
||||
@@ -92,7 +92,7 @@ func TestGetProfile_ByUUID(t *testing.T) {
|
||||
}
|
||||
db.Create(&profile)
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "/security/headers/profiles/"+testUUID, nil)
|
||||
req := httptest.NewRequest(http.MethodGet, "/security/headers/profiles/"+testUUID, http.NoBody)
|
||||
w := httptest.NewRecorder()
|
||||
router.ServeHTTP(w, req)
|
||||
|
||||
@@ -108,7 +108,7 @@ func TestGetProfile_ByUUID(t *testing.T) {
|
||||
func TestGetProfile_NotFound(t *testing.T) {
|
||||
router, _ := setupSecurityHeadersTestRouter(t)
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "/security/headers/profiles/99999", nil)
|
||||
req := httptest.NewRequest(http.MethodGet, "/security/headers/profiles/99999", http.NoBody)
|
||||
w := httptest.NewRecorder()
|
||||
router.ServeHTTP(w, req)
|
||||
|
||||
@@ -222,7 +222,7 @@ func TestDeleteProfile(t *testing.T) {
|
||||
}
|
||||
db.Create(&profile)
|
||||
|
||||
req := httptest.NewRequest(http.MethodDelete, fmt.Sprintf("/security/headers/profiles/%d", profile.ID), nil)
|
||||
req := httptest.NewRequest(http.MethodDelete, fmt.Sprintf("/security/headers/profiles/%d", profile.ID), http.NoBody)
|
||||
w := httptest.NewRecorder()
|
||||
router.ServeHTTP(w, req)
|
||||
|
||||
@@ -244,7 +244,7 @@ func TestDeleteProfile_CannotDeletePreset(t *testing.T) {
|
||||
}
|
||||
db.Create(&preset)
|
||||
|
||||
req := httptest.NewRequest(http.MethodDelete, fmt.Sprintf("/security/headers/profiles/%d", preset.ID), nil)
|
||||
req := httptest.NewRequest(http.MethodDelete, fmt.Sprintf("/security/headers/profiles/%d", preset.ID), http.NoBody)
|
||||
w := httptest.NewRecorder()
|
||||
router.ServeHTTP(w, req)
|
||||
|
||||
@@ -270,7 +270,7 @@ func TestDeleteProfile_InUse(t *testing.T) {
|
||||
}
|
||||
db.Create(&host)
|
||||
|
||||
req := httptest.NewRequest(http.MethodDelete, fmt.Sprintf("/security/headers/profiles/%d", profile.ID), nil)
|
||||
req := httptest.NewRequest(http.MethodDelete, fmt.Sprintf("/security/headers/profiles/%d", profile.ID), http.NoBody)
|
||||
w := httptest.NewRecorder()
|
||||
router.ServeHTTP(w, req)
|
||||
|
||||
@@ -280,7 +280,7 @@ func TestDeleteProfile_InUse(t *testing.T) {
|
||||
func TestGetPresets(t *testing.T) {
|
||||
router, _ := setupSecurityHeadersTestRouter(t)
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "/security/headers/presets", nil)
|
||||
req := httptest.NewRequest(http.MethodGet, "/security/headers/presets", http.NoBody)
|
||||
w := httptest.NewRecorder()
|
||||
router.ServeHTTP(w, req)
|
||||
|
||||
@@ -491,7 +491,7 @@ func TestListProfiles_DBError(t *testing.T) {
|
||||
sqlDB, _ := db.DB()
|
||||
sqlDB.Close()
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "/security/headers/profiles", nil)
|
||||
req := httptest.NewRequest(http.MethodGet, "/security/headers/profiles", http.NoBody)
|
||||
w := httptest.NewRecorder()
|
||||
router.ServeHTTP(w, req)
|
||||
|
||||
@@ -502,7 +502,7 @@ func TestGetProfile_UUID_NotFound(t *testing.T) {
|
||||
router, _ := setupSecurityHeadersTestRouter(t)
|
||||
|
||||
// Use a UUID that doesn't exist
|
||||
req := httptest.NewRequest(http.MethodGet, "/security/headers/profiles/non-existent-uuid-12345", nil)
|
||||
req := httptest.NewRequest(http.MethodGet, "/security/headers/profiles/non-existent-uuid-12345", http.NoBody)
|
||||
w := httptest.NewRecorder()
|
||||
router.ServeHTTP(w, req)
|
||||
|
||||
@@ -516,7 +516,7 @@ func TestGetProfile_ID_DBError(t *testing.T) {
|
||||
sqlDB, _ := db.DB()
|
||||
sqlDB.Close()
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "/security/headers/profiles/1", nil)
|
||||
req := httptest.NewRequest(http.MethodGet, "/security/headers/profiles/1", http.NoBody)
|
||||
w := httptest.NewRecorder()
|
||||
router.ServeHTTP(w, req)
|
||||
|
||||
@@ -530,7 +530,7 @@ func TestGetProfile_UUID_DBError(t *testing.T) {
|
||||
sqlDB, _ := db.DB()
|
||||
sqlDB.Close()
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "/security/headers/profiles/some-uuid-format", nil)
|
||||
req := httptest.NewRequest(http.MethodGet, "/security/headers/profiles/some-uuid-format", http.NoBody)
|
||||
w := httptest.NewRecorder()
|
||||
router.ServeHTTP(w, req)
|
||||
|
||||
@@ -661,7 +661,7 @@ func TestUpdateProfile_LookupDBError(t *testing.T) {
|
||||
func TestDeleteProfile_InvalidID(t *testing.T) {
|
||||
router, _ := setupSecurityHeadersTestRouter(t)
|
||||
|
||||
req := httptest.NewRequest(http.MethodDelete, "/security/headers/profiles/invalid", nil)
|
||||
req := httptest.NewRequest(http.MethodDelete, "/security/headers/profiles/invalid", http.NoBody)
|
||||
w := httptest.NewRecorder()
|
||||
router.ServeHTTP(w, req)
|
||||
|
||||
@@ -671,7 +671,7 @@ func TestDeleteProfile_InvalidID(t *testing.T) {
|
||||
func TestDeleteProfile_NotFound(t *testing.T) {
|
||||
router, _ := setupSecurityHeadersTestRouter(t)
|
||||
|
||||
req := httptest.NewRequest(http.MethodDelete, "/security/headers/profiles/99999", nil)
|
||||
req := httptest.NewRequest(http.MethodDelete, "/security/headers/profiles/99999", http.NoBody)
|
||||
w := httptest.NewRecorder()
|
||||
router.ServeHTTP(w, req)
|
||||
|
||||
@@ -695,7 +695,7 @@ func TestDeleteProfile_LookupDBError(t *testing.T) {
|
||||
sqlDB, _ := db.DB()
|
||||
sqlDB.Close()
|
||||
|
||||
req := httptest.NewRequest(http.MethodDelete, "/security/headers/profiles/1", nil)
|
||||
req := httptest.NewRequest(http.MethodDelete, "/security/headers/profiles/1", http.NoBody)
|
||||
w := httptest.NewRecorder()
|
||||
router.ServeHTTP(w, req)
|
||||
|
||||
@@ -722,7 +722,7 @@ func TestDeleteProfile_CountDBError(t *testing.T) {
|
||||
handler := NewSecurityHeadersHandler(db, nil)
|
||||
handler.RegisterRoutes(router.Group("/"))
|
||||
|
||||
req := httptest.NewRequest(http.MethodDelete, fmt.Sprintf("/security/headers/profiles/%d", profile.ID), nil)
|
||||
req := httptest.NewRequest(http.MethodDelete, fmt.Sprintf("/security/headers/profiles/%d", profile.ID), http.NoBody)
|
||||
w := httptest.NewRecorder()
|
||||
router.ServeHTTP(w, req)
|
||||
|
||||
@@ -752,7 +752,7 @@ func TestDeleteProfile_DeleteDBError(t *testing.T) {
|
||||
sqlDB, _ := db.DB()
|
||||
sqlDB.Close()
|
||||
|
||||
req := httptest.NewRequest(http.MethodDelete, fmt.Sprintf("/security/headers/profiles/%d", profile.ID), nil)
|
||||
req := httptest.NewRequest(http.MethodDelete, fmt.Sprintf("/security/headers/profiles/%d", profile.ID), http.NoBody)
|
||||
w := httptest.NewRecorder()
|
||||
router.ServeHTTP(w, req)
|
||||
|
||||
@@ -863,7 +863,7 @@ func TestGetProfile_UUID_DBError_NonNotFound(t *testing.T) {
|
||||
sqlDB.Close()
|
||||
|
||||
// Use a valid UUID format to ensure we hit the UUID lookup path
|
||||
req := httptest.NewRequest(http.MethodGet, "/security/headers/profiles/550e8400-e29b-41d4-a716-446655440000", nil)
|
||||
req := httptest.NewRequest(http.MethodGet, "/security/headers/profiles/550e8400-e29b-41d4-a716-446655440000", http.NoBody)
|
||||
w := httptest.NewRecorder()
|
||||
router.ServeHTTP(w, req)
|
||||
|
||||
|
||||
@@ -46,7 +46,7 @@ func TestWebSocketStatusHandler_GetConnections(t *testing.T) {
|
||||
// Create test request
|
||||
w := httptest.NewRecorder()
|
||||
c, _ := gin.CreateTestContext(w)
|
||||
c.Request = httptest.NewRequest(http.MethodGet, "/api/v1/websocket/connections", nil)
|
||||
c.Request = httptest.NewRequest(http.MethodGet, "/api/v1/websocket/connections", http.NoBody)
|
||||
|
||||
// Call handler
|
||||
handler.GetConnections(c)
|
||||
@@ -73,7 +73,7 @@ func TestWebSocketStatusHandler_GetConnectionsEmpty(t *testing.T) {
|
||||
// Create test request
|
||||
w := httptest.NewRecorder()
|
||||
c, _ := gin.CreateTestContext(w)
|
||||
c.Request = httptest.NewRequest(http.MethodGet, "/api/v1/websocket/connections", nil)
|
||||
c.Request = httptest.NewRequest(http.MethodGet, "/api/v1/websocket/connections", http.NoBody)
|
||||
|
||||
// Call handler
|
||||
handler.GetConnections(c)
|
||||
@@ -121,7 +121,7 @@ func TestWebSocketStatusHandler_GetStats(t *testing.T) {
|
||||
// Create test request
|
||||
w := httptest.NewRecorder()
|
||||
c, _ := gin.CreateTestContext(w)
|
||||
c.Request = httptest.NewRequest(http.MethodGet, "/api/v1/websocket/stats", nil)
|
||||
c.Request = httptest.NewRequest(http.MethodGet, "/api/v1/websocket/stats", http.NoBody)
|
||||
|
||||
// Call handler
|
||||
handler.GetStats(c)
|
||||
@@ -149,7 +149,7 @@ func TestWebSocketStatusHandler_GetStatsEmpty(t *testing.T) {
|
||||
// Create test request
|
||||
w := httptest.NewRecorder()
|
||||
c, _ := gin.CreateTestContext(w)
|
||||
c.Request = httptest.NewRequest(http.MethodGet, "/api/v1/websocket/stats", nil)
|
||||
c.Request = httptest.NewRequest(http.MethodGet, "/api/v1/websocket/stats", http.NoBody)
|
||||
|
||||
// Call handler
|
||||
handler.GetStats(c)
|
||||
|
||||
Reference in New Issue
Block a user