diff --git a/coverage.out b/coverage.out index 5b1a82f..0379615 100644 --- a/coverage.out +++ b/coverage.out @@ -45,8 +45,8 @@ github.com/GolangToolKits/grrt/reqRoute.go:165.13,167.3 1 1 github.com/GolangToolKits/grrt/reqRoute.go:167.8,169.3 1 1 github.com/GolangToolKits/grrt/reqRoute.go:170.2,170.12 1 1 github.com/GolangToolKits/grrt/reqRoute.go:173.55,180.22 6 1 -github.com/GolangToolKits/grrt/reqRoute.go:180.22,181.31 1 1 -github.com/GolangToolKits/grrt/reqRoute.go:181.31,184.9 3 1 +github.com/GolangToolKits/grrt/reqRoute.go:180.22,181.46 1 1 +github.com/GolangToolKits/grrt/reqRoute.go:181.46,184.9 3 1 github.com/GolangToolKits/grrt/reqRoute.go:185.9,185.21 1 1 github.com/GolangToolKits/grrt/reqRoute.go:185.21,187.4 1 1 github.com/GolangToolKits/grrt/reqRoute.go:188.3,188.31 1 1 @@ -133,21 +133,27 @@ 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,190.23 7 1 -github.com/GolangToolKits/grrt/reqRouter.go:190.23,191.13 1 1 -github.com/GolangToolKits/grrt/reqRouter.go:191.13,192.12 1 1 -github.com/GolangToolKits/grrt/reqRouter.go:193.9,193.19 1 1 -github.com/GolangToolKits/grrt/reqRouter.go:193.19,194.9 1 1 -github.com/GolangToolKits/grrt/reqRouter.go:196.3,198.17 3 1 -github.com/GolangToolKits/grrt/reqRouter.go:198.17,199.28 1 1 -github.com/GolangToolKits/grrt/reqRouter.go:199.28,200.38 1 1 -github.com/GolangToolKits/grrt/reqRouter.go:200.38,204.11 4 1 -github.com/GolangToolKits/grrt/reqRouter.go:207.9,209.4 1 1 -github.com/GolangToolKits/grrt/reqRouter.go:211.2,211.19 1 1 -github.com/GolangToolKits/grrt/reqRouter.go:214.96,216.36 2 1 -github.com/GolangToolKits/grrt/reqRouter.go:216.36,217.32 1 1 -github.com/GolangToolKits/grrt/reqRouter.go:217.32,219.4 1 1 -github.com/GolangToolKits/grrt/reqRouter.go:221.2,222.27 2 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/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/reqRoute.go b/reqRoute.go index c19a11c..bb7acb1 100644 --- a/reqRoute.go +++ b/reqRoute.go @@ -178,7 +178,7 @@ func (t *ReqRoute) chechCurlyPlacement(p string) bool { var lastOc int = 0 var lastCc int = 0 for i, c := range p { - if c == oc && i == lastOc+1 { + if c == oc && i == lastOc+1 && lastOc != 0 { t.printError(p, "Route can not have {{") rtn = false break diff --git a/reqRoute_test.go b/reqRoute_test.go index 0f399ba..8271867 100644 --- a/reqRoute_test.go +++ b/reqRoute_test.go @@ -39,6 +39,20 @@ func TestReqRoute_chechPath(t *testing.T) { }, want: false, }, + { + name: "url match success", + args: args{ + p: "/", + }, + want: true, + }, + { + name: "url match success", + args: args{ + p: "/{parm1}", + }, + want: true, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { @@ -222,6 +236,13 @@ func TestReqRoute_chechCurlyPlacement(t *testing.T) { }, want: false, }, + { + name: "test 3 fail", + args: args{ + p: "/{parm1}", + }, + want: true, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { @@ -386,6 +407,36 @@ func TestReqRoute_Path(t *testing.T) { pathVarNames: &[]string{}, }, }, + { + name: "test 4 ", + args: args{ + p: "/", + }, + fields: fields{ + pathVarNames: &[]string{}, + }, + want: &ReqRoute{ + path: "/", + active: true, + pathVarsUsed: true, + pathVarNames: &[]string{}, + }, + }, + { + name: "test 5 ", + args: args{ + p: "/{param1}", + }, + fields: fields{ + pathVarNames: &[]string{}, + }, + want: &ReqRoute{ + path: "/", + active: true, + pathVarsUsed: true, + pathVarNames: &[]string{"param1"}, + }, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { diff --git a/reqRouter.go b/reqRouter.go index 3fc2cb1..82bf88c 100644 --- a/reqRouter.go +++ b/reqRouter.go @@ -183,17 +183,35 @@ func (t *ReqRouter) findRouteAndVars(path string) (Route, *[]string) { var rnt Route sp := strings.Split(path, "/") var vars []string - var vcnt = len(sp) - 2 + // var vcnt = len(sp) - 2 + var vl int + if len(sp) == 2 && sp[1] == "" { + vl = len(sp) - 2 + } else { + vl = len(sp) - 1 + } + var vcnt = vl //log.Println("sp:", sp) var found = false var searchPath = "" for i, p := range sp { - if i == 0 { - continue - } else if found { + //if i == 0 { + //continue + if found { break } - searchPath += "/" + p + // if i == 0 { + // continue + // } else if found { + // break + // } + // searchPath += "/" + p + if searchPath == "/" { + searchPath += p + } else { + searchPath += "/" + p + } + // searchPath += "/" + p rts := t.namedRoutes[searchPath] if rts != nil { for _, rt := range *rts { @@ -201,6 +219,9 @@ func (t *ReqRouter) findRouteAndVars(path string) (Route, *[]string) { rnt = rt found = true vars = sp[i+1:] + if len(vars) == 1 && vars[0] == "" { + vars = []string{} + } break } } diff --git a/reqRouter_test.go b/reqRouter_test.go index 34647d4..65d2ef3 100644 --- a/reqRouter_test.go +++ b/reqRouter_test.go @@ -57,6 +57,16 @@ func TestReqRouter_HandleFunc(t *testing.T) { } nr2["/route/test1"] = &[]Route{rt2} + var nr3 = make(map[string]*[]Route) + var rt3 = &ReqRoute{ + path: "/", + active: true, + pathVarsUsed: false, + pathVarNames: &[]string{}, + methods: &[]string{}, + } + nr3["/"] = &[]Route{rt3} + type fields struct { namedRoutes map[string]*[]Route } @@ -65,10 +75,11 @@ func TestReqRouter_HandleFunc(t *testing.T) { f func(http.ResponseWriter, *http.Request) } tests := []struct { - name string - fields fields - args args - want Route + name string + fields fields + args args + want Route + wantPath string }{ // TODO: Add test cases. { @@ -87,6 +98,7 @@ func TestReqRouter_HandleFunc(t *testing.T) { pathVarNames: &[]string{}, methods: &[]string{}, }, + wantPath: "/route/test1", }, { name: "test 2", @@ -104,6 +116,7 @@ func TestReqRouter_HandleFunc(t *testing.T) { pathVarNames: &[]string{"name", "cat"}, methods: &[]string{}, }, + wantPath: "/route/test1", }, { name: "test 3", @@ -121,6 +134,43 @@ func TestReqRouter_HandleFunc(t *testing.T) { pathVarNames: &[]string{"name"}, methods: &[]string{}, }, + wantPath: "/route/test1", + }, + { + name: "test 4", + fields: fields{ + namedRoutes: nr, + }, + args: args{ + path: "/", + f: nf, + }, + want: &ReqRoute{ + path: "/", + active: true, + pathVarsUsed: false, + pathVarNames: &[]string{}, + methods: &[]string{}, + }, + wantPath: "/", + }, + { + name: "test 5", + fields: fields{ + namedRoutes: nr3, + }, + args: args{ + path: "/{name}", + f: nf, + }, + want: &ReqRoute{ + path: "/", + active: true, + pathVarsUsed: true, + pathVarNames: &[]string{"name"}, + methods: &[]string{}, + }, + wantPath: "/", }, } for _, tt := range tests { @@ -129,7 +179,7 @@ func TestReqRouter_HandleFunc(t *testing.T) { namedRoutes: tt.fields.namedRoutes, } // if got := tr.HandleFunc(tt.args.path, tt.args.f); !reflect.DeepEqual(got, tt.want) { - if got := tr.HandleFunc(tt.args.path, tt.args.f); got.GetPath() != "/route/test1" { + if got := tr.HandleFunc(tt.args.path, tt.args.f); got.GetPath() != tt.wantPath { t.Errorf("ReqRouter.HandleFunc() = %v, want %v", got, tt.want) } @@ -195,8 +245,9 @@ func TestReqRouter_HandleFuncFullCall(t *testing.T) { } func TestReqRouter_ServeHTTP(t *testing.T) { - var nf = func(http.ResponseWriter, *http.Request) { - + var fvars map[string]string + var nf = func(wnf http.ResponseWriter, rnf *http.Request) { + fvars = Vars(rnf) } var hdl = http.HandlerFunc(nf) @@ -224,6 +275,31 @@ func TestReqRouter_ServeHTTP(t *testing.T) { prt2 = append(prt2, &rt2) rts2[rt2.path] = &prt2 + var rt8 ReqRoute + rt8.active = true + rt8.path = "/" + rt8.handler = hdl + rt8.methods = &[]string{"GET"} + var rts8 = make(map[string]*[]Route) + + var prt8 []Route + prt8 = append(prt8, &rt8) + rts8[rt8.path] = &prt8 + + var rt88 ReqRoute + rt88.active = true + rt88.pathVarsUsed = true + rt88.path = "/" + rt88.handler = hdl + rt88.methods = &[]string{"GET"} + rt88.pathVarNames = &[]string{"param1"} + prt8 = append(prt8, &rt88) + rts8[rt88.path] = &prt8 + + // var prt8 []Route + // prt8 = append(prt8, &rt8) + // rts8[rt8.path] = &prt8 + var prrt ReqRoute prrt.active = true prrt.isPrefix = true @@ -243,14 +319,19 @@ func TestReqRouter_ServeHTTP(t *testing.T) { tw5 := httptest.NewRecorder() tw6 := httptest.NewRecorder() tw7 := httptest.NewRecorder() + tw8 := httptest.NewRecorder() + tw9 := httptest.NewRecorder() tr, _ := http.NewRequest("POST", "/test/test1", nil) tr22, _ := http.NewRequest("GET", "/test/test1/p1/p2", nil) tr2, _ := http.NewRequest("POST", "/test/te", nil) tr3, _ := http.NewRequest("PUT", "/test/test1", nil) - tr4, _ := http.NewRequest("POST", "/test/test1/param1/param2", nil) + tr4, _ := http.NewRequest("PUT", "/test/test1/param1/param2", nil) tr6, _ := http.NewRequest("POST", "/testPrefix/", nil) tr7, _ := http.NewRequest("OPTIONS", "/testPrefix/", nil) + + tr8, _ := http.NewRequest("GET", "/", nil) + tr9, _ := http.NewRequest("GET", "/p1", nil) // var prt []Route // prt = append(prt, &rt) // rts[rt.path] = &prt @@ -264,9 +345,12 @@ func TestReqRouter_ServeHTTP(t *testing.T) { r *http.Request } tests := []struct { - name string - fields fields - args args + name string + fields fields + args args + wantCode int + wantW *httptest.ResponseRecorder + wantVarLen int }{ // TODO: Add test cases. { @@ -278,6 +362,9 @@ func TestReqRouter_ServeHTTP(t *testing.T) { w: tw, r: tr, }, + wantW: tw, + wantCode: http.StatusOK, + wantVarLen: 0, }, { name: "test 2 404", @@ -288,6 +375,9 @@ func TestReqRouter_ServeHTTP(t *testing.T) { w: tw2, r: tr2, }, + wantW: tw2, + wantCode: http.StatusNotFound, + wantVarLen: 0, }, { name: "test 3 405", @@ -298,16 +388,22 @@ func TestReqRouter_ServeHTTP(t *testing.T) { w: tw3, r: tr3, }, + wantW: tw3, + wantCode: http.StatusMethodNotAllowed, + wantVarLen: 0, }, { name: "test 4 405", fields: fields{ - namedRoutes: rts, + namedRoutes: rts2, }, args: args{ w: tw4, r: tr4, }, + wantW: tw4, + wantCode: http.StatusMethodNotAllowed, + wantVarLen: 0, }, { name: "test 5", @@ -318,6 +414,9 @@ func TestReqRouter_ServeHTTP(t *testing.T) { w: tw5, r: tr22, }, + wantW: tw5, + wantCode: http.StatusOK, + wantVarLen: 2, }, { name: "test 6 prefix", @@ -328,6 +427,9 @@ func TestReqRouter_ServeHTTP(t *testing.T) { w: tw6, r: tr6, }, + wantW: tw6, + wantCode: http.StatusOK, + wantVarLen: 0, }, { name: "cors test", @@ -335,6 +437,35 @@ func TestReqRouter_ServeHTTP(t *testing.T) { w: tw7, r: tr7, }, + wantW: tw7, + wantCode: http.StatusOK, + wantVarLen: 0, + }, + { + name: "test 8", + fields: fields{ + namedRoutes: rts8, + }, + args: args{ + w: tw8, + r: tr8, + }, + wantW: tw8, + wantCode: http.StatusOK, + wantVarLen: 0, + }, + { + name: "test 9", + fields: fields{ + namedRoutes: rts8, + }, + args: args{ + w: tw9, + r: tr9, + }, + wantW: tw9, + wantCode: http.StatusOK, + wantVarLen: 1, }, } for _, tt := range tests { @@ -345,27 +476,31 @@ func TestReqRouter_ServeHTTP(t *testing.T) { corsEnabled: true, } tr.ServeHTTP(tt.args.w, tt.args.r) - if tt.name == "test 1" && tw.Code != http.StatusOK { - t.Fail() - } - if tt.name == "test 2 404" && tw2.Code != http.StatusNotFound { - t.Fail() - } - if tt.name == "test 3 405" && tw3.Code != http.StatusMethodNotAllowed { - t.Fail() - } - if tt.name == "test 4 405" && tw3.Code != http.StatusMethodNotAllowed { - t.Fail() - } - if tt.name == "test 5" && tw5.Code != http.StatusOK { - t.Fail() - } - if tt.name == "test 6 prefix" && tw6.Code != http.StatusOK { - t.Fail() - } - if tt.name == "cors test" && tw7.Code != http.StatusOK { + //vs := Vars(tt.args.r) + if tt.wantW.Code != tt.wantCode || len(fvars) != tt.wantVarLen { t.Fail() } + // if tt.name == "test 1" && tw.Code != http.StatusOK { + // t.Fail() + // } + // if tt.name == "test 2 404" && tw2.Code != http.StatusNotFound { + // t.Fail() + // } + // if tt.name == "test 3 405" && tw3.Code != http.StatusMethodNotAllowed { + // t.Fail() + // } + // if tt.name == "test 4 405" && tw3.Code != http.StatusMethodNotAllowed { + // t.Fail() + // } + // if tt.name == "test 5" && tw5.Code != http.StatusOK { + // t.Fail() + // } + // if tt.name == "test 6 prefix" && tw6.Code != http.StatusOK { + // t.Fail() + // } + // if tt.name == "cors test" && tw7.Code != http.StatusOK { + // t.Fail() + // } }) } }