-
Notifications
You must be signed in to change notification settings - Fork 0
/
context_test.go
116 lines (102 loc) · 2.36 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
package smile
import (
"compress/gzip"
"net/http"
"net/http/httptest"
"net/url"
"strings"
"testing"
)
func TestContext(t *testing.T) {
w := httptest.NewRecorder()
gz := gzip.NewWriter(w)
defer gz.Close()
post := url.Values{}
post.Add("key1", "string1")
post.Add("key1", "string2")
post.Add("test", "test1")
r := httptest.NewRequest("POST", "http://localhost/test?id=1", strings.NewReader(post.Encode()))
r.Header["Content-Type"] = []string{"application/form-data;charset=utf-8"}
r.PostForm = post
r.Header["User-Agent"] = []string{"QQBrowser-1.0"}
r.AddCookie(&http.Cookie{
Name: "testing",
Value: "hello world",
Path: "/",
Domain: "localhost",
HttpOnly: false,
Secure: false,
})
e := Default()
c := initContext(w, r, e)
ip := c.GetClientIP()
t.Log("IP:", ip)
cookie, _ := c.GetCookie("testing")
t.Log("cookie:", cookie)
header := c.GetHeader()
t.Log("header:", header)
schema := c.GetScheme()
t.Log("schema:", schema)
proto := c.GetProto()
t.Log("proto:", proto)
uri := c.GetURL()
t.Log("url:", uri)
host := c.GetHost()
t.Log("host", host)
method := c.GetMethod()
t.Log("method:", method)
path := c.GetPath()
t.Log("path", path)
postKey := c.GetPostParam("test")
t.Log("postKey-test:", postKey)
postParams := c.GetMultipartFormParam("key1")
t.Log("postParams-key1:", postParams)
postFile := c.GetMultipartFormFile("key1")
t.Log("post-Fil:", postFile)
id := c.GetQueryParam("id")
t.Log("queryParam-id:", id)
queryS := c.GetQueryString()
t.Log("queryString:", queryS)
ua := c.GetUserAgent()
t.Log("ua:", ua)
rb := c.GetRawBody()
t.Log("rb:", rb)
c.WriteString("testing string")
c.Flush()
c.Done()
c.SetHeader("resp", "testSetHeader")
c.SetCookie(&http.Cookie{
Name: "SetHeader",
Value: "hello world",
Path: "/",
Domain: "localhost",
HttpOnly: false,
Secure: false,
})
}
func TestHandlerChain(t *testing.T) {
w := httptest.NewRecorder()
r := httptest.NewRequest("GET", "http://localhost/test?id=1", nil)
c := initContext(w, r, Default())
hc := newHandlerChain()
n := 1
for i := 1; i < 6; i++ {
hc.add(func(c *Context) error {
logi := n
t.Log(logi)
if n == 2 {
t.Log(c.Abort())
}
n++
return nil
})
}
c.handlerChain = hc
if err := c.Next(); err != nil {
t.Error(err.Error())
}
hc.reset()
if err := hc.next(c); err != nil {
t.Error(err.Error())
}
}