-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathcontext_test.go
166 lines (147 loc) · 4.21 KB
/
context_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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package routing
import (
"errors"
"fmt"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
func TestContextParam(t *testing.T) {
c := NewContext(nil, nil)
values := []string{"a", "b", "c", "d"}
c.pvalues = values
c.pnames = nil
assert.Equal(t, "", c.Param(""))
assert.Equal(t, "", c.Param("Name"))
c.pnames = []string{"Name", "Age"}
assert.Equal(t, "", c.Param(""))
assert.Equal(t, "a", c.Param("Name"))
assert.Equal(t, "b", c.Param("Age"))
assert.Equal(t, "", c.Param("Xyz"))
}
func TestContextSetParam(t *testing.T) {
c := NewContext(nil, nil)
c.pnames = []string{"Name", "Age"}
c.pvalues = []string{"abc", "123"}
assert.Equal(t, "abc", c.Param("Name"))
c.SetParam("Name", "xyz")
assert.Equal(t, "xyz", c.Param("Name"))
assert.Equal(t, "", c.Param("unknown"))
c.SetParam("unknown", "xyz")
assert.Equal(t, "xyz", c.Param("unknown"))
}
func TestContextInit(t *testing.T) {
c := NewContext(nil, nil)
assert.Nil(t, c.Response)
assert.Nil(t, c.Request)
assert.Equal(t, 0, len(c.handlers))
req, _ := http.NewRequest("GET", "/users/", nil)
c.init(httptest.NewRecorder(), req)
assert.NotNil(t, c.Response)
assert.NotNil(t, c.Request)
assert.Equal(t, -1, c.index)
assert.Nil(t, c.data)
}
func TestContextURL(t *testing.T) {
router := New()
router.Get("/users/<id:\\d+>/<action>/*").Name("users")
c := &Context{router: router}
assert.Equal(t, "/users/123/address/", c.URL("users", "id", 123, "action", "address"))
assert.Equal(t, "", c.URL("abc", "id", 123, "action", "address"))
}
func TestContextGetSet(t *testing.T) {
c := NewContext(nil, nil)
c.init(nil, nil)
assert.Nil(t, c.Get("abc"))
c.Set("abc", "123")
c.Set("xyz", 123)
assert.Equal(t, "123", c.Get("abc").(string))
assert.Equal(t, 123, c.Get("xyz").(int))
}
func TestContextQueryForm(t *testing.T) {
req, _ := http.NewRequest("POST", "http://www.google.com/search?q=foo&q=bar&both=x&prio=1&empty=not",
strings.NewReader("z=post&both=y&prio=2&empty="))
req.Header.Set("Content-Type", "application/x-www-form-urlencoded; param=value")
c := NewContext(nil, req)
assert.Equal(t, "foo", c.Query("q"))
assert.Equal(t, "", c.Query("z"))
assert.Equal(t, "123", c.Query("z", "123"))
assert.Equal(t, "not", c.Query("empty", "123"))
assert.Equal(t, "post", c.PostForm("z"))
assert.Equal(t, "", c.PostForm("x"))
assert.Equal(t, "123", c.PostForm("q", "123"))
assert.Equal(t, "", c.PostForm("empty", "123"))
assert.Equal(t, "y", c.Form("both"))
assert.Equal(t, "", c.Form("x"))
assert.Equal(t, "123", c.Form("x", "123"))
}
func TestContextNextAbort(t *testing.T) {
c, res := testNewContext(
testNormalHandler("a"),
testNormalHandler("b"),
testNormalHandler("c"),
)
assert.Nil(t, c.Next())
assert.Equal(t, "<a/><b/><c/>", res.Body.String())
c, res = testNewContext(
testNextHandler("a"),
testNextHandler("b"),
testNextHandler("c"),
)
assert.Nil(t, c.Next())
assert.Equal(t, "<a><b><c></c></b></a>", res.Body.String())
c, res = testNewContext(
testNextHandler("a"),
testAbortHandler("b"),
testNormalHandler("c"),
)
assert.Nil(t, c.Next())
assert.Equal(t, "<a><b/></a>", res.Body.String())
c, res = testNewContext(
testNextHandler("a"),
testErrorHandler("b"),
testNormalHandler("c"),
)
err := c.Next()
if assert.NotNil(t, err) {
assert.Equal(t, "error:b", err.Error())
}
assert.Equal(t, "<a><b/></a>", res.Body.String())
}
func testNewContext(handlers ...Handler) (*Context, *httptest.ResponseRecorder) {
res := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "http://127.0.0.1/users", nil)
c := &Context{}
c.init(res, req)
c.handlers = handlers
return c, res
}
func testNextHandler(tag string) Handler {
return func(c *Context) error {
fmt.Fprintf(c.Response, "<%v>", tag)
err := c.Next()
fmt.Fprintf(c.Response, "</%v>", tag)
return err
}
}
func testAbortHandler(tag string) Handler {
return func(c *Context) error {
fmt.Fprintf(c.Response, "<%v/>", tag)
c.Abort()
return nil
}
}
func testErrorHandler(tag string) Handler {
return func(c *Context) error {
fmt.Fprintf(c.Response, "<%v/>", tag)
return errors.New("error:" + tag)
}
}
func testNormalHandler(tag string) Handler {
return func(c *Context) error {
fmt.Fprintf(c.Response, "<%v/>", tag)
return nil
}
}