Skip to content

Commit ee04b3f

Browse files
committed
updates
1 parent 522db3b commit ee04b3f

File tree

3 files changed

+123
-71
lines changed

3 files changed

+123
-71
lines changed

coverage.out

Lines changed: 67 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -90,70 +90,73 @@ github.com/GolangToolKits/grrt/reqRoute.go:280.2,280.19 1 1
9090
github.com/GolangToolKits/grrt/reqRoute.go:283.55,287.2 3 1
9191
github.com/GolangToolKits/grrt/reqRouter.go:36.71,40.53 1 1
9292
github.com/GolangToolKits/grrt/reqRouter.go:40.53,42.3 1 1
93-
github.com/GolangToolKits/grrt/reqRouter.go:42.8,45.16 3 1
94-
github.com/GolangToolKits/grrt/reqRouter.go:45.16,48.23 3 1
95-
github.com/GolangToolKits/grrt/reqRouter.go:48.23,51.5 1 1
96-
github.com/GolangToolKits/grrt/reqRouter.go:53.3,53.34 1 1
97-
github.com/GolangToolKits/grrt/reqRouter.go:53.34,55.4 1 1
98-
github.com/GolangToolKits/grrt/reqRouter.go:55.9,55.43 1 1
99-
github.com/GolangToolKits/grrt/reqRouter.go:55.43,57.4 1 1
100-
github.com/GolangToolKits/grrt/reqRouter.go:57.9,60.4 2 1
101-
github.com/GolangToolKits/grrt/reqRouter.go:65.38,69.2 3 1
102-
github.com/GolangToolKits/grrt/reqRouter.go:72.69,75.16 3 1
103-
github.com/GolangToolKits/grrt/reqRouter.go:75.16,77.3 1 1
104-
github.com/GolangToolKits/grrt/reqRouter.go:77.8,79.28 2 1
105-
github.com/GolangToolKits/grrt/reqRouter.go:79.28,80.55 1 1
106-
github.com/GolangToolKits/grrt/reqRouter.go:80.55,83.5 2 1
107-
github.com/GolangToolKits/grrt/reqRouter.go:85.3,85.12 1 1
108-
github.com/GolangToolKits/grrt/reqRouter.go:85.12,87.4 1 1
109-
github.com/GolangToolKits/grrt/reqRouter.go:89.2,89.11 1 1
110-
github.com/GolangToolKits/grrt/reqRouter.go:93.95,96.16 3 1
111-
github.com/GolangToolKits/grrt/reqRouter.go:96.16,98.3 1 1
112-
github.com/GolangToolKits/grrt/reqRouter.go:98.8,100.28 2 1
113-
github.com/GolangToolKits/grrt/reqRouter.go:100.28,101.55 1 1
114-
github.com/GolangToolKits/grrt/reqRouter.go:101.55,104.5 2 1
115-
github.com/GolangToolKits/grrt/reqRouter.go:106.3,106.12 1 1
116-
github.com/GolangToolKits/grrt/reqRouter.go:106.12,108.4 1 1
117-
github.com/GolangToolKits/grrt/reqRouter.go:110.2,110.11 1 1
118-
github.com/GolangToolKits/grrt/reqRouter.go:114.49,117.16 3 1
119-
github.com/GolangToolKits/grrt/reqRouter.go:117.16,119.3 1 1
120-
github.com/GolangToolKits/grrt/reqRouter.go:120.2,120.11 1 1
121-
github.com/GolangToolKits/grrt/reqRouter.go:124.34,126.2 1 1
122-
github.com/GolangToolKits/grrt/reqRouter.go:129.44,131.2 1 1
123-
github.com/GolangToolKits/grrt/reqRouter.go:134.55,137.28 3 1
124-
github.com/GolangToolKits/grrt/reqRouter.go:137.28,139.20 2 1
125-
github.com/GolangToolKits/grrt/reqRouter.go:139.20,140.12 1 1
126-
github.com/GolangToolKits/grrt/reqRouter.go:142.3,142.55 1 1
127-
github.com/GolangToolKits/grrt/reqRouter.go:147.55,151.2 3 1
128-
github.com/GolangToolKits/grrt/reqRouter.go:154.56,157.28 3 1
129-
github.com/GolangToolKits/grrt/reqRouter.go:157.28,159.20 2 1
130-
github.com/GolangToolKits/grrt/reqRouter.go:159.20,160.12 1 1
131-
github.com/GolangToolKits/grrt/reqRouter.go:162.3,162.55 1 1
132-
github.com/GolangToolKits/grrt/reqRouter.go:166.55,170.28 4 1
133-
github.com/GolangToolKits/grrt/reqRouter.go:170.28,172.3 1 1
134-
github.com/GolangToolKits/grrt/reqRouter.go:173.2,173.30 1 1
135-
github.com/GolangToolKits/grrt/reqRouter.go:176.49,180.2 3 1
136-
github.com/GolangToolKits/grrt/reqRouter.go:182.70,188.33 5 1
137-
github.com/GolangToolKits/grrt/reqRouter.go:188.33,190.3 1 1
138-
github.com/GolangToolKits/grrt/reqRouter.go:190.8,192.3 1 1
139-
github.com/GolangToolKits/grrt/reqRouter.go:193.2,197.23 4 1
140-
github.com/GolangToolKits/grrt/reqRouter.go:197.23,200.12 1 1
141-
github.com/GolangToolKits/grrt/reqRouter.go:200.12,201.9 1 1
142-
github.com/GolangToolKits/grrt/reqRouter.go:209.3,209.24 1 1
143-
github.com/GolangToolKits/grrt/reqRouter.go:209.24,211.4 1 1
144-
github.com/GolangToolKits/grrt/reqRouter.go:211.9,213.4 1 1
145-
github.com/GolangToolKits/grrt/reqRouter.go:215.3,216.17 2 1
146-
github.com/GolangToolKits/grrt/reqRouter.go:216.17,217.28 1 1
147-
github.com/GolangToolKits/grrt/reqRouter.go:217.28,218.38 1 1
148-
github.com/GolangToolKits/grrt/reqRouter.go:218.38,222.41 4 1
149-
github.com/GolangToolKits/grrt/reqRouter.go:222.41,224.7 1 1
150-
github.com/GolangToolKits/grrt/reqRouter.go:225.6,225.11 1 1
151-
github.com/GolangToolKits/grrt/reqRouter.go:228.9,230.4 1 1
152-
github.com/GolangToolKits/grrt/reqRouter.go:232.2,232.19 1 1
153-
github.com/GolangToolKits/grrt/reqRouter.go:235.96,237.36 2 1
154-
github.com/GolangToolKits/grrt/reqRouter.go:237.36,238.32 1 1
155-
github.com/GolangToolKits/grrt/reqRouter.go:238.32,240.4 1 1
156-
github.com/GolangToolKits/grrt/reqRouter.go:242.2,243.27 2 1
93+
github.com/GolangToolKits/grrt/reqRouter.go:42.8,49.22 5 1
94+
github.com/GolangToolKits/grrt/reqRouter.go:49.22,52.4 1 1
95+
github.com/GolangToolKits/grrt/reqRouter.go:54.3,54.16 1 1
96+
github.com/GolangToolKits/grrt/reqRouter.go:54.16,56.4 1 1
97+
github.com/GolangToolKits/grrt/reqRouter.go:58.3,58.34 1 1
98+
github.com/GolangToolKits/grrt/reqRouter.go:58.34,60.4 1 1
99+
github.com/GolangToolKits/grrt/reqRouter.go:60.9,60.43 1 1
100+
github.com/GolangToolKits/grrt/reqRouter.go:60.43,62.4 1 1
101+
github.com/GolangToolKits/grrt/reqRouter.go:62.9,65.4 2 1
102+
github.com/GolangToolKits/grrt/reqRouter.go:70.38,74.2 3 1
103+
github.com/GolangToolKits/grrt/reqRouter.go:77.69,80.16 3 1
104+
github.com/GolangToolKits/grrt/reqRouter.go:80.16,82.3 1 1
105+
github.com/GolangToolKits/grrt/reqRouter.go:82.8,84.28 2 1
106+
github.com/GolangToolKits/grrt/reqRouter.go:84.28,85.55 1 1
107+
github.com/GolangToolKits/grrt/reqRouter.go:85.55,88.5 2 1
108+
github.com/GolangToolKits/grrt/reqRouter.go:90.3,90.12 1 1
109+
github.com/GolangToolKits/grrt/reqRouter.go:90.12,92.4 1 1
110+
github.com/GolangToolKits/grrt/reqRouter.go:94.2,94.11 1 1
111+
github.com/GolangToolKits/grrt/reqRouter.go:98.95,101.16 3 1
112+
github.com/GolangToolKits/grrt/reqRouter.go:101.16,103.3 1 1
113+
github.com/GolangToolKits/grrt/reqRouter.go:103.8,105.28 2 1
114+
github.com/GolangToolKits/grrt/reqRouter.go:105.28,106.55 1 1
115+
github.com/GolangToolKits/grrt/reqRouter.go:106.55,109.5 2 1
116+
github.com/GolangToolKits/grrt/reqRouter.go:111.3,111.12 1 1
117+
github.com/GolangToolKits/grrt/reqRouter.go:111.12,113.4 1 1
118+
github.com/GolangToolKits/grrt/reqRouter.go:115.2,115.11 1 1
119+
github.com/GolangToolKits/grrt/reqRouter.go:119.49,122.16 3 1
120+
github.com/GolangToolKits/grrt/reqRouter.go:122.16,124.3 1 1
121+
github.com/GolangToolKits/grrt/reqRouter.go:125.2,125.11 1 1
122+
github.com/GolangToolKits/grrt/reqRouter.go:129.34,131.2 1 1
123+
github.com/GolangToolKits/grrt/reqRouter.go:134.44,136.2 1 1
124+
github.com/GolangToolKits/grrt/reqRouter.go:139.55,142.28 3 1
125+
github.com/GolangToolKits/grrt/reqRouter.go:142.28,144.20 2 1
126+
github.com/GolangToolKits/grrt/reqRouter.go:144.20,145.12 1 1
127+
github.com/GolangToolKits/grrt/reqRouter.go:147.3,147.55 1 1
128+
github.com/GolangToolKits/grrt/reqRouter.go:152.55,156.2 3 1
129+
github.com/GolangToolKits/grrt/reqRouter.go:159.56,162.28 3 1
130+
github.com/GolangToolKits/grrt/reqRouter.go:162.28,164.20 2 1
131+
github.com/GolangToolKits/grrt/reqRouter.go:164.20,165.12 1 1
132+
github.com/GolangToolKits/grrt/reqRouter.go:167.3,167.55 1 1
133+
github.com/GolangToolKits/grrt/reqRouter.go:171.55,175.28 4 1
134+
github.com/GolangToolKits/grrt/reqRouter.go:175.28,177.3 1 1
135+
github.com/GolangToolKits/grrt/reqRouter.go:178.2,178.30 1 1
136+
github.com/GolangToolKits/grrt/reqRouter.go:181.49,185.2 3 1
137+
github.com/GolangToolKits/grrt/reqRouter.go:187.70,193.33 5 1
138+
github.com/GolangToolKits/grrt/reqRouter.go:193.33,195.3 1 1
139+
github.com/GolangToolKits/grrt/reqRouter.go:195.8,197.3 1 1
140+
github.com/GolangToolKits/grrt/reqRouter.go:198.2,202.23 4 1
141+
github.com/GolangToolKits/grrt/reqRouter.go:202.23,205.12 1 1
142+
github.com/GolangToolKits/grrt/reqRouter.go:205.12,206.9 1 1
143+
github.com/GolangToolKits/grrt/reqRouter.go:214.3,214.24 1 1
144+
github.com/GolangToolKits/grrt/reqRouter.go:214.24,216.4 1 1
145+
github.com/GolangToolKits/grrt/reqRouter.go:216.9,218.4 1 1
146+
github.com/GolangToolKits/grrt/reqRouter.go:220.3,221.17 2 1
147+
github.com/GolangToolKits/grrt/reqRouter.go:221.17,222.28 1 1
148+
github.com/GolangToolKits/grrt/reqRouter.go:222.28,223.38 1 1
149+
github.com/GolangToolKits/grrt/reqRouter.go:223.38,227.41 4 1
150+
github.com/GolangToolKits/grrt/reqRouter.go:227.41,229.7 1 1
151+
github.com/GolangToolKits/grrt/reqRouter.go:230.6,230.11 1 1
152+
github.com/GolangToolKits/grrt/reqRouter.go:231.11,231.77 1 1
153+
github.com/GolangToolKits/grrt/reqRouter.go:231.77,233.6 1 1
154+
github.com/GolangToolKits/grrt/reqRouter.go:235.9,237.4 1 1
155+
github.com/GolangToolKits/grrt/reqRouter.go:239.2,239.19 1 1
156+
github.com/GolangToolKits/grrt/reqRouter.go:242.96,244.36 2 1
157+
github.com/GolangToolKits/grrt/reqRouter.go:244.36,245.32 1 1
158+
github.com/GolangToolKits/grrt/reqRouter.go:245.32,247.4 1 1
159+
github.com/GolangToolKits/grrt/reqRouter.go:249.2,250.27 2 1
157160
github.com/GolangToolKits/grrt/router.go:29.25,34.2 4 1
158161
github.com/GolangToolKits/grrt/router.go:37.46,39.49 2 1
159162
github.com/GolangToolKits/grrt/router.go:39.49,41.3 1 1

