mechanize 1.0.1.beta.20110107104205 → 2.0.pre.1

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of mechanize might be problematic. Click here for more details.

Files changed (89) hide show
  1. data.tar.gz.sig +2 -0
  2. data/{lib/mechanize/chain/post_connect_hook.rb → .gemtest} +0 -0
  3. data/CHANGELOG.rdoc +51 -6
  4. data/EXAMPLES.rdoc +5 -3
  5. data/GUIDE.rdoc +72 -32
  6. data/LICENSE.rdoc +20 -340
  7. data/Manifest.txt +20 -27
  8. data/README.rdoc +12 -9
  9. data/Rakefile +5 -2
  10. data/examples/spider.rb +13 -2
  11. data/lib/mechanize.rb +545 -267
  12. data/lib/mechanize/content_type_error.rb +1 -1
  13. data/lib/mechanize/cookie.rb +72 -65
  14. data/lib/mechanize/cookie_jar.rb +197 -148
  15. data/lib/mechanize/element_matcher.rb +35 -0
  16. data/lib/mechanize/file.rb +3 -1
  17. data/lib/mechanize/file_connection.rb +17 -0
  18. data/lib/mechanize/file_request.rb +26 -0
  19. data/lib/mechanize/file_response.rb +61 -47
  20. data/lib/mechanize/form.rb +57 -58
  21. data/lib/mechanize/form/image_button.rb +2 -3
  22. data/lib/mechanize/form/multi_select_list.rb +71 -55
  23. data/lib/mechanize/form/select_list.rb +34 -62
  24. data/lib/mechanize/monkey_patch.rb +13 -11
  25. data/lib/mechanize/page.rb +277 -270
  26. data/lib/mechanize/page/image.rb +6 -2
  27. data/lib/mechanize/redirect_limit_reached_error.rb +1 -1
  28. data/lib/mechanize/redirect_not_get_or_head_error.rb +1 -1
  29. data/lib/mechanize/response_code_error.rb +3 -3
  30. data/lib/mechanize/unsupported_scheme_error.rb +1 -1
  31. data/lib/mechanize/uri_resolver.rb +82 -0
  32. data/lib/mechanize/util.rb +76 -60
  33. data/test/helper.rb +35 -5
  34. data/test/htdocs/dir with spaces/foo.html +1 -0
  35. data/test/htdocs/rails_3_encoding_hack_form_test.html +27 -0
  36. data/test/htdocs/tc_base_images.html +10 -0
  37. data/test/htdocs/tc_images.html +8 -0
  38. data/test/htdocs/test_click.html +11 -0
  39. data/test/servlets.rb +3 -2
  40. data/test/test_authenticate.rb +5 -5
  41. data/test/test_errors.rb +8 -8
  42. data/test/test_follow_meta.rb +4 -4
  43. data/test/test_form_as_hash.rb +4 -4
  44. data/test/test_forms.rb +3 -7
  45. data/test/test_hash_api.rb +2 -2
  46. data/test/test_headers.rb +1 -1
  47. data/test/test_images.rb +19 -0
  48. data/test/test_mech.rb +6 -6
  49. data/test/test_mechanize.rb +687 -0
  50. data/test/{test_cookie_class.rb → test_mechanize_cookie.rb} +52 -45
  51. data/test/test_mechanize_cookie_jar.rb +400 -0
  52. data/test/test_mechanize_file.rb +7 -1
  53. data/test/test_mechanize_file_request.rb +19 -0
  54. data/test/test_mechanize_file_response.rb +21 -0
  55. data/test/test_mechanize_form_image_button.rb +12 -0
  56. data/test/test_mechanize_page.rb +165 -0
  57. data/test/test_mechanize_uri_resolver.rb +29 -0
  58. data/test/{test_util.rb → test_mechanize_util.rb} +1 -1
  59. data/test/test_multi_select.rb +12 -0
  60. data/test/test_post_form.rb +7 -0
  61. data/test/test_redirect_verb_handling.rb +6 -6
  62. data/test/test_scheme.rb +0 -7
  63. data/test/test_verbs.rb +3 -3
  64. metadata +106 -72
  65. metadata.gz.sig +0 -0
  66. data/lib/mechanize/chain.rb +0 -36
  67. data/lib/mechanize/chain/auth_headers.rb +0 -78
  68. data/lib/mechanize/chain/body_decoding_handler.rb +0 -50
  69. data/lib/mechanize/chain/connection_resolver.rb +0 -28
  70. data/lib/mechanize/chain/custom_headers.rb +0 -21
  71. data/lib/mechanize/chain/handler.rb +0 -9
  72. data/lib/mechanize/chain/header_resolver.rb +0 -48
  73. data/lib/mechanize/chain/parameter_resolver.rb +0 -22
  74. data/lib/mechanize/chain/pre_connect_hook.rb +0 -20
  75. data/lib/mechanize/chain/request_resolver.rb +0 -31
  76. data/lib/mechanize/chain/response_body_parser.rb +0 -36
  77. data/lib/mechanize/chain/response_header_handler.rb +0 -34
  78. data/lib/mechanize/chain/response_reader.rb +0 -39
  79. data/lib/mechanize/chain/ssl_resolver.rb +0 -40
  80. data/lib/mechanize/chain/uri_resolver.rb +0 -75
  81. data/test/chain/test_argument_validator.rb +0 -14
  82. data/test/chain/test_auth_headers.rb +0 -25
  83. data/test/chain/test_custom_headers.rb +0 -18
  84. data/test/chain/test_header_resolver.rb +0 -27
  85. data/test/chain/test_parameter_resolver.rb +0 -35
  86. data/test/chain/test_request_resolver.rb +0 -29
  87. data/test/chain/test_response_reader.rb +0 -24
  88. data/test/test_cookie_jar.rb +0 -324
  89. data/test/test_page.rb +0 -124
