MCPcopy
hub / github.com/gin-gonic/gin / TestRenderSecureJSON

Function TestRenderSecureJSON

render/render_test.go:74–100  ·  render/render_test.go::TestRenderSecureJSON
(t *testing.T)

Source from the content-addressed store, hash-verified

72}
73
74func TestRenderSecureJSON(t *testing.T) {
75 w1 := httptest.NewRecorder()
76 data := map[string]any{
77 "foo": "bar",
78 }
79
80 (SecureJSON{"while(1);", data}).WriteContentType(w1)
81 assert.Equal(t, "application/json; charset=utf-8", w1.Header().Get("Content-Type"))
82
83 err1 := (SecureJSON{"while(1);", data}).Render(w1)
84
85 require.NoError(t, err1)
86 assert.JSONEq(t, "{\"foo\":\"bar\"}", w1.Body.String())
87 assert.Equal(t, "application/json; charset=utf-8", w1.Header().Get("Content-Type"))
88
89 w2 := httptest.NewRecorder()
90 datas := []map[string]any{{
91 "foo": "bar",
92 }, {
93 "bar": "foo",
94 }}
95
96 err2 := (SecureJSON{"while(1);", datas}).Render(w2)
97 require.NoError(t, err2)
98 assert.Equal(t, "while(1);[{\"foo\":\"bar\"},{\"bar\":\"foo\"}]", w2.Body.String())
99 assert.Equal(t, "application/json; charset=utf-8", w2.Header().Get("Content-Type"))
100}
101
102func TestRenderSecureJSONFail(t *testing.T) {
103 w := httptest.NewRecorder()

Callers

nothing calls this directly

Calls 5

WriteContentTypeMethod · 0.65
RenderMethod · 0.65
StringMethod · 0.65
GetMethod · 0.45
HeaderMethod · 0.45

Tested by

no test coverage detected