Skip to content

Commit

Permalink
Fix variable scope
Browse files Browse the repository at this point in the history
  • Loading branch information
vardius committed Sep 3, 2020
1 parent 417a5d2 commit d2e6c87
Show file tree
Hide file tree
Showing 5 changed files with 156 additions and 11 deletions.
44 changes: 44 additions & 0 deletions fasthttp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package gorouter

import (
"fmt"
"net/http"
"reflect"
"strings"
"testing"
Expand Down Expand Up @@ -622,3 +623,46 @@ func TestFastHTTPMountSubRouter_second(t *testing.T) {
t.Errorf("Router mount sub router error: %s", string(ctx.Response.Body()))
}
}

func TestFastHTTPMountSubRouter_third(t *testing.T) {
t.Parallel()

subRouter := NewFastHTTPRouter().(*fastHTTPRouter)

subRouter.GET("/", func(ctx *fasthttp.RequestCtx) { _, _ = fmt.Fprint(ctx, "GET[/]") })
subRouter.GET("/{id}", func(ctx *fasthttp.RequestCtx) { _, _ = fmt.Fprint(ctx, "GET[/{id}]") })
subRouter.GET("/me", func(ctx *fasthttp.RequestCtx) { _, _ = fmt.Fprint(ctx, "GET[/me]") })
subRouter.USE(http.MethodGet, "/me", func(h fasthttp.RequestHandler) fasthttp.RequestHandler {
return func(ctx *fasthttp.RequestCtx) {
_, _ = fmt.Fprint(ctx, "USE[/me]")
h(ctx)
}
})
subRouter.POST("/google/callback", func(ctx *fasthttp.RequestCtx) { _, _ = fmt.Fprint(ctx, "POST[/google/callback]") })
subRouter.POST("/facebook/callback", func(ctx *fasthttp.RequestCtx) { _, _ = fmt.Fprint(ctx, "POST[/facebook/callback]") })
subRouter.POST("/dispatch/{command}", func(ctx *fasthttp.RequestCtx) { _, _ = fmt.Fprint(ctx, "POST[/dispatch/{command}]") })
subRouter.USE(http.MethodPost, "/dispatch/some-else", func(h fasthttp.RequestHandler) fasthttp.RequestHandler {
return func(ctx *fasthttp.RequestCtx) {
_, _ = fmt.Fprint(ctx, "USE[/dispatch/some-else]")
h(ctx)
}
})

mainRouter := NewFastHTTPRouter().(*fastHTTPRouter)

mainRouter.Mount("/v1", subRouter.HandleFastHTTP)

ctx := buildFastHTTPRequestContext(fasthttp.MethodPost, "/v1/dispatch/some-else")
mainRouter.HandleFastHTTP(ctx)

if string(ctx.Response.Body()) != "USE[/dispatch/some-else]POST[/dispatch/{command}]" {
t.Errorf("subrouter route did not match: %s", ctx.Response.Body())
}

ctx = buildFastHTTPRequestContext(fasthttp.MethodGet, "/v1/me")
mainRouter.HandleFastHTTP(ctx)

if string(ctx.Response.Body()) != "USE[/me]GET[/me]" {
t.Errorf("subrouter route did not match: %s", ctx.Response.Body())
}
}
12 changes: 1 addition & 11 deletions nethttp.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,17 +258,7 @@ func newPathSlashesStripper(stripSlashes int) func(r *http.Request) *http.Reques
r2.URL = new(url.URL)
*r2.URL = *r.URL

p := r.URL.Path
for stripSlashes > 0 && len(p) > 0 {
n := strings.IndexByte(p[1:], '/')
if n < 0 {
p = p[:0]
break
}
p = p[n+1:]
stripSlashes--
}

