12 |
7u83 |
1 |
-module(mochiweb_request_tests).
|
|
|
2 |
|
|
|
3 |
-ifdef(TEST).
|
|
|
4 |
|
|
|
5 |
-include_lib("eunit/include/eunit.hrl").
|
|
|
6 |
|
|
|
7 |
accepts_content_type_test() ->
|
|
|
8 |
Req1 = mochiweb_request:new(nil, 'GET', "/foo", {1, 1},
|
|
|
9 |
mochiweb_headers:make([{"Accept",
|
|
|
10 |
"multipart/related"}])),
|
|
|
11 |
?assertEqual(true,
|
|
|
12 |
(mochiweb_request:accepts_content_type("multipart/related",
|
|
|
13 |
Req1))),
|
|
|
14 |
?assertEqual(true,
|
|
|
15 |
(mochiweb_request:accepts_content_type(<<"multipart/related">>,
|
|
|
16 |
Req1))),
|
|
|
17 |
Req2 = mochiweb_request:new(nil, 'GET', "/foo", {1, 1},
|
|
|
18 |
mochiweb_headers:make([{"Accept",
|
|
|
19 |
"text/html"}])),
|
|
|
20 |
?assertEqual(false,
|
|
|
21 |
(mochiweb_request:accepts_content_type("multipart/related",
|
|
|
22 |
Req2))),
|
|
|
23 |
Req3 = mochiweb_request:new(nil, 'GET', "/foo", {1, 1},
|
|
|
24 |
mochiweb_headers:make([{"Accept",
|
|
|
25 |
"text/html, multipart/*"}])),
|
|
|
26 |
?assertEqual(true,
|
|
|
27 |
(mochiweb_request:accepts_content_type("multipart/related",
|
|
|
28 |
Req3))),
|
|
|
29 |
Req4 = mochiweb_request:new(nil, 'GET', "/foo", {1, 1},
|
|
|
30 |
mochiweb_headers:make([{"Accept",
|
|
|
31 |
"text/html, multipart/*; q=0.0"}])),
|
|
|
32 |
?assertEqual(false,
|
|
|
33 |
(mochiweb_request:accepts_content_type("multipart/related",
|
|
|
34 |
Req4))),
|
|
|
35 |
Req5 = mochiweb_request:new(nil, 'GET', "/foo", {1, 1},
|
|
|
36 |
mochiweb_headers:make([{"Accept",
|
|
|
37 |
"text/html, multipart/*; q=0"}])),
|
|
|
38 |
?assertEqual(false,
|
|
|
39 |
(mochiweb_request:accepts_content_type("multipart/related",
|
|
|
40 |
Req5))),
|
|
|
41 |
Req6 = mochiweb_request:new(nil, 'GET', "/foo", {1, 1},
|
|
|
42 |
mochiweb_headers:make([{"Accept",
|
|
|
43 |
"text/html, */*; q=0.0"}])),
|
|
|
44 |
?assertEqual(false,
|
|
|
45 |
(mochiweb_request:accepts_content_type("multipart/related",
|
|
|
46 |
Req6))),
|
|
|
47 |
Req7 = mochiweb_request:new(nil, 'GET', "/foo", {1, 1},
|
|
|
48 |
mochiweb_headers:make([{"Accept",
|
|
|
49 |
"multipart/*; q=0.0, */*"}])),
|
|
|
50 |
?assertEqual(false,
|
|
|
51 |
(mochiweb_request:accepts_content_type("multipart/related",
|
|
|
52 |
Req7))),
|
|
|
53 |
Req8 = mochiweb_request:new(nil, 'GET', "/foo", {1, 1},
|
|
|
54 |
mochiweb_headers:make([{"Accept",
|
|
|
55 |
"*/*; q=0.0, multipart/*"}])),
|
|
|
56 |
?assertEqual(true,
|
|
|
57 |
(mochiweb_request:accepts_content_type("multipart/related",
|
|
|
58 |
Req8))),
|
|
|
59 |
Req9 = mochiweb_request:new(nil, 'GET', "/foo", {1, 1},
|
|
|
60 |
mochiweb_headers:make([{"Accept",
|
|
|
61 |
"*/*; q=0.0, multipart/related"}])),
|
|
|
62 |
?assertEqual(true,
|
|
|
63 |
(mochiweb_request:accepts_content_type("multipart/related",
|
|
|
64 |
Req9))),
|
|
|
65 |
Req10 = mochiweb_request:new(nil, 'GET', "/foo", {1, 1},
|
|
|
66 |
mochiweb_headers:make([{"Accept",
|
|
|
67 |
"text/html; level=1"}])),
|
|
|
68 |
?assertEqual(true,
|
|
|
69 |
(mochiweb_request:accepts_content_type("text/html;level=1",
|
|
|
70 |
Req10))),
|
|
|
71 |
Req11 = mochiweb_request:new(nil, 'GET', "/foo", {1, 1},
|
|
|
72 |
mochiweb_headers:make([{"Accept",
|
|
|
73 |
"text/html; level=1, text/html"}])),
|
|
|
74 |
?assertEqual(true,
|
|
|
75 |
(mochiweb_request:accepts_content_type("text/html",
|
|
|
76 |
Req11))),
|
|
|
77 |
Req12 = mochiweb_request:new(nil, 'GET', "/foo", {1, 1},
|
|
|
78 |
mochiweb_headers:make([{"Accept",
|
|
|
79 |
"text/html; level=1; q=0.0, text/html"}])),
|
|
|
80 |
?assertEqual(false,
|
|
|
81 |
(mochiweb_request:accepts_content_type("text/html;level=1",
|
|
|
82 |
Req12))),
|
|
|
83 |
Req13 = mochiweb_request:new(nil, 'GET', "/foo", {1, 1},
|
|
|
84 |
mochiweb_headers:make([{"Accept",
|
|
|
85 |
"text/html; level=1; q=0.0, text/html"}])),
|
|
|
86 |
?assertEqual(false,
|
|
|
87 |
(mochiweb_request:accepts_content_type("text/html; level=1",
|
|
|
88 |
Req13))),
|
|
|
89 |
Req14 = mochiweb_request:new(nil, 'GET', "/foo", {1, 1},
|
|
|
90 |
mochiweb_headers:make([{"Accept",
|
|
|
91 |
"text/html;level=1;q=0.1, text/html"}])),
|
|
|
92 |
?assertEqual(true,
|
|
|
93 |
(mochiweb_request:accepts_content_type("text/html; level=1",
|
|
|
94 |
Req14))).
|
|
|
95 |
|
|
|
96 |
accepted_encodings_test() ->
|
|
|
97 |
Req1 = mochiweb_request:new(nil, 'GET', "/foo", {1, 1},
|
|
|
98 |
mochiweb_headers:make([])),
|
|
|
99 |
?assertEqual(["identity"],
|
|
|
100 |
(mochiweb_request:accepted_encodings(["gzip",
|
|
|
101 |
"identity"],
|
|
|
102 |
Req1))),
|
|
|
103 |
Req2 = mochiweb_request:new(nil, 'GET', "/foo", {1, 1},
|
|
|
104 |
mochiweb_headers:make([{"Accept-Encoding",
|
|
|
105 |
"gzip, deflate"}])),
|
|
|
106 |
?assertEqual(["gzip", "identity"],
|
|
|
107 |
(mochiweb_request:accepted_encodings(["gzip",
|
|
|
108 |
"identity"],
|
|
|
109 |
Req2))),
|
|
|
110 |
Req3 = mochiweb_request:new(nil, 'GET', "/foo", {1, 1},
|
|
|
111 |
mochiweb_headers:make([{"Accept-Encoding",
|
|
|
112 |
"gzip;q=0.5, deflate"}])),
|
|
|
113 |
?assertEqual(["deflate", "gzip", "identity"],
|
|
|
114 |
(mochiweb_request:accepted_encodings(["gzip", "deflate",
|
|
|
115 |
"identity"],
|
|
|
116 |
Req3))),
|
|
|
117 |
Req4 = mochiweb_request:new(nil, 'GET', "/foo", {1, 1},
|
|
|
118 |
mochiweb_headers:make([{"Accept-Encoding",
|
|
|
119 |
"identity, *;q=0"}])),
|
|
|
120 |
?assertEqual(["identity"],
|
|
|
121 |
(mochiweb_request:accepted_encodings(["gzip", "deflate",
|
|
|
122 |
"identity"],
|
|
|
123 |
Req4))),
|
|
|
124 |
Req5 = mochiweb_request:new(nil, 'GET', "/foo", {1, 1},
|
|
|
125 |
mochiweb_headers:make([{"Accept-Encoding",
|
|
|
126 |
"gzip; q=0.1, *;q=0"}])),
|
|
|
127 |
?assertEqual(["gzip"],
|
|
|
128 |
(mochiweb_request:accepted_encodings(["gzip", "deflate",
|
|
|
129 |
"identity"],
|
|
|
130 |
Req5))),
|
|
|
131 |
Req6 = mochiweb_request:new(nil, 'GET', "/foo", {1, 1},
|
|
|
132 |
mochiweb_headers:make([{"Accept-Encoding",
|
|
|
133 |
"gzip; q=, *;q=0"}])),
|
|
|
134 |
?assertEqual(bad_accept_encoding_value,
|
|
|
135 |
(mochiweb_request:accepted_encodings(["gzip", "deflate",
|
|
|
136 |
"identity"],
|
|
|
137 |
Req6))),
|
|
|
138 |
Req7 = mochiweb_request:new(nil, 'GET', "/foo", {1, 1},
|
|
|
139 |
mochiweb_headers:make([{"Accept-Encoding",
|
|
|
140 |
"gzip;q=2.0, *;q=0"}])),
|
|
|
141 |
?assertEqual(bad_accept_encoding_value,
|
|
|
142 |
(mochiweb_request:accepted_encodings(["gzip",
|
|
|
143 |
"identity"],
|
|
|
144 |
Req7))),
|
|
|
145 |
Req8 = mochiweb_request:new(nil, 'GET', "/foo", {1, 1},
|
|
|
146 |
mochiweb_headers:make([{"Accept-Encoding",
|
|
|
147 |
"deflate, *;q=0.0"}])),
|
|
|
148 |
?assertEqual([],
|
|
|
149 |
(mochiweb_request:accepted_encodings(["gzip",
|
|
|
150 |
"identity"],
|
|
|
151 |
Req8))).
|
|
|
152 |
|
|
|
153 |
accepted_content_types_test() ->
|
|
|
154 |
Req1 = mochiweb_request:new(nil, 'GET', "/foo", {1, 1},
|
|
|
155 |
mochiweb_headers:make([{"Accept",
|
|
|
156 |
"text/html"}])),
|
|
|
157 |
?assertEqual(["text/html"],
|
|
|
158 |
(mochiweb_request:accepted_content_types(["text/html",
|
|
|
159 |
"application/json"],
|
|
|
160 |
Req1))),
|
|
|
161 |
Req2 = mochiweb_request:new(nil, 'GET', "/foo", {1, 1},
|
|
|
162 |
mochiweb_headers:make([{"Accept",
|
|
|
163 |
"text/html, */*;q=0"}])),
|
|
|
164 |
?assertEqual(["text/html"],
|
|
|
165 |
(mochiweb_request:accepted_content_types(["text/html",
|
|
|
166 |
"application/json"],
|
|
|
167 |
Req2))),
|
|
|
168 |
Req3 = mochiweb_request:new(nil, 'GET', "/foo", {1, 1},
|
|
|
169 |
mochiweb_headers:make([{"Accept",
|
|
|
170 |
"text/*, */*;q=0"}])),
|
|
|
171 |
?assertEqual(["text/html"],
|
|
|
172 |
(mochiweb_request:accepted_content_types(["text/html",
|
|
|
173 |
"application/json"],
|
|
|
174 |
Req3))),
|
|
|
175 |
Req4 = mochiweb_request:new(nil, 'GET', "/foo", {1, 1},
|
|
|
176 |
mochiweb_headers:make([{"Accept",
|
|
|
177 |
"text/*;q=0.8, */*;q=0.5"}])),
|
|
|
178 |
?assertEqual(["text/html", "application/json"],
|
|
|
179 |
(mochiweb_request:accepted_content_types(["application/json",
|
|
|
180 |
"text/html"],
|
|
|
181 |
Req4))),
|
|
|
182 |
Req5 = mochiweb_request:new(nil, 'GET', "/foo", {1, 1},
|
|
|
183 |
mochiweb_headers:make([{"Accept",
|
|
|
184 |
"text/*;q=0.8, */*;q=0.5"}])),
|
|
|
185 |
?assertEqual(["text/html", "application/json"],
|
|
|
186 |
(mochiweb_request:accepted_content_types(["text/html",
|
|
|
187 |
"application/json"],
|
|
|
188 |
Req5))),
|
|
|
189 |
Req6 = mochiweb_request:new(nil, 'GET', "/foo", {1, 1},
|
|
|
190 |
mochiweb_headers:make([{"Accept",
|
|
|
191 |
"text/*;q=0.5, */*;q=0.5"}])),
|
|
|
192 |
?assertEqual(["application/json", "text/html"],
|
|
|
193 |
(mochiweb_request:accepted_content_types(["application/json",
|
|
|
194 |
"text/html"],
|
|
|
195 |
Req6))),
|
|
|
196 |
Req7 = mochiweb_request:new(nil, 'GET', "/foo", {1, 1},
|
|
|
197 |
mochiweb_headers:make([{"Accept",
|
|
|
198 |
"text/html;q=0.5, application/json;q=0.5"}])),
|
|
|
199 |
?assertEqual(["application/json", "text/html"],
|
|
|
200 |
(mochiweb_request:accepted_content_types(["application/json",
|
|
|
201 |
"text/html"],
|
|
|
202 |
Req7))),
|
|
|
203 |
Req8 = mochiweb_request:new(nil, 'GET', "/foo", {1, 1},
|
|
|
204 |
mochiweb_headers:make([{"Accept",
|
|
|
205 |
"text/html"}])),
|
|
|
206 |
?assertEqual([],
|
|
|
207 |
(mochiweb_request:accepted_content_types(["application/json"],
|
|
|
208 |
Req8))),
|
|
|
209 |
Req9 = mochiweb_request:new(nil, 'GET', "/foo", {1, 1},
|
|
|
210 |
mochiweb_headers:make([{"Accept",
|
|
|
211 |
"text/*;q=0.9, text/html;q=0.5, */*;q=0.7"}])),
|
|
|
212 |
?assertEqual(["application/json", "text/html"],
|
|
|
213 |
(mochiweb_request:accepted_content_types(["text/html",
|
|
|
214 |
"application/json"],
|
|
|
215 |
Req9))).
|
|
|
216 |
|
|
|
217 |
should_close_test() ->
|
|
|
218 |
F = fun (V, H) ->
|
|
|
219 |
mochiweb_request:should_close(mochiweb_request:new(nil,
|
|
|
220 |
'GET', "/",
|
|
|
221 |
V,
|
|
|
222 |
mochiweb_headers:make(H)))
|
|
|
223 |
end,
|
|
|
224 |
?assertEqual(true,
|
|
|
225 |
(F({1, 1}, [{"Connection", "close"}]))),
|
|
|
226 |
?assertEqual(true,
|
|
|
227 |
(F({1, 0}, [{"Connection", "close"}]))),
|
|
|
228 |
?assertEqual(true,
|
|
|
229 |
(F({1, 1}, [{"Connection", "ClOSe"}]))),
|
|
|
230 |
?assertEqual(false,
|
|
|
231 |
(F({1, 1}, [{"Connection", "closer"}]))),
|
|
|
232 |
?assertEqual(false, (F({1, 1}, []))),
|
|
|
233 |
?assertEqual(true, (F({1, 0}, []))),
|
|
|
234 |
?assertEqual(false,
|
|
|
235 |
(F({1, 0}, [{"Connection", "Keep-Alive"}]))),
|
|
|
236 |
ok.
|
|
|
237 |
|
|
|
238 |
-endif.
|