Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: go-chi/chi
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v3.1.0
Choose a base ref
...
head repository: go-chi/chi
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v3.1.1
Choose a head ref
  • 3 commits
  • 4 files changed
  • 2 contributors

Commits on Jul 12, 2017

  1. Fix typo (#228)

    solarhell authored and Peter Kieltyka committed Jul 12, 2017
    Copy the full SHA
    4c5a584 View commit details

Commits on Jul 21, 2017

  1. Add test case for mux mounts

    Peter Kieltyka committed Jul 21, 2017
    Copy the full SHA
    a40fe4f View commit details
  2. Merge branch 'master' of github.com:go-chi/chi

    Peter Kieltyka committed Jul 21, 2017

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature.
    Copy the full SHA
    12a98d3 View commit details
Showing with 87 additions and 23 deletions.
  1. +1 −1 _examples/rest/main.go
  2. +81 −0 mux_test.go
  3. +5 −1 tree.go
  4. +0 −21 tree_test.go
2 changes: 1 addition & 1 deletion _examples/rest/main.go
Original file line number Diff line number Diff line change
@@ -441,7 +441,7 @@ type User struct {
// and powerful data persistence adapter.
type Article struct {
ID string `json:"id"`
UserID int64 `json:"user_id` // the author
UserID int64 `json:"user_id"` // the author
Title string `json:"title"`
Slug string `json:"slug"`
}
81 changes: 81 additions & 0 deletions mux_test.go
Original file line number Diff line number Diff line change
@@ -207,6 +207,43 @@ func TestMuxBasic(t *testing.T) {
}
}

func TestMuxMounts(t *testing.T) {
r := NewRouter()

r.Get("/{hash}", func(w http.ResponseWriter, r *http.Request) {
v := URLParam(r, "hash")
w.Write([]byte(fmt.Sprintf("/%s", v)))
})

r.Route("/{hash}/share", func(r Router) {
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
v := URLParam(r, "hash")
w.Write([]byte(fmt.Sprintf("/%s/share", v)))
})
r.Get("/{network}", func(w http.ResponseWriter, r *http.Request) {
v := URLParam(r, "hash")
n := URLParam(r, "network")
w.Write([]byte(fmt.Sprintf("/%s/share/%s", v, n)))
})
})

m := NewRouter()
m.Mount("/sharing", r)

ts := httptest.NewServer(m)
defer ts.Close()

if _, body := testRequest(t, ts, "GET", "/sharing/aBc", nil); body != "/aBc" {
t.Fatalf(body)
}
if _, body := testRequest(t, ts, "GET", "/sharing/aBc/share", nil); body != "/aBc/share" {
t.Fatalf(body)
}
if _, body := testRequest(t, ts, "GET", "/sharing/aBc/share/twitter", nil); body != "/aBc/share/twitter" {
t.Fatalf(body)
}
}

func TestMuxPlain(t *testing.T) {
r := NewRouter()
r.Get("/hi", func(w http.ResponseWriter, r *http.Request) {
@@ -1497,3 +1534,47 @@ func (tfi *testFileInfo) Mode() os.FileMode { return 0755 }
func (tfi *testFileInfo) ModTime() time.Time { return time.Now() }
func (tfi *testFileInfo) IsDir() bool { return false }
func (tfi *testFileInfo) Sys() interface{} { return nil }

func BenchmarkMux(b *testing.B) {
h1 := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})
h2 := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})
h3 := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})
h4 := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})
h5 := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})
h6 := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})

mx := NewRouter()
mx.Get("/", h1)
mx.Get("/hi", h2)
mx.Get("/sup/{id}/and/{this}", h3)

mx.Route("/sharing/{hash}", func(mx Router) { // subrouter-1
mx.Get("/", h4)
mx.Get("/{network}", h5)
mx.Route("/direct", func(mx Router) { // subrouter-2
mx.Get("/", h6)
})
})

routes := []string{
"/",
"/sup/123/and/this",
"/sharing/aBc", // subrouter-1
"/sharing/aBc/twitter", // subrouter-1
"/sharing/aBc/direct", // subrouter-2
}

for _, path := range routes {
b.Run("route:"+path, func(b *testing.B) {
w := httptest.NewRecorder()
r, _ := http.NewRequest("GET", path, nil)

b.ReportAllocs()
b.ResetTimer()

for i := 0; i < b.N; i++ {
mx.ServeHTTP(w, r)
}
})
}
}
6 changes: 5 additions & 1 deletion tree.go
Original file line number Diff line number Diff line change
@@ -353,8 +353,12 @@ func (n *node) FindRoute(rctx *Context, method methodTyp, path string) endpoints
return nil
}

rp := RouteParams{}
rp.Keys = append(rp.Keys, rctx.routeParams.Keys...)
rp.Values = append(rp.Values, rctx.routeParams.Values...)

// Record the routing params in the request lifecycle
rctx.URLParams = append(rctx.URLParams, rctx.routeParams)
rctx.URLParams = append(rctx.URLParams, rp)

// Record the routing pattern in the request lifecycle
if rn.endpoints[method].pattern != "" {
21 changes: 0 additions & 21 deletions tree_test.go
Original file line number Diff line number Diff line change
@@ -422,24 +422,3 @@ func BenchmarkTreeGet(b *testing.B) {
tr.FindRoute(mctx, mGET, "/ping/123/456")
}
}

// func BenchmarkMuxGet(b *testing.B) {
// h1 := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})
// h2 := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})
// h3 := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})
//
// mx := NewRouter()
// mx.Get("/", h1)
// mx.Get("/hi", h2)
// mx.Get("/sup/{id}/and/{this}", h3)
//
// w := new(mockResponseWriter)
// r, _ := http.NewRequest("GET", "/sup/123/and/this", nil)
//
// b.ReportAllocs()
// b.ResetTimer()
//
// for i := 0; i < b.N; i++ {
// mx.ServeHTTP(w, r)
// }
// }