reqRouter.go

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,20 @@ func (t *ReqRouter) ServeHTTP(w http.ResponseWriter, r *http.Request) {
4141
t.handleCors(w)
4242
} else {
4343
path := r.URL.Path
44-
var rt = t.findPrefix(path)
44+
// var rt = t.findPrefix(path)
45+
var rt Route
46+
// if rt == nil {
47+
frt, fvars := t.findRouteAndVars(path)
48+
rt = frt
49+
if len(*fvars) > 0 {
50+
r = t.requestWithVars(r, rt.GetVarNames(), fvars)
51+
// rt = frt
52+
}
53+
// }
4554
if rt == nil {
46-
frt, fvars := t.findRouteAndVars(path)
47-
rt = frt
48-
if len(*fvars) > 0 {
49-
r = t.requestWithVars(r, rt.GetVarNames(), fvars)
50-
// rt = frt
51-
}
55+
rt = t.findPrefix(path)
5256
}
57+
// rt = t.findPrefix(path)
5358
if rt == nil || !rt.IsActive() {
5459
w.WriteHeader(http.StatusNotFound)
5560
} else if !rt.IsMethodAllowed(r.Method) {
@@ -223,6 +228,8 @@ func (t *ReqRouter) findRouteAndVars(path string) (Route, *[]string) {
223228
vars = []string{}
224229
}
225230
break
231+
} else if len(*rts) == 1 && rt.GetPath() == "/" && !rt.IsPathVarsUsed() {
232+
vcnt--
226233
}
227234
}
228235
} else {

reqRouter_test.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,7 @@ func TestReqRouter_ServeHTTP(t *testing.T) {
280280
rt8.path = "/"
281281
rt8.handler = hdl
282282
rt8.methods = &[]string{"GET"}
283+
283284
var rts8 = make(map[string]*[]Route)
284285

285286
var prt8 []Route
@@ -300,6 +301,32 @@ func TestReqRouter_ServeHTTP(t *testing.T) {
300301
// prt8 = append(prt8, &rt8)
301302
// rts8[rt8.path] = &prt8
302303

304+
var rt10 ReqRoute
305+
rt10.active = true
306+
rt10.pathVarsUsed = true
307+
rt10.path = "/product"
308+
rt10.handler = hdl
309+
rt10.methods = &[]string{"GET"}
310+
rt10.pathVarNames = &[]string{"id", "sku"}
311+
312+
var rt10b ReqRoute
313+
rt10b.active = true
314+
rt10b.pathVarsUsed = false
315+
rt10b.path = "/"
316+
rt10b.handler = hdl
317+
rt10b.methods = &[]string{"GET"}
318+
319+
var rts10 = make(map[string]*[]Route)
320+
321+
var prt10 []Route
322+
prt10 = append(prt10, &rt10)
323+
324+
rts10[rt10.path] = &prt10
325+
326+
var prt10b []Route
327+
prt10b = append(prt10b, &rt10b)
328+
rts10[rt10b.path] = &prt10b
329+
303330
var prrt ReqRoute
304331
prrt.active = true
305332
prrt.isPrefix = true
@@ -321,6 +348,7 @@ func TestReqRouter_ServeHTTP(t *testing.T) {
321348
tw7 := httptest.NewRecorder()
322349
tw8 := httptest.NewRecorder()
323350
tw9 := httptest.NewRecorder()
351+
tw10 := httptest.NewRecorder()
324352

325353
tr, _ := http.NewRequest("POST", "/test/test1", nil)
326354
tr22, _ := http.NewRequest("GET", "/test/test1/p1/p2", nil)
@@ -332,6 +360,7 @@ func TestReqRouter_ServeHTTP(t *testing.T) {
332360

333361
tr8, _ := http.NewRequest("GET", "/", nil)
334362
tr9, _ := http.NewRequest("GET", "/p1", nil)
363+
tr10, _ := http.NewRequest("GET", "/product/p1/p2", nil)
335364
// var prt []Route
336365
// prt = append(prt, &rt)
337366
// rts[rt.path] = &prt
@@ -467,6 +496,19 @@ func TestReqRouter_ServeHTTP(t *testing.T) {
467496
wantCode: http.StatusOK,
468497
wantVarLen: 1,
469498
},
499+
{
500+
name: "test 10",
501+
fields: fields{
502+
namedRoutes: rts10,
503+
},
504+
args: args{
505+
w: tw10,
506+
r: tr10,
507+
},
508+
wantW: tw10,
509+
wantCode: http.StatusOK,
510+
wantVarLen: 2,
511+
},
470512
}
471513
for _, tt := range tests {
472514
t.Run(tt.name, func(t *testing.T) {

0 commit comments

Comments
 (0)