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.
@@ -0,0 +1,63 @@
1
+ require 'httpclient'
2
+ require 'json'
3
+
4
+ # JSONClient auto-converts Hash <-> JSON in request and response.
5
+ # * For POST or PUT request, convert Hash body to JSON String with 'application/json; charset=utf-8' header.
6
+ # * For response, convert JSON String to Hash when content-type is '(application|text)/(x-)?json'
7
+ class JSONClient < HTTPClient
8
+ CONTENT_TYPE_JSON_REGEX = /(application|text)\/(x-)?json/i
9
+ CONTENT_TYPE_JSON = 'application/json; charset=utf-8'
10
+
11
+ attr_reader :content_type_json_request
12
+ attr_reader :content_type_json_response_regex
13
+
14
+ def initialize(*args)
15
+ super
16
+ @content_type_json_request = CONTENT_TYPE_JSON
17
+ @content_type_json_response_regex = CONTENT_TYPE_JSON_REGEX
18
+ end
19
+
20
+ def post(uri, *args, &block)
21
+ request(:post, uri, argument_to_hash_for_json(args), &block)
22
+ end
23
+
24
+ def put(uri, *args, &block)
25
+ request(:put, uri, argument_to_hash_for_json(args), &block)
26
+ end
27
+
28
+ def request(method, uri, *args, &block)
29
+ res = super
30
+ if @content_type_json_response_regex =~ res.content_type
31
+ res = wrap_json_response(res)
32
+ end
33
+ res
34
+ end
35
+
36
+ private
37
+
38
+ def argument_to_hash_for_json(args)
39
+ hash = argument_to_hash(args, :body, :header, :follow_redirect)
40
+ if hash[:body].is_a?(Hash)
41
+ hash[:header] = json_header(hash[:header])
42
+ hash[:body] = JSON.generate(hash[:body])
43
+ end
44
+ hash
45
+ end
46
+
47
+ def json_header(header)
48
+ header ||= {}
49
+ if header.is_a?(Hash)
50
+ header['Content-Type'] = @content_type_json_request
51
+ else
52
+ header << ['Content-Type', @content_type_json_request]
53
+ end
54
+ header
55
+ end
56
+
57
+ def wrap_json_response(original)
58
+ res = ::HTTP::Message.new_response(JSON.parse(original.content))
59
+ res.http_header = original.http_header
60
+ res.previous = original
61
+ res
62
+ end
63
+ end
@@ -115,14 +115,25 @@ class TestAuth < Test::Unit::TestCase
115
115
  res.body = 'digest_sess_auth OK' + req.query_string.to_s
116
116
  end
117
117
 
118
- # TODO: disables this test; it causes an exception because it seems
119
- # that there's version incompatibility between
120
- # rack-ntlm and rack-ntlm-test-services.
121
- def __test_ntlm_auth
118
+ # TODO: monkey patching for rack-ntlm-test-services's incompat.
119
+ module ::Net
120
+ module NTLM
121
+ # ruby-ntlm 0.3.0 -> 0.4.0
122
+ def self.decode_utf16le(*arg)
123
+ EncodeUtil.decode_utf16le(*arg)
124
+ end
125
+ # Make it work if @value == nil
126
+ class SecurityBuffer < FieldSet
127
+ def data_size
128
+ @active && @value ? @value.size : 0
129
+ end
130
+ end
131
+ end
132
+ end
133
+ def test_ntlm_auth
122
134
  c = HTTPClient.new
123
135
  c.set_auth("http://localhost:#{serverport}/ntlm_auth", 'admin', 'admin')
124
136
  assert_equal('ntlm_auth OK', c.get_content("http://localhost:#{serverport}/ntlm_auth"))
125
- puts c.inspect
126
137
  end
127
138
 
128
139
  def test_basic_auth
@@ -146,7 +157,11 @@ class TestAuth < Test::Unit::TestCase
146
157
  c.www_auth.basic_auth.instance_eval { @scheme = "BASIC" }
147
158
  #
148
159
  c.set_auth("http://localhost:#{serverport}/", 'admin', 'admin')
