-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontext_test.go
230 lines (182 loc) · 4.9 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
package rest
import (
"errors"
"io/ioutil"
"net/http"
"net/http/httptest"
"reflect"
"testing"
)
var ctx *context
func init() {
ctx = &context{
r: httptest.NewRequest(http.MethodGet, "/", nil),
w: httptest.NewRecorder(),
}
}
func TestContext_init(t *testing.T) {
ctx.init()
if ctx.headers == nil {
t.Error("context.init() should initialize the context")
}
}
func TestContext_destroy(t *testing.T) {
ctx.destroy()
if ctx.w != nil || ctx.r != nil {
t.Error("context.destroy() should destroy the context")
}
}
func TestContext_reset(t *testing.T) {
ctx.reset()
if !reflect.DeepEqual(ctx.headers, make(map[string]string)) {
t.Error("context.reset() should reset the context")
}
}
func TestContext_Request(t *testing.T) {
if ctx.Request() != ctx.r {
t.Error("context.Request() should return a pointer of request")
}
}
func TestContext_Params(t *testing.T) {
ctx.params = make(map[string]string)
params := ctx.Params()
if !reflect.DeepEqual(ctx.params, params) {
t.Error("context.Params() should return the object of request params")
}
}
func TestContext_Status(t *testing.T) {
status := 200
c := ctx.Status(status)
if c != ctx {
t.Error("context.Status(int) should return pointer of context")
}
if ctx.status != status {
t.Error("context.Status(int) should set a status")
}
}
func TestContext_Header(t *testing.T) {
c := ctx.Header("authtoken", "secret-key")
if c != ctx {
t.Error("context.Header(string, string) should return pointer of context")
}
if ctx.headers["authtoken"] != "secret-key" {
t.Error("context.Header(string, string) should set a header")
}
}
func TestContext_Throw(t *testing.T) {
ctx.Status(504).Throw("TIMEOUT", errors.New("request timeout"))
if ctx.code != "TIMEOUT" || !reflect.DeepEqual(ctx.err, errors.New("request timeout")) {
t.Error("context.Throw(string, error) should throw an error with error code")
}
}
func TestContext_JSON(t *testing.T) {
res := map[string]any{"message": "Hello, World!"}
r := httptest.NewRequest(http.MethodGet, "/", nil)
w := httptest.NewRecorder()
ctx = &context{
r: r,
w: w,
}
ctx.init()
defer ctx.destroy()
ctx.JSON(res)
body, err := ioutil.ReadAll(w.Body)
var expected = `{"message":"Hello, World!"}`
if err != nil || string(body) != expected {
t.Error("context.JSON() should write a response in JSON format")
}
if ctx.headers["Content-Type"] != headerJSON {
t.Error("context.JSON() should set a response header with Content-Type: " + headerJSON)
}
}
func TestContext_XML(t *testing.T) {
type Message struct {
Data string `xml:"data"`
}
r := httptest.NewRequest(http.MethodGet, "/", nil)
w := httptest.NewRecorder()
ctx = &context{
r: r,
w: w,
}
ctx.init()
defer ctx.destroy()
ctx.XML(&Message{Data: "Hello, World!"})
body, err := ioutil.ReadAll(w.Body)
var expected = "<Message><data>Hello, World!</data></Message>"
if err != nil || string(body) != expected {
t.Error("context.XML() should write a response in XML format")
}
if ctx.headers["Content-Type"] != headerXML {
t.Error("context.XML() should set a response header with Content-Type: " + headerXML)
}
}
func TestContext_Text(t *testing.T) {
res := "Hello, World!"
r := httptest.NewRequest(http.MethodGet, "/", nil)
w := httptest.NewRecorder()
ctx = &context{
r: r,
w: w,
}
ctx.init()
defer ctx.destroy()
ctx.Text(res)
body, err := ioutil.ReadAll(w.Body)
if err != nil || string(body) != res {
t.Error("context.Text() should write a response a plain text format")
}
if ctx.headers["Content-Type"] != headerText {
t.Error("context.Text() should set a response header with Content-Type: " + headerText)
}
}
func TestContext_Write(t *testing.T) {
res := []byte("Hello, World!")
r := httptest.NewRequest(http.MethodGet, "/", nil)
w := httptest.NewRecorder()
ctx = &context{
r: r,
w: w,
}
ctx.init()
defer ctx.destroy()
ctx.Write(res)
body, err := ioutil.ReadAll(w.Body)
if err != nil || string(body) != string(res) {
t.Error("context.Write() should write a response in bytes")
}
}
func TestContext_unhandledException404(t *testing.T) {
msg := "404 page not found"
r := httptest.NewRequest(http.MethodGet, "/greeting", nil)
w := httptest.NewRecorder()
ctx = &context{
r: r,
w: w,
}
ctx.init()
defer ctx.destroy()
ctx.Throw(ErrCodeNotFound, errors.New(msg))
ctx.unhandledException()
body, err := ioutil.ReadAll(w.Body)
if err != nil || string(body) != msg {
t.Error("context.unhandledException() should handle not found urls")
}
}
func TestContext_unhandledException500(t *testing.T) {
msg := "internal server error"
r := httptest.NewRequest(http.MethodGet, "/greeting", nil)
w := httptest.NewRecorder()
ctx = &context{
r: r,
w: w,
}
ctx.init()
defer ctx.destroy()
ctx.Throw(ErrCodeRuntimeError, errors.New(msg))
ctx.unhandledException()
body, err := ioutil.ReadAll(w.Body)
if err != nil || string(body) != msg {
t.Error("context.unhandledException() should handle not found urls")
}
}