-
Notifications
You must be signed in to change notification settings - Fork 0
/
params_test.go
79 lines (70 loc) · 1.87 KB
/
params_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package dimaggioRouter
import (
"net/http"
"net/http/httptest"
"testing"
)
func TestParams_GetByName(t *testing.T) {
params := Params{
Param{Key: "id", Value: "2"},
Param{Key: "name", Value: "sagad"},
Param{Key: "age", Value: "23"},
Param{Key: "type", Value: "ball"},
}
for i, _ := range params {
if val, _ := params.GetByName(params[i].Key); val != params[i].Value {
t.Errorf("Wrong value for %s: Got %s; Want %s", params[i].Key, val, params[i].Value)
}
}
}
func TestParams_GetByIndex(t *testing.T) {
params := Params{
Param{Key: "id", Value: "2"},
Param{Key: "name", Value: "sagad"},
Param{Key: "age", Value: "23"},
Param{Key: "type", Value: "ball"},
}
for i, _ := range params {
if val, _ := params.GetByIndex(i); val != params[i].Value {
t.Errorf("Wrong value for %d: Got %s; Want %s", i, val, params[i].Value)
}
}
}
func TestParams_GetQuery(t *testing.T) {
router := New()
wantKey := "name"
var value string
router.GET("/querystring", func(w http.ResponseWriter, r *http.Request, dp Params) {
name, err := dp.GetQuery(r, wantKey)
if err != nil {
t.Fatal(err.Error())
}
value = name
})
rec := httptest.NewRecorder()
r, _ := http.NewRequest(http.MethodGet, "/querystring?name=ali", nil)
router.ServeHTTP(rec, r)
if value == "" {
t.Fatalf("missing the query string parameter %v", wantKey)
}
}
// benchmark testing
func BenchmarkRouter_GET(b *testing.B) {
router := New()
router.GET("/user/$name", benchHandler)
r, _ := http.NewRequest("GET", "/user/gordon", nil)
benchRequest(b, router, r)
}
func benchHandler(_ http.ResponseWriter, _ *http.Request, _ Params) {}
func benchRequest(b *testing.B, router http.Handler, r *http.Request) {
rec := httptest.NewRecorder()
u := r.URL
rq := u.RawQuery
r.RequestURI = u.RequestURI()
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
u.RawQuery = rq
router.ServeHTTP(rec, r)
}
}