149
- assert_equal('basic_auth OK', c.get_content("http://localhost:#{serverport}/basic_auth"))
160
+ res = c.get("http://localhost:#{serverport}/basic_auth")
161
+ assert_equal('basic_auth OK', res.content)
162
+ assert_equal(200, res.status)
163
+ assert_equal(401, res.previous.status)
164
+ assert_equal(nil, res.previous.previous)
150
165
  ensure
151
166
  @basic_auth.instance_eval { @auth_scheme = webrick_backup }
152
167
  end
@@ -425,4 +440,34 @@ class TestAuth < Test::Unit::TestCase
425
440
  assert(str.index(%q(POST /photos)))
426
441
  assert(str.index(%q(Authorization: OAuth realm="http://photos.example.net/", oauth_consumer_key="dpf43f3p2l4k3l03", oauth_nonce="kllo9940pd9333jh", oauth_signature="wPkvxykrw%2BBTdCcGqKr%2B3I%2BPsiM%3D", oauth_signature_method="HMAC-SHA1", oauth_timestamp="1191242096", oauth_token="nnch734d00sl2jdk", oauth_version="1.0")))
427
442
  end
443
+
444
+ def test_basic_auth_post_with_multipart
445
+ c = HTTPClient.new
446
+ c.set_auth("http://localhost:#{serverport}/", 'admin', 'admin')
447
+ File.open(__FILE__) do |f|
448
+ # read 'f' twice for authorization negotiation
449
+ assert_equal('basic_auth OK', c.post("http://localhost:#{serverport}/basic_auth", :file => f).content)
450
+ end
451
+ end
452
+
453
+ def test_negotiate_and_basic
454
+ c = HTTPClient.new
455
+ c.test_loopback_http_response << %Q(HTTP/1.1 401 Unauthorized\r\nWWW-Authenticate: NTLM\r\nWWW-Authenticate: Basic realm="foo"\r\nConnection: Keep-Alive\r\nContent-Length: 0\r\n\r\n)
456
+ c.test_loopback_http_response << %Q(HTTP/1.1 401 Unauthorized\r\nWWW-Authenticate: NTLM TlRMTVNTUAACAAAAAAAAACgAAAABAAAAAAAAAAAAAAA=\r\nConnection: Keep-Alive\r\nContent-Length: 0\r\n\r\n)
457
+ c.test_loopback_http_response << %Q(HTTP/1.0 200 OK\r\nConnection: Keep-Alive\r\nContent-Length: 1\r\n\r\na)
458
+ c.test_loopback_http_response << %Q(HTTP/1.0 200 OK\r\nConnection: Keep-Alive\r\nContent-Length: 1\r\n\r\nb)
459
+ c.debug_dev = str = ''
460
+ c.set_auth('http://www.example.org/', 'admin', 'admin')
461
+ # Do NTLM negotiation
462
+ c.get('http://www.example.org/foo')
463
+ # BasicAuth authenticator should not respond to it because NTLM
464
+ # negotiation has been finished.
465
+ assert_match(%r(Authorization: NTLM), str)
466
+ assert_not_match(%r(Authorization: Basic), str)
467
+ # ditto for other resource that is protected with NTLM
468
+ c.debug_dev = str = ''
469
+ c.get('http://www.example.org/foo/subdir')
470
+ assert_not_match(%r(Authorization: NTLM), str)
471
+ assert_not_match(%r(Authorization: Basic), str)
472
+ end
428
473
  end
@@ -1,131 +1,20 @@
1
1
  require 'test/unit'
2
2
  require 'uri'
3
+ require 'tempfile'
3
4
 
5
+ require 'httpclient/util'
4
6
  require 'httpclient/cookie'
5
7
 
6
8
  class TestCookie < Test::Unit::TestCase
7
9
  include HTTPClient::Util
8
10
 
9
11
  def setup()
10
- @c = WebAgent::Cookie.new()
12
+ @c = WebAgent::Cookie.new('hoge', 'funi')
11
13
  end
12
14
 
13
15
  def test_s_new()
14
16
  assert_instance_of(WebAgent::Cookie, @c)
15
17
  end
