5 class TestWEBrickHTTPRequest < Test::Unit::TestCase
7 msg = <<-_end_of_message_
9 foobar # HTTP/0.9 request don't have header nor entity body.
11 req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
12 req.parse(StringIO.new(msg.gsub(/^ {6}/, "")))
13 assert_equal("GET", req.request_method)
14 assert_equal("/", req.unparsed_uri)
15 assert_equal(WEBrick::HTTPVersion.new("0.9"), req.http_version)
16 assert_equal(WEBrick::Config::HTTP[:ServerName], req.host)
17 assert_equal(80, req.port)
18 assert_equal(false, req.keep_alive?)
19 assert_equal(nil, req.body)
20 assert(req.query.empty?)
24 msg = <<-_end_of_message_
28 req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
29 req.parse(StringIO.new(msg.gsub(/^ {6}/, "")))
30 assert_equal("GET", req.request_method)
31 assert_equal("/", req.unparsed_uri)
32 assert_equal(WEBrick::HTTPVersion.new("1.0"), req.http_version)
33 assert_equal(WEBrick::Config::HTTP[:ServerName], req.host)
34 assert_equal(80, req.port)
35 assert_equal(false, req.keep_alive?)
36 assert_equal(nil, req.body)
37 assert(req.query.empty?)
41 msg = <<-_end_of_message_
45 req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
46 req.parse(StringIO.new(msg.gsub(/^ {6}/, "")))
47 assert_equal("GET", req.request_method)
48 assert_equal("/path", req.unparsed_uri)
49 assert_equal("", req.script_name)
50 assert_equal("/path", req.path_info)
51 assert_equal(WEBrick::HTTPVersion.new("1.1"), req.http_version)
52 assert_equal(WEBrick::Config::HTTP[:ServerName], req.host)
53 assert_equal(80, req.port)
54 assert_equal(true, req.keep_alive?)
55 assert_equal(nil, req.body)
56 assert(req.query.empty?)
59 def test_request_uri_too_large
60 msg = <<-_end_of_message_
61 GET /#{"a"*1024} HTTP/1.1
63 req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
64 assert_raise(WEBrick::HTTPStatus::RequestURITooLarge){
65 req.parse(StringIO.new(msg.gsub(/^ {6}/, "")))
69 def test_parse_headers
70 msg = <<-_end_of_message_
72 Host: test.ruby-lang.org:8080
74 Accept: text/*;q=0.3, text/html;q=0.7, text/html;level=1,
75 text/html;level=2;q=0.4, */*;q=0.5
76 Accept-Encoding: compress;q=0.5
77 Accept-Encoding: gzip;q=1.0, identity; q=0.4, *;q=0
78 Accept-Language: en;q=0.5, *; q=0
80 Content-Type: text/plain
86 req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
87 req.parse(StringIO.new(msg.gsub(/^ {6}/, "")))
89 URI.parse("http://test.ruby-lang.org:8080/path"), req.request_uri)
90 assert_equal("test.ruby-lang.org", req.host)
91 assert_equal(8080, req.port)
92 assert_equal(false, req.keep_alive?)
94 %w(text/html;level=1 text/html */* text/html;level=2 text/*),
96 assert_equal(%w(gzip compress identity *), req.accept_encoding)
97 assert_equal(%w(ja en *), req.accept_language)
98 assert_equal(7, req.content_length)
99 assert_equal("text/plain", req.content_type)
100 assert_equal("foobar\n", req.body)
101 assert_equal("", req["x-empty-header"])
102 assert_equal(nil, req["x-no-header"])
103 assert(req.query.empty?)
106 def test_parse_header2()
107 msg = <<-_end_of_message_
108 POST /foo/bar/../baz?q=a HTTP/1.0
116 req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
117 req.parse(StringIO.new(msg.gsub(/^ {6}/, "")))
118 assert_equal("POST", req.request_method)
119 assert_equal("/foo/baz", req.path)
120 assert_equal("", req.script_name)
121 assert_equal("/foo/baz", req.path_info)
122 assert_equal("9", req['content-length'])
123 assert_equal("FOO BAR BAZ", req['user-agent'])
124 assert_equal("hogehoge\n", req.body)
127 def test_parse_headers3
128 msg = <<-_end_of_message_
130 Host: test.ruby-lang.org
133 req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
134 req.parse(StringIO.new(msg.gsub(/^ {6}/, "")))
135 assert_equal(URI.parse("http://test.ruby-lang.org/path"), req.request_uri)
136 assert_equal("test.ruby-lang.org", req.host)
137 assert_equal(80, req.port)
139 msg = <<-_end_of_message_
144 req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
145 req.parse(StringIO.new(msg.gsub(/^ {6}/, "")))
146 assert_equal(URI.parse("http://192.168.1.1/path"), req.request_uri)
147 assert_equal("192.168.1.1", req.host)
148 assert_equal(80, req.port)
150 msg = <<-_end_of_message_
152 Host: [fe80::208:dff:feef:98c7]
155 req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
156 req.parse(StringIO.new(msg.gsub(/^ {6}/, "")))
157 assert_equal(URI.parse("http://[fe80::208:dff:feef:98c7]/path"),
159 assert_equal("[fe80::208:dff:feef:98c7]", req.host)
160 assert_equal(80, req.port)
162 msg = <<-_end_of_message_
164 Host: 192.168.1.1:8080
167 req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
168 req.parse(StringIO.new(msg.gsub(/^ {6}/, "")))
169 assert_equal(URI.parse("http://192.168.1.1:8080/path"), req.request_uri)
170 assert_equal("192.168.1.1", req.host)
171 assert_equal(8080, req.port)
173 msg = <<-_end_of_message_
175 Host: [fe80::208:dff:feef:98c7]:8080
178 req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
179 req.parse(StringIO.new(msg.gsub(/^ {6}/, "")))
180 assert_equal(URI.parse("http://[fe80::208:dff:feef:98c7]:8080/path"),
182 assert_equal("[fe80::208:dff:feef:98c7]", req.host)
183 assert_equal(8080, req.port)
186 def test_parse_get_params
187 param = "foo=1;foo=2;foo=3;bar=x"
188 msg = <<-_end_of_message_
189 GET /path?#{param} HTTP/1.1
190 Host: test.ruby-lang.org:8080
193 req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
194 req.parse(StringIO.new(msg.gsub(/^ {6}/, "")))
196 assert_equal("1", query["foo"])
197 assert_equal(["1", "2", "3"], query["foo"].to_ary)
198 assert_equal(["1", "2", "3"], query["foo"].list)
199 assert_equal("x", query["bar"])
200 assert_equal(["x"], query["bar"].list)
203 def test_parse_post_params
204 param = "foo=1;foo=2;foo=3;bar=x"
205 msg = <<-_end_of_message_
206 POST /path?foo=x;foo=y;foo=z;bar=1 HTTP/1.1
207 Host: test.ruby-lang.org:8080
208 Content-Length: #{param.size}
209 Content-Type: application/x-www-form-urlencoded
213 req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
214 req.parse(StringIO.new(msg.gsub(/^ {6}/, "")))
216 assert_equal("1", query["foo"])
217 assert_equal(["1", "2", "3"], query["foo"].to_ary)
218 assert_equal(["1", "2", "3"], query["foo"].list)
219 assert_equal("x", query["bar"])
220 assert_equal(["x"], query["bar"].list)
225 msg = <<-_end_of_message_
227 Host: test.ruby-lang.org:8080
228 Transfer-Encoding: chunked
231 msg.gsub!(/^ {6}/, "")
233 while chunk = io.read(100)
234 msg << chunk.size.to_s(16) << crlf
239 req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
240 req.parse(StringIO.new(msg))
241 assert_equal(File.read(__FILE__), req.body)
245 msg = <<-_end_of_message_
247 Host: localhost:10080
248 User-Agent: w3m/0.5.2
249 X-Forwarded-For: 123.123.123.123
250 X-Forwarded-Host: forward.example.com
251 X-Forwarded-Server: server.example.com
252 Connection: Keep-Alive
255 msg.gsub!(/^ {6}/, "")
256 req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
257 req.parse(StringIO.new(msg))
258 assert_equal("server.example.com", req.server_name)
259 assert_equal("http://forward.example.com/foo", req.request_uri.to_s)
260 assert_equal("forward.example.com", req.host)
261 assert_equal(80, req.port)
262 assert_equal("123.123.123.123", req.remote_ip)
265 msg = <<-_end_of_message_
267 Host: localhost:10080
268 User-Agent: w3m/0.5.2
269 X-Forwarded-For: 192.168.1.10, 172.16.1.1, 123.123.123.123
270 X-Forwarded-Host: forward.example.com:8080
271 X-Forwarded-Server: server.example.com
272 Connection: Keep-Alive
275 msg.gsub!(/^ {6}/, "")
276 req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
277 req.parse(StringIO.new(msg))
278 assert_equal("server.example.com", req.server_name)
279 assert_equal("http://forward.example.com:8080/foo", req.request_uri.to_s)
280 assert_equal("forward.example.com", req.host)
281 assert_equal(8080, req.port)
282 assert_equal("123.123.123.123", req.remote_ip)
285 msg = <<-_end_of_message_
287 Host: localhost:10080
288 Client-IP: 234.234.234.234
289 X-Forwarded-Proto: https
290 X-Forwarded-For: 192.168.1.10, 10.0.0.1, 123.123.123.123
291 X-Forwarded-Host: forward.example.com
292 X-Forwarded-Server: server.example.com
293 X-Requested-With: XMLHttpRequest
294 Connection: Keep-Alive
297 msg.gsub!(/^ {6}/, "")
298 req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
299 req.parse(StringIO.new(msg))
300 assert_equal("server.example.com", req.server_name)
301 assert_equal("https://forward.example.com/foo", req.request_uri.to_s)
302 assert_equal("forward.example.com", req.host)
303 assert_equal(443, req.port)
304 assert_equal("234.234.234.234", req.remote_ip)
308 def test_bad_messages
309 param = "foo=1;foo=2;foo=3;bar=x"
310 msg = <<-_end_of_message_
311 POST /path?foo=x;foo=y;foo=z;bar=1 HTTP/1.1
312 Host: test.ruby-lang.org:8080
313 Content-Type: application/x-www-form-urlencoded
317 assert_raise(WEBrick::HTTPStatus::LengthRequired){
318 req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
319 req.parse(StringIO.new(msg.gsub(/^ {6}/, "")))
323 msg = <<-_end_of_message_
324 POST /path?foo=x;foo=y;foo=z;bar=1 HTTP/1.1
325 Host: test.ruby-lang.org:8080
326 Content-Length: 100000
330 assert_raise(WEBrick::HTTPStatus::BadRequest){
331 req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
332 req.parse(StringIO.new(msg.gsub(/^ {6}/, "")))
336 msg = <<-_end_of_message_
337 POST /path?foo=x;foo=y;foo=z;bar=1 HTTP/1.1
338 Host: test.ruby-lang.org:8080
339 Transfer-Encoding: foobar
343 assert_raise(WEBrick::HTTPStatus::NotImplemented){
344 req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
345 req.parse(StringIO.new(msg.gsub(/^ {6}/, "")))