Test_Ctx_JSON() — fiber Function Reference
Architecture documentation for the Test_Ctx_JSON() function in ctx_test.go from the fiber codebase.
Entity Profile
Dependency Diagram
graph TD bb2c7974_fbb3_472a_f969_74777ec14c0d["Test_Ctx_JSON()"] 7b3d4933_5ae3_f84d_ff6d_0cb34e268026["ctx_test.go"] bb2c7974_fbb3_472a_f969_74777ec14c0d -->|defined in| 7b3d4933_5ae3_f84d_ff6d_0cb34e268026 style bb2c7974_fbb3_472a_f969_74777ec14c0d fill:#6366f1,stroke:#818cf8,color:#fff
Relationship Graph
Source Code
ctx_test.go lines 5837–5891
func Test_Ctx_JSON(t *testing.T) {
t.Parallel()
app := New()
c := app.AcquireCtx(&fasthttp.RequestCtx{})
require.Error(t, c.JSON(complex(1, 1)))
// Test without ctype
err := c.JSON(Map{ // map has no order
"Name": "Grame",
"Age": 20,
})
require.NoError(t, err)
require.JSONEq(t, `{"Age":20,"Name":"Grame"}`, string(c.Response().Body()))
require.Equal(t, "application/json; charset=utf-8", string(c.Response().Header.Peek("content-type")))
// Test with ctype
err = c.JSON(Map{ // map has no order
"Name": "Grame",
"Age": 20,
}, "application/problem+json")
require.NoError(t, err)
require.JSONEq(t, `{"Age":20,"Name":"Grame"}`, string(c.Response().Body()))
require.Equal(t, "application/problem+json", string(c.Response().Header.Peek("content-type")))
testEmpty := func(v any, r string) {
err := c.JSON(v)
require.NoError(t, err)
require.Equal(t, r, string(c.Response().Body()))
}
testEmpty(nil, "null")
testEmpty("", `""`)
testEmpty(0, "0")
testEmpty([]int{}, "[]")
t.Run("custom json encoder", func(t *testing.T) {
t.Parallel()
app := New(Config{
JSONEncoder: func(_ any) ([]byte, error) {
return []byte(`["custom","json"]`), nil
},
})
c := app.AcquireCtx(&fasthttp.RequestCtx{})
err := c.JSON(Map{ // map has no order
"Name": "Grame",
"Age": 20,
})
require.NoError(t, err)
require.Equal(t, `["custom","json"]`, string(c.Response().Body()))
require.Equal(t, "application/json; charset=utf-8", string(c.Response().Header.Peek("content-type")))
})
}
Domain
Subdomains
Defined In
Source
Frequently Asked Questions
What does Test_Ctx_JSON() do?
Test_Ctx_JSON() is a function in the fiber codebase, defined in ctx_test.go.
Where is Test_Ctx_JSON() defined?
Test_Ctx_JSON() is defined in ctx_test.go at line 5837.
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free