16
-
17
- def test_discard?
18
- assert_equal(false, !!(@c.discard?))
19
- @c.discard = true
20
- assert_equal(true, !!(@c.discard?))
21
- end
22
-
23
- def test_match()
24
- url = urify('http://www.rubycolor.org/hoge/funi/#919191')
25
-
26
- @c.domain = 'www.rubycolor.org'
27
- assert_equal(true, @c.match?(url))
28
-
29
- @c.domain = '.rubycolor.org'
30
- assert_equal(true, @c.match?(url))
31
-
32
- @c.domain = 'aaa.www.rubycolor.org'
33
- assert_equal(false, @c.match?(url))
34
-
35
- @c.domain = 'aaa.www.rubycolor.org'
36
- assert_equal(false, @c.match?(url))
37
-
38
- @c.domain = 'www.rubycolor.org'
39
- @c.path = '/'
40
- assert_equal(true, @c.match?(url))
41
-
42
- @c.domain = 'www.rubycolor.org'
43
- @c.path = '/hoge'
44
- assert_equal(true, @c.match?(url))
45
-
46
- @c.domain = 'www.rubycolor.org'
47
- @c.path = '/hoge/hoge'
48
- assert_equal(false, @c.match?(url))
49
-
50
- @c.domain = 'www.rubycolor.org'
51
- @c.path = '/hoge'
52
- @c.secure = true
53
- assert_equal(false, @c.match?(url))
54
-
55
- url2 = urify('https://www.rubycolor.org/hoge/funi/#919191')
56
- @c.domain = 'www.rubycolor.org'
57
- @c.path = '/hoge'
58
- @c.secure = true
59
- assert_equal(true, @c.match?(url2))
60
-
61
- @c.domain = 'www.rubycolor.org'
62
- @c.path = '/hoge'
63
- @c.secure = nil
64
- assert_equal(true, @c.match?(url2)) ## not false!
65
-
66
- url.port = 80
67
- @c.domain = 'www.rubycolor.org'
68
- @c.path = '/hoge'
69
- # @c.port = [80,8080]
70
- assert_equal(true, @c.match?(url))
71
-
72
- url_nopath = URI.parse('http://www.rubycolor.org')
73
- @c.domain = 'www.rubycolor.org'
74
- @c.path = '/'
75
- assert_equal(true, @c.match?(url_nopath))
76
-
77
- end
78
-
79
- def test_head_match?()
80
- assert_equal(true, @c.head_match?("",""))
81
- assert_equal(false, @c.head_match?("a",""))
82
- assert_equal(true, @c.head_match?("","a"))
83
- assert_equal(true, @c.head_match?("abcde","abcde"))
84
- assert_equal(true, @c.head_match?("abcde","abcdef"))
85
- assert_equal(false, @c.head_match?("abcdef","abcde"))
86
- assert_equal(false, @c.head_match?("abcde","bcde"))
87
- assert_equal(false, @c.head_match?("bcde","abcde"))
88
- end
89
-
90
- def test_tail_match?()
91
- assert_equal(true, @c.tail_match?("",""))
92
- assert_equal(false, @c.tail_match?("a",""))
93
- assert_equal(true, @c.tail_match?("","a"))
94
- assert_equal(true, @c.tail_match?("abcde","abcde"))
95
- assert_equal(false, @c.tail_match?("abcde","abcdef"))
96
- assert_equal(false, @c.tail_match?("abcdef","abcde"))
97
- assert_equal(false, @c.tail_match?("abcde","bcde"))
98
- assert_equal(true, @c.tail_match?("bcde","abcde"))
99
- end
100
-
101
-
102
- def test_domain_match()
103
- extend WebAgent::CookieUtils
104
- assert_equal(true, !!domain_match("hoge.co.jp","."))
105
- # assert_equal(true, !!domain_match("locahost",".local"))
106
- assert_equal(true, !!domain_match("192.168.10.1","192.168.10.1"))
107
- assert_equal(false, !!domain_match("192.168.10.1","192.168.10.2"))
108
- # assert_equal(false, !!domain_match("hoge.co.jp",".hoge.co.jp"))
109
- # allows; host == rubyforge.org, domain == .rubyforge.org
110
- assert_equal(true, !!domain_match("hoge.co.jp",".hoge.co.jp"))
111
- assert_equal(true, !!domain_match("www.hoge.co.jp", "www.hoge.co.jp"))
112
- assert_equal(false, !!domain_match("www.hoge.co.jp", "www2.hoge.co.jp"))
113
- assert_equal(true, !!domain_match("www.hoge.co.jp", ".hoge.co.jp"))
114
- assert_equal(true, !!domain_match("www.aa.hoge.co.jp", ".hoge.co.jp"))
115
- assert_equal(false, !!domain_match("www.hoge.co.jp", "hoge.co.jp"))
116
- end
117
-
118
- def test_join_quotedstr()
119
- arr1 = ['hoge=funi', 'hoge2=funi2']
120
- assert_equal(arr1, @c.instance_eval{join_quotedstr(arr1,';')})
121
- arr2 = ['hoge="fu', 'ni"', 'funi=funi']
122
- assert_equal(['hoge="fu;ni"','funi=funi'],
123
- @c.instance_eval{join_quotedstr(arr2,';')})
124
- arr3 = ['hoge="funi";hoge2="fu','ni2";hoge3="hoge"', 'funi="funi"']
125
- assert_equal(['hoge="funi";hoge2="fu,ni2";hoge3="hoge"', 'funi="funi"'],
126
- @c.instance_eval{join_quotedstr(arr3,',')})
127
- end
128
-
129
18
  end
