mirror of
https://github.com/steveiliop56/tinyauth.git
synced 2026-03-31 02:47:56 +00:00
refactor: tests (#731)
* tests: rework tests for context controller * tests: add tests for health controller * tests: add tests for oidc controller * tests: use testify assert in context and health controller * tests: add tests for user controller * tests: add tests for resources controller * tests: add well known controller tests * test: add proxy controller tests * chore: review comments * chore: more review comments * chore: cancel lockdown in testing * tests: fix get cookie domain tests * chore: add comment for testing passwords
This commit is contained in:
71
internal/controller/health_controller_test.go
Normal file
71
internal/controller/health_controller_test.go
Normal file
@@ -0,0 +1,71 @@
|
||||
package controller_test
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/steveiliop56/tinyauth/internal/controller"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestHealthController(t *testing.T) {
|
||||
tests := []struct {
|
||||
description string
|
||||
path string
|
||||
method string
|
||||
expected string
|
||||
}{
|
||||
{
|
||||
description: "Ensure health endpoint returns 200 OK",
|
||||
path: "/api/healthz",
|
||||
method: "GET",
|
||||
expected: func() string {
|
||||
expectedHealthResponse := map[string]any{
|
||||
"status": 200,
|
||||
"message": "Healthy",
|
||||
}
|
||||
bytes, err := json.Marshal(expectedHealthResponse)
|
||||
assert.NoError(t, err)
|
||||
return string(bytes)
|
||||
}(),
|
||||
},
|
||||
{
|
||||
description: "Ensure health endpoint returns 200 OK for HEAD request",
|
||||
path: "/api/healthz",
|
||||
method: "HEAD",
|
||||
expected: func() string {
|
||||
expectedHealthResponse := map[string]any{
|
||||
"status": 200,
|
||||
"message": "Healthy",
|
||||
}
|
||||
bytes, err := json.Marshal(expectedHealthResponse)
|
||||
assert.NoError(t, err)
|
||||
return string(bytes)
|
||||
}(),
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.description, func(t *testing.T) {
|
||||
router := gin.Default()
|
||||
group := router.Group("/api")
|
||||
gin.SetMode(gin.TestMode)
|
||||
|
||||
healthController := controller.NewHealthController(group)
|
||||
healthController.SetupRoutes()
|
||||
|
||||
recorder := httptest.NewRecorder()
|
||||
|
||||
request, err := http.NewRequest(test.method, test.path, nil)
|
||||
assert.NoError(t, err)
|
||||
|
||||
router.ServeHTTP(recorder, request)
|
||||
|
||||
assert.Equal(t, http.StatusOK, recorder.Code)
|
||||
assert.Equal(t, test.expected, recorder.Body.String())
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user