Files
user-system/internal/api/middleware/response_wrapper_test.go

120 lines
3.1 KiB
Go

package middleware
import (
"net/http"
"net/http/httptest"
"testing"
"github.com/gin-gonic/gin"
)
func TestResponseWrapper_WrapsSuccessfulJSONPayload(t *testing.T) {
gin.SetMode(gin.TestMode)
recorder := httptest.NewRecorder()
router := gin.New()
router.Use(ResponseWrapper())
router.GET("/users", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"id": 1, "name": "alice"})
})
req := httptest.NewRequest(http.MethodGet, "/users", nil)
router.ServeHTTP(recorder, req)
if recorder.Code != http.StatusOK {
t.Fatalf("expected 200, got %d", recorder.Code)
}
want := `{"code":0,"data":{"id":1,"name":"alice"},"message":"success"}`
if got := recorder.Body.String(); got != want {
t.Fatalf("body = %s, want %s", got, want)
}
}
func TestResponseWrapper_PassesThroughMarkedResponses(t *testing.T) {
gin.SetMode(gin.TestMode)
recorder := httptest.NewRecorder()
router := gin.New()
router.Use(ResponseWrapper())
router.GET("/users", func(c *gin.Context) {
WrapResponse(c)
c.JSON(http.StatusOK, gin.H{"code": 0, "message": "already wrapped"})
})
req := httptest.NewRequest(http.MethodGet, "/users", nil)
router.ServeHTTP(recorder, req)
if recorder.Code != http.StatusOK {
t.Fatalf("expected 200, got %d", recorder.Code)
}
want := `{"code":0,"message":"already wrapped"}`
if got := recorder.Body.String(); got != want {
t.Fatalf("body = %s, want %s", got, want)
}
}
func TestResponseWrapper_PassesThroughNonSuccessStatus(t *testing.T) {
gin.SetMode(gin.TestMode)
recorder := httptest.NewRecorder()
router := gin.New()
router.Use(ResponseWrapper())
router.GET("/users", func(c *gin.Context) {
c.JSON(http.StatusBadRequest, gin.H{"message": "bad request"})
})
req := httptest.NewRequest(http.MethodGet, "/users", nil)
router.ServeHTTP(recorder, req)
if recorder.Code != http.StatusBadRequest {
t.Fatalf("expected 400, got %d", recorder.Code)
}
want := `{"message":"bad request"}`
if got := recorder.Body.String(); got != want {
t.Fatalf("body = %s, want %s", got, want)
}
}
func TestResponseWrapper_PassesThroughInvalidJSON(t *testing.T) {
gin.SetMode(gin.TestMode)
recorder := httptest.NewRecorder()
router := gin.New()
router.Use(ResponseWrapper())
router.GET("/users", func(c *gin.Context) {
c.Writer.WriteHeader(http.StatusOK)
_, _ = c.Writer.WriteString("plain text")
})
req := httptest.NewRequest(http.MethodGet, "/users", nil)
router.ServeHTTP(recorder, req)
if recorder.Code != http.StatusOK {
t.Fatalf("expected 200, got %d", recorder.Code)
}
if got := recorder.Body.String(); got != "plain text" {
t.Fatalf("body = %q, want plain text", got)
}
}
func TestResponseWrapper_NoWrapperMarksContext(t *testing.T) {
gin.SetMode(gin.TestMode)
recorder := httptest.NewRecorder()
router := gin.New()
router.Use(NoWrapper())
router.GET("/users", func(c *gin.Context) {
if _, exists := c.Get("response_wrapped"); !exists {
t.Fatal("expected response_wrapped marker in context")
}
c.JSON(http.StatusOK, gin.H{"ok": true})
})
req := httptest.NewRequest(http.MethodGet, "/users", nil)
router.ServeHTTP(recorder, req)
if recorder.Code != http.StatusOK {
t.Fatalf("expected 200, got %d", recorder.Code)
}
}