mechanize 0.9.2 → 0.9.3

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.

@@ -0,0 +1,28 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), '..', "helper"))
2
+
3
+ class TestHeaderResolver < Test::Unit::TestCase
4
+ def setup
5
+ @chain = WWW::Mechanize::Chain.new([
6
+ WWW::Mechanize::Chain::HeaderResolver.new(
7
+ true,
8
+ 300,
9
+ WWW::Mechanize::CookieJar.new,
10
+ 'foobar',
11
+ {
12
+ 'hello' => 'world',
13
+ 'Content-Type' => 'utf-8'
14
+ }
15
+ )
16
+ ])
17
+ end
18
+
19
+ def test_handle
20
+ hash = {
21
+ :request => {},
22
+ :uri => URI.parse('http://google.com/')
23
+ }
24
+ @chain.handle(hash)
25
+ assert_equal 'world', hash[:request]['hello']
26
+ assert_equal 'utf-8', hash[:request]['Content-Type']
27
+ end
28
+ end
@@ -48,6 +48,8 @@ class Net::HTTP
48
48
  '/infinite_redirect' => InfiniteRedirectTest,
49
49
  '/infinite_refresh' => InfiniteRefreshTest,
50
50
  '/redirect' => RedirectTest,
51
+ '/refresh_without_url' => RefreshWithoutUrl,
52
+ '/refresh_with_empty_url' => RefreshWithEmptyUrl,
51
53
  '/digest_auth' => DigestAuthServlet,
52
54
  '/verb' => VerbServlet,
53
55
  }
@@ -164,6 +164,32 @@ class InfiniteRefreshTest < WEBrick::HTTPServlet::AbstractServlet
164
164
  end
165
165
  end
166
166
 
167
+ class RefreshWithoutUrl < WEBrick::HTTPServlet::AbstractServlet
168
+ @@count = 0
169
+ def do_GET(req, res)
170
+ res['Content-Type'] = "text/html"
171
+ @@count += 1
172
+ if @@count > 1
173
+ res['Refresh'] = "0; url=http://localhost/index.html";
174
+ else
175
+ res['Refresh'] = "0";
176
+ end
177
+ end
178
+ end
179
+
180
+ class RefreshWithEmptyUrl < WEBrick::HTTPServlet::AbstractServlet
181
+ @@count = 0
182
+ def do_GET(req, res)
183
+ res['Content-Type'] = "text/html"
184
+ @@count += 1
185
+ if @@count > 1
186
+ res['Refresh'] = "0; url=http://localhost/index.html";
187
+ else
188
+ res['Refresh'] = "0; url=";
189
+ end
190
+ end
191
+ end
192
+
167
193
  class InfiniteRedirectTest < WEBrick::HTTPServlet::AbstractServlet
168
194
  def do_GET(req, res)
169
195
  res['Content-Type'] = req.query['ct'] || "text/html"
@@ -43,7 +43,7 @@ class CookieClassTest < Test::Unit::TestCase
43
43
  "22-AUG-1993 12:59am",
44
44
  "22-AUG-1993 12:59 PM",
45
45
  #"Friday, August 04, 1995 3:54 PM",
46
- "06/21/95 04:24:34 PM",
46
+ #"06/21/95 04:24:34 PM",
47
47
  #"20/06/95 21:07",
48
48
  "95-06-08 19:32:48 EDT",
49
49
  ]
@@ -62,9 +62,9 @@ class CookieClassTest < Test::Unit::TestCase
62
62
  def test_parse_weird_cookie
63
63
  cookie = 'n/a, ASPSESSIONIDCSRRQDQR=FBLDGHPBNDJCPCGNCPAENELB; path=/'
64
64
  url = URI.parse('http://www.searchinnovation.com/')
