Skip to content

Commit 56fb737

Browse files
committed
Changed Params from pointer to value ref
Added unit test cases for utils/url
1 parent 21f6e04 commit 56fb737

File tree

3 files changed

+47
-4
lines changed

3 files changed

+47
-4
lines changed

context.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import (
1717
type Context struct {
1818
Request *http.Request
1919
Response http.ResponseWriter
20-
Params *map[string]string
20+
Params map[string]string
2121
data map[string]interface{}
2222
err error
2323
status int

utils/url.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ func Compile(str string) (regex *regexp.Regexp, keys []string, err error) {
4848
return
4949
}
5050

51-
func Exec(regex *regexp.Regexp, keys []string, uri []byte) *map[string]string {
52-
params := make(map[string]string)
51+
func Exec(regex *regexp.Regexp, keys []string, uri []byte) (params map[string]string) {
52+
params = make(map[string]string)
5353

5454
matches := regex.FindAllSubmatch(uri, -1)
5555

@@ -62,5 +62,5 @@ func Exec(regex *regexp.Regexp, keys []string, uri []byte) *map[string]string {
6262
}
6363
}
6464

65-
return &params
65+
return
6666
}

utils/url_test.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,16 @@ func TestCompile4(t *testing.T) {
122122
var regex1, keys1, _ = Compile("/*")
123123
var str1 = []byte("/foo/bar")
124124

125+
// wildcard
126+
func TestExec1(t *testing.T) {
127+
var x map[string]string
128+
x = Exec(regex1, keys1, str1)
129+
130+
if x["*"] != "/foo/bar" {
131+
t.Error("Error in asterisk")
132+
}
133+
}
134+
125135
func BenchmarkExec1(b *testing.B) {
126136
for n := 0; n < b.N; n++ {
127137
_ = Exec(regex1, keys1, str1)
@@ -132,6 +142,15 @@ func BenchmarkExec1(b *testing.B) {
132142
var regex2, keys2, _ = Compile("/:foo/:bar")
133143
var str2 = []byte("/foo/bar")
134144

145+
func TestExec2(t *testing.T) {
146+
var x map[string]string
147+
x = Exec(regex2, keys2, str2)
148+
149+
if x["foo"] != "foo" || x["bar"] != "bar" {
150+
t.Error("Error in params")
151+
}
152+
}
153+
135154
func BenchmarkExec2(b *testing.B) {
136155
for n := 0; n < b.N; n++ {
137156
_ = Exec(regex2, keys2, str2)
@@ -142,6 +161,15 @@ func BenchmarkExec2(b *testing.B) {
142161
var regex3, keys3, _ = Compile("/foo/bar")
143162
var str3 = []byte("/foo/bar")
144163

164+
func TestExec3(t *testing.T) {
165+
var x map[string]string
166+
x = Exec(regex3, keys3, str3)
167+
168+
if len(x) != 0 {
169+
t.Error("Error in plain text")
170+
}
171+
}
172+
145173
func BenchmarkExec3(b *testing.B) {
146174
for n := 0; n < b.N; n++ {
147175
_ = Exec(regex3, keys3, str3)
@@ -152,6 +180,21 @@ func BenchmarkExec3(b *testing.B) {
152180
var regex4, keys4, _ = Compile("/foo/:bar?")
153181
var str4 = []byte("/foo/bar")
154182

183+
func TestExec4(t *testing.T) {
184+
var x map[string]string
185+
x = Exec(regex4, keys4, str4)
186+
187+
if x["bar"] != "bar" {
188+
t.Error("Error in optional")
189+
}
190+
191+
x = Exec(regex4, keys4, []byte("/foo"))
192+
193+
if x["bar"] != "" {
194+
t.Error("Error in optional")
195+
}
196+
}
197+
155198
func BenchmarkExec4(b *testing.B) {
156199
for n := 0; n < b.N; n++ {
157200
_ = Exec(regex4, keys4, str4)

0 commit comments

Comments
 (0)