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

Function TestContextAbortWithStatusPureJSON

context_test.go:1861–1885  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

1859}
1860
1861func TestContextAbortWithStatusPureJSON(t *testing.T) {
1862 w := httptest.NewRecorder()
1863 c, _ := CreateTestContext(w)
1864 c.index = 4
1865
1866 in := new(testJSONAbortMsg)
1867 in.Bar = "barValue"
1868 in.Foo = "fooValue"
1869
1870 c.AbortWithStatusPureJSON(http.StatusUnsupportedMediaType, in)
1871
1872 assert.Equal(t, abortIndex, c.index)
1873 assert.Equal(t, http.StatusUnsupportedMediaType, c.Writer.Status())
1874 assert.Equal(t, http.StatusUnsupportedMediaType, w.Code)
1875 assert.True(t, c.IsAborted())
1876
1877 contentType := w.Header().Get("Content-Type")
1878 assert.Equal(t, "application/json; charset=utf-8", contentType)
1879
1880 buf := new(bytes.Buffer)
1881 _, err := buf.ReadFrom(w.Body)
1882 require.NoError(t, err)
1883 jsonStringBody := buf.String()
1884 assert.JSONEq(t, "{\"foo\":\"fooValue\",\"bar\":\"barValue\"}", jsonStringBody)
1885}
1886
1887func TestContextError(t *testing.T) {
1888 c, _ := CreateTestContext(httptest.NewRecorder())

Callers

nothing calls this directly

Calls 7

CreateTestContextFunction · 0.85
IsAbortedMethod · 0.80
StatusMethod · 0.65
StringMethod · 0.65
GetMethod · 0.45
HeaderMethod · 0.45

Tested by

no test coverage detected