Skip to content
Myr

fix(context): Copy() copies Errors and Accepted fields (#4695)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* Previously, Copy() only copied Keys and Params, leaving Errors and Accepted as nil even when set on the original context. Goroutines receiving a copied context could not observe errors attached before the copy, and content-negotiation state set by middleware was silently lost. * Replace assert.Equal(t, len(c.Errors), len(cp.Errors)) with assert.Len(t, cp.Errors, 2) to satisfy the testifylint linter rule ---------

authored by Amirhf · committed by GitHub · parent 2e4d4f3 · Browse files · 2 files+54 added−0 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Trailers

  • Co-authored-by: Bo-Yi Wu <appleboy.tw@gmail.com>

Diff stats

2 files+54 added−0 removed

  • other:+54 added−0 removed

2 files+54 added−0 removed

modified:context_test.go+44 added
687 unmodified lines
688 assert.NotEqual(t, cp.Keys["foo"], c.Keys["foo"])689 assert.Equal(t, cp.fullPath, c.fullPath)690}691692func TestContextCopyCopiesErrors(t *testing.T) {693 c, _ := CreateTestContext(httptest.NewRecorder())694 c.Request, _ = http.NewRequest(http.MethodGet, "/", nil)695 _ = c.Error(errors.New("first error"))696 _ = c.Error(errors.New("second error"))697698 cp := c.Copy()699700 // copied context has the same errors701 assert.Len(t, cp.Errors, 2)702 assert.Equal(t, c.Errors[0].Error(), cp.Errors[0].Error())703 assert.Equal(t, c.Errors[1].Error(), cp.Errors[1].Error())704705 // mutations on the copy do not affect the original706 _ = cp.Error(errors.New("third error"))707 assert.Len(t, c.Errors, 2)708 assert.Len(t, cp.Errors, 3)709}710711func TestContextCopyCopiesAccepted(t *testing.T) {712 c, _ := CreateTestContext(httptest.NewRecorder())713 c.Request, _ = http.NewRequest(http.MethodGet, "/", nil)714 c.SetAccepted("application/json", "text/html")715716 cp := c.Copy()717718 assert.Equal(t, c.Accepted, cp.Accepted)719720 // mutations on the copy do not affect the original721 cp.SetAccepted("text/plain")722 assert.Equal(t, []string{"application/json", "text/html"}, c.Accepted)723 assert.Equal(t, []string{"text/plain"}, cp.Accepted)724}725726func TestContextCopyNilErrorsAndAccepted(t *testing.T) {727 c, _ := CreateTestContext(httptest.NewRecorder())728 c.Request, _ = http.NewRequest(http.MethodGet, "/", nil)729730 cp := c.Copy()731732 assert.Nil(t, cp.Errors)733 assert.Nil(t, cp.Accepted)734}735736func TestContextHandlerName(t *testing.T) {737 c, _ := CreateTestContext(httptest.NewRecorder())738 c.handlers = HandlersChain{func(c *Context) {}, handlerNameTest}739
3175 unmodified lines
modified:context.go+10 added
139 unmodified lines
140 cParams := c.Params141 cp.Params = make([]Param, len(cParams))142 copy(cp.Params, cParams)143144 if c.Errors != nil {145 cp.Errors = make(errorMsgs, len(c.Errors))146 copy(cp.Errors, c.Errors)147 }148149 if c.Accepted != nil {150 cp.Accepted = make([]string, len(c.Accepted))151 copy(cp.Accepted, c.Accepted)152 }153154 return &cp155}156157// HandlerName returns the main handler's name. For example if the handler is "handleGetUsers()",
1369 unmodified lines