forked from birkirb/revel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
params_test.go
223 lines (190 loc) · 6.12 KB
/
params_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
// Copyright (c) 2012-2016 The Revel Framework Authors, All rights reserved.
// Revel Framework source code and usage is governed by a MIT style
// license that can be found in the LICENSE file.
package revel
import (
"bytes"
"fmt"
"io/ioutil"
"math/rand"
"net/http"
"net/url"
"reflect"
"testing"
"unsafe"
)
// Params: Testing Multipart forms
const (
MultipartBoundary = "A"
MultipartFormData = `--A
Content-Disposition: form-data; name="text1"
data1
--A
Content-Disposition: form-data; name="text2"
data2
--A
Content-Disposition: form-data; name="text2"
data3
--A
Content-Disposition: form-data; name="file1"; filename="test.txt"
Content-Type: text/plain
content1
--A
Content-Disposition: form-data; name="file2[]"; filename="test.txt"
Content-Type: text/plain
content2
--A
Content-Disposition: form-data; name="file2[]"; filename="favicon.ico"
Content-Type: image/x-icon
xyz
--A
Content-Disposition: form-data; name="file3[0]"; filename="test.txt"
Content-Type: text/plain
content3
--A
Content-Disposition: form-data; name="file3[1]"; filename="favicon.ico"
Content-Type: image/x-icon
zzz
--A--
`
)
const (
letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" // Alphanumeric characters (latin alphabet)
letterIdxBits = 6 // 6 bits to represent a letter index
letterIdxMask = 1<<letterIdxBits - 1 // All 1-bits, as many as letterIdxBits
letterIdxMax = 63 / letterIdxBits // # of letter indices fitting in 63 bits
)
func mustRandString(t *testing.T, n int64) string {
t.Helper()
b := make([]byte, n)
// A src.Int63() generates 63 random bits, enough for letterIdxMax characters
for i, cache, remain := n-1, rand.Int63(), letterIdxMax; i >= 0; {
if remain == 0 {
cache, remain = rand.Int63(), letterIdxMax
}
if idx := int(cache & letterIdxMask); idx < len(letterBytes) {
b[i] = letterBytes[idx]
i--
}
cache >>= letterIdxBits
remain--
}
return *(*string)(unsafe.Pointer(&b))
}
// The values represented by the form data.
type fh struct {
filename string
content []byte
}
var (
expectedValues = map[string][]string{
"text1": {"data1"},
"text2": {"data2", "data3"},
}
expectedFiles = map[string][]fh{
"file1": {fh{"test.txt", []byte("content1")}},
"file2[]": {fh{"test.txt", []byte("content2")}, fh{"favicon.ico", []byte("xyz")}},
"file3[0]": {fh{"test.txt", []byte("content3")}},
"file3[1]": {fh{"favicon.ico", []byte("zzz")}},
}
)
func getMultipartRequest() *http.Request {
req, _ := http.NewRequest("POST", "http://localhost/path",
bytes.NewBufferString(MultipartFormData))
req.Header.Set(
"Content-Type", fmt.Sprintf("multipart/form-data; boundary=%s", MultipartBoundary))
req.Header.Set(
"Content-Length", fmt.Sprintf("%d", len(MultipartFormData)))
return req
}
func TestLimitReader(t *testing.T) {
buf := bytes.NewBufferString(mustRandString(t, _50MB))
_, err := ioutil.ReadAll(LimitReader(buf, _50MB))
if err != nil {
t.Fatal(err)
}
buf = bytes.NewBufferString(mustRandString(t, (_50MB) + 1))
_, err = ioutil.ReadAll(LimitReader(buf, _50MB))
if err == nil {
t.Fatal("no error produced trying to read limit a file larger than the read limit")
}
}
func BenchmarkParams(b *testing.B) {
c := NewTestController(nil, showRequest)
c.Params = &Params{}
for i := 0; i < b.N; i++ {
ParamsFilter(c, NilChain)
}
}
func TestMultipartForm(t *testing.T) {
c := NewTestController(nil, getMultipartRequest())
c.Params = &Params{}
ParamsFilter(c, NilChain)
if !reflect.DeepEqual(expectedValues, map[string][]string(c.Params.Values)) {
t.Errorf("Param values: (expected) %v != %v (actual)",
expectedValues, map[string][]string(c.Params.Values))
}
actualFiles := make(map[string][]fh)
for key, fileHeaders := range c.Params.Files {
for _, fileHeader := range fileHeaders {
file, _ := fileHeader.Open()
content, _ := ioutil.ReadAll(file)
actualFiles[key] = append(actualFiles[key], fh{fileHeader.Filename, content})
}
}
if !reflect.DeepEqual(expectedFiles, actualFiles) {
t.Errorf("Param files: (expected) %v != %v (actual)", expectedFiles, actualFiles)
}
}
func TestBind(t *testing.T) {
params := Params{
Values: url.Values{
"x": {"5"},
},
}
var x int
params.Bind(&x, "x")
if x != 5 {
t.Errorf("Failed to bind x. Value: %d", x)
}
}
func TestResolveAcceptLanguage(t *testing.T) {
request := buildHTTPRequestWithAcceptLanguage("")
if result := ResolveAcceptLanguage(request); result != nil {
t.Errorf("Expected Accept-Language to resolve to an empty string but it was '%s'", result)
}
request = buildHTTPRequestWithAcceptLanguage("en-GB,en;q=0.8,nl;q=0.6")
if result := ResolveAcceptLanguage(request); len(result) != 3 {
t.Errorf("Unexpected Accept-Language values length of %d (expected %d)", len(result), 3)
} else {
if result[0].Language != "en-GB" {
t.Errorf("Expected '%s' to be most qualified but instead it's '%s'", "en-GB", result[0].Language)
}
if result[1].Language != "en" {
t.Errorf("Expected '%s' to be most qualified but instead it's '%s'", "en", result[1].Language)
}
if result[2].Language != "nl" {
t.Errorf("Expected '%s' to be most qualified but instead it's '%s'", "nl", result[2].Language)
}
}
request = buildHTTPRequestWithAcceptLanguage("en;q=0.8,nl;q=0.6,en-AU;q=malformed")
if result := ResolveAcceptLanguage(request); len(result) != 3 {
t.Errorf("Unexpected Accept-Language values length of %d (expected %d)", len(result), 3)
} else {
if result[0].Language != "en-AU" {
t.Errorf("Expected '%s' to be most qualified but instead it's '%s'", "en-AU", result[0].Language)
}
}
}
func BenchmarkResolveAcceptLanguage(b *testing.B) {
for i := 0; i < b.N; i++ {
request := buildHTTPRequestWithAcceptLanguage("en-GB,en;q=0.8,nl;q=0.6,fr;q=0.5,de-DE;q=0.4,no-NO;q=0.4,ru;q=0.2")
ResolveAcceptLanguage(request)
}
}
func buildHTTPRequestWithAcceptLanguage(acceptLanguage string) *Request {
request, _ := http.NewRequest("POST", "http://localhost/path", nil)
request.Header.Set("Accept-Language", acceptLanguage)
c := NewTestController(nil, request)
return c.Request
}