65
- WWW::Mechanize::Cookie.parse(url, cookie) { |cookie|
66
- assert_equal('ASPSESSIONIDCSRRQDQR', cookie.name)
67
- assert_equal('FBLDGHPBNDJCPCGNCPAENELB', cookie.value)
65
+ WWW::Mechanize::Cookie.parse(url, cookie) { |c|
66
+ assert_equal('ASPSESSIONIDCSRRQDQR', c.name)
67
+ assert_equal('FBLDGHPBNDJCPCGNCPAENELB', c.value)
68
68
  }
69
69
  end
70
70
 
@@ -10,6 +10,25 @@ class CookieJarTest < Test::Unit::TestCase
10
10
  c
11
11
  end
12
12
 
13
+ def test_two_cookies_same_domain_and_name_different_paths
14
+ values = { :name => 'Foo',
15
+ :value => 'Bar',
16
+ :path => '/',
17
+ :expires => Time.now + (10 * 86400),
18
+ :domain => 'rubyforge.org'
19
+ }
20
+
21
+ url = URI.parse('http://rubyforge.org/')
22
+
23
+ jar = WWW::Mechanize::CookieJar.new
24
+ cookie = cookie_from_hash(values)
25
+ jar.add(url, cookie)
26
+ jar.add(url, cookie_from_hash(values.merge(:path => '/onetwo')))
27
+
28
+ assert_equal(1, jar.cookies(url).length)
29
+ assert_equal 2, jar.cookies(URI.parse('http://rubyforge.org/onetwo')).length
30
+ end
31
+
13
32
  def test_domain_case
14
33
  values = { :name => 'Foo',
15
34
  :value => 'Bar',
@@ -30,7 +49,7 @@ class CookieJarTest < Test::Unit::TestCase
30
49
  jar.add(url, cookie_from_hash( values.merge( :domain => 'RuByForge.Org',
31
50
  :name => 'aaron'
32
51
  ) ) )
33
-
52
+
34
53
  assert_equal(2, jar.cookies(url).length)
35
54
 
36
55
  url2 = URI.parse('http://RuByFoRgE.oRg/')
@@ -57,7 +76,7 @@ class CookieJarTest < Test::Unit::TestCase
57
76
  jar.add(url, cookie_from_hash( values.merge( :domain => 'RuByForge.Org',
58
77
  :name => 'aaron'
59
78
  ) ) )
60
-
79
+
61
80
  assert_equal(2, jar.cookies(url).length)
62
81
 
63
82
  url2 = URI.parse('http://RuByFoRgE.oRg/')
@@ -240,11 +259,11 @@ class CookieJarTest < Test::Unit::TestCase
240
259
  # When given a URI with a blank path, CookieJar#cookies should return
241
260
  # cookies with the path '/':
242
261
  url = URI.parse('http://rubyforge.org')
243
- assert_equal '', url.path
244
- assert_equal(0, jar.cookies(url).length)
262
+ assert_equal '', url.path
263
+ assert_equal(0, jar.cookies(url).length)
245
264
  # Now add a cookie with the path set to '/':
246
- jar.add(url, cookie_from_hash(values.merge( :name => 'has_root_path',
247
- :path => '/')))
265
+ jar.add(url, cookie_from_hash(values.merge( :name => 'has_root_path',
266
+ :path => '/')))
248
267
  assert_equal(1, jar.cookies(url).length)
249
268
  end
250
269
 
@@ -282,8 +301,8 @@ class CookieJarTest < Test::Unit::TestCase
282
301
  :expires => Time.now - (10 * 86400))))
283
302
  assert_equal(0, jar.cookies(url).length)
284
303
  end
285
-
286
-
304
+
305
+
287
306
  def test_save_and_read_cookiestxt
