diff --git a/context_test.go b/context_test.go index 5b63a647..7ddc4079 100644 --- a/context_test.go +++ b/context_test.go @@ -9,6 +9,7 @@ import ( "context" "errors" "fmt" + "github.com/gin-gonic/gin/render" "html/template" "io" "io/fs" @@ -3123,3 +3124,32 @@ func TestContextNext(t *testing.T) { assert.True(t, exists) assert.Equal(t, "value3", value) } + +// MyJSON customizing JSON rendering +type MyJSON struct { + Data any + render.JSON +} + +// Render rewrite the Render function +func (r MyJSON) Render(w http.ResponseWriter) error { + _, err := w.Write([]byte("test")) + return err +} + +func NewMyJSON(data any) render.Render { + return &MyJSON{Data: data} +} + +// TestCustomJSONRender the test uses a custom JSON render. +// The final result is that the user can customize the JSON render without affecting the original function. +func TestCustomJSONRender(t *testing.T) { + w := httptest.NewRecorder() + c, _ := CreateTestContext(w) + c.RegisterJsonRender("MyTestJSON", NewMyJSON) + c.SetJSONRender("MyTestJSON") + + c.JSON(http.StatusCreated, H{"foo": "bar", "html": ""}) + + t.Log(w.Body.String()) +}