130
19
 
131
20
  class TestCookieManager < Test::Unit::TestCase
@@ -139,13 +28,13 @@ class TestCookieManager < Test::Unit::TestCase
139
28
  end
140
29
 
141
30
  def test_parse()
142
- str = "inkid=n92b0ADOgACIgUb9lsjHqAAAHu2a; expires=Wed, 01-Dec-2010 00:00:00 GMT; path=/"
31
+ str = "inkid=n92b0ADOgACIgUb9lsjHqAAAHu2a; expires=Wed, 01-Dec-2999 00:00:00 GMT; path=/"
143
32
  @cm.parse(str, urify('http://www.test.jp'))
144
33
  cookie = @cm.cookies[0]
145
34
  assert_instance_of(WebAgent::Cookie, cookie)
146
35
  assert_equal("inkid", cookie.name)
147
36
  assert_equal("n92b0ADOgACIgUb9lsjHqAAAHu2a", cookie.value)
148
- assert_equal(Time.gm(2010, 12, 1, 0,0,0), cookie.expires)
37
+ assert_equal(Time.gm(2999, 12, 1, 0,0,0), cookie.expires)
149
38
  assert_equal("/", cookie.path)
150
39
  end
151
40
 
@@ -157,7 +46,8 @@ class TestCookieManager < Test::Unit::TestCase
157
46
  assert_equal("xmen", cookie.name)
158
47
  assert_equal("off,0,0,1", cookie.value)
159
48
  assert_equal("/", cookie.path)
160
- assert_equal(".excite.co.jp", cookie.domain)
49
+ assert_equal("excite.co.jp", cookie.domain)
50
+ assert_equal(".excite.co.jp", cookie.dot_domain)
161
51
  assert_equal(Time.gm(2037,12,31,12,0,0), cookie.expires)
162
52
  end
163
53
 
@@ -169,7 +59,8 @@ class TestCookieManager < Test::Unit::TestCase
169
59
  assert_equal("xmen", cookie.name)
170
60
  assert_equal("off,0,0,1", cookie.value)
171
61
  assert_equal("/", cookie.path)
172
- assert_equal(".excite.co.jp", cookie.domain)
62
+ assert_equal("excite.co.jp", cookie.domain)
63
+ assert_equal(".excite.co.jp", cookie.dot_domain)
173
64
  assert_equal(Time.gm(2037,12,31,12,0,0), cookie.expires)
174
65
  assert_equal(true, cookie.secure?)
175
66
  assert_equal(true, cookie.http_only?)
@@ -183,7 +74,8 @@ class TestCookieManager < Test::Unit::TestCase
183
74
  assert_equal("xmen", cookie.name)
184
75
  assert_equal("off,0,0,1", cookie.value)
