httpclient 2.5.3.3 → 2.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/bin/httpclient +11 -5
- data/bin/jsonclient +32 -78
- data/lib/httpclient.rb +49 -17
- data/lib/httpclient/auth.rb +60 -87
- data/lib/httpclient/cookie.rb +160 -388
- data/lib/httpclient/http.rb +40 -11
- data/lib/httpclient/session.rb +6 -22
- data/lib/httpclient/version.rb +1 -1
- data/lib/httpclient/webagent-cookie.rb +459 -0
- data/lib/jsonclient.rb +63 -0
- data/test/test_auth.rb +51 -6
- data/test/test_cookie.rb +128 -231
- data/test/test_http-access2.rb +6 -8
- data/test/test_httpclient.rb +96 -33
- data/test/test_jsonclient.rb +80 -0
- data/test/test_webagent-cookie.rb +465 -0
- metadata +6 -2
data/test/test_http-access2.rb
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
# -*- encoding: utf-8 -*-
|
2
2
|
require 'http-access2'
|
3
3
|
require File.expand_path('helper', File.dirname(__FILE__))
|
4
|
+
require 'tempfile'
|
4
5
|
|
5
6
|
|
6
7
|
module HTTPAccess2
|
@@ -356,17 +357,14 @@ class TestClient < Test::Unit::TestCase
|
|
356
357
|
end
|
357
358
|
|
358
359
|
def test_cookies
|
359
|
-
cookiefile =
|
360
|
-
'test_cookies_file')
|
360
|
+
cookiefile = Tempfile.new('test_cookies_file')
|
361
361
|
# from [ruby-talk:164079]
|
362
|
-
File.open(cookiefile, "wb") do |f|
|
363
|
-
f << "http://rubyforge.org//account/login.php session_ser LjEwMy45Ni40Ni0q%2A-fa0537de8cc31
|
362
|
+
File.open(cookiefile.path, "wb") do |f|
|
363
|
+
f << "http://rubyforge.org//account/login.php session_ser LjEwMy45Ni40Ni0q%2A-fa0537de8cc31 2131676286 .rubyforge.org / 13\n"
|
364
364
|
end
|
365
|
-
cm = WebAgent::CookieManager::new(cookiefile)
|
365
|
+
cm = WebAgent::CookieManager::new(cookiefile.path)
|
366
366
|
cm.load_cookies
|
367
|
-
|
368
|
-
url = cookie.url
|
369
|
-
assert(cookie.domain_match(url.host, cookie.domain))
|
367
|
+
assert_equal(1, cm.cookies.size)
|
370
368
|
end
|
371
369
|
|
372
370
|
private
|
data/test/test_httpclient.rb
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
# -*- encoding: utf-8 -*-
|
2
2
|
require File.expand_path('helper', File.dirname(__FILE__))
|
3
|
+
require 'tempfile'
|
3
4
|
|
4
5
|
|
5
6
|
class TestHTTPClient < Test::Unit::TestCase
|
@@ -710,7 +711,17 @@ EOS
|
|
710
711
|
def test_get_follow_redirect
|
711
712
|
assert_equal('hello', @client.get(serverurl + 'hello', :follow_redirect => true).body)
|
712
713
|
assert_equal('hello', @client.get(serverurl + 'redirect1', :follow_redirect => true).body)
|
713
|
-
|
714
|
+
|
715
|
+
res = @client.get(serverurl + 'redirect2', :follow_redirect => true)
|
716
|
+
assert_equal('hello', res.body)
|
717
|
+
assert_equal("http://localhost:#{@serverport}/hello", res.header.request_uri.to_s)
|
718
|
+
assert_equal("http://localhost:#{@serverport}/redirect3", res.previous.header.request_uri.to_s)
|
719
|
+
assert_equal("http://localhost:#{@serverport}/redirect2", res.previous.previous.header.request_uri.to_s)
|
720
|
+
assert_equal(nil, res.previous.previous.previous)
|
721
|
+
end
|
722
|
+
|
723
|
+
def test_get_follow_redirect_with_query
|
724
|
+
assert_equal('hello?1=2&3=4', @client.get(serverurl + 'redirect1', :query => {1 => 2, 3 => 4}, :follow_redirect => true).body)
|
714
725
|
end
|
715
726
|
|
716
727
|
def test_get_async
|
@@ -908,7 +919,8 @@ EOS
|
|
908
919
|
assert_match(/\r\n2\r\n/m, res.content)
|
909
920
|
assert_match(/\r\nContent-Disposition: form-data; name="3"; filename=""\r\n/m, res.content)
|
910
921
|
assert_match(/\r\nContent-Length:/m, str.string)
|
911
|
-
|
922
|
+
# HTTPClient reads from head to 'size'; CHUNK_SIZE bytes then 1 byte, that's all.
|
923
|
+
assert_equal(2, myio.called)
|
912
924
|
end
|
913
925
|
|
914
926
|
def test_post_with_io_nosize # streaming + chunked post
|
@@ -925,6 +937,42 @@ EOS
|
|
925
937
|
assert_match(/\r\nTransfer-Encoding: chunked\r\n/m, str.string)
|
926
938
|
end
|
927
939
|
|
940
|
+
def test_post_with_sized_io
|
941
|
+
myio = StringIO.new("45")
|
942
|
+
def myio.size
|
943
|
+
1
|
944
|
+
end
|
945
|
+
res = @client.post(serverurl + 'servlet', myio)
|
946
|
+
assert_equal('post,4', res.content)
|
947
|
+
end
|
948
|
+
|
949
|
+
def test_post_with_sized_io_part
|
950
|
+
myio = StringIO.new("45")
|
951
|
+
def myio.size
|
952
|
+
1
|
953
|
+
end
|
954
|
+
@client.debug_dev = str = StringIO.new
|
955
|
+
res = @client.post(serverurl + 'servlet', { :file => myio })
|
956
|
+
assert_match(/\r\n4\r\n/, str.string, 'should send "4" not "45"')
|
957
|
+
end
|
958
|
+
|
959
|
+
def test_post_with_unknown_sized_io_part
|
960
|
+
myio1 = StringIO.new("123")
|
961
|
+
myio2 = StringIO.new("45")
|
962
|
+
class << myio1
|
963
|
+
undef :size
|
964
|
+
end
|
965
|
+
class << myio2
|
966
|
+
# This does not work because other file is 'unknown sized'
|
967
|
+
def size
|
968
|
+
1
|
969
|
+
end
|
970
|
+
end
|
971
|
+
@client.debug_dev = str = StringIO.new
|
972
|
+
res = @client.post(serverurl + 'servlet', { :file1 => myio1, :file2 => myio2 })
|
973
|
+
assert_match(/\r\n45\r\n/, str.string)
|
974
|
+
end
|
975
|
+
|
928
976
|
def test_post_async
|
929
977
|
param = {'1'=>'2', '3'=>'4'}
|
930
978
|
conn = @client.post_async(serverurl + 'servlet', param)
|
@@ -1220,14 +1268,14 @@ EOS
|
|
1220
1268
|
|
1221
1269
|
def test_receive_timeout
|
1222
1270
|
# this test takes 2 sec
|
1223
|
-
assert_equal('hello', @client.get_content(serverurl + 'sleep?sec=2'))
|
1271
|
+
assert_equal('hello?sec=2', @client.get_content(serverurl + 'sleep?sec=2'))
|
1224
1272
|
@client.receive_timeout = 1
|
1225
|
-
assert_equal('hello', @client.get_content(serverurl + 'sleep?sec=0'))
|
1273
|
+
assert_equal('hello?sec=0', @client.get_content(serverurl + 'sleep?sec=0'))
|
1226
1274
|
assert_raise(HTTPClient::ReceiveTimeoutError) do
|
1227
1275
|
@client.get_content(serverurl + 'sleep?sec=2')
|
1228
1276
|
end
|
1229
1277
|
@client.receive_timeout = 3
|
1230
|
-
assert_equal('hello', @client.get_content(serverurl + 'sleep?sec=2'))
|
1278
|
+
assert_equal('hello?sec=2', @client.get_content(serverurl + 'sleep?sec=2'))
|
1231
1279
|
end
|
1232
1280
|
|
1233
1281
|
def test_receive_timeout_post
|
@@ -1262,22 +1310,18 @@ EOS
|
|
1262
1310
|
end
|
1263
1311
|
|
1264
1312
|
def test_cookies
|
1265
|
-
cookiefile =
|
1266
|
-
File.open(cookiefile, "wb") do |f|
|
1267
|
-
f << "http://rubyforge.org/account/login.php\tsession_ser\tLjEwMy45Ni40Ni0q%2A-fa0537de8cc31\t2000000000\
|
1268
|
-
end
|
1269
|
-
@client.set_cookie_store(cookiefile)
|
1270
|
-
cookie = @client.cookie_manager.cookies.first
|
1271
|
-
url = cookie.url
|
1272
|
-
assert(cookie.domain_match(url.host, cookie.domain))
|
1313
|
+
cookiefile = Tempfile.new('test_cookies_file')
|
1314
|
+
File.open(cookiefile.path, "wb") do |f|
|
1315
|
+
f << "http://rubyforge.org/account/login.php\tsession_ser\tLjEwMy45Ni40Ni0q%2A-fa0537de8cc31\t2000000000\trubyforge.org\t/account/\t9\n"
|
1316
|
+
end
|
1317
|
+
@client.set_cookie_store(cookiefile.path)
|
1273
1318
|
#
|
1274
1319
|
@client.reset_all
|
1275
|
-
@client.test_loopback_http_response << "HTTP/1.0 200 OK\nSet-Cookie:
|
1320
|
+
@client.test_loopback_http_response << "HTTP/1.0 200 OK\nSet-Cookie: session_ser=bar; expires=#{Time.at(1924873200).gmtime.httpdate}\n\nOK"
|
1276
1321
|
@client.get_content('http://rubyforge.org/account/login.php')
|
1277
1322
|
@client.save_cookie_store
|
1278
|
-
str = File.read(cookiefile)
|
1279
|
-
assert_match(%r(http://rubyforge.org/account/login.php\
|
1280
|
-
File.unlink(cookiefile)
|
1323
|
+
str = File.read(cookiefile.path)
|
1324
|
+
assert_match(%r(http://rubyforge.org/account/login.php\tsession_ser\tbar\t1924873200\trubyforge.org\t/account/\t9), str)
|
1281
1325
|
end
|
1282
1326
|
|
1283
1327
|
def test_eof_error_length
|
@@ -1503,6 +1547,7 @@ EOS
|
|
1503
1547
|
res = HTTP::Message.new_response('response')
|
1504
1548
|
res.contenttype = 'text/plain'
|
1505
1549
|
res.header.body_date = Time.at(946652400)
|
1550
|
+
res.header.request_uri = 'http://www.example.com/'
|
1506
1551
|
assert_nil(res.cookies)
|
1507
1552
|
#
|
1508
1553
|
res.header['Set-Cookie'] = [
|
@@ -1579,8 +1624,11 @@ EOS
|
|
1579
1624
|
server = TCPServer.open('127.0.0.1', 0)
|
1580
1625
|
server.listen(30) # set enough backlogs
|
1581
1626
|
endpoint = "http://127.0.0.1:#{server.addr[1]}/"
|
1582
|
-
|
1627
|
+
queue = Queue.new
|
1628
|
+
Thread.new(queue) { |qs|
|
1583
1629
|
Thread.abort_on_exception = true
|
1630
|
+
# want 5 requests issued
|
1631
|
+
5.times { qs.pop }
|
1584
1632
|
# emulate 10 keep-alive connections
|
1585
1633
|
10.times do |idx|
|
1586
1634
|
sock = server.accept
|
@@ -1609,22 +1657,29 @@ EOS
|
|
1609
1657
|
sock.close
|
1610
1658
|
end
|
1611
1659
|
}
|
1612
|
-
# allocate 10 keep-alive connections
|
1660
|
+
# try to allocate 10 keep-alive connections; it's a race so some
|
1661
|
+
# threads can reuse the connection so actual number of keep-alive
|
1662
|
+
# connections should be smaller than 10.
|
1613
1663
|
(0...10).to_a.map {
|
1614
|
-
Thread.new {
|
1615
|
-
|
1664
|
+
Thread.new(queue) { |qc|
|
1665
|
+
Thread.abort_on_exception = true
|
1666
|
+
conn = client.get_async(endpoint)
|
1667
|
+
qc.push(true)
|
1668
|
+
assert_equal("12345", conn.pop.content.read)
|
1616
1669
|
}
|
1617
1670
|
}.each { |th| th.join }
|
1618
|
-
# send 5 requests,
|
1619
|
-
#
|
1671
|
+
# send 5 requests, some of these should get KeepAliveDesconnected
|
1672
|
+
# but should retry with new connection.
|
1620
1673
|
(0...5).to_a.map {
|
1621
1674
|
Thread.new {
|
1675
|
+
Thread.abort_on_exception = true
|
1622
1676
|
assert_equal("23456", client.get(endpoint).content)
|
1623
1677
|
}
|
1624
1678
|
}.each { |th| th.join }
|
1625
|
-
# rest requests won't get KeepAliveDisconnected
|
1679
|
+
# rest requests won't get KeepAliveDisconnected
|
1626
1680
|
(0...10).to_a.map {
|
1627
1681
|
Thread.new {
|
1682
|
+
Thread.abort_on_exception = true
|
1628
1683
|
assert_equal("34567", client.get(endpoint).content)
|
1629
1684
|
}
|
1630
1685
|
}.each { |th| th.join }
|
@@ -1793,45 +1848,53 @@ private
|
|
1793
1848
|
HTTPClient::NO_PROXY_HOSTS.replace(backup)
|
1794
1849
|
end
|
1795
1850
|
|
1851
|
+
def add_query_string(req)
|
1852
|
+
if req.query_string
|
1853
|
+
'?' + req.query_string
|
1854
|
+
else
|
1855
|
+
''
|
1856
|
+
end
|
1857
|
+
end
|
1858
|
+
|
1796
1859
|
def do_hello(req, res)
|
1797
1860
|
res['content-type'] = 'text/html'
|
1798
|
-
res.body = "hello"
|
1861
|
+
res.body = "hello" + add_query_string(req)
|
1799
1862
|
end
|
1800
1863
|
|
1801
1864
|
def do_sleep(req, res)
|
1802
1865
|
sec = req.query['sec'].to_i
|
1803
1866
|
sleep sec
|
1804
1867
|
res['content-type'] = 'text/html'
|
1805
|
-
res.body = "hello"
|
1868
|
+
res.body = "hello" + add_query_string(req)
|
1806
1869
|
end
|
1807
1870
|
|
1808
1871
|
def do_servlet_redirect(req, res)
|
1809
|
-
res.set_redirect(WEBrick::HTTPStatus::Found, serverurl + "servlet")
|
1872
|
+
res.set_redirect(WEBrick::HTTPStatus::Found, serverurl + "servlet" + add_query_string(req))
|
1810
1873
|
end
|
1811
1874
|
|
1812
1875
|
def do_redirect1(req, res)
|
1813
|
-
res.set_redirect(WEBrick::HTTPStatus::MovedPermanently, serverurl + "hello")
|
1876
|
+
res.set_redirect(WEBrick::HTTPStatus::MovedPermanently, serverurl + "hello" + add_query_string(req))
|
1814
1877
|
end
|
1815
1878
|
|
1816
1879
|
def do_redirect2(req, res)
|
1817
|
-
res.set_redirect(WEBrick::HTTPStatus::TemporaryRedirect, serverurl + "redirect3")
|
1880
|
+
res.set_redirect(WEBrick::HTTPStatus::TemporaryRedirect, serverurl + "redirect3" + add_query_string(req))
|
1818
1881
|
end
|
1819
1882
|
|
1820
1883
|
def do_redirect3(req, res)
|
1821
|
-
res.set_redirect(WEBrick::HTTPStatus::Found, serverurl + "hello")
|
1884
|
+
res.set_redirect(WEBrick::HTTPStatus::Found, serverurl + "hello" + add_query_string(req))
|
1822
1885
|
end
|
1823
1886
|
|
1824
1887
|
def do_redirect_self(req, res)
|
1825
|
-
res.set_redirect(WEBrick::HTTPStatus::Found, serverurl + "redirect_self")
|
1888
|
+
res.set_redirect(WEBrick::HTTPStatus::Found, serverurl + "redirect_self" + add_query_string(req))
|
1826
1889
|
end
|
1827
1890
|
|
1828
1891
|
def do_relative_redirect(req, res)
|
1829
|
-
res.set_redirect(WEBrick::HTTPStatus::Found, "hello")
|
1892
|
+
res.set_redirect(WEBrick::HTTPStatus::Found, "hello" + add_query_string(req))
|
1830
1893
|
end
|
1831
1894
|
|
1832
1895
|
def do_redirect_see_other(req, res)
|
1833
1896
|
if req.request_method == 'POST'
|
1834
|
-
res.set_redirect(WEBrick::HTTPStatus::SeeOther, serverurl + "redirect_see_other") # self
|
1897
|
+
res.set_redirect(WEBrick::HTTPStatus::SeeOther, serverurl + "redirect_see_other" + add_query_string(req)) # self
|
1835
1898
|
else
|
1836
1899
|
res.body = 'hello'
|
1837
1900
|
end
|
@@ -0,0 +1,80 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('helper', File.dirname(__FILE__))
|
3
|
+
require 'jsonclient'
|
4
|
+
|
5
|
+
|
6
|
+
class TestJSONClient < Test::Unit::TestCase
|
7
|
+
include Helper
|
8
|
+
|
9
|
+
def setup
|
10
|
+
super
|
11
|
+
setup_server
|
12
|
+
@client = JSONClient.new
|
13
|
+
end
|
14
|
+
|
15
|
+
def teardown
|
16
|
+
super
|
17
|
+
end
|
18
|
+
|
19
|
+
def test_post
|
20
|
+
res = @client.post(serverurl + 'json', {'a' => 1, 'b' => {'c' => 2}})
|
21
|
+
assert_equal(2, res.content['b']['c'])
|
22
|
+
assert_equal('application/json; charset=utf-8', res.content_type)
|
23
|
+
# #previous contains the original response
|
24
|
+
assert_equal(1, JSON.parse(res.previous.content)['a'])
|
25
|
+
end
|
26
|
+
|
27
|
+
def test_post_with_header
|
28
|
+
res = @client.post(serverurl + 'json', :header => {'X-foo' => 'bar'}, :body => {'a' => 1, 'b' => {'c' => 2}})
|
29
|
+
assert_equal(2, res.content['b']['c'])
|
30
|
+
assert_equal('application/json; charset=utf-8', res.content_type)
|
31
|
+
end
|
32
|
+
|
33
|
+
def test_post_with_array_header
|
34
|
+
res = @client.post(serverurl + 'json', :header => [['X-foo', 'bar']], :body => {'a' => 1, 'b' => {'c' => 2}})
|
35
|
+
assert_equal(2, res.content['b']['c'])
|
36
|
+
assert_equal('application/json; charset=utf-8', res.content_type)
|
37
|
+
end
|
38
|
+
|
39
|
+
def test_post_non_json_body
|
40
|
+
res = @client.post(serverurl + 'json', 'a=b&c=d')
|
41
|
+
assert_equal('a=b&c=d', res.content)
|
42
|
+
assert_equal('application/x-www-form-urlencoded', res.content_type)
|
43
|
+
end
|
44
|
+
|
45
|
+
def test_put
|
46
|
+
res = @client.put(serverurl + 'json', {'a' => 1, 'b' => {'c' => 2}})
|
47
|
+
assert_equal(2, res.content['b']['c'])
|
48
|
+
assert_equal('application/json; charset=utf-8', res.content_type)
|
49
|
+
end
|
50
|
+
|
51
|
+
def test_get_not_affected
|
52
|
+
res = @client.get(serverurl + 'json', {'a' => 1, 'b' => {'c' => 2}})
|
53
|
+
assert_equal('', res.content)
|
54
|
+
assert_equal('', res.content_type)
|
55
|
+
end
|
56
|
+
|
57
|
+
class JSONServlet < WEBrick::HTTPServlet::AbstractServlet
|
58
|
+
def get_instance(*arg)
|
59
|
+
self
|
60
|
+
end
|
61
|
+
|
62
|
+
def service(req, res)
|
63
|
+
res['content-type'] = req['content-type']
|
64
|
+
res.body = req.body
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
def setup_server
|
69
|
+
@server = WEBrick::HTTPServer.new(
|
70
|
+
:BindAddress => "localhost",
|
71
|
+
:Logger => @logger,
|
72
|
+
:Port => 0,
|
73
|
+
:AccessLog => [],
|
74
|
+
:DocumentRoot => File.dirname(File.expand_path(__FILE__))
|
75
|
+
)
|
76
|
+
@serverport = @server.config[:Port]
|
77
|
+
@server.mount('/json', JSONServlet.new(@server))
|
78
|
+
@server_thread = start_server_thread(@server)
|
79
|
+
end
|
80
|
+
end
|
@@ -0,0 +1,465 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require 'uri'
|
3
|
+
require 'tempfile'
|
4
|
+
|
5
|
+
# This testcase is located for reference, not for running.
|
6
|
+
if false
|
7
|
+
|
8
|
+
require 'httpclient/webagent-cookie'
|
9
|
+
|
10
|
+
class TestCookie < Test::Unit::TestCase
|
11
|
+
include HTTPClient::Util
|
12
|
+
|
13
|
+
def setup()
|
14
|
+
@c = WebAgent::Cookie.new()
|
15
|
+
end
|
16
|
+
|
17
|
+
def test_s_new()
|
18
|
+
assert_instance_of(WebAgent::Cookie, @c)
|
19
|
+
end
|
20
|
+
|
21
|
+
def test_discard?
|
22
|
+
assert_equal(false, !!(@c.discard?))
|
23
|
+
@c.discard = true
|
24
|
+
assert_equal(true, !!(@c.discard?))
|
25
|
+
end
|
26
|
+
|
27
|
+
def test_match()
|
28
|
+
url = urify('http://www.rubycolor.org/hoge/funi/#919191')
|
29
|
+
|
30
|
+
@c.domain = 'www.rubycolor.org'
|
31
|
+
assert_equal(true, @c.match?(url))
|
32
|
+
|
33
|
+
@c.domain = '.rubycolor.org'
|
34
|
+
assert_equal(true, @c.match?(url))
|
35
|
+
|
36
|
+
@c.domain = 'aaa.www.rubycolor.org'
|
37
|
+
assert_equal(false, @c.match?(url))
|
38
|
+
|
39
|
+
@c.domain = 'aaa.www.rubycolor.org'
|
40
|
+
assert_equal(false, @c.match?(url))
|
41
|
+
|
42
|
+
@c.domain = 'www.rubycolor.org'
|
43
|
+
@c.path = '/'
|
44
|
+
assert_equal(true, @c.match?(url))
|
45
|
+
|
46
|
+
@c.domain = 'www.rubycolor.org'
|
47
|
+
@c.path = '/hoge'
|
48
|
+
assert_equal(true, @c.match?(url))
|
49
|
+
|
50
|
+
@c.domain = 'www.rubycolor.org'
|
51
|
+
@c.path = '/hoge/hoge'
|
52
|
+
assert_equal(false, @c.match?(url))
|
53
|
+
|
54
|
+
@c.domain = 'www.rubycolor.org'
|
55
|
+
@c.path = '/hoge'
|
56
|
+
@c.secure = true
|
57
|
+
assert_equal(false, @c.match?(url))
|
58
|
+
|
59
|
+
url2 = urify('https://www.rubycolor.org/hoge/funi/#919191')
|
60
|
+
@c.domain = 'www.rubycolor.org'
|
61
|
+
@c.path = '/hoge'
|
62
|
+
@c.secure = true
|
63
|
+
assert_equal(true, @c.match?(url2))
|
64
|
+
|
65
|
+
@c.domain = 'www.rubycolor.org'
|
66
|
+
@c.path = '/hoge'
|
67
|
+
@c.secure = nil
|
68
|
+
assert_equal(true, @c.match?(url2)) ## not false!
|
69
|
+
|
70
|
+
url.port = 80
|
71
|
+
@c.domain = 'www.rubycolor.org'
|
72
|
+
@c.path = '/hoge'
|
73
|
+
# @c.port = [80,8080]
|
74
|
+
assert_equal(true, @c.match?(url))
|
75
|
+
|
76
|
+
url_nopath = URI.parse('http://www.rubycolor.org')
|
77
|
+
@c.domain = 'www.rubycolor.org'
|
78
|
+
@c.path = '/'
|
79
|
+
assert_equal(true, @c.match?(url_nopath))
|
80
|
+
|
81
|
+
end
|
82
|
+
|
83
|
+
def test_head_match?()
|
84
|
+
assert_equal(true, @c.head_match?("",""))
|
85
|
+
assert_equal(false, @c.head_match?("a",""))
|
86
|
+
assert_equal(true, @c.head_match?("","a"))
|
87
|
+
assert_equal(true, @c.head_match?("abcde","abcde"))
|
88
|
+
assert_equal(true, @c.head_match?("abcde","abcdef"))
|
89
|
+
assert_equal(false, @c.head_match?("abcdef","abcde"))
|
90
|
+
assert_equal(false, @c.head_match?("abcde","bcde"))
|
91
|
+
assert_equal(false, @c.head_match?("bcde","abcde"))
|
92
|
+
end
|
93
|
+
|
94
|
+
def test_tail_match?()
|
95
|
+
assert_equal(true, @c.tail_match?("",""))
|
96
|
+
assert_equal(false, @c.tail_match?("a",""))
|
97
|
+
assert_equal(true, @c.tail_match?("","a"))
|
98
|
+
assert_equal(true, @c.tail_match?("abcde","abcde"))
|
99
|
+
assert_equal(false, @c.tail_match?("abcde","abcdef"))
|
100
|
+
assert_equal(false, @c.tail_match?("abcdef","abcde"))
|
101
|
+
assert_equal(false, @c.tail_match?("abcde","bcde"))
|
102
|
+
assert_equal(true, @c.tail_match?("bcde","abcde"))
|
103
|
+
end
|
104
|
+
|
105
|
+
|
106
|
+
def test_domain_match()
|
107
|
+
extend WebAgent::CookieUtils
|
108
|
+
assert_equal(true, !!domain_match("hoge.co.jp","."))
|
109
|
+
# assert_equal(true, !!domain_match("locahost",".local"))
|
110
|
+
assert_equal(true, !!domain_match("192.168.10.1","192.168.10.1"))
|
111
|
+
assert_equal(false, !!domain_match("192.168.10.1","192.168.10.2"))
|
112
|
+
# assert_equal(false, !!domain_match("hoge.co.jp",".hoge.co.jp"))
|
113
|
+
# allows; host == rubyforge.org, domain == .rubyforge.org
|
114
|
+
assert_equal(true, !!domain_match("hoge.co.jp",".hoge.co.jp"))
|
115
|
+
assert_equal(true, !!domain_match("www.hoge.co.jp", "www.hoge.co.jp"))
|
116
|
+
assert_equal(false, !!domain_match("www.hoge.co.jp", "www2.hoge.co.jp"))
|
117
|
+
assert_equal(true, !!domain_match("www.hoge.co.jp", ".hoge.co.jp"))
|
118
|
+
assert_equal(true, !!domain_match("www.aa.hoge.co.jp", ".hoge.co.jp"))
|
119
|
+
assert_equal(false, !!domain_match("www.hoge.co.jp", "hoge.co.jp"))
|
120
|
+
end
|
121
|
+
|
122
|
+
def test_join_quotedstr()
|
123
|
+
arr1 = ['hoge=funi', 'hoge2=funi2']
|
124
|
+
assert_equal(arr1, @c.instance_eval{join_quotedstr(arr1,';')})
|
125
|
+
arr2 = ['hoge="fu', 'ni"', 'funi=funi']
|
126
|
+
assert_equal(['hoge="fu;ni"','funi=funi'],
|
127
|
+
@c.instance_eval{join_quotedstr(arr2,';')})
|
128
|
+
arr3 = ['hoge="funi";hoge2="fu','ni2";hoge3="hoge"', 'funi="funi"']
|
129
|
+
assert_equal(['hoge="funi";hoge2="fu,ni2";hoge3="hoge"', 'funi="funi"'],
|
130
|
+
@c.instance_eval{join_quotedstr(arr3,',')})
|
131
|
+
end
|
132
|
+
|
133
|
+
end
|
134
|
+
|
135
|
+
class TestCookieManager < Test::Unit::TestCase
|
136
|
+
include HTTPClient::Util
|
137
|
+
|
138
|
+
def setup()
|
139
|
+
@cm = WebAgent::CookieManager.new()
|
140
|
+
end
|
141
|
+
|
142
|
+
def teardown()
|
143
|
+
end
|
144
|
+
|
145
|
+
def test_parse()
|
146
|
+
str = "inkid=n92b0ADOgACIgUb9lsjHqAAAHu2a; expires=Wed, 01-Dec-2010 00:00:00 GMT; path=/"
|
147
|
+
@cm.parse(str, urify('http://www.test.jp'))
|
148
|
+
cookie = @cm.cookies[0]
|
149
|
+
assert_instance_of(WebAgent::Cookie, cookie)
|
150
|
+
assert_equal("inkid", cookie.name)
|
151
|
+
assert_equal("n92b0ADOgACIgUb9lsjHqAAAHu2a", cookie.value)
|
152
|
+
assert_equal(Time.gm(2010, 12, 1, 0,0,0), cookie.expires)
|
153
|
+
assert_equal("/", cookie.path)
|
154
|
+
end
|
155
|
+
|
156
|
+
def test_parse2()
|
157
|
+
str = "xmen=off,0,0,1; path=/; domain=.excite.co.jp; expires=Wednesday, 31-Dec-2037 12:00:00 GMT"
|
158
|
+
@cm.parse(str, urify('http://www.excite.co.jp'))
|
159
|
+
cookie = @cm.cookies[0]
|
160
|
+
assert_instance_of(WebAgent::Cookie, cookie)
|
161
|
+
assert_equal("xmen", cookie.name)
|
162
|
+
assert_equal("off,0,0,1", cookie.value)
|
163
|
+
assert_equal("/", cookie.path)
|
164
|
+
assert_equal(".excite.co.jp", cookie.domain)
|
165
|
+
assert_equal(Time.gm(2037,12,31,12,0,0), cookie.expires)
|
166
|
+
end
|
167
|
+
|
168
|
+
def test_parse3()
|
169
|
+
str = "xmen=off,0,0,1; path=/; domain=.excite.co.jp; expires=Wednesday, 31-Dec-2037 12:00:00 GMT;Secure;HTTPOnly"
|
170
|
+
@cm.parse(str, urify('http://www.excite.co.jp'))
|
171
|
+
cookie = @cm.cookies[0]
|
172
|
+
assert_instance_of(WebAgent::Cookie, cookie)
|
173
|
+
assert_equal("xmen", cookie.name)
|
174
|
+
assert_equal("off,0,0,1", cookie.value)
|
175
|
+
assert_equal("/", cookie.path)
|
176
|
+
assert_equal(".excite.co.jp", cookie.domain)
|
177
|
+
assert_equal(Time.gm(2037,12,31,12,0,0), cookie.expires)
|
178
|
+
assert_equal(true, cookie.secure?)
|
179
|
+
assert_equal(true, cookie.http_only?)
|
180
|
+
end
|
181
|
+
|
182
|
+
def test_parse_double_semicolon()
|
183
|
+
str = "xmen=off,0,0,1;; path=\"/;;\"; domain=.excite.co.jp; expires=Wednesday, 31-Dec-2037 12:00:00 GMT"
|
184
|
+
@cm.parse(str, urify('http://www.excite.co.jp'))
|
185
|
+
cookie = @cm.cookies[0]
|
186
|
+
assert_instance_of(WebAgent::Cookie, cookie)
|
187
|
+
assert_equal("xmen", cookie.name)
|
188
|
+
assert_equal("off,0,0,1", cookie.value)
|
189
|
+
assert_equal("/;;", cookie.path)
|
190
|
+
assert_equal(".excite.co.jp", cookie.domain)
|
191
|
+
assert_equal(Time.gm(2037,12,31,12,0,0), cookie.expires)
|
192
|
+
end
|
193
|
+
|
194
|
+
# def test_make_portlist()
|
195
|
+
# assert_equal([80,8080], @cm.instance_eval{make_portlist("80,8080")})
|
196
|
+
# assert_equal([80], @cm.instance_eval{make_portlist("80")})
|
197
|
+
# assert_equal([80,8080,10080], @cm.instance_eval{make_portlist(" 80, 8080, 10080 \n")})
|
198
|
+
# end
|
199
|
+
|
200
|
+
def test_check_expired_cookies()
|
201
|
+
c1 = WebAgent::Cookie.new()
|
202
|
+
c2 = c1.dup
|
203
|
+
c3 = c1.dup
|
204
|
+
c4 = c1.dup
|
205
|
+
c1.expires = Time.now - 100
|
206
|
+
c2.expires = Time.now + 100
|
207
|
+
c3.expires = Time.now - 10
|
208
|
+
c4.expires = nil
|
209
|
+
cookies = [c1,c2,c3,c4]
|
210
|
+
@cm.cookies = cookies
|
211
|
+
@cm.check_expired_cookies()
|
212
|
+
# expires == nil cookies (session cookie) exists.
|
213
|
+
assert_equal([c2,c4], @cm.cookies)
|
214
|
+
end
|
215
|
+
|
216
|
+
def test_parse_expires
|
217
|
+
str = "inkid=n92b0ADOgACIgUb9lsjHqAAAHu2a; expires=; path=/"
|
218
|
+
@cm.parse(str, urify('http://www.test.jp'))
|
219
|
+
cookie = @cm.cookies[0]
|
220
|
+
assert_equal("inkid", cookie.name)
|
221
|
+
assert_equal("n92b0ADOgACIgUb9lsjHqAAAHu2a", cookie.value)
|
222
|
+
assert_equal(nil, cookie.expires)
|
223
|
+
assert_equal("/", cookie.path)
|
224
|
+
#
|
225
|
+
str = "inkid=n92b0ADOgACIgUb9lsjHqAAAHu2a; path=/; expires="
|
226
|
+
@cm.parse(str, urify('http://www.test.jp'))
|
227
|
+
cookie = @cm.cookies[0]
|
228
|
+
assert_equal("inkid", cookie.name)
|
229
|
+
assert_equal("n92b0ADOgACIgUb9lsjHqAAAHu2a", cookie.value)
|
230
|
+
assert_equal(nil, cookie.expires)
|
231
|
+
assert_equal("/", cookie.path)
|
232
|
+
#
|
233
|
+
str = "inkid=n92b0ADOgACIgUb9lsjHqAAAHu2a; path=/; expires=\"\""
|
234
|
+
@cm.parse(str, urify('http://www.test.jp'))
|
235
|
+
cookie = @cm.cookies[0]
|
236
|
+
assert_equal("inkid", cookie.name)
|
237
|
+
assert_equal("n92b0ADOgACIgUb9lsjHqAAAHu2a", cookie.value)
|
238
|
+
assert_equal(nil, cookie.expires)
|
239
|
+
assert_equal("/", cookie.path)
|
240
|
+
end
|
241
|
+
|
242
|
+
def test_parse_after_expiration
|
243
|
+
str = "inkid=n92b0ADOgACIgUb9lsjHqAAAHu2a; expires=Wed, 01-Dec-2010 00:00:00 GMT; path=/"
|
244
|
+
@cm.parse(str, urify('http://www.test.jp'))
|
245
|
+
cookie = @cm.cookies[0]
|
246
|
+
assert_instance_of(WebAgent::Cookie, cookie)
|
247
|
+
assert_equal("inkid", cookie.name)
|
248
|
+
assert_equal("n92b0ADOgACIgUb9lsjHqAAAHu2a", cookie.value)
|
249
|
+
assert_equal(Time.gm(2010, 12, 1, 0,0,0), cookie.expires)
|
250
|
+
assert_equal("/", cookie.path)
|
251
|
+
|
252
|
+
time = Time.at(Time.now.to_i + 60).utc
|
253
|
+
expires = time.strftime("%a, %d-%b-%Y %H:%M:%S GMT")
|
254
|
+
str = "inkid=n92b0ADOgACIgUb9lsjHqAAAHu2a; expires=#{expires}; path=/"
|
255
|
+
@cm.parse(str, urify('http://www.test.jp'))
|
256
|
+
cookie = @cm.cookies[0]
|
257
|
+
assert_equal("inkid", cookie.name)
|
258
|
+
assert_equal("n92b0ADOgACIgUb9lsjHqAAAHu2a", cookie.value)
|
259
|
+
assert_equal(time, cookie.expires)
|
260
|
+
assert_equal("/", cookie.path)
|
261
|
+
end
|
262
|
+
|
263
|
+
def test_find_cookie()
|
264
|
+
str = "xmen=off,0,0,1; path=/; domain=.excite2.co.jp; expires=Wednesday, 31-Dec-2037 12:00:00 GMT"
|
265
|
+
@cm.parse(str, urify("http://www.excite2.co.jp/"))
|
266
|
+
|
267
|
+
str = "xmen=off,0,0,2; path=/; domain=.excite.co.jp; expires=Wednesday, 31-Dec-2037 12:00:00 GMT"
|
268
|
+
@cm.parse(str, urify("http://www.excite.co.jp/"))
|
269
|
+
|
270
|
+
@cm.cookies[0].use = true
|
271
|
+
@cm.cookies[1].use = true
|
272
|
+
|
273
|
+
url = urify('http://www.excite.co.jp/hoge/funi/')
|
274
|
+
cookie_str = @cm.find(url)
|
275
|
+
assert_equal("xmen=off,0,0,2", cookie_str)
|
276
|
+
end
|
277
|
+
|
278
|
+
def test_load_cookies()
|
279
|
+
begin
|
280
|
+
File.open("tmp_test.tmp","w") {|f|
|
281
|
+
f.write <<EOF
|
282
|
+
http://www.zdnet.co.jp/news/0106/08/e_gibson.html NGUserID d29b8f49-10875-992421294-1 2145801600 www.zdnet.co.jp / 9 0
|
283
|
+
http://www.zdnet.co.jp/news/0106/08/e_gibson.html PACK zd3-992421294-7436 1293839999 .zdnet.co.jp / 13 0
|
284
|
+
http://example.org/ key value 0 .example.org / 13 0
|
285
|
+
http://example.org/ key value .example.org / 13 0
|
286
|
+
EOF
|
287
|
+
}
|
288
|
+
|
289
|
+
@cm.cookies_file = 'tmp_test.tmp'
|
290
|
+
@cm.load_cookies()
|
291
|
+
c0, c1, c2, c3 = @cm.cookies
|
292
|
+
assert_equal('http://www.zdnet.co.jp/news/0106/08/e_gibson.html', c0.url.to_s)
|
293
|
+
assert_equal('NGUserID', c0.name)
|
294
|
+
assert_equal('d29b8f49-10875-992421294-1', c0.value)
|
295
|
+
assert_equal(Time.at(2145801600), c0.expires)
|
296
|
+
assert_equal('www.zdnet.co.jp', c0.domain)
|
297
|
+
assert_equal('/', c0.path)
|
298
|
+
assert_equal(9, c0.flag)
|
299
|
+
#
|
300
|
+
assert_equal('http://www.zdnet.co.jp/news/0106/08/e_gibson.html', c1.url.to_s)
|
301
|
+
assert_equal('PACK', c1.name)
|
302
|
+
assert_equal('zd3-992421294-7436', c1.value)
|
303
|
+
assert_equal(Time.at(1293839999), c1.expires)
|
304
|
+
assert_equal('.zdnet.co.jp', c1.domain)
|
305
|
+
assert_equal('/', c1.path)
|
306
|
+
assert_equal(13, c1.flag)
|
307
|
+
#
|
308
|
+
assert_equal(nil, c2.expires)
|
309
|
+
assert_equal(nil, c3.expires) # allow empty 'expires' (should not happen)
|
310
|
+
ensure
|
311
|
+
File.unlink("tmp_test.tmp")
|
312
|
+
end
|
313
|
+
end
|
314
|
+
|
315
|
+
def test_save_cookie()
|
316
|
+
str = <<EOF
|
317
|
+
http://www.zdnet.co.jp/news/0106/08/e_gibson.html NGUserID d29b8f49-10875-992421294-1 2145801600 www.zdnet.co.jp / 9
|
318
|
+
http://www.zdnet.co.jp/news/0106/08/e_gibson.html PACK zd3-992421294-7436 2145801600 .zdnet.co.jp / 13
|
319
|
+
EOF
|
320
|
+
begin
|
321
|
+
File.open("tmp_test.tmp","w") {|f|
|
322
|
+
f.write str
|
323
|
+
}
|
324
|
+
@cm.cookies_file = 'tmp_test.tmp'
|
325
|
+
@cm.load_cookies()
|
326
|
+
@cm.instance_eval{@is_saved = false}
|
327
|
+
@cm.cookies_file = 'tmp_test2.tmp'
|
328
|
+
@cm.save_cookies()
|
329
|
+
str2 = ''
|
330
|
+
File.open("tmp_test2.tmp","r") {|f|
|
331
|
+
str2 = f.read()
|
332
|
+
}
|
333
|
+
assert_equal(str, str2)
|
334
|
+
#
|
335
|
+
assert(File.exist?('tmp_test2.tmp'))
|
336
|
+
File.unlink("tmp_test2.tmp")
|
337
|
+
@cm.save_cookies()
|
338
|
+
assert(!File.exist?('tmp_test2.tmp'))
|
339
|
+
@cm.save_cookies(true)
|
340
|
+
assert(File.exist?('tmp_test2.tmp'))
|
341
|
+
ensure
|
342
|
+
File.unlink("tmp_test.tmp")
|
343
|
+
if FileTest.exist?("tmp_test2.tmp")
|
344
|
+
File.unlink("tmp_test2.tmp")
|
345
|
+
end
|
346
|
+
end
|
347
|
+
end
|
348
|
+
|
349
|
+
def test_not_saved_expired_cookies
|
350
|
+
begin
|
351
|
+
@cm.cookies_file = 'tmp_test.tmp'
|
352
|
+
uri = urify('http://www.example.org')
|
353
|
+
@cm.parse("foo=1; path=/", uri)
|
354
|
+
@cm.parse("bar=2; path=/; expires=", uri)
|
355
|
+
@cm.parse("baz=3; path=/; expires=\"\"", uri)
|
356
|
+
@cm.parse("qux=4; path=/; expires=#{(Time.now + 10).asctime}", uri)
|
357
|
+
@cm.parse("quxx=5; path=/; expires=#{(Time.now - 10).asctime}", uri)
|
358
|
+
@cm.save_cookies()
|
359
|
+
@cm.load_cookies
|
360
|
+
assert_equal(1, @cm.cookies.size) # +10 cookies only
|
361
|
+
ensure
|
362
|
+
File.unlink("tmp_test.tmp") if File.exist?("tmp_test.tmp")
|
363
|
+
end
|
364
|
+
end
|
365
|
+
|
366
|
+
def test_add()
|
367
|
+
c = WebAgent::Cookie.new()
|
368
|
+
c.name = "hoge"
|
369
|
+
c.value = "funi"
|
370
|
+
c.url = urify("http://www.inac.co.jp/hoge")
|
371
|
+
@cm.add(c)
|
372
|
+
c = @cm.cookies[0]
|
373
|
+
assert_equal('hoge', c.name)
|
374
|
+
assert_equal('funi', c.value)
|
375
|
+
assert_equal(nil, c.expires)
|
376
|
+
end
|
377
|
+
|
378
|
+
def test_add2()
|
379
|
+
c = WebAgent::Cookie.new()
|
380
|
+
c.name = "hoge"
|
381
|
+
c.value = "funi"
|
382
|
+
c.path = ''
|
383
|
+
c.url = urify("http://www.inac.co.jp/hoge/hoge2/hoge3")
|
384
|
+
@cm.add(c)
|
385
|
+
#
|
386
|
+
c = WebAgent::Cookie.new()
|
387
|
+
c.name = "hoge"
|
388
|
+
c.value = "funi"
|
389
|
+
#c.path = '' NO path given -> same as URL
|
390
|
+
c.url = urify("http://www.inac.co.jp/hoge/hoge2/hoge3")
|
391
|
+
@cm.add(c)
|
392
|
+
#
|
393
|
+
c1, c2 = @cm.cookies
|
394
|
+
assert_equal('', c1.path)
|
395
|
+
assert_equal('/hoge/hoge2', c2.path)
|
396
|
+
end
|
397
|
+
|
398
|
+
def test_check_cookie_accept_domain()
|
399
|
+
@cm.accept_domains = [".example1.co.jp", "www1.example.jp"]
|
400
|
+
@cm.reject_domains = [".example2.co.jp", "www2.example.jp"]
|
401
|
+
check1 = @cm.check_cookie_accept_domain("www.example1.co.jp")
|
402
|
+
assert_equal(true, check1)
|
403
|
+
check2 = @cm.check_cookie_accept_domain("www.example2.co.jp")
|
404
|
+
assert_equal(false, check2)
|
405
|
+
check3 = @cm.check_cookie_accept_domain("www1.example.jp")
|
406
|
+
assert_equal(true, check3)
|
407
|
+
check4 = @cm.check_cookie_accept_domain("www2.example.jp")
|
408
|
+
assert_equal(false, check4)
|
409
|
+
check5 = @cm.check_cookie_accept_domain("aa.www2.example.jp")
|
410
|
+
assert_equal(true, check5)
|
411
|
+
check6 = @cm.check_cookie_accept_domain("aa.www2.example.jp")
|
412
|
+
assert_equal(true, check6)
|
413
|
+
assert_equal(false, @cm.check_cookie_accept_domain(nil))
|
414
|
+
end
|
415
|
+
|
416
|
+
def test_escaped
|
417
|
+
uri = urify('http://www.example.org')
|
418
|
+
|
419
|
+
@cm.parse("bar=2; path=/", uri)
|
420
|
+
c = @cm.cookies.first
|
421
|
+
assert_equal('2', c.value)
|
422
|
+
assert_equal('bar=2', @cm.find(uri))
|
423
|
+
|
424
|
+
@cm.parse("bar=2; path=/", uri)
|
425
|
+
c = @cm.cookies.first
|
426
|
+
assert_equal('2', c.value)
|
427
|
+
assert_equal('bar=2', @cm.find(uri))
|
428
|
+
|
429
|
+
@cm.parse("bar=; path=/", uri)
|
430
|
+
c = @cm.cookies.first
|
431
|
+
assert_equal(nil, c.value)
|
432
|
+
assert_equal('bar=', @cm.find(uri))
|
433
|
+
|
434
|
+
@cm.parse("bar=; path=/", uri)
|
435
|
+
c = @cm.cookies.first
|
436
|
+
assert_equal(nil, c.value)
|
437
|
+
assert_equal('bar=', @cm.find(uri))
|
438
|
+
end
|
439
|
+
|
440
|
+
def test_load_cookies_escaped
|
441
|
+
uri = urify('http://example.org/')
|
442
|
+
f = Tempfile.new('test_cookie')
|
443
|
+
File.open(f.path, 'w') do |f|
|
444
|
+
f.write <<EOF
|
445
|
+
http://example.org/ key "value" 0 .example.org / 13 0
|
446
|
+
http://example.org/ key "" .example.org / 13 0
|
447
|
+
http://example.org/ key .example.org / 13 0
|
448
|
+
EOF
|
449
|
+
end
|
450
|
+
@cm.cookies_file = f.path
|
451
|
+
@cm.load_cookies
|
452
|
+
c0, c1, c2 = @cm.cookies
|
453
|
+
assert_equal('"value"', c0.value)
|
454
|
+
assert_equal('""', c1.value)
|
455
|
+
assert_equal('', c2.value)
|
456
|
+
assert_equal('key="value"', @cm.find(uri))
|
457
|
+
c0.value = ''
|
458
|
+
assert_equal('key=', @cm.find(uri))
|
459
|
+
c0.value = '""'
|
460
|
+
assert_equal('key=""', @cm.find(uri))
|
461
|
+
end
|
462
|
+
|
463
|
+
end
|
464
|
+
|
465
|
+
end
|