MCPcopy
hub / github.com/tinyauthapp/tinyauth / TestHealthController

Function TestHealthController

internal/controller/health_controller_test.go:15–73  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

13)
14
15func TestHealthController(t *testing.T) {
16 tlog.NewTestLogger().Init()
17 tests := []struct {
18 description string
19 path string
20 method string
21 expected string
22 }{
23 {
24 description: "Ensure health endpoint returns 200 OK",
25 path: "/api/healthz",
26 method: "GET",
27 expected: func() string {
28 expectedHealthResponse := map[string]any{
29 "status": 200,
30 "message": "Healthy",
31 }
32 bytes, err := json.Marshal(expectedHealthResponse)
33 assert.NoError(t, err)
34 return string(bytes)
35 }(),
36 },
37 {
38 description: "Ensure health endpoint returns 200 OK for HEAD request",
39 path: "/api/healthz",
40 method: "HEAD",
41 expected: func() string {
42 expectedHealthResponse := map[string]any{
43 "status": 200,
44 "message": "Healthy",
45 }
46 bytes, err := json.Marshal(expectedHealthResponse)
47 assert.NoError(t, err)
48 return string(bytes)
49 }(),
50 },
51 }
52
53 for _, test := range tests {
54 t.Run(test.description, func(t *testing.T) {
55 router := gin.Default()
56 group := router.Group("/api")
57 gin.SetMode(gin.TestMode)
58
59 healthController := controller.NewHealthController(group)
60 healthController.SetupRoutes()
61
62 recorder := httptest.NewRecorder()
63
64 request, err := http.NewRequest(test.method, test.path, nil)
65 assert.NoError(t, err)
66
67 router.ServeHTTP(recorder, request)
68
69 assert.Equal(t, http.StatusOK, recorder.Code)
70 assert.Equal(t, test.expected, recorder.Body.String())
71 })
72 }

Callers

nothing calls this directly

Calls 4

SetupRoutesMethod · 0.95
NewTestLoggerFunction · 0.92
NewHealthControllerFunction · 0.92
InitMethod · 0.45

Tested by

no test coverage detected