288
307
  values = { :name => 'Foo',
289
308
  :value => 'Bar',
@@ -301,7 +320,7 @@ class CookieJarTest < Test::Unit::TestCase
301
320
  jar.add(url, cookie)
302
321
  jar.add(url, cookie_from_hash(values.merge( :name => 'Baz' )))
303
322
  assert_equal(2, jar.cookies(url).length)
304
-
323
+
305
324
  jar.save_as("cookies.txt", :cookiestxt)
306
325
  jar.clear!
307
326
  assert_equal(0, jar.cookies(url).length)
@@ -11,6 +11,17 @@ class FollowMetaTest < Test::Unit::TestCase
11
11
  assert_equal(1, page.meta.length)
12
12
  end
13
13
 
14
+ def test_meta_refresh_does_not_send_referer
15
+ @agent.follow_meta_refresh = true
16
+ requests = []
17
+ @agent.pre_connect_hooks << lambda { |params|
18
+ requests << params[:request]
19
+ }
20
+
21
+ page = @agent.get('http://localhost/tc_follow_meta.html')
22
+ assert_nil requests[1]['referer']
23
+ end
24
+
14
25
  def test_follow_meta_if_set
15
26
  @agent.follow_meta_refresh = true
16
27
 
@@ -22,6 +33,34 @@ class FollowMetaTest < Test::Unit::TestCase
22
33
  assert_equal('http://localhost/index.html', page.uri.to_s)
23
34
  assert_equal('http://localhost/index.html', @agent.history.last.uri.to_s)
24
35
  end
36
+
37
+ def test_follow_meta_with_empty_url
38
+ @agent.follow_meta_refresh = true
39
+
40
+ page = @agent.get('http://localhost/refresh_with_empty_url')
41
+
42
+ assert_equal(3, @agent.history.length)
43
+ assert_equal('http://localhost/refresh_with_empty_url',
44
+ @agent.history[0].uri.to_s)
45
+ assert_equal('http://localhost/refresh_with_empty_url',
46
+ @agent.history[1].uri.to_s)
47
+ assert_equal('http://localhost/index.html', page.uri.to_s)
48
+ assert_equal('http://localhost/index.html', @agent.history.last.uri.to_s)
49
+ end
50
+
51
+ def test_follow_meta_without_url
52
+ @agent.follow_meta_refresh = true
53
+
54
+ page = @agent.get('http://localhost/refresh_without_url')
55
+
56
+ assert_equal(3, @agent.history.length)
57
+ assert_equal('http://localhost/refresh_without_url',
58
+ @agent.history[0].uri.to_s)
59
+ assert_equal('http://localhost/refresh_without_url',
60
+ @agent.history[1].uri.to_s)
61
+ assert_equal('http://localhost/index.html', page.uri.to_s)
62
+ assert_equal('http://localhost/index.html', @agent.history.last.uri.to_s)
63
+ end
25
64
 
26
65
  def test_always_follow_302
27
66
  @agent.follow_meta_refresh = false
@@ -23,17 +23,6 @@ class FormsMechTest < Test::Unit::TestCase
23
23
  assert_equal 'bar', headers['foo']
24
24
  end
25
25
 
26
- def test_submit_takes_arbirary_headers
27
- page = @agent.get('http://localhost:2000/form_no_action.html')
28
- assert form = page.forms.first
29
- form.action = '/http_headers'
30
- page = form.submit(nil, { 'foo' => 'bar' })
31
- headers = Hash[*(
32
- page.body.split("\n").map { |x| x.split('|') }.flatten
33
- )]
34
- assert_equal 'bar', headers['foo']
35
- end
36
-
37
26
  # Test submitting form with two fields of the same name
38
27
  def test_post_multival
39
28
  page = @agent.get("http://localhost/form_multival.html")
@@ -10,6 +10,13 @@ class TestMechMethods < Test::Unit::TestCase
10
10
  assert_equal('http://localhost/?foo=~2', page.uri.to_s)
11
11
  end
12
12
 
13
+ def test_parser_can_be_set
14
+ @agent.html_parser = {}
15
+ assert_raises(NoMethodError) {
16
+ @agent.get('http://localhost/?foo=~2').links
17
+ }
18
+ end
19
+
13
20
  def test_submit_takes_arbirary_headers
14
21
  page = @agent.get('http://localhost:2000/form_no_action.html')
15
22
  assert form = page.forms.first
@@ -91,12 +98,14 @@ class TestMechMethods < Test::Unit::TestCase
91
98
  }
92
99
  end
93
100
 
94
- def test_kcode_url
95
- $KCODE = 'u'
96
- page = @agent.get("http://localhost/?a=#{[0xd6].pack('U')}")
97
- assert_not_nil(page)
98
- assert_equal('http://localhost/?a=%D6', page.uri.to_s)
99
- $KCODE = 'NONE'
101
+ unless RUBY_VERSION >= '1.9.0'
102
+ def test_kcode_url
103
+ $KCODE = 'u'
104
+ page = @agent.get("http://localhost/?a=#{[0xd6].pack('U')}")
105
+ assert_not_nil(page)
106
+ assert_equal('http://localhost/?a=%D6', page.uri.to_s)
107
+ $KCODE = 'NONE'
108
+ end
100
109
  end
101
110
 
102
111
  def test_history