185
76
  assert_equal("/;;", cookie.path)
186
- assert_equal(".excite.co.jp", cookie.domain)
77
+ assert_equal("excite.co.jp", cookie.domain)
78
+ assert_equal(".excite.co.jp", cookie.dot_domain)
187
79
  assert_equal(Time.gm(2037,12,31,12,0,0), cookie.expires)
188
80
  end
189
81
 
@@ -194,19 +86,21 @@ class TestCookieManager < Test::Unit::TestCase
194
86
  # end
195
87
 
196
88
  def test_check_expired_cookies()
197
- c1 = WebAgent::Cookie.new()
198
- c2 = c1.dup
199
- c3 = c1.dup
200
- c4 = c1.dup
201
- c1.expires = Time.now - 100
202
- c2.expires = Time.now + 100
203
- c3.expires = Time.now - 10
89
+ format = "%a, %d-%b-%Y %H:%M:%S GMT"
90
+ c1 = WebAgent::Cookie.new('hoge1', 'funi', :domain => 'http://www.example.com/', :path => '/')
91
+ c2 = WebAgent::Cookie.new('hoge2', 'funi', :domain => 'http://www.example.com/', :path => '/')
92
+ c3 = WebAgent::Cookie.new('hoge3', 'funi', :domain => 'http://www.example.com/', :path => '/')
93
+ c4 = WebAgent::Cookie.new('hoge4', 'funi', :domain => 'http://www.example.com/', :path => '/')
94
+ c1.expires = (Time.now - 100).gmtime.strftime(format)
95
+ c2.expires = (Time.now + 100).gmtime.strftime(format)
96
+ c3.expires = (Time.now - 10).gmtime.strftime(format)
204
97
  c4.expires = nil
205
98
  cookies = [c1,c2,c3,c4]
206
99
  @cm.cookies = cookies
207
- @cm.check_expired_cookies()
208
- # expires == nil cookies (session cookie) exists.
209
- assert_equal([c2,c4], @cm.cookies)
100
+ assert_equal(c2.name, @cm.cookies[0].name)
101
+ assert_equal(c2.expires, @cm.cookies[0].expires)
102
+ assert_equal(c4.name, @cm.cookies[1].name)
103
+ assert_equal(c4.expires, @cm.cookies[1].expires)
210
104
  end
211
105
 
212
106
  def test_parse_expires
@@ -236,13 +130,13 @@ class TestCookieManager < Test::Unit::TestCase
236
130
  end
237
131
 
238
132
  def test_parse_after_expiration
239
- str = "inkid=n92b0ADOgACIgUb9lsjHqAAAHu2a; expires=Wed, 01-Dec-2010 00:00:00 GMT; path=/"
133
+ str = "inkid=n92b0ADOgACIgUb9lsjHqAAAHu2a; expires=Wed, 01-Dec-2999 00:00:00 GMT; path=/"
240
134
  @cm.parse(str, urify('http://www.test.jp'))
241
135
  cookie = @cm.cookies[0]
242
136
  assert_instance_of(WebAgent::Cookie, cookie)
243
137
  assert_equal("inkid", cookie.name)
244
138
  assert_equal("n92b0ADOgACIgUb9lsjHqAAAHu2a", cookie.value)
245
- assert_equal(Time.gm(2010, 12, 1, 0,0,0), cookie.expires)
139
+ assert_equal(Time.gm(2999, 12, 1, 0,0,0), cookie.expires)
246
140
  assert_equal("/", cookie.path)
247
141
 
248
142
  time = Time.at(Time.now.to_i + 60).utc
@@ -263,49 +157,43 @@ class TestCookieManager < Test::Unit::TestCase
263
157
  str = "xmen=off,0,0,2; path=/; domain=.excite.co.jp; expires=Wednesday, 31-Dec-2037 12:00:00 GMT"
264
158
  @cm.parse(str, urify("http://www.excite.co.jp/"))
265
159
 
266
- @cm.cookies[0].use = true
267
- @cm.cookies[1].use = true
268
-
269
160
  url = urify('http://www.excite.co.jp/hoge/funi/')
270
161
  cookie_str = @cm.find(url)
