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

Function TestInterceptedHeader

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

Source from the content-addressed store, hash-verified

3350}
3351
3352func TestInterceptedHeader(t *testing.T) {
3353 w := httptest.NewRecorder()
3354 c, r := CreateTestContext(w)
3355
3356 r.Use(func(c *Context) {
3357 i := interceptedWriter{
3358 ResponseWriter: c.Writer,
3359 b: bytes.NewBuffer(nil),
3360 }
3361 c.Writer = i
3362 c.Next()
3363 c.Header("X-Test", "overridden")
3364 c.Writer = i.ResponseWriter
3365 })
3366 r.GET("/", func(c *Context) {
3367 c.Header("X-Test", "original")
3368 c.Header("X-Test-2", "present")
3369 c.String(http.StatusOK, "hello world")
3370 })
3371 c.Request = httptest.NewRequest(http.MethodGet, "/", nil)
3372 r.HandleContext(c)
3373 // Result() has headers frozen when WriteHeaderNow() has been called
3374 // Compared to this time, this is when the response headers will be flushed
3375 // As response is flushed on c.String, the Header cannot be set by the first
3376 // middleware. Assert this
3377 assert.Empty(t, w.Result().Header.Get("X-Test"))
3378 assert.Equal(t, "present", w.Result().Header.Get("X-Test-2"))
3379}
3380
3381func TestContextNext(t *testing.T) {
3382 c, _ := CreateTestContext(httptest.NewRecorder())

Callers

nothing calls this directly

Calls 8

CreateTestContextFunction · 0.85
NextMethod · 0.80
HandleContextMethod · 0.80
UseMethod · 0.65
GETMethod · 0.65
StringMethod · 0.65
HeaderMethod · 0.45
GetMethod · 0.45

Tested by

no test coverage detected