@@ -0,0 +1,65 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), "helper"))
2
+
3
+ class MetaTest < Test::Unit::TestCase
4
+ Meta = WWW::Mechanize::Page::Meta
5
+
6
+ #
7
+ # CONTENT_REGEXP test
8
+ #
9
+
10
+ def test_content_regexp
11
+ r = Meta::CONTENT_REGEXP
12
+
13
+ assert r =~ "0; url=http://localhost:8080/path"
14
+ assert_equal "0", $1
15
+ assert_equal "http://localhost:8080/path", $3
16
+
17
+ assert r =~ "100.001; url=http://localhost:8080/path"
18
+ assert_equal "100.001", $1
19
+ assert_equal "http://localhost:8080/path", $3
20
+
21
+ assert r =~ "0; url='http://localhost:8080/path'"
22
+ assert_equal "0", $1
23
+ assert_equal "http://localhost:8080/path", $3
24
+
25
+ assert r =~ "0; url=\"http://localhost:8080/path\""
26
+ assert_equal "0", $1
27
+ assert_equal "http://localhost:8080/path", $3
28
+
29
+ assert r =~ "0; url="
30
+ assert_equal "0", $1
31
+ assert_equal "", $3
32
+
33
+ assert r =~ "0"
34
+ assert_equal "0", $1
35
+ assert_equal nil, $3
36
+
37
+ assert r =~ " 0; "
38
+ assert_equal "0", $1
39
+ assert_equal nil, $3
40
+
41
+ assert r =~ "0; UrL=http://localhost:8080/path"
42
+ assert_equal "0", $1
43
+ assert_equal "http://localhost:8080/path", $3
44
+ end
45
+
46
+ #
47
+ # parse test
48
+ #
49
+
50
+ def test_parse_documentation
51
+ uri = URI.parse('http://current.com/')
52
+
53
+ assert_equal ['5', 'http://example.com/'], Meta.parse("5;url=http://example.com/", uri)
54
+ assert_equal ['5', 'http://current.com/'], Meta.parse("5;url=", uri)
55
+ assert_equal ['5', 'http://current.com/'], Meta.parse("5", uri)
56
+ assert_equal nil, Meta.parse("invalid content", uri)
57
+ end
58
+
59
+ def test_parse_returns_nil_if_no_delay_and_url_can_be_parsed
60
+ uri = URI.parse('http://current.com/')
61
+
62
+ assert_equal nil, Meta.parse("invalid content", uri)
63
+ assert_equal nil, Meta.parse("invalid content", uri) {|delay, url| 'not nil' }
64
+ end
65
+ end
@@ -7,17 +7,57 @@ class TestPage < Test::Unit::TestCase
7
7
  @agent = WWW::Mechanize.new
8
8
  end
9
9
 
10
+ def test_page_gets_charset_from_page
11
+ page = @agent.get("http://localhost/tc_charset.html")
12
+ assert_equal 'windows-1255', page.encoding
13
+ end
14
+
15
+ def test_double_semicolon
16
+ page = @agent.get("http://localhost/http_headers?content-disposition=#{CGI.escape('attachment;; filename=fooooo')}")
17
+ assert page.parser
18
+ end
19
+
10
20
  def test_broken_charset
11
21
  page = @agent.get("http://localhost/http_headers?content-type=#{CGI.escape('text/html; charset=akldsjfhaldjfksh')}")
12
22
  assert page.parser
13
23
  end
14
24
 
25
+ def test_mostly_broken_charset
26
+ page = @agent.get("http://localhost/http_headers?content-type=#{CGI.escape('text/html; charset=ISO_8859-1')}")
27
+ assert_equal 'ISO_8859-1', page.encoding
28
+ end
29
+
30
+ def test_another_mostly_broken_charset
31
+ page = @agent.get("http://localhost/http_headers?content-type=#{CGI.escape('text/html; charset=UTF8')}")
32
+ assert_equal 'UTF8', page.parser.encoding
33
+ assert_equal 'UTF8', page.encoding
34
+ end
35
+
15
36
  def test_upper_case_content_type
16
37
  page = @agent.get("http://localhost/http_headers?content-type=#{CGI.escape('text/HTML')}")
17
38
  assert_instance_of WWW::Mechanize::Page, page
18
39
  assert_equal 'text/HTML', page.content_type
19
40
  end
20
41
 
