OSDN Git Service

ruby-1.9.1-rc1
[splhack/AndroidRuby.git] / lib / ruby-1.9.1-rc1 / test / webrick / test_httprequest.rb
1 require "webrick"
2 require "stringio"
3 require "test/unit"
4
5 class TestWEBrickHTTPRequest < Test::Unit::TestCase
6   def test_parse_09
7     msg = <<-_end_of_message_
8       GET /
9       foobar    # HTTP/0.9 request don't have header nor entity body.
10     _end_of_message_
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?)
21   end
22
23   def test_parse_10
24     msg = <<-_end_of_message_
25       GET / HTTP/1.0
26
27     _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?)
38   end
39
40   def test_parse_11
41     msg = <<-_end_of_message_
42       GET /path HTTP/1.1
43
44     _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?)
57   end
58
59   def test_request_uri_too_large
60     msg = <<-_end_of_message_
61       GET /#{"a"*1024} HTTP/1.1
62     _end_of_message_
63     req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
64     assert_raise(WEBrick::HTTPStatus::RequestURITooLarge){
65       req.parse(StringIO.new(msg.gsub(/^ {6}/, "")))
66     }
67   end
68
69   def test_parse_headers
70     msg = <<-_end_of_message_
71       GET /path HTTP/1.1
72       Host: test.ruby-lang.org:8080
73       Connection: close
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
79       Accept-Language: ja
80       Content-Type: text/plain
81       Content-Length: 7
82       X-Empty-Header: 
83
84       foobar
85     _end_of_message_
86     req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
87     req.parse(StringIO.new(msg.gsub(/^ {6}/, "")))
88     assert_equal(
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?)
93     assert_equal(
94       %w(text/html;level=1 text/html */* text/html;level=2 text/*),
95       req.accept)
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?)
104   end
105
106   def test_parse_header2()
107     msg = <<-_end_of_message_
108       POST /foo/bar/../baz?q=a HTTP/1.0
109       Content-Length: 9
110       User-Agent:
111         FOO   BAR
112         BAZ
113
114       hogehoge
115     _end_of_message_
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)
125   end
126
127   def test_parse_headers3
128     msg = <<-_end_of_message_
129       GET /path HTTP/1.1
130       Host: test.ruby-lang.org
131
132     _end_of_message_
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)
138
139     msg = <<-_end_of_message_
140       GET /path HTTP/1.1
141       Host: 192.168.1.1
142
143     _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)
149
150     msg = <<-_end_of_message_
151       GET /path HTTP/1.1
152       Host: [fe80::208:dff:feef:98c7]
153
154     _end_of_message_
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"),
158                  req.request_uri)
159     assert_equal("[fe80::208:dff:feef:98c7]", req.host)
160     assert_equal(80, req.port)
161
162     msg = <<-_end_of_message_
163       GET /path HTTP/1.1
164       Host: 192.168.1.1:8080
165
166     _end_of_message_
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)
172
173     msg = <<-_end_of_message_
174       GET /path HTTP/1.1
175       Host: [fe80::208:dff:feef:98c7]:8080
176
177     _end_of_message_
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"),
181                  req.request_uri)
182     assert_equal("[fe80::208:dff:feef:98c7]", req.host)
183     assert_equal(8080, req.port)
184   end
185
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
191
192     _end_of_message_
193     req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
194     req.parse(StringIO.new(msg.gsub(/^ {6}/, "")))
195     query = req.query
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)
201   end
202
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
210
211       #{param}
212     _end_of_message_
213     req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
214     req.parse(StringIO.new(msg.gsub(/^ {6}/, "")))
215     query = req.query
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)
221   end
222
223   def test_chunked
224     crlf = "\x0d\x0a"
225     msg = <<-_end_of_message_
226       POST /path HTTP/1.1
227       Host: test.ruby-lang.org:8080
228       Transfer-Encoding: chunked
229
230     _end_of_message_
231     msg.gsub!(/^ {6}/, "")
232     open(__FILE__){|io|
233       while chunk = io.read(100)
234         msg << chunk.size.to_s(16) << crlf
235         msg << chunk << crlf
236       end
237     }
238     msg << "0" << crlf
239     req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
240     req.parse(StringIO.new(msg))
241     assert_equal(File.read(__FILE__), req.body)
242   end
243
244   def test_forwarded
245     msg = <<-_end_of_message_
246       GET /foo HTTP/1.1
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
253
254     _end_of_message_
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)
263     assert(!req.ssl?)
264
265     msg = <<-_end_of_message_
266       GET /foo HTTP/1.1
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
273
274     _end_of_message_
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)
283     assert(!req.ssl?)
284
285     msg = <<-_end_of_message_
286       GET /foo HTTP/1.1
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
295
296     _end_of_message_
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)
305     assert(req.ssl?)
306   end
307
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
314
315       #{param}
316     _end_of_message_
317     assert_raise(WEBrick::HTTPStatus::LengthRequired){
318       req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
319       req.parse(StringIO.new(msg.gsub(/^ {6}/, "")))
320       req.body
321     }
322
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
327
328       body is too short.
329     _end_of_message_
330     assert_raise(WEBrick::HTTPStatus::BadRequest){
331       req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
332       req.parse(StringIO.new(msg.gsub(/^ {6}/, "")))
333       req.body
334     }
335
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
340
341       body is too short.
342     _end_of_message_
343     assert_raise(WEBrick::HTTPStatus::NotImplemented){
344       req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
345       req.parse(StringIO.new(msg.gsub(/^ {6}/, "")))
346       req.body
347     }
348   end
349 end