Skip to content

Commit 248879b

Browse files
committed
added SetURLVars function
1 parent eab76f0 commit 248879b

File tree

4 files changed

+52
-6
lines changed

4 files changed

+52
-6
lines changed

coverage.out

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
mode: set
2+
github.com/GolangToolKits/grrt/router.go:29.25,32.2 2 1
3+
github.com/GolangToolKits/grrt/router.go:35.46,37.49 2 1
4+
github.com/GolangToolKits/grrt/router.go:40.2,40.12 1 1
5+
github.com/GolangToolKits/grrt/router.go:37.49,39.3 1 1
6+
github.com/GolangToolKits/grrt/router.go:44.72,47.2 2 1
27
github.com/GolangToolKits/grrt/reqRoute.go:28.32,32.2 3 1
38
github.com/GolangToolKits/grrt/reqRoute.go:35.56,36.14 1 1
49
github.com/GolangToolKits/grrt/reqRoute.go:39.2,39.10 1 1
@@ -148,7 +153,3 @@ github.com/GolangToolKits/grrt/reqRouter.go:239.96,241.36 2 1
148153
github.com/GolangToolKits/grrt/reqRouter.go:246.2,247.27 2 1
149154
github.com/GolangToolKits/grrt/reqRouter.go:241.36,242.32 1 1
150155
github.com/GolangToolKits/grrt/reqRouter.go:242.32,244.4 1 1
151-
github.com/GolangToolKits/grrt/router.go:26.25,29.2 2 1
152-
github.com/GolangToolKits/grrt/router.go:32.46,34.49 2 1
153-
github.com/GolangToolKits/grrt/router.go:37.2,37.12 1 1
154-
github.com/GolangToolKits/grrt/router.go:34.49,36.3 1 1

reqRouter_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1034,7 +1034,7 @@ func TestReqRouter_CORSAllowCredentials(t *testing.T) {
10341034
allowedMethods: tt.fields.allowedMethods,
10351035
}
10361036
tr.CORSAllowCredentials()
1037-
if tr.corsAllowCredentials != true{
1037+
if tr.corsAllowCredentials != true {
10381038
t.Fail()
10391039
}
10401040
})

router.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ package grrt
44
// Use of this source code is governed by the MIT License
55
// that can be found in the LICENSE file.
66

7-
import "net/http"
7+
import (
8+
"context"
9+
"net/http"
10+
)
811

912
// Router Router
1013
type Router interface {
@@ -37,4 +40,10 @@ func Vars(r *http.Request) map[string]string {
3740
return rtn
3841
}
3942

43+
// SetURLVars SetURLVars
44+
func SetURLVars(r *http.Request, vars map[string]string) *http.Request {
45+
ctxi := context.WithValue(r.Context(), varsKey, vars)
46+
return r.WithContext(ctxi)
47+
}
48+
4049
// go mod init github.com/GolangToolKits/grrt

router_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,39 @@ func TestVars(t *testing.T) {
6565
})
6666
}
6767
}
68+
69+
func TestSetURLVars(t *testing.T) {
70+
tr, _ := http.NewRequest("GET", "/test/test1/p1/p2", nil)
71+
vars := make(map[string]string)
72+
vars["param1"] = "test1"
73+
vars["param2"] = "test2"
74+
type args struct {
75+
r *http.Request
76+
vars map[string]string
77+
}
78+
tests := []struct {
79+
name string
80+
args args
81+
want *http.Request
82+
}{
83+
// TODO: Add test cases.
84+
{
85+
name: "test 1",
86+
args: args{
87+
r: tr,
88+
vars: vars,
89+
},
90+
want: tr,
91+
},
92+
}
93+
for _, tt := range tests {
94+
t.Run(tt.name, func(t *testing.T) {
95+
96+
got := SetURLVars(tt.args.r, tt.args.vars)
97+
fvars := Vars(got)
98+
if fvars["param1"] != "test1" || fvars["param2"] != "test2" {
99+
t.Fail()
100+
}
101+
})
102+
}
103+
}

0 commit comments

Comments
 (0)