p := pathutils.StripLeadingSlashes(r.URL.Path, stripSlashes)
if p != "" {
r2.URL.Path = p
} else {
Expand Down
78 changes: 78 additions & 0 deletions nethttp_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package gorouter

import (
"fmt"
"net/http"
"net/http/httptest"
"reflect"
Expand Down Expand Up @@ -702,3 +703,80 @@ func TestMountSubRouter_second(t *testing.T) {
t.Errorf("Router mount subrouter middleware error: %s", w.Body.String())
}
}

func TestMountSubRouter_third(t *testing.T) {
t.Parallel()

subRouter := New().(*router)

subRouter.GET("/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { _, _ = fmt.Fprint(w, "GET[/]") }))
subRouter.GET("/{id}", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { _, _ = fmt.Fprint(w, "GET[/{id}]") }))
subRouter.GET("/me", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { _, _ = fmt.Fprint(w, "GET[/me]") }))
subRouter.USE(http.MethodGet, "/me", func(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_, _ = fmt.Fprint(w, "USE[/me]")
h.ServeHTTP(w, r)
})
})
subRouter.POST("/google/callback", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { _, _ = fmt.Fprint(w, "POST[/google/callback]") }))
subRouter.POST("/facebook/callback", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { _, _ = fmt.Fprint(w, "POST[/facebook/callback]") }))
subRouter.POST("/dispatch/{command}", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { _, _ = fmt.Fprint(w, "POST[/dispatch/{command}]") }))
subRouter.USE(http.MethodPost, "/dispatch/some-else", func(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_, _ = fmt.Fprint(w, "USE[/dispatch/some-else]")
h.ServeHTTP(w, r)
})
})

mainRouter := New().(*router)

mainRouter.Mount("/v1", subRouter)

w := httptest.NewRecorder()
req, err := http.NewRequest(http.MethodGet, "/v1", nil)
if err != nil {
t.Fatal(err)
}

mainRouter.ServeHTTP(w, req)

if w.Body.String() != "GET[/]" {
t.Errorf("subrouter route did not match: %s", w.Body.String())
}

w = httptest.NewRecorder()
req, err = http.NewRequest(http.MethodPost, "/v1/dispatch/some-else", nil)
if err != nil {
t.Fatal(err)
}

mainRouter.ServeHTTP(w, req)

if w.Body.String() != "USE[/dispatch/some-else]POST[/dispatch/{command}]" {
t.Errorf("subrouter route did not match: %s", w.Body.String())
}

w = httptest.NewRecorder()
req, err = http.NewRequest(http.MethodGet, "/v1/me", nil)
if err != nil {
t.Fatal(err)
}

mainRouter.ServeHTTP(w, req)

if w.Body.String() != "USE[/me]GET[/me]" {
t.Errorf("subrouter route did not match: %s", w.Body.String())
}

w = httptest.NewRecorder()
req, err = http.NewRequest(http.MethodGet, "/v1", nil)
if err != nil {
t.Fatal(err)
}

mainRouter.ServeHTTP(w, req)

if w.Body.String() != "GET[/]" {
t.Errorf("subrouter route did not match: %s", w.Body.String())
}
}
13 changes: 13 additions & 0 deletions path/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,16 @@ func GetNameFromPart(pathPart string) (name string, exp string) {

return
}

func StripLeadingSlashes(path string, stripSlashes int) string {
for stripSlashes > 0 && len(path) > 0 {
n := strings.IndexByte(path[1:], '/')
if n < 0 {
path = path[:0]
break
}
path = path[n+1:]
stripSlashes--
}
return path
}
20 changes: 20 additions & 0 deletions path/path_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,23 @@ func TestGetNameFromPart(t *testing.T) {
})
}
}

func TestStripLeadingSlashes(t *testing.T) {
tests := []struct {
name string
path string
stripSlashes int
want string
}{
{"slashesCount = 0", "/foo/bar", 0, "/foo/bar"},
{"slashesCount = 1", "/foo/bar", 1, "/bar"},
{"slashesCount = 2", "/foo/bar", 2, ""},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := StripLeadingSlashes(tt.path, tt.stripSlashes); got != tt.want {
t.Errorf("StripLeadingSlashes() = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit d2e6c87

Please sign in to comment.