From ee04b3fd8ba38c1c8c02b799e250fee75a4b9942 Mon Sep 17 00:00:00 2001 From: Ken Date: Mon, 20 Mar 2023 20:53:49 -0400 Subject: [PATCH] updates --- coverage.out | 131 ++++++++++++++++++++++++---------------------- reqRouter.go | 21 +++++--- reqRouter_test.go | 42 +++++++++++++++ 3 files changed, 123 insertions(+), 71 deletions(-) diff --git a/coverage.out b/coverage.out index 0379615..5686824 100644 --- a/coverage.out +++ b/coverage.out @@ -90,70 +90,73 @@ github.com/GolangToolKits/grrt/reqRoute.go:280.2,280.19 1 1 github.com/GolangToolKits/grrt/reqRoute.go:283.55,287.2 3 1 github.com/GolangToolKits/grrt/reqRouter.go:36.71,40.53 1 1 github.com/GolangToolKits/grrt/reqRouter.go:40.53,42.3 1 1 -github.com/GolangToolKits/grrt/reqRouter.go:42.8,45.16 3 1 -github.com/GolangToolKits/grrt/reqRouter.go:45.16,48.23 3 1 -github.com/GolangToolKits/grrt/reqRouter.go:48.23,51.5 1 1 -github.com/GolangToolKits/grrt/reqRouter.go:53.3,53.34 1 1 -github.com/GolangToolKits/grrt/reqRouter.go:53.34,55.4 1 1 -github.com/GolangToolKits/grrt/reqRouter.go:55.9,55.43 1 1 -github.com/GolangToolKits/grrt/reqRouter.go:55.43,57.4 1 1 -github.com/GolangToolKits/grrt/reqRouter.go:57.9,60.4 2 1 -github.com/GolangToolKits/grrt/reqRouter.go:65.38,69.2 3 1 -github.com/GolangToolKits/grrt/reqRouter.go:72.69,75.16 3 1 -github.com/GolangToolKits/grrt/reqRouter.go:75.16,77.3 1 1 -github.com/GolangToolKits/grrt/reqRouter.go:77.8,79.28 2 1 -github.com/GolangToolKits/grrt/reqRouter.go:79.28,80.55 1 1 -github.com/GolangToolKits/grrt/reqRouter.go:80.55,83.5 2 1 -github.com/GolangToolKits/grrt/reqRouter.go:85.3,85.12 1 1 -github.com/GolangToolKits/grrt/reqRouter.go:85.12,87.4 1 1 -github.com/GolangToolKits/grrt/reqRouter.go:89.2,89.11 1 1 -github.com/GolangToolKits/grrt/reqRouter.go:93.95,96.16 3 1 -github.com/GolangToolKits/grrt/reqRouter.go:96.16,98.3 1 1 -github.com/GolangToolKits/grrt/reqRouter.go:98.8,100.28 2 1 -github.com/GolangToolKits/grrt/reqRouter.go:100.28,101.55 1 1 -github.com/GolangToolKits/grrt/reqRouter.go:101.55,104.5 2 1 -github.com/GolangToolKits/grrt/reqRouter.go:106.3,106.12 1 1 -github.com/GolangToolKits/grrt/reqRouter.go:106.12,108.4 1 1 -github.com/GolangToolKits/grrt/reqRouter.go:110.2,110.11 1 1 -github.com/GolangToolKits/grrt/reqRouter.go:114.49,117.16 3 1 -github.com/GolangToolKits/grrt/reqRouter.go:117.16,119.3 1 1 -github.com/GolangToolKits/grrt/reqRouter.go:120.2,120.11 1 1 -github.com/GolangToolKits/grrt/reqRouter.go:124.34,126.2 1 1 -github.com/GolangToolKits/grrt/reqRouter.go:129.44,131.2 1 1 -github.com/GolangToolKits/grrt/reqRouter.go:134.55,137.28 3 1 -github.com/GolangToolKits/grrt/reqRouter.go:137.28,139.20 2 1 -github.com/GolangToolKits/grrt/reqRouter.go:139.20,140.12 1 1 -github.com/GolangToolKits/grrt/reqRouter.go:142.3,142.55 1 1 -github.com/GolangToolKits/grrt/reqRouter.go:147.55,151.2 3 1 -github.com/GolangToolKits/grrt/reqRouter.go:154.56,157.28 3 1 -github.com/GolangToolKits/grrt/reqRouter.go:157.28,159.20 2 1 -github.com/GolangToolKits/grrt/reqRouter.go:159.20,160.12 1 1 -github.com/GolangToolKits/grrt/reqRouter.go:162.3,162.55 1 1 -github.com/GolangToolKits/grrt/reqRouter.go:166.55,170.28 4 1 -github.com/GolangToolKits/grrt/reqRouter.go:170.28,172.3 1 1 -github.com/GolangToolKits/grrt/reqRouter.go:173.2,173.30 1 1 -github.com/GolangToolKits/grrt/reqRouter.go:176.49,180.2 3 1 -github.com/GolangToolKits/grrt/reqRouter.go:182.70,188.33 5 1 -github.com/GolangToolKits/grrt/reqRouter.go:188.33,190.3 1 1 -github.com/GolangToolKits/grrt/reqRouter.go:190.8,192.3 1 1 -github.com/GolangToolKits/grrt/reqRouter.go:193.2,197.23 4 1 -github.com/GolangToolKits/grrt/reqRouter.go:197.23,200.12 1 1 -github.com/GolangToolKits/grrt/reqRouter.go:200.12,201.9 1 1 -github.com/GolangToolKits/grrt/reqRouter.go:209.3,209.24 1 1 -github.com/GolangToolKits/grrt/reqRouter.go:209.24,211.4 1 1 -github.com/GolangToolKits/grrt/reqRouter.go:211.9,213.4 1 1 -github.com/GolangToolKits/grrt/reqRouter.go:215.3,216.17 2 1 -github.com/GolangToolKits/grrt/reqRouter.go:216.17,217.28 1 1 -github.com/GolangToolKits/grrt/reqRouter.go:217.28,218.38 1 1 -github.com/GolangToolKits/grrt/reqRouter.go:218.38,222.41 4 1 -github.com/GolangToolKits/grrt/reqRouter.go:222.41,224.7 1 1 -github.com/GolangToolKits/grrt/reqRouter.go:225.6,225.11 1 1 -github.com/GolangToolKits/grrt/reqRouter.go:228.9,230.4 1 1 -github.com/GolangToolKits/grrt/reqRouter.go:232.2,232.19 1 1 -github.com/GolangToolKits/grrt/reqRouter.go:235.96,237.36 2 1 -github.com/GolangToolKits/grrt/reqRouter.go:237.36,238.32 1 1 -github.com/GolangToolKits/grrt/reqRouter.go:238.32,240.4 1 1 -github.com/GolangToolKits/grrt/reqRouter.go:242.2,243.27 2 1 +github.com/GolangToolKits/grrt/reqRouter.go:42.8,49.22 5 1 +github.com/GolangToolKits/grrt/reqRouter.go:49.22,52.4 1 1 +github.com/GolangToolKits/grrt/reqRouter.go:54.3,54.16 1 1 +github.com/GolangToolKits/grrt/reqRouter.go:54.16,56.4 1 1 +github.com/GolangToolKits/grrt/reqRouter.go:58.3,58.34 1 1 +github.com/GolangToolKits/grrt/reqRouter.go:58.34,60.4 1 1 +github.com/GolangToolKits/grrt/reqRouter.go:60.9,60.43 1 1 +github.com/GolangToolKits/grrt/reqRouter.go:60.43,62.4 1 1 +github.com/GolangToolKits/grrt/reqRouter.go:62.9,65.4 2 1 +github.com/GolangToolKits/grrt/reqRouter.go:70.38,74.2 3 1 +github.com/GolangToolKits/grrt/reqRouter.go:77.69,80.16 3 1 +github.com/GolangToolKits/grrt/reqRouter.go:80.16,82.3 1 1 +github.com/GolangToolKits/grrt/reqRouter.go:82.8,84.28 2 1 +github.com/GolangToolKits/grrt/reqRouter.go:84.28,85.55 1 1 +github.com/GolangToolKits/grrt/reqRouter.go:85.55,88.5 2 1 +github.com/GolangToolKits/grrt/reqRouter.go:90.3,90.12 1 1 +github.com/GolangToolKits/grrt/reqRouter.go:90.12,92.4 1 1 +github.com/GolangToolKits/grrt/reqRouter.go:94.2,94.11 1 1 +github.com/GolangToolKits/grrt/reqRouter.go:98.95,101.16 3 1 +github.com/GolangToolKits/grrt/reqRouter.go:101.16,103.3 1 1 +github.com/GolangToolKits/grrt/reqRouter.go:103.8,105.28 2 1 +github.com/GolangToolKits/grrt/reqRouter.go:105.28,106.55 1 1 +github.com/GolangToolKits/grrt/reqRouter.go:106.55,109.5 2 1 +github.com/GolangToolKits/grrt/reqRouter.go:111.3,111.12 1 1 +github.com/GolangToolKits/grrt/reqRouter.go:111.12,113.4 1 1 +github.com/GolangToolKits/grrt/reqRouter.go:115.2,115.11 1 1 +github.com/GolangToolKits/grrt/reqRouter.go:119.49,122.16 3 1 +github.com/GolangToolKits/grrt/reqRouter.go:122.16,124.3 1 1 +github.com/GolangToolKits/grrt/reqRouter.go:125.2,125.11 1 1 +github.com/GolangToolKits/grrt/reqRouter.go:129.34,131.2 1 1 +github.com/GolangToolKits/grrt/reqRouter.go:134.44,136.2 1 1 +github.com/GolangToolKits/grrt/reqRouter.go:139.55,142.28 3 1 +github.com/GolangToolKits/grrt/reqRouter.go:142.28,144.20 2 1 +github.com/GolangToolKits/grrt/reqRouter.go:144.20,145.12 1 1 +github.com/GolangToolKits/grrt/reqRouter.go:147.3,147.55 1 1 +github.com/GolangToolKits/grrt/reqRouter.go:152.55,156.2 3 1 +github.com/GolangToolKits/grrt/reqRouter.go:159.56,162.28 3 1 +github.com/GolangToolKits/grrt/reqRouter.go:162.28,164.20 2 1 +github.com/GolangToolKits/grrt/reqRouter.go:164.20,165.12 1 1 +github.com/GolangToolKits/grrt/reqRouter.go:167.3,167.55 1 1 +github.com/GolangToolKits/grrt/reqRouter.go:171.55,175.28 4 1 +github.com/GolangToolKits/grrt/reqRouter.go:175.28,177.3 1 1 +github.com/GolangToolKits/grrt/reqRouter.go:178.2,178.30 1 1 +github.com/GolangToolKits/grrt/reqRouter.go:181.49,185.2 3 1 +github.com/GolangToolKits/grrt/reqRouter.go:187.70,193.33 5 1 +github.com/GolangToolKits/grrt/reqRouter.go:193.33,195.3 1 1 +github.com/GolangToolKits/grrt/reqRouter.go:195.8,197.3 1 1 +github.com/GolangToolKits/grrt/reqRouter.go:198.2,202.23 4 1 +github.com/GolangToolKits/grrt/reqRouter.go:202.23,205.12 1 1 +github.com/GolangToolKits/grrt/reqRouter.go:205.12,206.9 1 1 +github.com/GolangToolKits/grrt/reqRouter.go:214.3,214.24 1 1 +github.com/GolangToolKits/grrt/reqRouter.go:214.24,216.4 1 1 +github.com/GolangToolKits/grrt/reqRouter.go:216.9,218.4 1 1 +github.com/GolangToolKits/grrt/reqRouter.go:220.3,221.17 2 1 +github.com/GolangToolKits/grrt/reqRouter.go:221.17,222.28 1 1 +github.com/GolangToolKits/grrt/reqRouter.go:222.28,223.38 1 1 +github.com/GolangToolKits/grrt/reqRouter.go:223.38,227.41 4 1 +github.com/GolangToolKits/grrt/reqRouter.go:227.41,229.7 1 1 +github.com/GolangToolKits/grrt/reqRouter.go:230.6,230.11 1 1 +github.com/GolangToolKits/grrt/reqRouter.go:231.11,231.77 1 1 +github.com/GolangToolKits/grrt/reqRouter.go:231.77,233.6 1 1 +github.com/GolangToolKits/grrt/reqRouter.go:235.9,237.4 1 1 +github.com/GolangToolKits/grrt/reqRouter.go:239.2,239.19 1 1 +github.com/GolangToolKits/grrt/reqRouter.go:242.96,244.36 2 1 +github.com/GolangToolKits/grrt/reqRouter.go:244.36,245.32 1 1 +github.com/GolangToolKits/grrt/reqRouter.go:245.32,247.4 1 1 +github.com/GolangToolKits/grrt/reqRouter.go:249.2,250.27 2 1 github.com/GolangToolKits/grrt/router.go:29.25,34.2 4 1 github.com/GolangToolKits/grrt/router.go:37.46,39.49 2 1 github.com/GolangToolKits/grrt/router.go:39.49,41.3 1 1 diff --git a/reqRouter.go b/reqRouter.go index 82bf88c..c346333 100644 --- a/reqRouter.go +++ b/reqRouter.go @@ -41,15 +41,20 @@ func (t *ReqRouter) ServeHTTP(w http.ResponseWriter, r *http.Request) { t.handleCors(w) } else { path := r.URL.Path - var rt = t.findPrefix(path) + // var rt = t.findPrefix(path) + var rt Route + // if rt == nil { + frt, fvars := t.findRouteAndVars(path) + rt = frt + if len(*fvars) > 0 { + r = t.requestWithVars(r, rt.GetVarNames(), fvars) + // rt = frt + } + // } if rt == nil { - frt, fvars := t.findRouteAndVars(path) - rt = frt - if len(*fvars) > 0 { - r = t.requestWithVars(r, rt.GetVarNames(), fvars) - // rt = frt - } + rt = t.findPrefix(path) } + // rt = t.findPrefix(path) if rt == nil || !rt.IsActive() { w.WriteHeader(http.StatusNotFound) } else if !rt.IsMethodAllowed(r.Method) { @@ -223,6 +228,8 @@ func (t *ReqRouter) findRouteAndVars(path string) (Route, *[]string) { vars = []string{} } break + } else if len(*rts) == 1 && rt.GetPath() == "/" && !rt.IsPathVarsUsed() { + vcnt-- } } } else { diff --git a/reqRouter_test.go b/reqRouter_test.go index 65d2ef3..251c21b 100644 --- a/reqRouter_test.go +++ b/reqRouter_test.go @@ -280,6 +280,7 @@ func TestReqRouter_ServeHTTP(t *testing.T) { rt8.path = "/" rt8.handler = hdl rt8.methods = &[]string{"GET"} + var rts8 = make(map[string]*[]Route) var prt8 []Route @@ -300,6 +301,32 @@ func TestReqRouter_ServeHTTP(t *testing.T) { // prt8 = append(prt8, &rt8) // rts8[rt8.path] = &prt8 + var rt10 ReqRoute + rt10.active = true + rt10.pathVarsUsed = true + rt10.path = "/product" + rt10.handler = hdl + rt10.methods = &[]string{"GET"} + rt10.pathVarNames = &[]string{"id", "sku"} + + var rt10b ReqRoute + rt10b.active = true + rt10b.pathVarsUsed = false + rt10b.path = "/" + rt10b.handler = hdl + rt10b.methods = &[]string{"GET"} + + var rts10 = make(map[string]*[]Route) + + var prt10 []Route + prt10 = append(prt10, &rt10) + + rts10[rt10.path] = &prt10 + + var prt10b []Route + prt10b = append(prt10b, &rt10b) + rts10[rt10b.path] = &prt10b + var prrt ReqRoute prrt.active = true prrt.isPrefix = true @@ -321,6 +348,7 @@ func TestReqRouter_ServeHTTP(t *testing.T) { tw7 := httptest.NewRecorder() tw8 := httptest.NewRecorder() tw9 := httptest.NewRecorder() + tw10 := httptest.NewRecorder() tr, _ := http.NewRequest("POST", "/test/test1", nil) tr22, _ := http.NewRequest("GET", "/test/test1/p1/p2", nil) @@ -332,6 +360,7 @@ func TestReqRouter_ServeHTTP(t *testing.T) { tr8, _ := http.NewRequest("GET", "/", nil) tr9, _ := http.NewRequest("GET", "/p1", nil) + tr10, _ := http.NewRequest("GET", "/product/p1/p2", nil) // var prt []Route // prt = append(prt, &rt) // rts[rt.path] = &prt @@ -467,6 +496,19 @@ func TestReqRouter_ServeHTTP(t *testing.T) { wantCode: http.StatusOK, wantVarLen: 1, }, + { + name: "test 10", + fields: fields{ + namedRoutes: rts10, + }, + args: args{ + w: tw10, + r: tr10, + }, + wantW: tw10, + wantCode: http.StatusOK, + wantVarLen: 2, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) {