271
- assert_equal("xmen=off,0,0,2", cookie_str)
162
+ assert_equal("xmen=\"off,0,0,2\"", cookie_str)
272
163
  end
273
164
 
274
165
  def test_load_cookies()
275
- begin
276
- File.open("tmp_test.tmp","w") {|f|
277
- f.write <<EOF
166
+ cookiefile = Tempfile.new('test_cookie')
167
+ File.open(cookiefile.path, 'w') do |f|
168
+ f.write <<EOF
278
169
  http://www.zdnet.co.jp/news/0106/08/e_gibson.html NGUserID d29b8f49-10875-992421294-1 2145801600 www.zdnet.co.jp / 9 0
279
- http://www.zdnet.co.jp/news/0106/08/e_gibson.html PACK zd3-992421294-7436 1293839999 .zdnet.co.jp / 13 0
170
+ http://www.zdnet.co.jp/news/0106/08/e_gibson.html PACK zd3-992421294-7436 2293839999 .zdnet.co.jp / 13 0
280
171
  http://example.org/ key value 0 .example.org / 13 0
281
172
  http://example.org/ key value .example.org / 13 0
282
173
  EOF
283
- }
284
-
285
- @cm.cookies_file = 'tmp_test.tmp'
286
- @cm.load_cookies()
287
- c0, c1, c2, c3 = @cm.cookies
288
- assert_equal('http://www.zdnet.co.jp/news/0106/08/e_gibson.html', c0.url.to_s)
289
- assert_equal('NGUserID', c0.name)
290
- assert_equal('d29b8f49-10875-992421294-1', c0.value)
291
- assert_equal(Time.at(2145801600), c0.expires)
292
- assert_equal('www.zdnet.co.jp', c0.domain)
293
- assert_equal('/', c0.path)
294
- assert_equal(9, c0.flag)
295
- #
296
- assert_equal('http://www.zdnet.co.jp/news/0106/08/e_gibson.html', c1.url.to_s)
297
- assert_equal('PACK', c1.name)
298
- assert_equal('zd3-992421294-7436', c1.value)
299
- assert_equal(Time.at(1293839999), c1.expires)
300
- assert_equal('.zdnet.co.jp', c1.domain)
301
- assert_equal('/', c1.path)
302
- assert_equal(13, c1.flag)
303
- #
304
- assert_equal(nil, c2.expires)
305
- assert_equal(nil, c3.expires) # allow empty 'expires' (should not happen)
306
- ensure
307
- File.unlink("tmp_test.tmp")
308
174
  end
175
+
176
+ @cm.cookies_file = cookiefile.path
177
+ @cm.load_cookies()
178
+ c0, c1, c2 = @cm.cookies
179
+ assert_equal('http://www.zdnet.co.jp/news/0106/08/e_gibson.html', c0.url.to_s)
180
+ assert_equal('NGUserID', c0.name)
181
+ assert_equal('d29b8f49-10875-992421294-1', c0.value)
182
+ assert_equal(Time.at(2145801600), c0.expires)
183
+ assert_equal('www.zdnet.co.jp', c0.domain)
184
+ assert_equal('/', c0.path)
185
+ assert_equal(9, c0.flag)
186
+ #
187
+ assert_equal('http://www.zdnet.co.jp/news/0106/08/e_gibson.html', c1.url.to_s)
188
+ assert_equal('PACK', c1.name)
189
+ assert_equal('zd3-992421294-7436', c1.value)
190
+ assert_equal(Time.at(2293839999), c1.expires)
191
+ assert_equal('zdnet.co.jp', c1.domain)
192
+ assert_equal('.zdnet.co.jp', c1.dot_domain)
193
+ assert_equal('/', c1.path)
194
+ assert_equal(13, c1.flag)
195
+ #
196
+ assert_equal(nil, c2.expires)
309
197
  end
310
198
 
311
199
  def test_save_cookie()
@@ -313,56 +201,44 @@ EOF
313
201
  http://www.zdnet.co.jp/news/0106/08/e_gibson.html NGUserID d29b8f49-10875-992421294-1 2145801600 www.zdnet.co.jp / 9
