-
Notifications
You must be signed in to change notification settings - Fork 299
Expand file tree
/
Copy pathtest_http-access2.rb
More file actions
525 lines (465 loc) · 15 KB
/
test_http-access2.rb
File metadata and controls
525 lines (465 loc) · 15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
# -*- encoding: utf-8 -*-
require 'http-access2'
require File.expand_path('helper', File.dirname(__FILE__))
require 'tempfile'
module HTTPAccess2
class TestClient < Test::Unit::TestCase
include Helper
include HTTPClient::Util
def setup
super
setup_server
setup_client
end
def teardown
super
end
def test_initialize
setup_proxyserver
escape_noproxy do
@proxyio.truncate(0)
@client = HTTPAccess2::Client.new(proxyurl)
assert_equal(urify(proxyurl), @client.proxy)
assert_equal(200, @client.head(serverurl).status)
assert(!@proxyio.string.empty?)
end
end
def test_agent_name
@client = HTTPAccess2::Client.new(nil, "agent_name_foo")
str = "".dup
@client.debug_dev = str
@client.get(serverurl)
lines = str.split(/(?:\r?\n)+/)
assert_equal("= Request", lines[0])
assert_match(/^User-Agent: agent_name_foo/, lines[4])
end
def test_from
@client = HTTPAccess2::Client.new(nil, nil, "from_bar")
str = "".dup
@client.debug_dev = str
@client.get(serverurl)
lines = str.split(/(?:\r?\n)+/)
assert_equal("= Request", lines[0])
assert_match(/^From: from_bar/, lines[5])
end
def test_debug_dev
str = "".dup
@client.debug_dev = str
assert(str.empty?)
@client.get(serverurl)
assert(!str.empty?)
end
def test_debug_dev_shared_dev
str = "".dup
@client.debug_dev = str
@client.get(serverurl)
str << "another stuff\n"
lines = str.split(/(?:\r?\n)+/)
assert_equal('another stuff', lines[-1])
end
def _test_protocol_version_http09
@client.protocol_version = 'HTTP/0.9'
str = "".dup
@client.debug_dev = str
@client.get(serverurl + 'hello')
lines = str.split(/(?:\r?\n)+/)
assert_equal("= Request", lines[0])
assert_equal("! CONNECTION ESTABLISHED", lines[2])
assert_equal("GET /hello HTTP/0.9", lines[3])
assert_equal("Connection: close", lines[5])
assert_equal("= Response", lines[6])
assert_match(/^hello/, lines[7])
end
def test_protocol_version_http10
@client.protocol_version = 'HTTP/1.0'
str = "".dup
@client.debug_dev = str
@client.get(serverurl + 'hello')
lines = str.split(/(?:\r?\n)+/)
assert_equal("= Request", lines[0])
assert_equal("! CONNECTION ESTABLISHED", lines[2])
assert_equal("GET /hello HTTP/1.0", lines[3])
assert_equal("Connection: close", lines[7])
assert_equal("= Response", lines[8])
end
def test_protocol_version_http11
str = "".dup
@client.debug_dev = str
@client.get(serverurl)
lines = str.split(/(?:\r?\n)+/)
assert_equal("= Request", lines[0])
assert_equal("! CONNECTION ESTABLISHED", lines[2])
assert_equal("GET / HTTP/1.1", lines[3])
assert_equal("Host: localhost:#{serverport}", lines[7])
@client.protocol_version = 'HTTP/1.1'
str = "".dup
@client.debug_dev = str
@client.get(serverurl)
lines = str.split(/(?:\r?\n)+/)
assert_equal("= Request", lines[0])
assert_equal("! CONNECTION ESTABLISHED", lines[2])
assert_equal("GET / HTTP/1.1", lines[3])
@client.protocol_version = 'HTTP/1.0'
str = "".dup
@client.debug_dev = str
@client.get(serverurl)
lines = str.split(/(?:\r?\n)+/)
assert_equal("= Request", lines[0])
assert_equal("! CONNECTION ESTABLISHED", lines[2])
assert_equal("GET / HTTP/1.0", lines[3])
end
def test_proxy
setup_proxyserver
escape_noproxy do
begin
@client.proxy = "http://あ"
rescue => e
assert_match(/InvalidURIError/, e.class.to_s)
end
@client.proxy = ""
assert_nil(@client.proxy)
@client.proxy = "http://foo:1234"
assert_equal(urify("http://foo:1234"), @client.proxy)
uri = urify("http://bar:2345")
@client.proxy = uri
assert_equal(uri, @client.proxy)
#
@proxyio.truncate(0)
@client.proxy = nil
assert_equal(200, @client.head(serverurl).status)
assert(@proxyio.string.empty?)
#
@proxyio.truncate(0)
@client.proxy = proxyurl
assert_equal(200, @client.head(serverurl).status)
assert(!@proxyio.string.empty?)
end
end
def test_noproxy_for_localhost
@proxyio.truncate(0)
@client.proxy = proxyurl
assert_equal(200, @client.head(serverurl).status)
assert(@proxyio.string.empty?)
end
def test_no_proxy
setup_proxyserver
escape_noproxy do
# proxy is not set.
@client.no_proxy = 'localhost'
@proxyio.truncate(0)
@client.proxy = nil
assert_equal(200, @client.head(serverurl).status)
assert(/accept/ !~ @proxyio.string)
#
@proxyio.truncate(0)
@client.proxy = proxyurl
assert_equal(200, @client.head(serverurl).status)
assert(/accept/ !~ @proxyio.string)
#
@client.no_proxy = 'foobar'
@proxyio.truncate(0)
@client.proxy = proxyurl
assert_equal(200, @client.head(serverurl).status)
assert(/accept/ =~ @proxyio.string)
#
@client.no_proxy = 'foobar,localhost:baz'
@proxyio.truncate(0)
@client.proxy = proxyurl
assert_equal(200, @client.head(serverurl).status)
assert(/accept/ !~ @proxyio.string)
#
@client.no_proxy = 'foobar,localhost:443'
@proxyio.truncate(0)
@client.proxy = proxyurl
assert_equal(200, @client.head(serverurl).status)
assert(/accept/ =~ @proxyio.string)
#
@client.no_proxy = "foobar,localhost:443:localhost:#{serverport},baz"
@proxyio.truncate(0)
@client.proxy = proxyurl
assert_equal(200, @client.head(serverurl).status)
assert(/accept/ !~ @proxyio.string)
end
end
def test_get_content
assert_equal('hello', @client.get_content(serverurl + 'hello'))
assert_equal('hello', @client.get_content(serverurl + 'redirect1'))
assert_equal('hello', @client.get_content(serverurl + 'redirect2'))
assert_raises(HTTPClient::Session::BadResponse) do
@client.get_content(serverurl + 'notfound')
end
assert_raises(HTTPClient::Session::BadResponse) do
@client.get_content(serverurl + 'redirect_self')
end
called = false
@client.redirect_uri_callback = lambda { |uri, res|
newuri = res.header['location'][0]
called = true
newuri
}
assert_equal('hello', @client.get_content(serverurl + 'relative_redirect'))
assert(called)
end
def test_post_content
assert_equal('hello', @client.post_content(serverurl + 'hello'))
assert_equal('hello', @client.post_content(serverurl + 'redirect1'))
assert_equal('hello', @client.post_content(serverurl + 'redirect2'))
assert_raises(HTTPClient::Session::BadResponse) do
@client.post_content(serverurl + 'notfound')
end
assert_raises(HTTPClient::Session::BadResponse) do
@client.post_content(serverurl + 'redirect_self')
end
called = false
@client.redirect_uri_callback = lambda { |uri, res|
newuri = res.header['location'][0]
called = true
newuri
}
assert_equal('hello', @client.post_content(serverurl + 'relative_redirect'))
assert(called)
end
def test_head
assert_equal("head", @client.head(serverurl + 'servlet').header["x-head"][0])
param = {'1'=>'2', '3'=>'4'}
res = @client.head(serverurl + 'servlet', param)
assert_equal(param, params(res.header["x-query"][0]))
end
def test_get
assert_equal("get", @client.get(serverurl + 'servlet').content)
param = {'1'=>'2', '3'=>'4'}
res = @client.get(serverurl + 'servlet', param)
assert_equal(param, params(res.header["x-query"][0]))
end
def test_post
assert_equal("post", @client.post(serverurl + 'servlet', '').content)
param = {'1'=>'2', '3'=>'4'}
res = @client.get(serverurl + 'servlet', param)
assert_equal(param, params(res.header["x-query"][0]))
end
def test_put
assert_equal("put", @client.put(serverurl + 'servlet').content)
param = {'1'=>'2', '3'=>'4'}
res = @client.get(serverurl + 'servlet', param)
assert_equal(param, params(res.header["x-query"][0]))
end
def test_delete
assert_equal("delete", @client.delete(serverurl + 'servlet').content)
param = {'1'=>'2', '3'=>'4'}
res = @client.get(serverurl + 'servlet', param)
assert_equal(param, params(res.header["x-query"][0]))
end
def test_options
assert_equal("options", @client.options(serverurl + 'servlet').content)
param = {'1'=>'2', '3'=>'4'}
res = @client.get(serverurl + 'servlet', param)
assert_equal(param, params(res.header["x-query"][0]))
end
def test_trace
assert_equal("trace", @client.trace(serverurl + 'servlet').content)
param = {'1'=>'2', '3'=>'4'}
res = @client.get(serverurl + 'servlet', param)
assert_equal(param, params(res.header["x-query"][0]))
end
def test_get_query
assert_equal({'1'=>'2'}, check_query_get({1=>2}))
assert_equal({'a'=>'A', 'B'=>'b'}, check_query_get({"a"=>"A", "B"=>"b"}))
assert_equal({'&'=>'&'}, check_query_get({"&"=>"&"}))
assert_equal({'= '=>' =+'}, check_query_get({"= "=>" =+"}))
assert_equal(
['=', '&'].sort,
check_query_get([["=", "="], ["=", "&"]])['='].to_ary.sort
)
assert_equal({'123'=>'45'}, check_query_get('123=45'))
assert_equal({'12 3'=>'45', ' '=>' '}, check_query_get('12+3=45&+=+'))
assert_equal({}, check_query_get(''))
end
def test_post_body
assert_equal({'1'=>'2'}, check_query_post({1=>2}))
assert_equal({'a'=>'A', 'B'=>'b'}, check_query_post({"a"=>"A", "B"=>"b"}))
assert_equal({'&'=>'&'}, check_query_post({"&"=>"&"}))
assert_equal({'= '=>' =+'}, check_query_post({"= "=>" =+"}))
assert_equal(
['=', '&'].sort,
check_query_post([["=", "="], ["=", "&"]])['='].to_ary.sort
)
assert_equal({'123'=>'45'}, check_query_post('123=45'))
assert_equal({'12 3'=>'45', ' '=>' '}, check_query_post('12+3=45&+=+'))
assert_equal({}, check_query_post(''))
#
post_body = StringIO.new("foo=bar&foo=baz")
assert_equal(
["bar", "baz"],
check_query_post(post_body)["foo"].to_ary.sort
)
end
def test_extra_headers
str = "".dup
@client.debug_dev = str
@client.head(serverurl, nil, {"ABC" => "DEF"})
lines = str.split(/(?:\r?\n)+/)
assert_equal("= Request", lines[0])
assert_match("ABC: DEF", lines[4])
#
str = "".dup
@client.debug_dev = str
@client.get(serverurl, nil, [["ABC", "DEF"], ["ABC", "DEF"]])
lines = str.split(/(?:\r?\n)+/)
assert_equal("= Request", lines[0])
assert_match("ABC: DEF", lines[4])
assert_match("ABC: DEF", lines[5])
end
def test_timeout
client = HTTPClient.new
assert_equal(60, client.connect_timeout)
assert_equal(120, client.send_timeout)
assert_equal(60, client.receive_timeout)
#
client.connect_timeout = 1
client.send_timeout = 2
client.receive_timeout = 3
assert_equal(1, client.connect_timeout)
assert_equal(2, client.send_timeout)
assert_equal(3, client.receive_timeout)
end
def test_connect_timeout
# ToDo
end
def test_send_timeout
# ToDo
end
def test_receive_timeout
# this test takes 2 sec
assert_equal('hello', @client.get_content(serverurl + 'sleep?sec=2'))
@client.reset_all
@client.receive_timeout = 1
assert_equal('hello', @client.get_content(serverurl + 'sleep?sec=0'))
assert_raise(HTTPClient::ReceiveTimeoutError) do
@client.get_content(serverurl + 'sleep?sec=2')
end
@client.reset_all
@client.receive_timeout = 3
assert_equal('hello', @client.get_content(serverurl + 'sleep?sec=2'))
end
def test_cookies
cookiefile = Tempfile.new('test_cookies_file')
# from [ruby-talk:164079]
File.open(cookiefile.path, "wb") do |f|
f << "http://rubyforge.org//account/login.php session_ser LjEwMy45Ni40Ni0q%2A-fa0537de8cc31 2131676286 .rubyforge.org / 13\n"
end
cm = WebAgent::CookieManager::new(cookiefile.path)
cm.load_cookies
assert_equal(1, cm.cookies.size)
end
private
def check_query_get(query)
WEBrick::HTTPUtils.parse_query(
@client.get(serverurl + 'servlet', query).header["x-query"][0]
)
end
def check_query_post(query)
WEBrick::HTTPUtils.parse_query(
@client.post(serverurl + 'servlet', query).header["x-query"][0]
)
end
def setup_server
@server = WEBrick::HTTPServer.new(
:BindAddress => "localhost",
:Logger => @logger,
:Port => 0,
:AccessLog => [],
:DocumentRoot => File.dirname(File.expand_path(__FILE__))
)
@serverport = @server.config[:Port]
[:hello, :sleep, :redirect1, :redirect2, :redirect3, :redirect_self, :relative_redirect].each do |sym|
@server.mount(
"/#{sym}",
WEBrick::HTTPServlet::ProcHandler.new(method("do_#{sym}").to_proc)
)
end
@server.mount('/servlet', TestServlet.new(@server))
@server_thread = start_server_thread(@server)
end
def escape_noproxy
backup = HTTPAccess2::Client::NO_PROXY_HOSTS.dup
HTTPAccess2::Client::NO_PROXY_HOSTS.clear
yield
ensure
HTTPAccess2::Client::NO_PROXY_HOSTS.replace(backup)
end
def do_hello(req, res)
res['content-type'] = 'text/html'
res.body = "hello"
end
def do_sleep(req, res)
sec = req.query['sec'].to_i
sleep sec
res['content-type'] = 'text/html'
res.body = "hello"
end
def do_redirect1(req, res)
res.set_redirect(WEBrick::HTTPStatus::MovedPermanently, serverurl + "hello")
end
def do_redirect2(req, res)
res.set_redirect(WEBrick::HTTPStatus::TemporaryRedirect, serverurl + "redirect3")
end
def do_redirect3(req, res)
res.set_redirect(WEBrick::HTTPStatus::Found, serverurl + "hello")
end
def do_redirect_self(req, res)
res.set_redirect(WEBrick::HTTPStatus::Found, serverurl + "redirect_self")
end
def do_relative_redirect(req, res)
res.set_redirect(WEBrick::HTTPStatus::Found, "hello")
end
class TestServlet < WEBrick::HTTPServlet::AbstractServlet
def get_instance(*arg)
self
end
def do_HEAD(req, res)
res["x-head"] = 'head' # use this for test purpose only.
res["x-query"] = query_response(req)
end
def do_GET(req, res)
res.body = 'get'
res["x-query"] = query_response(req)
end
def do_POST(req, res)
res.body = 'post'
res["x-query"] = body_response(req)
end
def do_PUT(req, res)
res.body = 'put'
end
def do_DELETE(req, res)
res.body = 'delete'
end
def do_OPTIONS(req, res)
# check RFC for legal response.
res.body = 'options'
end
def do_TRACE(req, res)
# client SHOULD reflect the message received back to the client as the
# entity-body of a 200 (OK) response. [RFC2616]
res.body = 'trace'
res["x-query"] = query_response(req)
end
private
def query_response(req)
query_escape(WEBrick::HTTPUtils.parse_query(req.query_string))
end
def body_response(req)
query_escape(WEBrick::HTTPUtils.parse_query(req.body))
end
def query_escape(query)
escaped = []
query.collect do |k, v|
v.to_ary.each do |ve|
escaped << CGI.escape(k) + '=' + CGI.escape(ve)
end
end
escaped.join('&')
end
end
end
end