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

Function TestContextAbortWithStatusJSON

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

Source from the content-addressed store, hash-verified

1833}
1834
1835func TestContextAbortWithStatusJSON(t *testing.T) {
1836 w := httptest.NewRecorder()
1837 c, _ := CreateTestContext(w)
1838 c.index = 4
1839
1840 in := new(testJSONAbortMsg)
1841 in.Bar = "barValue"
1842 in.Foo = "fooValue"
1843
1844 c.AbortWithStatusJSON(http.StatusUnsupportedMediaType, in)
1845
1846 assert.Equal(t, abortIndex, c.index)
1847 assert.Equal(t, http.StatusUnsupportedMediaType, c.Writer.Status())
1848 assert.Equal(t, http.StatusUnsupportedMediaType, w.Code)
1849 assert.True(t, c.IsAborted())
1850
1851 contentType := w.Header().Get("Content-Type")
1852 assert.Equal(t, "application/json; charset=utf-8", contentType)
1853
1854 buf := new(bytes.Buffer)
1855 _, err := buf.ReadFrom(w.Body)
1856 require.NoError(t, err)
1857 jsonStringBody := buf.String()
1858 assert.JSONEq(t, "{\"foo\":\"fooValue\",\"bar\":\"barValue\"}", jsonStringBody)
1859}
1860
1861func TestContextAbortWithStatusPureJSON(t *testing.T) {
1862 w := httptest.NewRecorder()

Callers

nothing calls this directly

Calls 7

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

Tested by

no test coverage detected