Skip to content
This repository
Newer
Older
100644 182 lines (148 sloc) 8.077 kb
ad20685d » fdmanana
2010-11-08 Added mochiweb_request:accept_content_type/1 function.
1 -module(mochiweb_request_tests).
2
3 -ifdef(TEST).
cb460384 » etrepum
2010-12-01 include eunit only if TEST is already defined
4 -include_lib("eunit/include/eunit.hrl").
ad20685d » fdmanana
2010-11-08 Added mochiweb_request:accept_content_type/1 function.
5
6 accepts_content_type_test() ->
7 Req1 = mochiweb_request:new(nil, 'GET', "/foo", {1, 1},
8 mochiweb_headers:make([{"Accept", "multipart/related"}])),
9 ?assertEqual(true, Req1:accepts_content_type("multipart/related")),
c6616013 » fdmanana
2011-06-17 Removed one mochiweb_request:accepts_content_type/1 function clause
10 ?assertEqual(true, Req1:accepts_content_type(<<"multipart/related">>)),
ad20685d » fdmanana
2010-11-08 Added mochiweb_request:accept_content_type/1 function.
11
12 Req2 = mochiweb_request:new(nil, 'GET', "/foo", {1, 1},
13 mochiweb_headers:make([{"Accept", "text/html"}])),
14 ?assertEqual(false, Req2:accepts_content_type("multipart/related")),
15
16 Req3 = mochiweb_request:new(nil, 'GET', "/foo", {1, 1},
17 mochiweb_headers:make([{"Accept", "text/html, multipart/*"}])),
18 ?assertEqual(true, Req3:accepts_content_type("multipart/related")),
19
20 Req4 = mochiweb_request:new(nil, 'GET', "/foo", {1, 1},
21 mochiweb_headers:make([{"Accept", "text/html, multipart/*; q=0.0"}])),
22 ?assertEqual(false, Req4:accepts_content_type("multipart/related")),
23
24 Req5 = mochiweb_request:new(nil, 'GET', "/foo", {1, 1},
25 mochiweb_headers:make([{"Accept", "text/html, multipart/*; q=0"}])),
26 ?assertEqual(false, Req5:accepts_content_type("multipart/related")),
27
28 Req6 = mochiweb_request:new(nil, 'GET', "/foo", {1, 1},
29 mochiweb_headers:make([{"Accept", "text/html, */*; q=0.0"}])),
30 ?assertEqual(false, Req6:accepts_content_type("multipart/related")),
31
32 Req7 = mochiweb_request:new(nil, 'GET', "/foo", {1, 1},
33 mochiweb_headers:make([{"Accept", "multipart/*; q=0.0, */*"}])),
34 ?assertEqual(false, Req7:accepts_content_type("multipart/related")),
35
36 Req8 = mochiweb_request:new(nil, 'GET', "/foo", {1, 1},
37 mochiweb_headers:make([{"Accept", "*/*; q=0.0, multipart/*"}])),
38 ?assertEqual(true, Req8:accepts_content_type("multipart/related")),
39
40 Req9 = mochiweb_request:new(nil, 'GET', "/foo", {1, 1},
41 mochiweb_headers:make([{"Accept", "*/*; q=0.0, multipart/related"}])),
42 ?assertEqual(true, Req9:accepts_content_type("multipart/related")),
43
44 Req10 = mochiweb_request:new(nil, 'GET', "/foo", {1, 1},
45 mochiweb_headers:make([{"Accept", "text/html; level=1"}])),
46 ?assertEqual(true, Req10:accepts_content_type("text/html;level=1")),
47
48 Req11 = mochiweb_request:new(nil, 'GET', "/foo", {1, 1},
49 mochiweb_headers:make([{"Accept", "text/html; level=1, text/html"}])),
50 ?assertEqual(true, Req11:accepts_content_type("text/html")),
51
52 Req12 = mochiweb_request:new(nil, 'GET', "/foo", {1, 1},
53 mochiweb_headers:make([{"Accept", "text/html; level=1; q=0.0, text/html"}])),
54 ?assertEqual(false, Req12:accepts_content_type("text/html;level=1")),
55
56 Req13 = mochiweb_request:new(nil, 'GET', "/foo", {1, 1},
57 mochiweb_headers:make([{"Accept", "text/html; level=1; q=0.0, text/html"}])),
58 ?assertEqual(false, Req13:accepts_content_type("text/html; level=1")),
59
60 Req14 = mochiweb_request:new(nil, 'GET', "/foo", {1, 1},
61 mochiweb_headers:make([{"Accept", "text/html;level=1;q=0.1, text/html"}])),
62 ?assertEqual(true, Req14:accepts_content_type("text/html; level=1")).
63
b5b187bd » fdmanana
2011-03-11 Add missing test for MochiReq:accepted_encodings/1
64 accepted_encodings_test() ->
65 Req1 = mochiweb_request:new(nil, 'GET', "/foo", {1, 1},
66 mochiweb_headers:make([])),
67 ?assertEqual(["identity"],
68 Req1:accepted_encodings(["gzip", "identity"])),
69
70 Req2 = mochiweb_request:new(nil, 'GET', "/foo", {1, 1},
71 mochiweb_headers:make([{"Accept-Encoding", "gzip, deflate"}])),
72 ?assertEqual(["gzip", "identity"],
73 Req2:accepted_encodings(["gzip", "identity"])),
74
75 Req3 = mochiweb_request:new(nil, 'GET', "/foo", {1, 1},
76 mochiweb_headers:make([{"Accept-Encoding", "gzip;q=0.5, deflate"}])),
77 ?assertEqual(["deflate", "gzip", "identity"],
78 Req3:accepted_encodings(["gzip", "deflate", "identity"])),
79
80 Req4 = mochiweb_request:new(nil, 'GET', "/foo", {1, 1},
81 mochiweb_headers:make([{"Accept-Encoding", "identity, *;q=0"}])),
82 ?assertEqual(["identity"],
83 Req4:accepted_encodings(["gzip", "deflate", "identity"])),
84
85 Req5 = mochiweb_request:new(nil, 'GET', "/foo", {1, 1},
86 mochiweb_headers:make([{"Accept-Encoding", "gzip; q=0.1, *;q=0"}])),
87 ?assertEqual(["gzip"],
88 Req5:accepted_encodings(["gzip", "deflate", "identity"])),
89
90 Req6 = mochiweb_request:new(nil, 'GET', "/foo", {1, 1},
91 mochiweb_headers:make([{"Accept-Encoding", "gzip; q=, *;q=0"}])),
92 ?assertEqual(bad_accept_encoding_value,
93 Req6:accepted_encodings(["gzip", "deflate", "identity"])),
94
95 Req7 = mochiweb_request:new(nil, 'GET', "/foo", {1, 1},
96 mochiweb_headers:make([{"Accept-Encoding", "gzip;q=2.0, *;q=0"}])),
97 ?assertEqual(bad_accept_encoding_value,
98 Req7:accepted_encodings(["gzip", "identity"])),
99
100 Req8 = mochiweb_request:new(nil, 'GET', "/foo", {1, 1},
101 mochiweb_headers:make([{"Accept-Encoding", "deflate, *;q=0.0"}])),
102 ?assertEqual([],
103 Req8:accepted_encodings(["gzip", "identity"])).
104
68fd47da » fdmanana
2011-06-17 Add mochiweb_request:accepted_content_types/1
105 accepted_content_types_test() ->
106 Req1 = mochiweb_request:new(nil, 'GET', "/foo", {1, 1},
107 mochiweb_headers:make([{"Accept", "text/html"}])),
108 ?assertEqual(["text/html"],
109 Req1:accepted_content_types(["text/html", "application/json"])),
110
111 Req2 = mochiweb_request:new(nil, 'GET', "/foo", {1, 1},
112 mochiweb_headers:make([{"Accept", "text/html, */*;q=0"}])),
113 ?assertEqual(["text/html"],
114 Req2:accepted_content_types(["text/html", "application/json"])),
115
116 Req3 = mochiweb_request:new(nil, 'GET', "/foo", {1, 1},
117 mochiweb_headers:make([{"Accept", "text/*, */*;q=0"}])),
118 ?assertEqual(["text/html"],
119 Req3:accepted_content_types(["text/html", "application/json"])),
120
121 Req4 = mochiweb_request:new(nil, 'GET', "/foo", {1, 1},
122 mochiweb_headers:make([{"Accept", "text/*;q=0.8, */*;q=0.5"}])),
123 ?assertEqual(["text/html", "application/json"],
124 Req4:accepted_content_types(["application/json", "text/html"])),
125
126 Req5 = mochiweb_request:new(nil, 'GET', "/foo", {1, 1},
127 mochiweb_headers:make([{"Accept", "text/*;q=0.8, */*;q=0.5"}])),
128 ?assertEqual(["text/html", "application/json"],
129 Req5:accepted_content_types(["text/html", "application/json"])),
130
131 Req6 = mochiweb_request:new(nil, 'GET', "/foo", {1, 1},
132 mochiweb_headers:make([{"Accept", "text/*;q=0.5, */*;q=0.5"}])),
133 ?assertEqual(["application/json", "text/html"],
134 Req6:accepted_content_types(["application/json", "text/html"])),
135
136 Req7 = mochiweb_request:new(nil, 'GET', "/foo", {1, 1},
137 mochiweb_headers:make(
138 [{"Accept", "text/html;q=0.5, application/json;q=0.5"}])),
139 ?assertEqual(["application/json", "text/html"],
140 Req7:accepted_content_types(["application/json", "text/html"])),
141
142 Req8 = mochiweb_request:new(nil, 'GET', "/foo", {1, 1},
143 mochiweb_headers:make([{"Accept", "text/html"}])),
144 ?assertEqual([],
a2e3bb5e » fdmanana
2011-06-21 Add one more test for mochiweb_request:accepted_content_types/1
145 Req8:accepted_content_types(["application/json"])),
146
147 Req9 = mochiweb_request:new(nil, 'GET', "/foo", {1, 1},
148 mochiweb_headers:make([{"Accept", "text/*;q=0.9, text/html;q=0.5, */*;q=0.7"}])),
149 ?assertEqual(["application/json", "text/html"],
150 Req9:accepted_content_types(["text/html", "application/json"])).
68fd47da » fdmanana
2011-06-17 Add mochiweb_request:accepted_content_types/1
151
9d205a8c » etrepum
2012-07-27 bump to 2.3.2, Case insensitive match for "Connection: close" (#81)
152 should_close_test() ->
153 F = fun (V, H) ->
154 (mochiweb_request:new(
155 nil, 'GET', "/", V,
156 mochiweb_headers:make(H)
157 )):should_close()
158 end,
159 ?assertEqual(
160 true,
161 F({1, 1}, [{"Connection", "close"}])),
162 ?assertEqual(
163 true,
164 F({1, 0}, [{"Connection", "close"}])),
165 ?assertEqual(
166 true,
167 F({1, 1}, [{"Connection", "ClOSe"}])),
168 ?assertEqual(
169 false,
170 F({1, 1}, [{"Connection", "closer"}])),
171 ?assertEqual(
172 false,
173 F({1, 1}, [])),
174 ?assertEqual(
175 true,
176 F({1, 0}, [])),
177 ?assertEqual(
178 false,
179 F({1, 0}, [{"Connection", "Keep-Alive"}])),
180 ok.
181
ad20685d » fdmanana
2010-11-08 Added mochiweb_request:accept_content_type/1 function.
182 -endif.
Something went wrong with that request. Please try again.