-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontext_test.go
162 lines (147 loc) · 3.72 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
package celerity
import (
"bytes"
"errors"
"net/http"
"net/url"
"testing"
)
func TestNewContext(t *testing.T) {
c := NewContext()
if c.URLParams == nil {
t.Error("URLParams not initialized")
}
if c.QueryParams == nil {
t.Error("QueryParams not initialized")
}
if len(c.RequestID) != 32 {
t.Errorf("request id invalid: %s", c.RequestID)
}
}
func TestRespond(t *testing.T) {
c := NewContext()
data := map[string]string{"name": "jim"}
r := c.Respond(data)
if r.StatusCode != 200 {
t.Errorf("Status code not set to 200 (%d)", r.StatusCode)
}
if r.Error != nil {
t.Errorf("Error not empty: %s", r.Error.Error())
}
if v, ok := r.Data.(map[string]string); ok {
if v["name"] != "jim" {
t.Errorf("name not set correctly in data: %s", v["name"])
}
} else {
t.Error("response data not properly set")
}
}
func TestFail(t *testing.T) {
c := NewContext()
r := c.Fail(errors.New("some error"))
if r.Error == nil {
t.Error("error object not set on response")
} else if r.Error.Error() != "some error" {
t.Error("Incorrect error object")
}
if r.StatusCode != 500 {
t.Errorf("Status code not 500: %d", r.StatusCode)
}
}
func TestFailProduction(t *testing.T) {
c := NewContext()
r := c.Fail(errors.New("some error"))
if r.Error == nil {
t.Error("error object not set on response")
} else if r.Error.Error() != "some error" {
t.Error("Incorrect error object")
}
if r.StatusCode != 500 {
t.Errorf("Status code not 500: %d", r.StatusCode)
}
}
func TestURLParams(t *testing.T) {
c := NewContext()
c.SetParams(map[string]string{"n": "1"})
if c.URLParams.String("n") != "1" {
t.Error("string parameter not retrieved correctly")
}
if c.URLParams.Int("n") != 1 {
t.Error("int parameter not retrieved correctly")
}
if c.URLParams.String("notfound") != "" {
t.Error("nonexistant string parameter not retrieved correctly")
}
if c.URLParams.Int("notfound") != -1 {
t.Error("nonexistant int parameter not retrieved correctly")
}
}
func TestQueryParams(t *testing.T) {
url, _ := url.Parse("http://example.com?user=1&name=alice")
c := NewContext()
c.SetQueryParamsFromURL(url)
if v := c.QueryParams.String("name"); v != "alice" {
t.Errorf("name param not correct: %s", v)
}
if v := c.QueryParams.Int("user"); v != 1 {
t.Errorf("name param not correct: %d", v)
}
}
func TestHeader(t *testing.T) {
c := NewContext()
if r, err := http.NewRequest("GET", "/", bytes.NewBuffer(nil)); err != nil {
t.Fatal(err.Error())
} else {
c.Request = r
}
c.Request.Header.Set("test-header", "header-value")
if c.Header("test-header") != "header-value" {
t.Error("header value incorrect")
}
}
func TestGet(t *testing.T) {
c := NewContext()
c.Set("test", "123")
if c.Get("test").(string) != "123" {
t.Errorf("get returned incorrect value")
}
if c.Get("1231") != nil {
t.Error("get for nonexistant key should return nil")
}
}
func TestBody(t *testing.T) {
payload := []byte(`
{
"foo": {
"foos": "bar"
}
}
`)
req, _ := http.NewRequest("GET", "/test", bytes.NewReader(payload))
c := RequestContext(req)
v := c.ExtractValue("foo.foos").String()
if v != "bar" {
t.Errorf("bar value not found: %s", c.Body())
}
}
func TestChainResponses(t *testing.T) {
c := NewContext()
data := map[string]string{"name": "jim"}
r := c.Respond(data).Status(201).MetaValue("test", "123")
if r.StatusCode != 201 {
t.Errorf("Status code not set to 201 (%d)", r.StatusCode)
}
if r.Error != nil {
t.Errorf("Error not empty: %s", r.Error.Error())
}
if v, ok := r.Data.(map[string]string); ok {
if v["name"] != "jim" {
t.Errorf("name not set correctly in data: %s", v["name"])
}
} else {
t.Error("response data not properly set")
}
if r.Meta["test"].(string) != "123" {
t.Error("meta not properly set")
}
}