42
+ def test_encoding_override_before_parser_initialized
43
+ # document has a bad encoding information - windows-1255
44
+ page = @agent.get("http://localhost/tc_bad_charset.html")
45
+ # encoding is wrong, so user wants to force ISO-8859-2
46
+ page.encoding = 'ISO-8859-2'
47
+ assert_equal 'ISO-8859-2', page.encoding
48
+ end
49
+
50
+ def test_encoding_override_after_parser_was_initialized
51
+ # document has a bad encoding information - windows-1255
52
+ page = @agent.get("http://localhost/tc_bad_charset.html")
53
+ page.parser
54
+ # autodetection sets encoding to windows-1255
55
+ assert_equal 'windows-1255', page.encoding
56
+ # encoding is wrong, so user wants to force ISO-8859-2
57
+ page.encoding = 'ISO-8859-2'
58
+ assert_equal 'ISO-8859-2', page.encoding
59
+ end
60
+
21
61
  def test_page_gets_charset_sent_by_server
22
62
  page = @agent.get("http://localhost/http_headers?content-type=#{CGI.escape('text/html; charset=UTF-8')}")
23
63
  assert_equal 'UTF-8', page.encoding
@@ -48,6 +88,17 @@ class TestPage < Test::Unit::TestCase
48
88
  assert_equal(nil, page.title)
49
89
  end
50
90
 
91
+ def test_page_decoded_with_charset
92
+ page = WWW::Mechanize::Page.new(
93
+ URI.parse('http://tenderlovemaking.com/'),
94
+ { 'content-type' => 'text/html; charset=EUC-JP' },
95
+ '<html><body>hello</body></html>',
96
+ 400,
97
+ @agent
98
+ )
99
+ assert_equal 'EUC-JP', page.parser.encoding
100
+ end
101
+
51
102
  def test_find_form_with_hash
52
103
  page = @agent.get("http://localhost/tc_form_action.html")
53
104
  form = page.form(:name => 'post_form1')
@@ -65,3 +116,4 @@ class TestPage < Test::Unit::TestCase
65
116
  assert_equal(form, form_by_action)
66
117
  end
67
118
  end
119
+
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mechanize
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.2
4
+ version: 0.9.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Aaron Patterson
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2009-03-05 00:00:00 -08:00
13
+ date: 2009-06-08 00:00:00 -07:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
@@ -31,9 +31,14 @@ dependencies:
31
31
  requirements:
32
32
  - - ">="
33
33
  - !ruby/object:Gem::Version
34
- version: 1.9.0
34
+ version: 1.12.2
35
35
  version:
36
- description: The Mechanize library is used for automating interaction with websites. Mechanize automatically stores and sends cookies, follows redirects, can follow links, and submit forms. Form fields can be populated and submitted. Mechanize also keeps track of the sites that you have visited as a history.
36
+ description: |-
37
+ The Mechanize library is used for automating interaction with websites.
38
+ Mechanize automatically stores and sends cookies, follows redirects,
39
+ can follow links, and submit forms. Form fields can be populated and
40
+ submitted. Mechanize also keeps track of the sites that you have visited as
41
+ a history.
37
42
  email:
38
43
  - aaronp@rubyforge.org
39
44
  - mike.dalessio@gmail.com
@@ -220,7 +225,9 @@ files:
220
225
  - test/test_upload.rb
221
226
  - test/test_verbs.rb
222
227
  has_rdoc: true
223
- homepage: " http://mechanize.rubyforge.org/"
228
+ homepage: http://mechanize.rubyforge.org/
229
+ licenses: []
230
+
224
231
  post_install_message:
225
232
  rdoc_options:
226
233
  - --main
@@ -242,13 +249,14 @@ required_rubygems_version: !ruby/object:Gem::Requirement
242
249
  requirements: []
243
250
 
244
251
  rubyforge_project: mechanize
245
- rubygems_version: 1.3.1
252
+ rubygems_version: 1.3.3
246
253
  signing_key:
247
- specification_version: 2
254
+ specification_version: 3
248
255
  summary: Mechanize provides automated web-browsing
249
256
  test_files:
250
257
  - test/chain/test_argument_validator.rb
251
258
  - test/chain/test_custom_headers.rb
259
+ - test/chain/test_header_resolver.rb
252
260
  - test/chain/test_parameter_resolver.rb
253
261
  - test/chain/test_request_resolver.rb
254
262
  - test/chain/test_response_reader.rb
@@ -280,6 +288,7 @@ test_files:
280
288
  - test/test_links.rb
281
289
  - test/test_mech.rb
282
290
  - test/test_mechanize_file.rb
291
+ - test/test_meta.rb
283
292
  - test/test_multi_select.rb
284
293
  - test/test_no_attributes.rb
285
294
  - test/test_option.rb