Skip to content

Commit 1eb11d9

Browse files
author
Dean Karn
authored
Merge pull request #3 from go-playground/fix-group-logic
Fix Group logic
2 parents bc0108c + 8b84c82 commit 1eb11d9

File tree

7 files changed

+80
-17
lines changed

7 files changed

+80
-17
lines changed

.travis.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
language: go
22
go:
3-
- 1.7.5
4-
- 1.8.1
3+
- 1.7.6
4+
- 1.8.3
55
- tip
66
matrix:
77
allow_failures:
@@ -33,6 +33,6 @@ script:
3333
- go test -race
3434

3535
after_success: |
36-
[ $TRAVIS_GO_VERSION = 1.8.1 ] &&
36+
[ $TRAVIS_GO_VERSION = 1.8.3 ] &&
3737
overalls -project="github.com/go-playground/pure" -covermode=count -ignore=.git,examples -debug &&
3838
goveralls -coverprofile=overalls.coverprofile -service travis-ci -repotoken $COVERALLS_TOKEN

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package pure
22
============
3-
<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)
3+
<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)
44
[![Build Status](https://travis-ci.org/go-playground/pure.svg?branch=master)](https://travis-ci.org/go-playground/pure)
55
[![Coverage Status](https://coveralls.io/repos/github/go-playground/pure/badge.svg?branch=master)](https://coveralls.io/github/go-playground/pure?branch=master)
66
[![Go Report Card](https://goreportcard.com/badge/github.com/go-playground/pure)](https://goreportcard.com/report/github.com/go-playground/pure)
@@ -38,7 +38,7 @@ import (
3838
"net/http"
3939

4040
"github.com/go-playground/pure"
41-
mw "github.com/go-playground/pure/examples/middleware/logging-recovery"
41+
mw "github.com/go-playground/pure/_examples/middleware/logging-recovery"
4242
)
4343

4444
func main() {
@@ -152,7 +152,7 @@ comply with the following rule(s):
152152

153153
* Are completely reusable by the community without modification
154154

155-
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/...
155+
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/...
156156

157157
Benchmarks
158158
-----------
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
"net/http"
55

66
"github.com/go-playground/pure"
7-
mw "github.com/go-playground/pure/examples/middleware/logging-recovery"
7+
mw "github.com/go-playground/pure/_examples/middleware/logging-recovery"
88
)
99

1010
func main() {

examples/middleware/logging-recovery/logging_recovery.go renamed to _examples/middleware/logging-recovery/logging_recovery.go

File renamed without changes.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
"net/http"
55

66
"github.com/go-playground/pure"
7-
mw "github.com/go-playground/pure/examples/middleware/logging-recovery"
7+
mw "github.com/go-playground/pure/_examples/middleware/logging-recovery"
88
)
99

1010
func main() {

group.go

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,6 @@ func (g *routeGroup) Match(methods []string, path string, h http.HandlerFunc) {
144144

145145
// GroupWithNone creates a new sub router with specified prefix and no middleware attached.
146146
func (g *routeGroup) GroupWithNone(prefix string) IRouteGroup {
147-
148147
return &routeGroup{
149148
prefix: g.prefix + prefix,
150149
pure: g.pure,
@@ -154,29 +153,23 @@ func (g *routeGroup) GroupWithNone(prefix string) IRouteGroup {
154153

155154
// GroupWithMore creates a new sub router with specified prefix, retains existing middleware and adds new middleware.
156155
func (g *routeGroup) GroupWithMore(prefix string, middleware ...Middleware) IRouteGroup {
157-
158156
rg := &routeGroup{
159157
prefix: g.prefix + prefix,
160158
pure: g.pure,
161-
middleware: make([]Middleware, len(middleware)),
159+
middleware: make([]Middleware, len(g.middleware)),
162160
}
163-
164-
copy(rg.middleware, g.pure.middleware)
161+
copy(rg.middleware, g.middleware)
165162
rg.Use(middleware...)
166-
167163
return rg
168164
}
169165

170166
// Group creates a new sub router with specified prefix and retains existing middleware.
171167
func (g *routeGroup) Group(prefix string) IRouteGroup {
172-
173168
rg := &routeGroup{
174169
prefix: g.prefix + prefix,
175170
pure: g.pure,
176171
middleware: make([]Middleware, len(g.middleware)),
177172
}
178-
179173
copy(rg.middleware, g.middleware)
180-
181174
return rg
182175
}

group_test.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,3 +177,73 @@ func TestMatch(t *testing.T) {
177177
}
178178
}
179179
}
180+
181+
func TestGrouplogic(t *testing.T) {
182+
183+
var aa, bb, cc, tl int
184+
185+
aM := func(next http.HandlerFunc) http.HandlerFunc {
186+
return func(w http.ResponseWriter, r *http.Request) {
187+
aa++
188+
next(w, r)
189+
}
190+
}
191+
192+
bM := func(next http.HandlerFunc) http.HandlerFunc {
193+
return func(w http.ResponseWriter, r *http.Request) {
194+
bb++
195+
next(w, r)
196+
}
197+
}
198+
199+
cM := func(next http.HandlerFunc) http.HandlerFunc {
200+
return func(w http.ResponseWriter, r *http.Request) {
201+
cc++
202+
next(w, r)
203+
}
204+
}
205+
206+
p := New()
207+
p.Use(func(next http.HandlerFunc) http.HandlerFunc {
208+
return func(w http.ResponseWriter, r *http.Request) {
209+
tl++
210+
next(w, r)
211+
}
212+
})
213+
214+
a := p.GroupWithMore("/a", aM)
215+
a.Get("/test", func(w http.ResponseWriter, r *http.Request) {
216+
w.Write([]byte("a-ok"))
217+
})
218+
219+
b := a.GroupWithMore("/b", bM)
220+
b.Get("/test", func(w http.ResponseWriter, r *http.Request) {
221+
w.Write([]byte("b-ok"))
222+
})
223+
224+
c := b.GroupWithMore("/c", cM)
225+
c.Get("/test", func(w http.ResponseWriter, r *http.Request) {
226+
w.Write([]byte("c-ok"))
227+
})
228+
229+
code, body := request(http.MethodGet, "/a/test", p)
230+
Equal(t, code, http.StatusOK)
231+
Equal(t, body, "a-ok")
232+
Equal(t, tl, 1)
233+
Equal(t, aa, 1)
234+
235+
code, body = request(http.MethodGet, "/a/b/test", p)
236+
Equal(t, code, http.StatusOK)
237+
Equal(t, body, "b-ok")
238+
Equal(t, tl, 2)
239+
Equal(t, aa, 2)
240+
Equal(t, bb, 1)
241+
242+
code, body = request(http.MethodGet, "/a/b/c/test", p)
243+
Equal(t, code, http.StatusOK)
244+
Equal(t, body, "c-ok")
245+
Equal(t, tl, 3)
246+
Equal(t, aa, 3)
247+
Equal(t, bb, 2)
248+
Equal(t, cc, 1)
249+
}

0 commit comments

Comments
 (0)