You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
91 lines
2.5 KiB
Go
91 lines
2.5 KiB
Go
package httputil
|
|
|
|
// Tests for HTTP response helpers and health status derivation.
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"github.com/gofiber/fiber/v3"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
// TestNewPaginatedResponse ensures new paginated response behavior is handled correctly.
|
|
func TestNewPaginatedResponse(t *testing.T) {
|
|
resp := NewPaginatedResponse([]string{"a"}, 1, 2, 3)
|
|
require.NotNil(t, resp.Meta)
|
|
require.Equal(t, 2, resp.Meta.TotalPages)
|
|
}
|
|
|
|
// TestHealthStatus ensures health status behavior is handled correctly.
|
|
func TestHealthStatus(t *testing.T) {
|
|
app := fiber.New()
|
|
app.Get("/health", func(c fiber.Ctx) error {
|
|
return Health(c, "service", "0.1.0", map[string]string{
|
|
"db": "ok",
|
|
"cache": "down",
|
|
})
|
|
})
|
|
|
|
req := httptest.NewRequest(http.MethodGet, "/health", nil)
|
|
resp, err := app.Test(req)
|
|
require.NoError(t, err)
|
|
defer resp.Body.Close()
|
|
|
|
var body HealthResponse
|
|
require.NoError(t, json.NewDecoder(resp.Body).Decode(&body))
|
|
require.Equal(t, "unhealthy", body.Status)
|
|
}
|
|
|
|
// TestResponseHelpers ensures response helpers behavior is handled correctly.
|
|
func TestResponseHelpers(t *testing.T) {
|
|
app := fiber.New()
|
|
app.Get("/ok", func(c fiber.Ctx) error {
|
|
return OK(c, fiber.Map{"ok": true})
|
|
})
|
|
app.Post("/created", func(c fiber.Ctx) error {
|
|
return Created(c, fiber.Map{"id": "1"})
|
|
})
|
|
app.Delete("/no-content", func(c fiber.Ctx) error {
|
|
return NoContent(c)
|
|
})
|
|
app.Get("/paginated", func(c fiber.Ctx) error {
|
|
return Paginated(c, []string{"a"}, 1, 2, 3)
|
|
})
|
|
app.Get("/message", func(c fiber.Ctx) error {
|
|
return Message(c, "hello")
|
|
})
|
|
|
|
req := httptest.NewRequest(http.MethodGet, "/ok", nil)
|
|
resp, err := app.Test(req)
|
|
require.NoError(t, err)
|
|
defer resp.Body.Close()
|
|
require.Equal(t, http.StatusOK, resp.StatusCode)
|
|
|
|
req = httptest.NewRequest(http.MethodPost, "/created", nil)
|
|
resp, err = app.Test(req)
|
|
require.NoError(t, err)
|
|
defer resp.Body.Close()
|
|
require.Equal(t, http.StatusCreated, resp.StatusCode)
|
|
|
|
req = httptest.NewRequest(http.MethodDelete, "/no-content", nil)
|
|
resp, err = app.Test(req)
|
|
require.NoError(t, err)
|
|
defer resp.Body.Close()
|
|
require.Equal(t, http.StatusNoContent, resp.StatusCode)
|
|
|
|
req = httptest.NewRequest(http.MethodGet, "/paginated", nil)
|
|
resp, err = app.Test(req)
|
|
require.NoError(t, err)
|
|
defer resp.Body.Close()
|
|
require.Equal(t, http.StatusOK, resp.StatusCode)
|
|
|
|
req = httptest.NewRequest(http.MethodGet, "/message", nil)
|
|
resp, err = app.Test(req)
|
|
require.NoError(t, err)
|
|
defer resp.Body.Close()
|
|
require.Equal(t, http.StatusOK, resp.StatusCode)
|
|
}
|