Skip to content

Commit

Permalink
fix many redirects (gin-gonic#1760) (gin-gonic#1764)
Browse files Browse the repository at this point in the history
* fix many redirects (gin-gonic#1760)

* fix @thinkerou review
  • Loading branch information
vkd authored and thinkerou committed Feb 18, 2019
1 parent 5acf660 commit a768f06
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
3 changes: 3 additions & 0 deletions gin.go
Original file line number Diff line number Diff line change
Expand Up @@ -355,8 +355,11 @@ func (engine *Engine) ServeHTTP(w http.ResponseWriter, req *http.Request) {
// This can be done by setting c.Request.URL.Path to your new target.
// Disclaimer: You can loop yourself to death with this, use wisely.
func (engine *Engine) HandleContext(c *Context) {
oldIndexValue := c.index
c.reset()
engine.handleHTTPRequest(c)

c.index = oldIndexValue
}

func (engine *Engine) handleHTTPRequest(c *Context) {
Expand Down
39 changes: 39 additions & 0 deletions gin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (
"net/http"
"net/http/httptest"
"reflect"
"strconv"
"sync/atomic"
"testing"
"time"

Expand Down Expand Up @@ -488,6 +490,43 @@ func TestEngineHandleContext(t *testing.T) {
})
}

func TestEngineHandleContextManyReEntries(t *testing.T) {
expectValue := 10000

var handlerCounter, middlewareCounter int64

r := New()
r.Use(func(c *Context) {
atomic.AddInt64(&middlewareCounter, 1)
})
r.GET("/:count", func(c *Context) {
countStr := c.Param("count")
count, err := strconv.Atoi(countStr)
assert.NoError(t, err)

n, err := c.Writer.Write([]byte("."))
assert.NoError(t, err)
assert.Equal(t, 1, n)

switch {
case count > 0:
c.Request.URL.Path = "/" + strconv.Itoa(count-1)
r.HandleContext(c)
}
}, func(c *Context) {
atomic.AddInt64(&handlerCounter, 1)
})

assert.NotPanics(t, func() {
w := performRequest(r, "GET", "/"+strconv.Itoa(expectValue-1)) // include 0 value
assert.Equal(t, 200, w.Code)
assert.Equal(t, expectValue, w.Body.Len())
})

assert.Equal(t, int64(expectValue), handlerCounter)
assert.Equal(t, int64(expectValue), middlewareCounter)
}

func assertRoutePresent(t *testing.T, gotRoutes RoutesInfo, wantRoute RouteInfo) {
for _, gotRoute := range gotRoutes {
if gotRoute.Path == wantRoute.Path && gotRoute.Method == wantRoute.Method {
Expand Down

0 comments on commit a768f06

Please sign in to comment.