-
Notifications
You must be signed in to change notification settings - Fork 19
/
gongular_test.go
158 lines (120 loc) · 3.57 KB
/
gongular_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
package gongular
import (
"net/http"
"testing"
"github.com/stretchr/testify/assert"
)
type simpleHandler struct{}
func (s *simpleHandler) Handle(c *Context) error {
c.SetBody("selam")
return nil
}
func TestSimpleGetHandler(t *testing.T) {
e := newEngineTest()
e.GetRouter().GET("/", &simpleHandler{})
resp, content := get(t, e, "/")
assert.Equal(t, http.StatusOK, resp.Code)
assert.Equal(t, "\"selam\"", content)
}
func TestSimplePostHandler(t *testing.T) {
e := newEngineTest()
e.GetRouter().POST("/", &simpleHandler{})
resp, content := post(t, e, "/", nil)
assert.Equal(t, http.StatusOK, resp.Code)
assert.Equal(t, "\"selam\"", content)
}
func TestSimplePutHandler(t *testing.T) {
e := newEngineTest()
e.GetRouter().PUT("/", &simpleHandler{})
resp, content := respWrap(t, e, "/", http.MethodPut, nil)
assert.Equal(t, http.StatusOK, resp.Code)
assert.Equal(t, "\"selam\"", content)
}
func TestSimpleHttpMethodsHandler(t *testing.T) {
e := newEngineTest()
fns := map[string]func(string, ...RequestHandler){
http.MethodHead: e.GetRouter().HEAD,
http.MethodConnect: e.GetRouter().CONNECT,
http.MethodDelete: e.GetRouter().DELETE,
http.MethodOptions: e.GetRouter().OPTIONS,
http.MethodPatch: e.GetRouter().PATCH,
http.MethodTrace: e.GetRouter().TRACE,
http.MethodPut: e.GetRouter().PUT,
}
for method, fn := range fns {
path := "/" + method
fn(path, &simpleHandler{})
resp, content := respWrap(t, e, path, method, nil)
assert.Equal(t, http.StatusOK, resp.Code)
assert.Equal(t, "\"selam\"", content)
}
}
func TestSimpleHttpMethodsAlternativeHandler(t *testing.T) {
fns := []string{
http.MethodHead,
http.MethodConnect,
http.MethodDelete,
http.MethodOptions,
http.MethodPatch,
http.MethodTrace,
http.MethodPut,
}
for _, method := range fns {
e := newEngineTest()
path := "/" + method
e.GetRouter().Method(method, path, &simpleHandler{})
resp, content := respWrap(t, e, path, method, nil)
assert.Equal(t, http.StatusOK, resp.Code)
assert.Equal(t, "\"selam\"", content)
}
}
type statusSetHandler struct{}
func (s *statusSetHandler) Handle(c *Context) error {
c.Status(http.StatusExpectationFailed)
c.Status(http.StatusFailedDependency)
return nil
}
func TestSetStatus(t *testing.T) {
e := newEngineTest()
e.GetRouter().GET("/", &statusSetHandler{})
resp, _ := get(t, e, "/")
assert.Equal(t, http.StatusExpectationFailed, resp.Code)
}
type statusMustSetHandler struct{}
func (s *statusMustSetHandler) Handle(c *Context) error {
c.Status(http.StatusExpectationFailed)
c.MustStatus(http.StatusTeapot)
return nil
}
func TestSetMustStatus(t *testing.T) {
e := newEngineTest()
e.GetRouter().GET("/", &statusMustSetHandler{})
resp, _ := get(t, e, "/")
assert.Equal(t, http.StatusTeapot, resp.Code)
}
type headerHandler struct{}
func (s *headerHandler) Handle(c *Context) error {
c.Header("X-API-KEY", "selam")
c.Header("DNT", "1")
return nil
}
func TestSetHeader(t *testing.T) {
e := newEngineTest()
e.GetRouter().GET("/header", &headerHandler{})
resp, _ := get(t, e, "/header")
assert.Equal(t, http.StatusOK, resp.Code)
assert.Equal(t, "selam", resp.Header().Get("X-API-KEY"))
assert.Equal(t, "1", resp.Header().Get("DNT"))
}
type setByteBody struct{}
func (s *setByteBody) Handle(c *Context) error {
c.SetBody([]byte{20, 30, 40, 60, 243})
return nil
}
func TestSetByteBody(t *testing.T) {
e := newEngineTest()
e.GetRouter().GET("/bytez", &setByteBody{})
resp, content := respBytes(t, e, "/bytez", http.MethodGet)
assert.Equal(t, http.StatusOK, resp.Code)
assert.Equal(t, []byte{20, 30, 40, 60, 243}, content)
}