Skip to content

Commit

Permalink
Fix Group logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Dean Karn committed Jun 25, 2017
1 parent bc0108c commit 8b84c82
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 17 deletions.
6 changes: 3 additions & 3 deletions .travis.yml
@@ -1,7 +1,7 @@
language: go
go:
- 1.7.5
- 1.8.1
- 1.7.6
- 1.8.3
- tip
matrix:
allow_failures:
Expand Down Expand Up @@ -33,6 +33,6 @@ script:
- go test -race

after_success: |
[ $TRAVIS_GO_VERSION = 1.8.1 ] &&
[ $TRAVIS_GO_VERSION = 1.8.3 ] &&
overalls -project="github.com/go-playground/pure" -covermode=count -ignore=.git,examples -debug &&
goveralls -coverprofile=overalls.coverprofile -service travis-ci -repotoken $COVERALLS_TOKEN
6 changes: 3 additions & 3 deletions README.md
@@ -1,6 +1,6 @@
package pure
============
<img align="right" src="https://raw.githubusercontent.com/go-playground/pure/master/logo.png">![Project status](https://img.shields.io/badge/version-4.1.0-green.svg)
<img align="right" src="https://raw.githubusercontent.com/go-playground/pure/master/logo.png">![Project status](https://img.shields.io/badge/version-4.1.1-green.svg)
[![Build Status](https://travis-ci.org/go-playground/pure.svg?branch=master)](https://travis-ci.org/go-playground/pure)
[![Coverage Status](https://coveralls.io/repos/github/go-playground/pure/badge.svg?branch=master)](https://coveralls.io/github/go-playground/pure?branch=master)
[![Go Report Card](https://goreportcard.com/badge/github.com/go-playground/pure)](https://goreportcard.com/report/github.com/go-playground/pure)
Expand Down Expand Up @@ -38,7 +38,7 @@ import (
"net/http"

"github.com/go-playground/pure"
mw "github.com/go-playground/pure/examples/middleware/logging-recovery"
mw "github.com/go-playground/pure/_examples/middleware/logging-recovery"
)

func main() {
Expand Down Expand Up @@ -152,7 +152,7 @@ comply with the following rule(s):

* Are completely reusable by the community without modification

Other middleware will be listed under the examples/middleware/... folder for a quick copy/paste modify. As an example a LoddingAndRecovery middleware is very application dependent and therefore will be listed under the examples/middleware/...
Other middleware will be listed under the _examples/middleware/... folder for a quick copy/paste modify. As an example a LoddingAndRecovery middleware is very application dependent and therefore will be listed under the _examples/middleware/...

Benchmarks
-----------
Expand Down
2 changes: 1 addition & 1 deletion examples/basic/main.go → _examples/basic/main.go
Expand Up @@ -4,7 +4,7 @@ import (
"net/http"

"github.com/go-playground/pure"
mw "github.com/go-playground/pure/examples/middleware/logging-recovery"
mw "github.com/go-playground/pure/_examples/middleware/logging-recovery"
)

func main() {
Expand Down
2 changes: 1 addition & 1 deletion examples/params/main.go → _examples/params/main.go
Expand Up @@ -4,7 +4,7 @@ import (
"net/http"

"github.com/go-playground/pure"
mw "github.com/go-playground/pure/examples/middleware/logging-recovery"
mw "github.com/go-playground/pure/_examples/middleware/logging-recovery"
)

func main() {
Expand Down
11 changes: 2 additions & 9 deletions group.go
Expand Up @@ -144,7 +144,6 @@ func (g *routeGroup) Match(methods []string, path string, h http.HandlerFunc) {

// GroupWithNone creates a new sub router with specified prefix and no middleware attached.
func (g *routeGroup) GroupWithNone(prefix string) IRouteGroup {

return &routeGroup{
prefix: g.prefix + prefix,
pure: g.pure,
Expand All @@ -154,29 +153,23 @@ func (g *routeGroup) GroupWithNone(prefix string) IRouteGroup {

// GroupWithMore creates a new sub router with specified prefix, retains existing middleware and adds new middleware.
func (g *routeGroup) GroupWithMore(prefix string, middleware ...Middleware) IRouteGroup {

rg := &routeGroup{
prefix: g.prefix + prefix,
pure: g.pure,
middleware: make([]Middleware, len(middleware)),
middleware: make([]Middleware, len(g.middleware)),
}

copy(rg.middleware, g.pure.middleware)
copy(rg.middleware, g.middleware)
rg.Use(middleware...)

return rg
}

// Group creates a new sub router with specified prefix and retains existing middleware.
func (g *routeGroup) Group(prefix string) IRouteGroup {

rg := &routeGroup{
prefix: g.prefix + prefix,
pure: g.pure,
middleware: make([]Middleware, len(g.middleware)),
}

copy(rg.middleware, g.middleware)

return rg
}
70 changes: 70 additions & 0 deletions group_test.go
Expand Up @@ -177,3 +177,73 @@ func TestMatch(t *testing.T) {
}
}
}

func TestGrouplogic(t *testing.T) {

var aa, bb, cc, tl int

aM := func(next http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
aa++
next(w, r)
}
}

bM := func(next http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
bb++
next(w, r)
}
}

cM := func(next http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
cc++
next(w, r)
}
}

p := New()
p.Use(func(next http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
tl++
next(w, r)
}
})

a := p.GroupWithMore("/a", aM)
a.Get("/test", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("a-ok"))
})

b := a.GroupWithMore("/b", bM)
b.Get("/test", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("b-ok"))
})

c := b.GroupWithMore("/c", cM)
c.Get("/test", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("c-ok"))
})

code, body := request(http.MethodGet, "/a/test", p)
Equal(t, code, http.StatusOK)
Equal(t, body, "a-ok")
Equal(t, tl, 1)
Equal(t, aa, 1)

code, body = request(http.MethodGet, "/a/b/test", p)
Equal(t, code, http.StatusOK)
Equal(t, body, "b-ok")
Equal(t, tl, 2)
Equal(t, aa, 2)
Equal(t, bb, 1)

code, body = request(http.MethodGet, "/a/b/c/test", p)
Equal(t, code, http.StatusOK)
Equal(t, body, "c-ok")
Equal(t, tl, 3)
Equal(t, aa, 3)
Equal(t, bb, 2)
Equal(t, cc, 1)
}

0 comments on commit 8b84c82

Please sign in to comment.