@@ -1,40 +0,0 @@
1
- class Mechanize
2
- class Chain
3
- class SSLResolver
4
- include Mechanize::Handler
5
-
6
- def initialize(ca_file, verify_callback, cert, key, pass)
7
- @ca_file = ca_file
8
- @verify_callback = verify_callback
9
- @cert = cert
10
- @key = key
11
- @pass = pass
12
- end
13
-
14
- def handle(ctx, params)
15
- uri = params[:uri]
16
- http_obj = params[:connection]
17
-
18
- ssl = nil
19
- if http_obj.instance_variable_defined?(:@ssl_context)
20
- ssl = http_obj.instance_variable_get(:@ssl_context)
21
- end
22
-
23
- if uri.scheme == 'https' && ! http_obj.started? && (ssl.nil? || ! ssl.frozen?)
24
- http_obj.use_ssl = true
25
- http_obj.verify_mode = OpenSSL::SSL::VERIFY_NONE
26
- if @ca_file
27
- http_obj.ca_file = @ca_file
28
- http_obj.verify_mode = OpenSSL::SSL::VERIFY_PEER
29
- http_obj.verify_callback = @verify_callback if @verify_callback
30
- end
31
- if @cert && @key
32
- http_obj.cert = OpenSSL::X509::Certificate.new(::File.read(@cert))
33
- http_obj.key = OpenSSL::PKey::RSA.new(::File.read(@key), @pass)
34
- end
35
- end
36
- super
37
- end
38
- end
39
- end
40
- end
@@ -1,75 +0,0 @@
1
- class Mechanize
2
- class Chain
3
- class URIResolver
4
- include Mechanize::Handler
5
-
6
- def initialize(scheme_handlers)
7
- @scheme_handlers = scheme_handlers
8
- end
9
-
10
- def handle(ctx, params)
11
- raise ArgumentError.new('uri must be specified') unless params[:uri]
12
- params[:uri] = params[:uri].dup if params[:uri].is_a?(URI)
13
- uri = params[:uri]
14
- referer = params[:referer]
15
- unless uri.is_a?(URI)
16
- uri = uri.to_s.strip.gsub(/[^#{0.chr}-#{126.chr}]/) { |match|
17
- if RUBY_VERSION >= "1.9.0"
18
- CGI.escape(match)
19
- else
20
- sprintf('%%%X', match.unpack($KCODE == 'UTF8' ? 'U' : 'c')[0])
21
- end
22
- }
23
-
24
- escaped_uri = Util.html_unescape(
25
- uri.split(/(?:%[0-9A-Fa-f]{2})+|#/).zip(
26
- uri.scan(/(?:%[0-9A-Fa-f]{2})+|#/)
27
- ).map { |x,y|
28
- "#{WEBrick::HTTPUtils.escape(x)}#{y}"
29
- }.join('')
30
- )
31
-
32
- begin
33
- uri = URI.parse(escaped_uri)
34
- rescue
35
- uri = URI.parse(WEBrick::HTTPUtils.escape(escaped_uri))
36
- end
37
-
38
- end
39
- uri = @scheme_handlers[
40
- uri.relative? ? 'relative' : uri.scheme.downcase
41
- ].call(uri, params[:referer])
42
-
43
- if params[:referer] && params[:referer].uri
44
- if uri.path.length == 0 && uri.relative?
45
- uri.path = params[:referer].uri.path
46
- end
47
- end
48
-
49
- uri.path = '/' if uri.path.length == 0
50
-
51
- if uri.relative?
52
- raise 'need absolute URL' unless referer && referer.uri
53
- base = nil
54
- if referer.respond_to?(:bases) && referer.parser
55
- base = referer.bases.last
56
- end
57
-
58
- uri = ((base && base.uri && base.uri.absolute?) ?
59
- base.uri :
60
- referer.uri) + uri
61
- uri = referer.uri + uri
62
- # Strip initial "/.." bits from the path
63
- uri.path.sub!(/^(\/\.\.)+(?=\/)/, '')
64
- end
65
-
66
- unless ['http', 'https', 'file'].include?(uri.scheme.downcase)
67
- raise "unsupported scheme: #{uri.scheme}"
68
- end
69
- params[:uri] = uri
70
-
71
- super
72
- end
73
- end
74
- end
75
- end
@@ -1,14 +0,0 @@
1
- require "helper"
2
-
3
- class TestURIResolver < Test::Unit::TestCase
4
- def test_handle
5
- v = Mechanize::Chain.new([
6
- Mechanize::Chain::URIResolver.new(Hash.new { |h,k|
7
- h[k] = lambda { |u,r| u }
8
- })
9
- ])
10
- assert_raises(ArgumentError) { v.handle({}) }
11
- assert_nothing_raised { v.handle({:uri => 'http://google.com/'}) }
12
- assert_raises(RuntimeError) { v.handle({:uri => 'google'}) }
13
- end
14
- end
@@ -1,25 +0,0 @@
1
- require "helper"
2
-
3
- class TestAuthHeaders < Test::Unit::TestCase
4
- def test_auth
5
- url = URI.parse('http://www.anthonychaves.net/tests.xml')
6
- digest = %Q!Digest realm="www.anthonychaves.net", qop="auth", algorithm=MD5, nonce="MTI0NTEyMTYyNjo0ZTY2MjhlZWMyZmM1ZjA0M2Y1Njc1MGU0YTA2MWY5OQ==", opaque="9f455d4e71e8d46a6d3aaef8bf8b0d9e"!
7
- v = Mechanize::Chain.new([
8
- Mechanize::Chain::AuthHeaders.new({(url.host) => :digest}, "anthony", "password", digest)
9
- ])
10
-
11
- hash = {
12
- :request => Net::HTTP::Get.new(url.request_uri),
13
- :uri => url
14
- }
15
- v.handle(hash)
16
- actual_authorization = hash[:request]['Authorization']
17
- # The chain gave our request an Authorization header with client-generated values and derivatives.
18
- # They should be scrubbed before comparing to the expected result because they change
19
- # on each invokation
20
- actual_authorization.gsub!(/cnonce=\"\w+?\"/, "cnonce=\"scrubbed_cnonce\"").gsub!(/response=\"\w+?\"/, "response=\"scrubbed_response\"")
21
-
22
- expected_authorization = %Q!Digest username="anthony", qop=auth, uri="/tests.xml", algorithm="MD5", opaque="9f455d4e71e8d46a6d3aaef8bf8b0d9e", nonce="MTI0NTEyMTYyNjo0ZTY2MjhlZWMyZmM1ZjA0M2Y1Njc1MGU0YTA2MWY5OQ==", realm="www.anthonychaves.net", nc=00000001, cnonce="scrubbed_cnonce", response="scrubbed_response"!
23
- assert_equal(expected_authorization, actual_authorization)
24
- end
25
- end
@@ -1,18 +0,0 @@
1
- require "helper"
2
-
3
- class TestCustomHeaders < Test::Unit::TestCase
4
- def test_handle
5
- v = Mechanize::Chain.new([
6
- Mechanize::Chain::CustomHeaders.new
7
- ])
8
- url = URI.parse('http://tenderlovemaking.com/')
9
- hash = {
10
- :request => Net::HTTP::Get.new(url.request_uri),
11
- :headers => { 'Content-Length' => 300 }
12
- }
13
- v.handle(hash)
14
- headers = hash[:request].to_hash
15
- assert(headers.key?('content-length'))
16
- assert_equal([300], headers['content-length'])
17
- end
18
- end
@@ -1,27 +0,0 @@
1
- require "helper"
2
-
3
- class TestHeaderResolver < Test::Unit::TestCase
4
- def setup
5
- @chain = Mechanize::Chain.new([
6
- Mechanize::Chain::HeaderResolver.new(
7
- Mechanize::CookieJar.new,
8
- 'foobar',
9
- true,
10
- {
11
- 'hello' => 'world',
12
- 'Content-Type' => 'utf-8'
13
- }
14
- )
15
- ])
16
- end
17
-
18
- def test_handle
19
- hash = {
20
- :request => {},
21
- :uri => URI.parse('http://google.com/')
22
- }
23
- @chain.handle(hash)
24
- assert_equal 'world', hash[:request]['hello']
25
- assert_equal 'utf-8', hash[:request]['Content-Type']
26
- end
27
- end
@@ -1,35 +0,0 @@
1
- require "helper"
2
-
3
- class TestParameterResolver < Test::Unit::TestCase
4
- def test_handle_get
5
- v = Mechanize::Chain.new([
6
- Mechanize::Chain::ParameterResolver.new
7
- ])
8
- hash = {
9
- :uri => URI.parse('http://google.com/'),
10
- :params => { :q => 'hello' },
11
- :verb => :get
12
- }
13
- assert_nothing_raised {
14
- v.handle(hash)
15
- }
16
- assert_equal('q=hello', hash[:uri].query)
17
- assert_nil(hash[:params])
18
- end
19
-
20
- def test_handle_post
21
- v = Mechanize::Chain.new([
22
- Mechanize::Chain::ParameterResolver.new
23
- ])
24
- hash = {
25
- :uri => URI.parse('http://google.com/'),
26
- :params => { :q => 'hello' },
27
- :verb => :post
28
- }
29
- assert_nothing_raised {
30
- v.handle(hash)
31
- }
32
- assert_equal('', hash[:uri].query.to_s)
33
- assert_equal({ :q => 'hello' }, hash[:params])
34
- end
35
- end
@@ -1,29 +0,0 @@
1
- require "helper"
2
-
3
- class TestRequestResolver < Test::Unit::TestCase
4
- def test_handle_get
5
- v = Mechanize::Chain.new([
6
- Mechanize::Chain::RequestResolver.new
7
- ])
8
- hash = {
9
- :uri => URI.parse('http://google.com'),
10
- :verb => :get
11
- }
12
- assert_nothing_raised { v.handle(hash) }
13
- assert_kind_of(Net::HTTP::Get, hash[:request])
14
- assert_equal('/', hash[:request].path)
15
- end
16
-
17
- def test_handle_post
18
- v = Mechanize::Chain.new([
19
- Mechanize::Chain::RequestResolver.new
20
- ])
21
- hash = {
22
- :uri => URI.parse('http://google.com'),
23
- :verb => :post
24
- }
25
- assert_nothing_raised { v.handle(hash) }
26
- assert_kind_of(Net::HTTP::Post, hash[:request])
27
- assert_equal('/', hash[:request].path)
28
- end
29
- end
@@ -1,24 +0,0 @@
1
- require "helper"
2
-
3
- class TestResponseReader < Test::Unit::TestCase
4
- def test_handle_bad_response_code
5
- response = Object.new
6
- class << response
7
- def read_body
8
- end
9
- def code; 999; end
10
- end
11
-
12
- v = Mechanize::Chain.new([
13
- Mechanize::Chain::ResponseReader.new(response)
14
- ])
15
- assert_raises(Mechanize::ResponseCodeError) {
16
- begin
17
- v.handle({})
18
- rescue Exception => x
19
- assert_equal(999, x.response_code)
20
- raise x
21
- end
22
- }
23
- end
24
- end
@@ -1,324 +0,0 @@
1
- require "helper"
2
-
3
- class CookieJarTest < Test::Unit::TestCase
4
- def cookie_values(options = {})
5
- {
6
- :name => 'Foo',
7
- :value => 'Bar',
8
- :path => '/',
9
- :expires => Time.now + (10 * 86400),
10
- :domain => 'rubyforge.org'
11
- }.merge(options)
12
- end
13
-
14
- def cookie_from_hash(hash)
15
- c = Mechanize::Cookie.new(hash[:name], hash[:value])
16
- hash.each { |k,v|
17
- next if k == :name || k == :value
18
- c.send("#{k}=", v)
19
- }
20
- c
21
- end
22
-
23
- def test_two_cookies_same_domain_and_name_different_paths
24
- url = URI.parse('http://rubyforge.org/')
25
-
26
- jar = Mechanize::CookieJar.new
27
- cookie = cookie_from_hash(cookie_values)
28
- jar.add(url, cookie)
29
- jar.add(url, cookie_from_hash(cookie_values(:path => '/onetwo')))
30
-
31
- assert_equal(1, jar.cookies(url).length)
32
- assert_equal 2, jar.cookies(URI.parse('http://rubyforge.org/onetwo')).length
33
- end
34
-
35
- def test_domain_case
36
- url = URI.parse('http://rubyforge.org/')
37
-
38
- jar = Mechanize::CookieJar.new
39
- assert_equal(0, jar.cookies(url).length)
40
-
41
- # Add one cookie with an expiration date in the future
42
- cookie = cookie_from_hash(cookie_values)
43
- jar.add(url, cookie)
44
- assert_equal(1, jar.cookies(url).length)
45
-
46
- jar.add(url, cookie_from_hash(
47
- cookie_values(:domain => 'RuByForge.Org', :name => 'aaron')))
48
-
49
- assert_equal(2, jar.cookies(url).length)
50
-
51
- url2 = URI.parse('http://RuByFoRgE.oRg/')
52
- assert_equal(2, jar.cookies(url2).length)
53
- end
54
-
55
- def test_empty_value
56
- values = cookie_values(:value => "")
57
- url = URI.parse('http://rubyforge.org/')
58
-
59
- jar = Mechanize::CookieJar.new
60
- assert_equal(0, jar.cookies(url).length)
61
-
62
- # Add one cookie with an expiration date in the future
63
- cookie = cookie_from_hash(values)
64
- jar.add(url, cookie)
65
- assert_equal(1, jar.cookies(url).length)
66
-
67
- jar.add(url, cookie_from_hash( values.merge( :domain => 'RuByForge.Org',
68
- :name => 'aaron'
69
- ) ) )
70
-
71
- assert_equal(2, jar.cookies(url).length)
72
-
73
- url2 = URI.parse('http://RuByFoRgE.oRg/')
74
- assert_equal(2, jar.cookies(url2).length)
75
- end
76
-
77
- def test_add_future_cookies
78
- url = URI.parse('http://rubyforge.org/')
79
-
80
- jar = Mechanize::CookieJar.new
81
- assert_equal(0, jar.cookies(url).length)
82
-
83
- # Add one cookie with an expiration date in the future
84
- cookie = cookie_from_hash(cookie_values)
85
- jar.add(url, cookie)
86
- assert_equal(1, jar.cookies(url).length)
87
-
88
- # Add the same cookie, and we should still only have one
89
- jar.add(url, cookie_from_hash(cookie_values))
90
- assert_equal(1, jar.cookies(url).length)
91
-
92
- # Make sure we can get the cookie from different paths
93
- assert_equal(1, jar.cookies(URI.parse('http://rubyforge.org/login')).length)
94
-
95
- # Make sure we can't get the cookie from different domains
96
- assert_equal(0, jar.cookies(URI.parse('http://google.com/')).length)
97
- end
98
-
99
- def test_add_multiple_cookies
100
- url = URI.parse('http://rubyforge.org/')
101
-
102
- jar = Mechanize::CookieJar.new
103
- assert_equal(0, jar.cookies(url).length)
104
-
105
- # Add one cookie with an expiration date in the future
106
- cookie = cookie_from_hash(cookie_values)
107
- jar.add(url, cookie)
108
- assert_equal(1, jar.cookies(url).length)
109
-
110
- # Add the same cookie, and we should still only have one
111
- jar.add(url, cookie_from_hash(cookie_values(:name => 'Baz')))
112
- assert_equal(2, jar.cookies(url).length)
113
-
114
- # Make sure we can get the cookie from different paths
115
- assert_equal(2, jar.cookies(URI.parse('http://rubyforge.org/login')).length)
116
-
117
- # Make sure we can't get the cookie from different domains
118
- assert_equal(0, jar.cookies(URI.parse('http://google.com/')).length)
119
- end
120
-
121
- def test_clear_cookies
122
- url = URI.parse('http://rubyforge.org/')
123
-
124
- jar = Mechanize::CookieJar.new
125
- assert_equal(0, jar.cookies(url).length)
126
-
127
- # Add one cookie with an expiration date in the future
128
- cookie = cookie_from_hash(cookie_values)
129
- jar.add(url, cookie)
130
- jar.add(url, cookie_from_hash(cookie_values(:name => 'Baz')))
131
- assert_equal(2, jar.cookies(url).length)
132
-
133
- jar.clear!
134
-
135
- assert_equal(0, jar.cookies(url).length)
136
- end
137
-
138
- def test_save_cookies
139
- url = URI.parse('http://rubyforge.org/')
140
-
141
- jar = Mechanize::CookieJar.new
142
- assert_equal(0, jar.cookies(url).length)
143
-
144
- # Add one cookie with an expiration date in the future
145
- cookie = cookie_from_hash(cookie_values)
146
- jar.add(url, cookie)
147
- jar.add(url, cookie_from_hash(cookie_values(:name => 'Baz')))
148
- assert_equal(2, jar.cookies(url).length)
149
-
150
- jar.save_as("cookies.yml")
151
- jar.clear!
152
- assert_equal(0, jar.cookies(url).length)
153
-
154
- jar.load("cookies.yml")
155
- assert_equal(2, jar.cookies(url).length)
156
- FileUtils.rm("cookies.yml")
157
- end
158
-
159
- def test_expire_cookies
160
- url = URI.parse('http://rubyforge.org/')
161
-
162
- jar = Mechanize::CookieJar.new
163
- assert_equal(0, jar.cookies(url).length)
164
-
165
- # Add one cookie with an expiration date in the future
166
- cookie = cookie_from_hash(cookie_values)
167
- jar.add(url, cookie)
168
- assert_equal(1, jar.cookies(url).length)
169
-
170
- # Add a second cookie
171
- jar.add(url, cookie_from_hash(cookie_values(:name => 'Baz')))
172
- assert_equal(2, jar.cookies(url).length)
173
-
174
- # Make sure we can get the cookie from different paths
175
- assert_equal(2, jar.cookies(URI.parse('http://rubyforge.org/login')).length)
176
-
177
- # Expire the first cookie
178
- jar.add(url, cookie_from_hash(
179
- cookie_values(:expires => Time.now - (10 * 86400))))
180
- assert_equal(1, jar.cookies(url).length)
181
-
182
- # Expire the second cookie
183
- jar.add(url, cookie_from_hash(
184
- cookie_values( :name => 'Baz', :expires => Time.now - (10 * 86400))))
185
- assert_equal(0, jar.cookies(url).length)
186
- end
187
-
188
- def test_session_cookies
189
- values = cookie_values(:expires => nil)
190
- url = URI.parse('http://rubyforge.org/')
191
-
192
- jar = Mechanize::CookieJar.new
193
- assert_equal(0, jar.cookies(url).length)
194
-
195
- # Add one cookie with an expiration date in the future
196
- cookie = cookie_from_hash(values)
197
- jar.add(url, cookie)
198
- assert_equal(1, jar.cookies(url).length)
199
-
200
- # Add a second cookie
201
- jar.add(url, cookie_from_hash(values.merge(:name => 'Baz')))
202
- assert_equal(2, jar.cookies(url).length)
203
-
204
- # Make sure we can get the cookie from different paths
205
- assert_equal(2, jar.cookies(URI.parse('http://rubyforge.org/login')).length)
206
-
207
- # Expire the first cookie
208
- jar.add(url, cookie_from_hash(values.merge(:expires => Time.now - (10 * 86400))))
209
- assert_equal(1, jar.cookies(url).length)
210
-
211
- # Expire the second cookie
212
- jar.add(url, cookie_from_hash(
213
- values.merge(:name => 'Baz', :expires => Time.now - (10 * 86400))))
214
- assert_equal(0, jar.cookies(url).length)
215
-
216
- # When given a URI with a blank path, CookieJar#cookies should return
217
- # cookies with the path '/':
218
- url = URI.parse('http://rubyforge.org')
219
- assert_equal '', url.path
220
- assert_equal(0, jar.cookies(url).length)
221
- # Now add a cookie with the path set to '/':
222
- jar.add(url, cookie_from_hash(values.merge( :name => 'has_root_path',
223
- :path => '/')))
224
- assert_equal(1, jar.cookies(url).length)
225
- end
226
-
227
- def test_paths
228
- values = cookie_values(:path => "/login", :expires => nil)
229
- url = URI.parse('http://rubyforge.org/login')
230
-
231
- jar = Mechanize::CookieJar.new
232
- assert_equal(0, jar.cookies(url).length)
233
-
234
- # Add one cookie with an expiration date in the future
235
- cookie = cookie_from_hash(values)
236
- jar.add(url, cookie)
237
- assert_equal(1, jar.cookies(url).length)
238
-
239
- # Add a second cookie
240
- jar.add(url, cookie_from_hash(values.merge( :name => 'Baz' )))
241
- assert_equal(2, jar.cookies(url).length)
242
-
243
- # Make sure we don't get the cookie in a different path
244
- assert_equal(0, jar.cookies(URI.parse('http://rubyforge.org/hello')).length)
245
- assert_equal(0, jar.cookies(URI.parse('http://rubyforge.org/')).length)
246
-
247
- # Expire the first cookie
248
- jar.add(url, cookie_from_hash(values.merge( :expires => Time.now - (10 * 86400))))
249
- assert_equal(1, jar.cookies(url).length)
250
-
251
- # Expire the second cookie
252
- jar.add(url, cookie_from_hash(values.merge( :name => 'Baz',
253
- :expires => Time.now - (10 * 86400))))
254
- assert_equal(0, jar.cookies(url).length)
255
- end
256
-
257
-
258
- def test_save_and_read_cookiestxt
259
- url = URI.parse('http://rubyforge.org/')
260
-
261
- jar = Mechanize::CookieJar.new
262
- assert_equal(0, jar.cookies(url).length)
263
-
264
- # Add one cookie with an expiration date in the future
265
- cookie = cookie_from_hash(cookie_values)
266
- jar.add(url, cookie)
267
- jar.add(url, cookie_from_hash(cookie_values(:name => 'Baz')))
268
- assert_equal(2, jar.cookies(url).length)
269
-
270
- jar.save_as("cookies.txt", :cookiestxt)
271
- jar.clear!
272
- assert_equal(0, jar.cookies(url).length)
273
-
274
- jar.load("cookies.txt", :cookiestxt)
275
- assert_equal(2, jar.cookies(url).length)
276
-
277
- FileUtils.rm("cookies.txt")
278
- end
279
-
280
- def test_save_and_read_cookiestxt_with_session_cookies
281
- url = URI.parse('http://rubyforge.org/')
282
-
283
- jar = Mechanize::CookieJar.new
284
-
285
- jar.add(url, cookie_from_hash(cookie_values(:expires => nil)))
286
- jar.save_as("cookies.txt", :cookiestxt)
287
- jar.clear!
288
- assert_equal(0, jar.cookies(url).length)
289
-
290
- jar.load("cookies.txt", :cookiestxt)
291
- assert_equal(1, jar.cookies(url).length)
292
- assert_nil jar.cookies(url).first.expires
293
- FileUtils.rm("cookies.txt")
294
- end
295
-
296
- def test_save_and_read_expired_cookies
297
- url = URI.parse('http://rubyforge.org/')
298
-
299
- jar = Mechanize::CookieJar.new
300
- jar.jar['rubyforge.org'] = {}
301
-
302
- assert_nothing_raised do
303
- jar.add(url, cookie_from_hash(cookie_values))
304
- end
305
- end
306
-
307
- def test_ssl_cookies
308
- # thanks to michal "ocher" ochman for reporting the bug responsible for this test.
309
- values = cookie_values(:expires => nil)
310
- values_ssl = values.merge(:domain => "#{values[:domain]}:443")
311
- url = URI.parse('https://rubyforge.org/login')
312
-
313
- jar = Mechanize::CookieJar.new
314
- assert_equal(0, jar.cookies(url).length)
315
-
316
- cookie = cookie_from_hash(values)
317
- jar.add(url, cookie)
318
- assert_equal(1, jar.cookies(url).length, "did not handle SSL cookie")
319
-
320
- cookie = cookie_from_hash(values_ssl)
321
- jar.add(url, cookie)
322
- assert_equal(2, jar.cookies(url).length, "did not handle SSL cookie with :443")
323
- end
324
- end