314
202
  http://www.zdnet.co.jp/news/0106/08/e_gibson.html PACK zd3-992421294-7436 2145801600 .zdnet.co.jp / 13
315
203
  EOF
316
- begin
317
- File.open("tmp_test.tmp","w") {|f|
318
- f.write str
319
- }
320
- @cm.cookies_file = 'tmp_test.tmp'
321
- @cm.load_cookies()
322
- @cm.instance_eval{@is_saved = false}
323
- @cm.cookies_file = 'tmp_test2.tmp'
324
- @cm.save_cookies()
325
- str2 = ''
326
- File.open("tmp_test2.tmp","r") {|f|
327
- str2 = f.read()
328
- }
329
- assert_equal(str, str2)
330
- #
331
- assert(File.exist?('tmp_test2.tmp'))
332
- File.unlink("tmp_test2.tmp")
333
- @cm.save_cookies()
334
- assert(!File.exist?('tmp_test2.tmp'))
335
- @cm.save_cookies(true)
336
- assert(File.exist?('tmp_test2.tmp'))
337
- ensure
338
- File.unlink("tmp_test.tmp")
339
- if FileTest.exist?("tmp_test2.tmp")
340
- File.unlink("tmp_test2.tmp")
341
- end
204
+ cookiefile = Tempfile.new('test_cookie')
205
+ cookiefile2 = Tempfile.new('test_cookie2')
206
+ File.open(cookiefile.path, 'w') do |f|
207
+ f.write str
342
208
  end
209
+
210
+ @cm.cookies_file = cookiefile.path
211
+ @cm.load_cookies()
212
+ @cm.instance_eval{@is_saved = false}
213
+ @cm.cookies_file = cookiefile2.path
214
+ @cm.save_cookies()
215
+ str2 = ''
216
+ File.open(cookiefile2.path, 'r') do |f|
217
+ str2 = f.read
218
+ end
219
+ assert_equal(str.split.sort, str2.split.sort)
220
+ assert(File.exist?(cookiefile2.path))
221
+ File.unlink(cookiefile2.path)
222
+ @cm.save_cookies()
223
+ assert(File.exist?(cookiefile2.path))
343
224
  end
344
225
 
345
226
  def test_not_saved_expired_cookies
346
- begin
347
- @cm.cookies_file = 'tmp_test.tmp'
348
- uri = urify('http://www.example.org')
349
- @cm.parse("foo=1; path=/", uri)
350
- @cm.parse("bar=2; path=/; expires=", uri)
351
- @cm.parse("baz=3; path=/; expires=\"\"", uri)
352
- @cm.parse("qux=4; path=/; expires=#{(Time.now + 10).asctime}", uri)
353
- @cm.parse("quxx=5; path=/; expires=#{(Time.now - 10).asctime}", uri)
354
- @cm.save_cookies()
355
- @cm.load_cookies
356
- assert_equal(1, @cm.cookies.size) # +10 cookies only
357
- ensure
358
- File.unlink("tmp_test.tmp") if File.exist?("tmp_test.tmp")
359
- end
227
+ cookiefile = Tempfile.new('test_cookie')
228
+ @cm.cookies_file = cookiefile.path
229
+ uri = urify('http://www.example.org')
230
+ @cm.parse("foo=1; path=/", uri)
231
+ @cm.parse("bar=2; path=/; expires=", uri)
232
+ @cm.parse("baz=3; path=/; expires=\"\"", uri)
233
+ @cm.parse("qux=4; path=/; expires=#{(Time.now.gmtime + 10).asctime}", uri)
234
+ @cm.parse("quxx=5; path=/; expires=#{(Time.now.gmtime - 10).asctime}", uri)
235
+ @cm.save_cookies
236
+ @cm.load_cookies
237
+ assert_equal(1, @cm.cookies.size) # +10 cookies only
360
238
  end
361
239
 
362
240
  def test_add()
363
- c = WebAgent::Cookie.new()
364
- c.name = "hoge"
365
- c.value = "funi"
241
+ c = WebAgent::Cookie.new('hoge', 'funi')
366
242
  c.url = urify("http://www.inac.co.jp/hoge")
367
243
  @cm.add(c)
