PageRenderTime 31ms CodeModel.GetById 13ms app.highlight 16ms RepoModel.GetById 1ms app.codeStats 0ms

/src/mochiweb_request_tests.erl

http://github.com/basho/mochiweb
Erlang | 152 lines | 119 code | 33 blank | 0 comment | 0 complexity | 3ac9447edc1eef131b81dd29a7062fca MD5 | raw file
  1-module(mochiweb_request_tests).
  2
  3-ifdef(TEST).
  4-include_lib("eunit/include/eunit.hrl").
  5
  6accepts_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")),
 10    ?assertEqual(true, Req1:accepts_content_type(<<"multipart/related">>)),
 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
 64accepted_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
105accepted_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([],
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"])).
151
152-endif.