-
Notifications
You must be signed in to change notification settings - Fork 382
/
Copy pathrequest_test.go
213 lines (183 loc) · 5.58 KB
/
request_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
package rest
import (
"crypto/tls"
"io"
"net/http"
"strings"
"testing"
)
func defaultRequest(method string, urlStr string, body io.Reader, t *testing.T) *Request {
origReq, err := http.NewRequest(method, urlStr, body)
if err != nil {
t.Fatal(err)
}
return &Request{
origReq,
nil,
map[string]interface{}{},
}
}
func TestRequestEmptyJson(t *testing.T) {
req := defaultRequest("POST", "http://localhost", strings.NewReader(""), t)
err := req.DecodeJsonPayload(nil)
if err != ErrJsonPayloadEmpty {
t.Error("Expected ErrJsonPayloadEmpty")
}
}
func TestRequestBaseUrl(t *testing.T) {
req := defaultRequest("GET", "http://localhost", nil, t)
urlBase := req.BaseUrl()
urlString := urlBase.String()
expected := "http://localhost"
if urlString != expected {
t.Error(expected + " was the expected URL base, but instead got " + urlString)
}
}
func TestRequestUrlScheme(t *testing.T) {
req := defaultRequest("GET", "https://localhost", nil, t)
urlBase := req.BaseUrl()
expected := "https"
if urlBase.Scheme != expected {
t.Error(expected + " was the expected scheme, but instead got " + urlBase.Scheme)
}
}
func TestRequestUrlSchemeHTTP(t *testing.T) {
req := defaultRequest("GET", "http://localhost", nil, t)
urlBase := req.BaseUrl()
expected := "http"
if urlBase.Scheme != expected {
t.Error(expected + " was the expected scheme, but instead got " + urlBase.Scheme)
}
}
func TestRequestUrlSchemeHTTP2TLS(t *testing.T) {
req := defaultRequest("GET", "http://localhost", nil, t)
req.Proto = "HTTP"
req.ProtoMajor = 2
req.ProtoMinor = 0
req.TLS = &tls.ConnectionState{}
urlBase := req.BaseUrl()
expected := "https"
if urlBase.Scheme != expected {
t.Error(expected + " was the expected scheme, but instead got " + urlBase.Scheme)
}
}
func TestRequestUrlFor(t *testing.T) {
req := defaultRequest("GET", "http://localhost", nil, t)
path := "/foo/bar"
urlObj := req.UrlFor(path, nil)
if urlObj.Path != path {
t.Error(path + " was expected to be the path, but got " + urlObj.Path)
}
expected := "http://localhost/foo/bar"
if urlObj.String() != expected {
t.Error(expected + " was expected, but the returned URL was " + urlObj.String())
}
}
func TestRequestUrlForQueryString(t *testing.T) {
req := defaultRequest("GET", "http://localhost", nil, t)
params := map[string][]string{
"id": {"foo", "bar"},
}
urlObj := req.UrlFor("/foo/bar", params)
expected := "http://localhost/foo/bar?id=foo&id=bar"
if urlObj.String() != expected {
t.Error(expected + " was expected, but the returned URL was " + urlObj.String())
}
}
func TestCorsInfoSimpleCors(t *testing.T) {
req := defaultRequest("GET", "http://localhost", nil, t)
req.Request.Header.Set("Origin", "http://another.host")
corsInfo := req.GetCorsInfo()
if corsInfo == nil {
t.Error("Expected non nil CorsInfo")
}
if corsInfo.IsCors == false {
t.Error("This is a CORS request")
}
if corsInfo.IsPreflight == true {
t.Error("This is not a Preflight request")
}
}
func TestCorsInfoNullOrigin(t *testing.T) {
req := defaultRequest("GET", "http://localhost", nil, t)
req.Request.Header.Set("Origin", "null")
corsInfo := req.GetCorsInfo()
if corsInfo == nil {
t.Error("Expected non nil CorsInfo")
}
if corsInfo.IsCors == false {
t.Error("This is a CORS request")
}
if corsInfo.IsPreflight == true {
t.Error("This is not a Preflight request")
}
if corsInfo.OriginUrl != nil {
t.Error("OriginUrl cannot be set")
}
}
func TestCorsInfoPreflightCors(t *testing.T) {
req := defaultRequest("OPTIONS", "http://localhost", nil, t)
req.Request.Header.Set("Origin", "http://another.host")
corsInfo := req.GetCorsInfo()
if corsInfo == nil {
t.Error("Expected non nil CorsInfo")
}
if corsInfo.IsCors == false {
t.Error("This is a CORS request")
}
if corsInfo.IsPreflight == true {
t.Error("This is NOT a Preflight request")
}
// Preflight must have the Access-Control-Request-Method header
req.Request.Header.Set("Access-Control-Request-Method", "PUT")
corsInfo = req.GetCorsInfo()
if corsInfo == nil {
t.Error("Expected non nil CorsInfo")
}
if corsInfo.IsCors == false {
t.Error("This is a CORS request")
}
if corsInfo.IsPreflight == false {
t.Error("This is a Preflight request")
}
if corsInfo.Origin != "http://another.host" {
t.Error("Origin must be identical to the header value")
}
if corsInfo.OriginUrl == nil {
t.Error("OriginUrl must be set")
}
}
func TestCorsInfoEmptyAccessControlRequestHeaders(t *testing.T) {
req := defaultRequest("OPTIONS", "http://localhost", nil, t)
req.Request.Header.Set("Origin", "http://another.host")
// make it a preflight request
req.Request.Header.Set("Access-Control-Request-Method", "PUT")
// WebKit based browsers may send `Access-Control-Request-Headers:` with
// no value, in which case, the header will be present in requests
// Header map, but its value is an empty string.
req.Request.Header.Set("Access-Control-Request-Headers", "")
corsInfo := req.GetCorsInfo()
if corsInfo == nil {
t.Error("Expected non nil CorsInfo")
}
if corsInfo.IsCors == false {
t.Error("This is a CORS request")
}
if len(corsInfo.AccessControlRequestHeaders) > 0 {
t.Error("Access-Control-Request-Headers should have been removed")
}
req.Request.Header.Set("Access-Control-Request-Headers", "")
corsInfo = req.GetCorsInfo()
if corsInfo == nil {
t.Error("Expected non nil CorsInfo")
}
if corsInfo.IsCors == false {
t.Error("This is a CORS request")
}
if corsInfo.IsPreflight == false {
t.Error("This is a Preflight request")
}
if len(corsInfo.AccessControlRequestHeaders) > 0 {
t.Error("Empty Access-Control-Request-Headers header should have been removed")
}
}