368
244
  c = @cm.cookies[0]
@@ -372,41 +248,62 @@ EOF
372
248
  end
373
249
 
374
250
  def test_add2()
375
- c = WebAgent::Cookie.new()
376
- c.name = "hoge"
377
- c.value = "funi"
251
+ c = WebAgent::Cookie.new('hoge', 'funi')
378
252
  c.path = ''
379
253
  c.url = urify("http://www.inac.co.jp/hoge/hoge2/hoge3")
380
254
  @cm.add(c)
381
255
  #
382
- c = WebAgent::Cookie.new()
383
- c.name = "hoge"
384
- c.value = "funi"
256
+ c = WebAgent::Cookie.new('hoge', 'funi')
385
257
  #c.path = '' NO path given -> same as URL
386
258
  c.url = urify("http://www.inac.co.jp/hoge/hoge2/hoge3")
387
259
  @cm.add(c)
388
260
  #
389
261
  c1, c2 = @cm.cookies
390
- assert_equal('', c1.path)
391
- assert_equal('/hoge/hoge2', c2.path)
262
+ assert_equal('/hoge/hoge2/', c1.path)
263
+ assert_equal('/', c2.path)
264
+ end
265
+
266
+ def test_keep_escaped
267
+ uri = urify('http://www.example.org')
268
+
269
+ @cm.parse("bar=2; path=/", uri)
270
+ c = @cm.cookies.first
271
+ assert_equal('2', c.value)
272
+ assert_equal('bar=2', @cm.find(uri))
273
+
274
+ @cm.parse("bar=\"2\"; path=/", uri)
275
+ c = @cm.cookies.first
276
+ assert_equal('2', c.value)
277
+ assert_equal('bar=2', @cm.find(uri))
278
+
279
+ @cm.parse("bar=; path=/", uri)
280
+ c = @cm.cookies.first
281
+ assert_equal('', c.value)
282
+ assert_equal('bar=', @cm.find(uri))
283
+
284
+ @cm.parse("bar=\"\"; path=/", uri)
285
+ c = @cm.cookies.first
286
+ assert_equal('', c.value)
287
+ assert_equal('bar=', @cm.find(uri))
392
288
  end
393
289
 
394
- def test_check_cookie_accept_domain()
395
- @cm.accept_domains = [".example1.co.jp", "www1.example.jp"]
396
- @cm.reject_domains = [".example2.co.jp", "www2.example.jp"]
397
- check1 = @cm.check_cookie_accept_domain("www.example1.co.jp")
398
- assert_equal(true, check1)
399
- check2 = @cm.check_cookie_accept_domain("www.example2.co.jp")
400
- assert_equal(false, check2)
401
- check3 = @cm.check_cookie_accept_domain("www1.example.jp")
402
- assert_equal(true, check3)
403
- check4 = @cm.check_cookie_accept_domain("www2.example.jp")
404
- assert_equal(false, check4)
405
- check5 = @cm.check_cookie_accept_domain("aa.www2.example.jp")
406
- assert_equal(true, check5)
407
- check6 = @cm.check_cookie_accept_domain("aa.www2.example.jp")
408
- assert_equal(true, check6)
409
- assert_equal(false, @cm.check_cookie_accept_domain(nil))
290
+ def test_load_cookies_escaped
291
+ uri = urify('http://example.org/')
292
+ f = Tempfile.new('test_cookie')
293
+ File.open(f.path, 'w') do |f|
294
+ f.write <<EOF
295
+ http://example.org/ key1 "value" 0 .example.org / 13 0
296
+ http://example.org/ key2 "" 0 .example.org / 13 0
297
+ http://example.org/ key3 0 .example.org / 13 0
298
+ EOF
299
+ end
300
+ @cm.cookies_file = f.path
301
+ @cm.load_cookies
302
+ c0, c1, c2 = @cm.cookies
303
+ assert_equal('"value"', c0.value)
304
+ assert_equal('""', c1.value)
305
+ assert_equal('', c2.value)
306
+ assert_equal('key1="\\"value\\""; key2="\\"\\""; key3=', @cm.find(uri))
410
307
  end
411
308
 
412
309
  end