Skip to content
Myr

test(engine): add regression tests for HandleContext with NoRoute (#4571)

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

Add two regression tests for GitHub issue #1848 to verify that Context.handlers are properly isolated in HandleContext when used from a NoRoute handler with group and engine middleware.

authored by Abhiyan Khanal · committed by GitHub · parent ecd26c8 · Browse files · 1 file+72 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: Claude Opus 4.6 <noreply@anthropic.com>
  • Co-authored-by: Bo-Yi Wu <appleboy.tw@gmail.com>

Diff stats

1 file+72 added−0 removed

  • other:+72 added−0 removed

1 file+72 added−0 removed

modified:gin_test.go+72 added
741 unmodified lines
742 assert.Equal(t, int64(2), middlewareCounterV1)743 assert.Equal(t, int64(1), handlerCounterV2)744}745746func TestEngineHandleContextNoRouteWithGroupMiddleware(t *testing.T) {747 // Scenario from issue #1848:748 // - Engine with no global middleware (gin.New())749 // - A group with middleware750 // - A route in that group751 // - NoRoute handler that redirects via HandleContext752 // The group middleware should run exactly once per HandleContext call,753 // not accumulate across redirects.754755 var middlewareCount, handlerCount int64756757 r := New()758 grp := r.Group("", func(c *Context) {759 atomic.AddInt64(&middlewareCount, 1)760 c.Next()761 })762 grp.GET("/target", func(c *Context) {763 atomic.AddInt64(&handlerCount, 1)764 c.String(http.StatusOK, "ok")765 })766767 r.NoRoute(func(c *Context) {768 c.Request.URL.Path = "/target"769 r.HandleContext(c)770 })771772 // Access a non-existent route to trigger NoRoute -> HandleContext773 w := PerformRequest(r, "GET", "/nonexistent")774 assert.Equal(t, http.StatusOK, w.Code)775 assert.Equal(t, "ok", w.Body.String())776 // Middleware and handler should each run exactly once777 assert.Equal(t, int64(1), atomic.LoadInt64(&middlewareCount))778 assert.Equal(t, int64(1), atomic.LoadInt64(&handlerCount))779}780781func TestEngineHandleContextNoRouteWithEngineMiddleware(t *testing.T) {782 // When engine middleware exists and NoRoute redirects via HandleContext,783 // verify the handlers run the expected number of times.784785 var engineMwCount, groupMwCount, handlerCount int64786787 r := New()788 r.Use(func(c *Context) {789 atomic.AddInt64(&engineMwCount, 1)790 c.Next()791 })792793 grp := r.Group("", func(c *Context) {794 atomic.AddInt64(&groupMwCount, 1)795 c.Next()796 })797 grp.GET("/target", func(c *Context) {798 atomic.AddInt64(&handlerCount, 1)799 c.String(http.StatusOK, "ok")800 })801802 r.NoRoute(func(c *Context) {803 c.Request.URL.Path = "/target"804 r.HandleContext(c)805 })806807 w := PerformRequest(r, "GET", "/nonexistent")808 assert.Equal(t, http.StatusOK, w.Code)809 assert.Equal(t, "ok", w.Body.String())810 // Handler and group middleware should each run once (from HandleContext)811 assert.Equal(t, int64(1), atomic.LoadInt64(&handlerCount))812 assert.Equal(t, int64(1), atomic.LoadInt64(&groupMwCount))813 // Engine middleware runs twice: once for the NoRoute chain, once for the HandleContext chain814 // This is expected behavior since HandleContext re-enters the full handler chain815 assert.Equal(t, int64(2), atomic.LoadInt64(&engineMwCount))816}817818func TestEngineHandleContextUseEscapedPathPercentEncoded(t *testing.T) {819 r := New()820 r.UseEscapedPath = true821 r.UnescapePathValues = false
337 unmodified lines