mechanize 2.0.pre.1 → 2.0.pre.2
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.
- data.tar.gz.sig +2 -2
- data/CHANGELOG.rdoc +24 -2
- data/Manifest.txt +15 -19
- data/Rakefile +6 -3
- data/lib/mechanize.rb +168 -28
- data/lib/mechanize/form.rb +14 -2
- data/lib/mechanize/page.rb +43 -14
- data/lib/mechanize/page/link.rb +10 -0
- data/lib/mechanize/redirect_not_get_or_head_error.rb +2 -1
- data/lib/mechanize/robots_disallowed_error.rb +29 -0
- data/lib/mechanize/util.rb +30 -6
- data/test/helper.rb +6 -0
- data/test/htdocs/canonical_uri.html +9 -0
- data/test/htdocs/nofollow.html +9 -0
- data/test/htdocs/noindex.html +9 -0
- data/test/htdocs/norobots.html +8 -0
- data/test/htdocs/rel_nofollow.html +8 -0
- data/test/htdocs/robots.html +8 -0
- data/test/htdocs/robots.txt +2 -0
- data/test/htdocs/tc_links.html +3 -3
- data/test/test_links.rb +9 -0
- data/test/test_mechanize.rb +617 -2
- data/test/{test_forms.rb → test_mechanize_form.rb} +45 -1
- data/test/test_mechanize_form_check_box.rb +37 -0
- data/test/test_mechanize_form_encoding.rb +118 -0
- data/test/{test_field_precedence.rb → test_mechanize_form_field.rb} +4 -16
- data/test/test_mechanize_page.rb +60 -1
- data/test/test_mechanize_redirect_not_get_or_head_error.rb +18 -0
- data/test/test_mechanize_subclass.rb +22 -0
- data/test/test_mechanize_util.rb +87 -2
- data/test/test_robots.rb +87 -0
- metadata +51 -43
- metadata.gz.sig +0 -0
- data/lib/mechanize/uri_resolver.rb +0 -82
- data/test/test_authenticate.rb +0 -71
- data/test/test_bad_links.rb +0 -25
- data/test/test_blank_form.rb +0 -16
- data/test/test_checkboxes.rb +0 -61
- data/test/test_content_type.rb +0 -13
- data/test/test_encoded_links.rb +0 -20
- data/test/test_errors.rb +0 -49
- data/test/test_follow_meta.rb +0 -119
- data/test/test_get_headers.rb +0 -52
- data/test/test_gzipping.rb +0 -22
- data/test/test_hash_api.rb +0 -45
- data/test/test_mech.rb +0 -283
- data/test/test_mech_proxy.rb +0 -16
- data/test/test_mechanize_uri_resolver.rb +0 -29
- data/test/test_redirect_verb_handling.rb +0 -49
- data/test/test_subclass.rb +0 -30
data/test/test_bad_links.rb
DELETED
@@ -1,25 +0,0 @@
|
|
1
|
-
require "helper"
|
2
|
-
|
3
|
-
class TestBadLinks < Test::Unit::TestCase
|
4
|
-
def setup
|
5
|
-
@agent = Mechanize.new
|
6
|
-
@page = @agent.get("http://localhost/tc_bad_links.html")
|
7
|
-
end
|
8
|
-
|
9
|
-
def test_space_in_link
|
10
|
-
assert_nothing_raised do
|
11
|
-
@agent.click @page.links.first
|
12
|
-
end
|
13
|
-
assert_match(/alt_text.html$/, @agent.history.last.uri.to_s)
|
14
|
-
assert_equal(2, @agent.history.length)
|
15
|
-
end
|
16
|
-
|
17
|
-
def test_space_in_url
|
18
|
-
page = nil
|
19
|
-
assert_nothing_raised do
|
20
|
-
page = @agent.get("http://localhost/tc_bad_links.html ")
|
21
|
-
end
|
22
|
-
assert_match(/tc_bad_links.html$/, @agent.history.last.uri.to_s)
|
23
|
-
assert_equal(2, @agent.history.length)
|
24
|
-
end
|
25
|
-
end
|
data/test/test_blank_form.rb
DELETED
@@ -1,16 +0,0 @@
|
|
1
|
-
require "helper"
|
2
|
-
|
3
|
-
class BlankFormTest < Test::Unit::TestCase
|
4
|
-
def setup
|
5
|
-
@agent = Mechanize.new
|
6
|
-
end
|
7
|
-
|
8
|
-
def test_blank_form_query_string
|
9
|
-
page = @agent.get('http://localhost/tc_blank_form.html')
|
10
|
-
form = page.forms.first
|
11
|
-
query = form.build_query
|
12
|
-
assert(query.length > 0)
|
13
|
-
assert query.all? { |x| x[1] == '' }
|
14
|
-
end
|
15
|
-
end
|
16
|
-
|
data/test/test_checkboxes.rb
DELETED
@@ -1,61 +0,0 @@
|
|
1
|
-
require "helper"
|
2
|
-
|
3
|
-
class TestCheckBoxes < Test::Unit::TestCase
|
4
|
-
def setup
|
5
|
-
@agent = Mechanize.new
|
6
|
-
@page = @agent.get('http://localhost/tc_checkboxes.html')
|
7
|
-
end
|
8
|
-
|
9
|
-
def test_select_one
|
10
|
-
form = @page.forms.first
|
11
|
-
form.checkbox_with(:name => 'green').check
|
12
|
-
assert(form.checkbox_with(:name => 'green').checked)
|
13
|
-
%w{ red blue yellow brown }.each do |color|
|
14
|
-
assert_equal(false, form.checkbox_with(:name => color).checked)
|
15
|
-
end
|
16
|
-
end
|
17
|
-
|
18
|
-
def test_select_all
|
19
|
-
form = @page.forms.first
|
20
|
-
form.checkboxes.each do |b|
|
21
|
-
b.check
|
22
|
-
end
|
23
|
-
form.checkboxes.each do |b|
|
24
|
-
assert_equal(true, b.checked)
|
25
|
-
end
|
26
|
-
end
|
27
|
-
|
28
|
-
def test_select_none
|
29
|
-
form = @page.forms.first
|
30
|
-
form.checkboxes.each do |b|
|
31
|
-
b.uncheck
|
32
|
-
end
|
33
|
-
form.checkboxes.each do |b|
|
34
|
-
assert_equal(false, b.checked)
|
35
|
-
end
|
36
|
-
end
|
37
|
-
|
38
|
-
def test_check_one
|
39
|
-
form = @page.forms.first
|
40
|
-
assert_equal(2, form.checkboxes_with(:name => 'green').length)
|
41
|
-
form.checkboxes_with(:name => 'green')[1].check
|
42
|
-
assert_equal(false, form.checkboxes_with(:name => 'green')[0].checked)
|
43
|
-
assert_equal(true, form.checkboxes_with(:name => 'green')[1].checked)
|
44
|
-
page = @agent.submit(form)
|
45
|
-
assert_equal(1, page.links.length)
|
46
|
-
assert_equal('green:on', page.links.first.text)
|
47
|
-
end
|
48
|
-
|
49
|
-
def test_check_two
|
50
|
-
form = @page.forms.first
|
51
|
-
assert_equal(2, form.checkboxes_with(:name => 'green').length)
|
52
|
-
form.checkboxes_with(:name => 'green')[0].check
|
53
|
-
form.checkboxes_with(:name => 'green')[1].check
|
54
|
-
assert_equal(true, form.checkboxes_with(:name => 'green')[0].checked)
|
55
|
-
assert_equal(true, form.checkboxes_with(:name => 'green')[1].checked)
|
56
|
-
page = @agent.submit(form)
|
57
|
-
assert_equal(2, page.links.length)
|
58
|
-
assert_equal('green:on', page.links.first.text)
|
59
|
-
assert_equal('green:on', page.links[1].text)
|
60
|
-
end
|
61
|
-
end
|
data/test/test_content_type.rb
DELETED
@@ -1,13 +0,0 @@
|
|
1
|
-
require "helper"
|
2
|
-
|
3
|
-
class TestContentType < Test::Unit::TestCase
|
4
|
-
def setup
|
5
|
-
@agent = Mechanize.new
|
6
|
-
end
|
7
|
-
|
8
|
-
def test_application_xhtml_xml
|
9
|
-
url = 'http://localhost/content_type_test?ct=application/xhtml%2Bxml'
|
10
|
-
page = @agent.get url
|
11
|
-
assert_equal Mechanize::Page, page.class, "xhtml docs should return a Page"
|
12
|
-
end
|
13
|
-
end
|
data/test/test_encoded_links.rb
DELETED
@@ -1,20 +0,0 @@
|
|
1
|
-
require "helper"
|
2
|
-
|
3
|
-
class TestEncodedLinks < Test::Unit::TestCase
|
4
|
-
def setup
|
5
|
-
@agent = Mechanize.new
|
6
|
-
@page = @agent.get("http://localhost/tc_encoded_links.html")
|
7
|
-
end
|
8
|
-
|
9
|
-
def test_click_link
|
10
|
-
link = @page.links.first
|
11
|
-
assert_equal('/form_post?a=b&b=c', link.href)
|
12
|
-
page = @agent.click(link)
|
13
|
-
assert_equal("http://localhost/form_post?a=b&b=c", page.uri.to_s)
|
14
|
-
end
|
15
|
-
|
16
|
-
def test_hpricot_link
|
17
|
-
page = @agent.click(@page.search('a').first)
|
18
|
-
assert_equal("http://localhost/form_post?a=b&b=c", page.uri.to_s)
|
19
|
-
end
|
20
|
-
end
|
data/test/test_errors.rb
DELETED
@@ -1,49 +0,0 @@
|
|
1
|
-
require "helper"
|
2
|
-
|
3
|
-
class MechErrorsTest < Test::Unit::TestCase
|
4
|
-
def setup
|
5
|
-
@agent = Mechanize.new
|
6
|
-
end
|
7
|
-
|
8
|
-
def test_bad_form_method
|
9
|
-
page = @agent.get("http://localhost/bad_form_test.html")
|
10
|
-
assert_raise ArgumentError do
|
11
|
-
@agent.submit(page.forms.first)
|
12
|
-
end
|
13
|
-
end
|
14
|
-
|
15
|
-
def test_non_exist
|
16
|
-
begin
|
17
|
-
@agent.get("http://localhost/bad_form_test.html")
|
18
|
-
rescue RuntimeError => ex
|
19
|
-
assert_equal("404", ex.inspect)
|
20
|
-
end
|
21
|
-
end
|
22
|
-
|
23
|
-
def test_too_many_radio
|
24
|
-
page = @agent.get("http://localhost/form_test.html")
|
25
|
-
form = page.form_with(:name => 'post_form1')
|
26
|
-
form.radiobuttons.each { |r| r.checked = true }
|
27
|
-
assert_raise Mechanize::Error do
|
28
|
-
@agent.submit(form)
|
29
|
-
end
|
30
|
-
end
|
31
|
-
|
32
|
-
def test_unknown_agent
|
33
|
-
assert_raise ArgumentError do
|
34
|
-
@agent.user_agent_alias = "Aaron's Browser"
|
35
|
-
end
|
36
|
-
end
|
37
|
-
|
38
|
-
def test_bad_url
|
39
|
-
assert_raise ArgumentError do
|
40
|
-
@agent.get('/foo.html')
|
41
|
-
end
|
42
|
-
end
|
43
|
-
|
44
|
-
def test_unsupported_scheme
|
45
|
-
assert_raise(Mechanize::UnsupportedSchemeError) {
|
46
|
-
@agent.get('ftp://server.com/foo.html')
|
47
|
-
}
|
48
|
-
end
|
49
|
-
end
|
data/test/test_follow_meta.rb
DELETED
@@ -1,119 +0,0 @@
|
|
1
|
-
require 'helper'
|
2
|
-
|
3
|
-
class FollowMetaTest < Test::Unit::TestCase
|
4
|
-
def setup
|
5
|
-
@agent = Mechanize.new
|
6
|
-
end
|
7
|
-
|
8
|
-
def test_dont_follow_meta_in_body
|
9
|
-
@agent.follow_meta_refresh = true
|
10
|
-
requests = []
|
11
|
-
@agent.pre_connect_hooks << lambda { |_, request|
|
12
|
-
requests << request
|
13
|
-
}
|
14
|
-
|
15
|
-
@agent.get('http://localhost/tc_meta_in_body.html')
|
16
|
-
assert_equal 1, requests.length
|
17
|
-
end
|
18
|
-
|
19
|
-
def test_dont_follow_meta_by_default
|
20
|
-
page = @agent.get('http://localhost/tc_follow_meta.html')
|
21
|
-
assert_equal('http://localhost/tc_follow_meta.html', page.uri.to_s)
|
22
|
-
assert_equal(1, page.meta.length)
|
23
|
-
end
|
24
|
-
|
25
|
-
def test_meta_refresh_does_not_send_referer
|
26
|
-
@agent.follow_meta_refresh = true
|
27
|
-
requests = []
|
28
|
-
@agent.pre_connect_hooks << lambda { |_, request|
|
29
|
-
requests << request
|
30
|
-
}
|
31
|
-
|
32
|
-
@agent.get('http://localhost/tc_follow_meta.html')
|
33
|
-
assert_nil requests[1]['referer']
|
34
|
-
end
|
35
|
-
|
36
|
-
def test_follow_meta_if_set
|
37
|
-
@agent.follow_meta_refresh = true
|
38
|
-
|
39
|
-
page = @agent.get('http://localhost/tc_follow_meta.html')
|
40
|
-
|
41
|
-
assert_equal(2, @agent.history.length)
|
42
|
-
assert_equal('http://localhost/tc_follow_meta.html',
|
43
|
-
@agent.history[0].uri.to_s)
|
44
|
-
assert_equal('http://localhost/index.html', page.uri.to_s)
|
45
|
-
assert_equal('http://localhost/index.html', @agent.history.last.uri.to_s)
|
46
|
-
end
|
47
|
-
|
48
|
-
def test_follow_meta_with_empty_url
|
49
|
-
@agent.follow_meta_refresh = true
|
50
|
-
|
51
|
-
page = @agent.get('http://localhost/refresh_with_empty_url')
|
52
|
-
|
53
|
-
assert_equal(3, @agent.history.length)
|
54
|
-
assert_equal('http://localhost/refresh_with_empty_url',
|
55
|
-
@agent.history[0].uri.to_s)
|
56
|
-
assert_equal('http://localhost/refresh_with_empty_url',
|
57
|
-
@agent.history[1].uri.to_s)
|
58
|
-
assert_equal('http://localhost/index.html', page.uri.to_s)
|
59
|
-
assert_equal('http://localhost/index.html', @agent.history.last.uri.to_s)
|
60
|
-
end
|
61
|
-
|
62
|
-
def test_follow_meta_without_url
|
63
|
-
@agent.follow_meta_refresh = true
|
64
|
-
|
65
|
-
page = @agent.get('http://localhost/refresh_without_url')
|
66
|
-
|
67
|
-
assert_equal(3, @agent.history.length)
|
68
|
-
assert_equal('http://localhost/refresh_without_url',
|
69
|
-
@agent.history[0].uri.to_s)
|
70
|
-
assert_equal('http://localhost/refresh_without_url',
|
71
|
-
@agent.history[1].uri.to_s)
|
72
|
-
assert_equal('http://localhost/index.html', page.uri.to_s)
|
73
|
-
assert_equal('http://localhost/index.html', @agent.history.last.uri.to_s)
|
74
|
-
end
|
75
|
-
|
76
|
-
def test_always_follow_302
|
77
|
-
@agent.follow_meta_refresh = false
|
78
|
-
page = @agent.get('http://localhost/response_code?code=302&ct=test/xml')
|
79
|
-
assert_equal('http://localhost/index.html', page.uri.to_s)
|
80
|
-
assert_equal(2, @agent.history.length)
|
81
|
-
end
|
82
|
-
|
83
|
-
def test_infinite_refresh_throws_exception
|
84
|
-
@agent.follow_meta_refresh = true
|
85
|
-
assert_raises(Mechanize::RedirectLimitReachedError) {
|
86
|
-
begin
|
87
|
-
@agent.get('http://localhost/infinite_refresh')
|
88
|
-
rescue Mechanize::RedirectLimitReachedError => ex
|
89
|
-
raise ex
|
90
|
-
end
|
91
|
-
}
|
92
|
-
end
|
93
|
-
|
94
|
-
def test_dont_honor_http_refresh_by_default
|
95
|
-
page = @agent.get('http://localhost/http_refresh?refresh_time=0')
|
96
|
-
assert_equal('http://localhost/http_refresh?refresh_time=0', page.uri.to_s)
|
97
|
-
end
|
98
|
-
|
99
|
-
def test_honor_http_refresh_if_set
|
100
|
-
@agent.follow_meta_refresh = true
|
101
|
-
page = @agent.get('http://localhost/http_refresh?refresh_time=0')
|
102
|
-
assert_equal('http://localhost/index.html', page.uri.to_s)
|
103
|
-
assert_equal(2, @agent.history.length)
|
104
|
-
end
|
105
|
-
|
106
|
-
def test_honor_http_refresh_delay_if_set
|
107
|
-
@agent.follow_meta_refresh = true
|
108
|
-
class << @agent
|
109
|
-
attr_accessor :slept
|
110
|
-
def sleep *args
|
111
|
-
@slept = args
|
112
|
-
end
|
113
|
-
end
|
114
|
-
|
115
|
-
@agent.get('http://localhost/http_refresh?refresh_time=1')
|
116
|
-
assert_equal [1], @agent.slept
|
117
|
-
end
|
118
|
-
|
119
|
-
end
|
data/test/test_get_headers.rb
DELETED
@@ -1,52 +0,0 @@
|
|
1
|
-
require "helper"
|
2
|
-
|
3
|
-
class TestGetHeaders < Test::Unit::TestCase
|
4
|
-
def setup
|
5
|
-
@agent = Mechanize.new
|
6
|
-
end
|
7
|
-
|
8
|
-
def test_bad_header_symbol
|
9
|
-
assert_raises(ArgumentError) do
|
10
|
-
@agent.get(:url => "http://localhost/file_upload.html", :headers => { :foobar => "is fubar"})
|
11
|
-
end
|
12
|
-
end
|
13
|
-
|
14
|
-
def test_host_header
|
15
|
-
page = @agent.get(:url => 'http://localhost/http_headers', :headers => { :etag => '160604-24bc-9fe2c40'})
|
16
|
-
assert_header(page, 'host' => 'localhost')
|
17
|
-
end
|
18
|
-
|
19
|
-
def test_etag_header
|
20
|
-
page = @agent.get(:url => 'http://localhost/http_headers', :headers => { :etag => '160604-24bc-9fe2c40'})
|
21
|
-
assert_header(page, 'etag' => '160604-24bc-9fe2c40')
|
22
|
-
end
|
23
|
-
|
24
|
-
def test_if_modified_since_header
|
25
|
-
value = (Time.now - 600).strftime("%a, %d %b %Y %H:%M:%S %z")
|
26
|
-
page = @agent.get(:url => 'http://localhost/http_headers', :headers => { :if_modified_since => value})
|
27
|
-
assert_header(page, 'if-modified-since' => value)
|
28
|
-
end
|
29
|
-
|
30
|
-
def test_if_modified_since_response
|
31
|
-
value = (Time.now - 600).strftime("%a, %d %b %Y %H:%M:%S %z")
|
32
|
-
page = @agent.get(:url => 'http://localhost/if_modified_since', :headers => { :if_modified_since => value})
|
33
|
-
assert_equal "304", page.code
|
34
|
-
assert_equal "0", page.header['content-length']
|
35
|
-
end
|
36
|
-
|
37
|
-
def test_string_header
|
38
|
-
page = @agent.get(:url => 'http://localhost/http_headers', :headers => { "X-BS-French-Header" => 'Ou est la bibliotheque?'})
|
39
|
-
assert_header(page, 'x-bs-french-header' => 'Ou est la bibliotheque?')
|
40
|
-
end
|
41
|
-
|
42
|
-
def assert_header(page, header)
|
43
|
-
headers = {}
|
44
|
-
page.body.split(/[\r\n]+/).each do |page_header|
|
45
|
-
headers.[]=(*page_header.chomp.split(/\|/))
|
46
|
-
end
|
47
|
-
header.each do |key, value|
|
48
|
-
assert(headers.has_key?(key))
|
49
|
-
assert_equal(value, headers[key])
|
50
|
-
end
|
51
|
-
end
|
52
|
-
end
|
data/test/test_gzipping.rb
DELETED
@@ -1,22 +0,0 @@
|
|
1
|
-
require "helper"
|
2
|
-
|
3
|
-
class TestGzip < Test::Unit::TestCase
|
4
|
-
def setup
|
5
|
-
@agent = Mechanize.new
|
6
|
-
end
|
7
|
-
|
8
|
-
def test_request_empty_gzip
|
9
|
-
assert_nothing_raised do
|
10
|
-
@agent.get("http://localhost/gzip")
|
11
|
-
end
|
12
|
-
end
|
13
|
-
|
14
|
-
def test_request_gzip
|
15
|
-
page = nil
|
16
|
-
assert_nothing_raised do
|
17
|
-
page = @agent.get("http://localhost/gzip?file=index.html")
|
18
|
-
end
|
19
|
-
assert_kind_of(Mechanize::Page, page)
|
20
|
-
assert_match('Hello World', page.body)
|
21
|
-
end
|
22
|
-
end
|
data/test/test_hash_api.rb
DELETED
@@ -1,45 +0,0 @@
|
|
1
|
-
require "helper"
|
2
|
-
|
3
|
-
class TestHashApi < Test::Unit::TestCase
|
4
|
-
def setup
|
5
|
-
@agent = Mechanize.new
|
6
|
-
end
|
7
|
-
|
8
|
-
def test_title
|
9
|
-
page = @agent.get(:url => "http://localhost/file_upload.html")
|
10
|
-
assert_equal('File Upload Form', page.title)
|
11
|
-
end
|
12
|
-
|
13
|
-
def test_page_gets_yielded
|
14
|
-
pages = nil
|
15
|
-
@agent.get(:url => "http://localhost/file_upload.html") { |page|
|
16
|
-
pages = page
|
17
|
-
}
|
18
|
-
assert pages
|
19
|
-
assert_equal('File Upload Form', pages.title)
|
20
|
-
end
|
21
|
-
|
22
|
-
def test_get_with_params
|
23
|
-
page = @agent.get(:url => 'http://localhost/', :params => { :q => 'hello' })
|
24
|
-
assert_equal('http://localhost/?q=hello', page.uri.to_s)
|
25
|
-
end
|
26
|
-
|
27
|
-
def test_get_with_referer
|
28
|
-
request = nil
|
29
|
-
@agent.pre_connect_hooks << lambda { |_, req|
|
30
|
-
request = req
|
31
|
-
}
|
32
|
-
|
33
|
-
@agent.get( :url => 'http://localhost/',
|
34
|
-
:referer => URI.parse('http://google.com/')
|
35
|
-
)
|
36
|
-
|
37
|
-
assert request
|
38
|
-
assert_equal 'http://google.com/', request['Referer']
|
39
|
-
|
40
|
-
@agent.get( :url => 'http://localhost/',
|
41
|
-
:params => [],
|
42
|
-
:referer => 'http://tenderlovemaking.com/')
|
43
|
-
assert_equal 'http://tenderlovemaking.com/', request['Referer']
|
44
|
-
end
|
45
|
-
end
|
data/test/test_mech.rb
DELETED
@@ -1,283 +0,0 @@
|
|
1
|
-
require "helper"
|
2
|
-
|
3
|
-
class TestMechMethods < Test::Unit::TestCase
|
4
|
-
def setup
|
5
|
-
@agent = Mechanize.new
|
6
|
-
end
|
7
|
-
|
8
|
-
def test_get_with_tilde
|
9
|
-
page = @agent.get('http://localhost/?foo=~2')
|
10
|
-
assert_equal('http://localhost/?foo=~2', page.uri.to_s)
|
11
|
-
end
|
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
|
-
|
20
|
-
def test_submit_takes_arbirary_headers
|
21
|
-
page = @agent.get('http://localhost:2000/form_no_action.html')
|
22
|
-
assert form = page.forms.first
|
23
|
-
form.action = '/http_headers'
|
24
|
-
page = @agent.submit(form, nil, { 'foo' => 'bar' })
|
25
|
-
headers = Hash[*(
|
26
|
-
page.body.split("\n").map { |x| x.split('|') }.flatten
|
27
|
-
)]
|
28
|
-
assert_equal 'bar', headers['foo']
|
29
|
-
end
|
30
|
-
|
31
|
-
def test_get_with_params
|
32
|
-
page = @agent.get('http://localhost/', { :q => 'hello' })
|
33
|
-
assert_equal('http://localhost/?q=hello', page.uri.to_s)
|
34
|
-
end
|
35
|
-
|
36
|
-
def test_get_with_upper_http
|
37
|
-
page = @agent.get('HTTP://localhost/', { :q => 'hello' })
|
38
|
-
assert_equal('HTTP://localhost/?q=hello', page.uri.to_s)
|
39
|
-
end
|
40
|
-
|
41
|
-
def test_get_no_referer
|
42
|
-
requests = []
|
43
|
-
@agent.pre_connect_hooks << lambda { |_, request|
|
44
|
-
requests << request
|
45
|
-
}
|
46
|
-
|
47
|
-
@agent.get('http://localhost/')
|
48
|
-
@agent.get('http://localhost/')
|
49
|
-
assert_equal(2, requests.length)
|
50
|
-
requests.each do |request|
|
51
|
-
assert_nil request['referer']
|
52
|
-
end
|
53
|
-
end
|
54
|
-
|
55
|
-
def test_with_anchor
|
56
|
-
page = @agent.get('http://localhost/?foo=bar"')
|
57
|
-
assert_equal('http://localhost/?foo=bar%22', page.uri.to_s)
|
58
|
-
end
|
59
|
-
|
60
|
-
def test_post_connect_hook_gets_called
|
61
|
-
response = nil
|
62
|
-
@agent.post_connect_hooks << lambda { |_, res|
|
63
|
-
response = res
|
64
|
-
}
|
65
|
-
|
66
|
-
@agent.get('http://localhost/')
|
67
|
-
assert(response)
|
68
|
-
end
|
69
|
-
|
70
|
-
def test_get_with_referer
|
71
|
-
request = nil
|
72
|
-
@agent.pre_connect_hooks << lambda { |_, req|
|
73
|
-
request = req
|
74
|
-
}
|
75
|
-
|
76
|
-
@agent.get('http://localhost/', URI.parse('http://google.com/'))
|
77
|
-
assert_equal 'http://google.com/', request['Referer']
|
78
|
-
|
79
|
-
@agent.get('http://localhost/', [], 'http://tenderlovemaking.com/')
|
80
|
-
assert_equal 'http://tenderlovemaking.com/', request['Referer']
|
81
|
-
end
|
82
|
-
|
83
|
-
def test_get_with_file_referer
|
84
|
-
assert_nothing_raised do
|
85
|
-
@agent.get('http://localhost', [], Mechanize::File.new(URI.parse('http://tenderlovemaking.com/crossdomain.xml')))
|
86
|
-
end
|
87
|
-
end
|
88
|
-
|
89
|
-
def test_weird_url
|
90
|
-
assert_nothing_raised {
|
91
|
-
@agent.get('http://localhost/?action=bing&bang=boom=1|a=|b=|c=')
|
92
|
-
}
|
93
|
-
assert_nothing_raised {
|
94
|
-
@agent.get('http://localhost/?a=b&b=c&c=d')
|
95
|
-
}
|
96
|
-
assert_nothing_raised {
|
97
|
-
@agent.get("http://localhost/?a=#{[0xd6].pack('U')}")
|
98
|
-
}
|
99
|
-
end
|
100
|
-
|
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
|
109
|
-
end
|
110
|
-
|
111
|
-
def test_history
|
112
|
-
0.upto(25) do |i|
|
113
|
-
assert_equal(i, @agent.history.size)
|
114
|
-
@agent.get("http://localhost/")
|
115
|
-
end
|
116
|
-
page = @agent.get("http://localhost/form_test.html")
|
117
|
-
|
118
|
-
assert_equal("http://localhost/form_test.html",
|
119
|
-
@agent.history.last.uri.to_s)
|
120
|
-
assert_equal("http://localhost/",
|
121
|
-
@agent.history[-2].uri.to_s)
|
122
|
-
assert_equal("http://localhost/",
|
123
|
-
@agent.history[-2].uri.to_s)
|
124
|
-
|
125
|
-
assert_equal(true, @agent.visited?("http://localhost/"))
|
126
|
-
assert_equal(true, @agent.visited?("/form_test.html"))
|
127
|
-
assert_equal(false, @agent.visited?("http://google.com/"))
|
128
|
-
assert_equal(true, @agent.visited?(page.links.first))
|
129
|
-
|
130
|
-
end
|
131
|
-
|
132
|
-
def test_visited
|
133
|
-
@agent.get("http://localhost/content_type_test?ct=application/pdf")
|
134
|
-
assert_equal(true,
|
135
|
-
@agent.visited?("http://localhost/content_type_test?ct=application/pdf"))
|
136
|
-
assert_equal(false,
|
137
|
-
@agent.visited?("http://localhost/content_type_test"))
|
138
|
-
assert_equal(false,
|
139
|
-
@agent.visited?("http://localhost/content_type_test?ct=text/html"))
|
140
|
-
end
|
141
|
-
|
142
|
-
def test_visited_after_redirect
|
143
|
-
@agent.get("http://localhost/response_code?code=302")
|
144
|
-
assert_equal("http://localhost/index.html",
|
145
|
-
@agent.current_page.uri.to_s)
|
146
|
-
assert_equal(true,
|
147
|
-
@agent.visited?('http://localhost/response_code?code=302'))
|
148
|
-
end
|
149
|
-
|
150
|
-
def test_max_history
|
151
|
-
@agent.max_history = 10
|
152
|
-
0.upto(10) do |i|
|
153
|
-
assert_equal(i, @agent.history.size)
|
154
|
-
@agent.get("http://localhost/")
|
155
|
-
end
|
156
|
-
|
157
|
-
0.upto(10) do |i|
|
158
|
-
assert_equal(10, @agent.history.size)
|
159
|
-
@agent.get("http://localhost/")
|
160
|
-
end
|
161
|
-
end
|
162
|
-
|
163
|
-
def test_max_history_order
|
164
|
-
@agent.max_history = 2
|
165
|
-
assert_equal(0, @agent.history.length)
|
166
|
-
|
167
|
-
@agent.get('http://localhost/form_test.html')
|
168
|
-
assert_equal(1, @agent.history.length)
|
169
|
-
|
170
|
-
@agent.get('http://localhost/empty_form.html')
|
171
|
-
assert_equal(2, @agent.history.length)
|
172
|
-
|
173
|
-
@agent.get('http://localhost/tc_checkboxes.html')
|
174
|
-
assert_equal(2, @agent.history.length)
|
175
|
-
assert_equal('http://localhost/empty_form.html', @agent.history[0].uri.to_s)
|
176
|
-
assert_equal('http://localhost/tc_checkboxes.html',
|
177
|
-
@agent.history[1].uri.to_s)
|
178
|
-
end
|
179
|
-
|
180
|
-
def test_back_button
|
181
|
-
0.upto(5) do |i|
|
182
|
-
assert_equal(i, @agent.history.size)
|
183
|
-
@agent.get("http://localhost/")
|
184
|
-
end
|
185
|
-
@agent.get("http://localhost/form_test.html")
|
186
|
-
|
187
|
-
assert_equal("http://localhost/form_test.html",
|
188
|
-
@agent.history.last.uri.to_s)
|
189
|
-
assert_equal("http://localhost/",
|
190
|
-
@agent.history[-2].uri.to_s)
|
191
|
-
|
192
|
-
assert_equal(7, @agent.history.size)
|
193
|
-
@agent.back
|
194
|
-
assert_equal(6, @agent.history.size)
|
195
|
-
assert_equal("http://localhost/",
|
196
|
-
@agent.history.last.uri.to_s)
|
197
|
-
end
|
198
|
-
|
199
|
-
def test_google
|
200
|
-
page = @agent.get("http://localhost/google.html")
|
201
|
-
search = page.forms.find { |f| f.name == "f" }
|
202
|
-
assert_not_nil(search)
|
203
|
-
assert_not_nil(search.field_with(:name => 'q'))
|
204
|
-
assert_not_nil(search.field_with(:name => 'hl'))
|
205
|
-
assert_not_nil(search.fields.find { |f| f.name == 'ie' })
|
206
|
-
end
|
207
|
-
|
208
|
-
def test_click
|
209
|
-
@agent.user_agent_alias = 'Mac Safari'
|
210
|
-
page = @agent.get("http://localhost/frame_test.html")
|
211
|
-
link = page.link_with(:text => "Form Test")
|
212
|
-
assert_not_nil(link)
|
213
|
-
page = @agent.click(link)
|
214
|
-
assert_equal("http://localhost/form_test.html",
|
215
|
-
@agent.history.last.uri.to_s)
|
216
|
-
end
|
217
|
-
|
218
|
-
def test_click_hpricot
|
219
|
-
page = @agent.get("http://localhost/frame_test.html")
|
220
|
-
|
221
|
-
link = (page/"//a[@class='bar']").first
|
222
|
-
assert_not_nil(link)
|
223
|
-
page = @agent.click(link)
|
224
|
-
assert_equal("http://localhost/form_test.html",
|
225
|
-
@agent.history.last.uri.to_s)
|
226
|
-
end
|
227
|
-
|
228
|
-
def test_click_hpricot_frame
|
229
|
-
page = @agent.get("http://localhost/frame_test.html")
|
230
|
-
|
231
|
-
link = (page/"//frame[@name='frame2']").first
|
232
|
-
assert_not_nil(link)
|
233
|
-
page = @agent.click(link)
|
234
|
-
assert_equal("http://localhost/form_test.html",
|
235
|
-
@agent.history.last.uri.to_s)
|
236
|
-
end
|
237
|
-
|
238
|
-
def test_new_find
|
239
|
-
page = @agent.get("http://localhost/frame_test.html")
|
240
|
-
assert_equal(3, page.frames.size)
|
241
|
-
|
242
|
-
find_orig = page.frames.find_all { |f| f.name == 'frame1' }
|
243
|
-
find1 = page.frames_with(:name => 'frame1')
|
244
|
-
|
245
|
-
find_orig.zip(find1).each { |a,b|
|
246
|
-
assert_equal(a, b)
|
247
|
-
}
|
248
|
-
end
|
249
|
-
|
250
|
-
def test_get_file
|
251
|
-
page = @agent.get("http://localhost/frame_test.html")
|
252
|
-
content_length = page.header['Content-Length']
|
253
|
-
page_as_string = @agent.get_file("http://localhost/frame_test.html")
|
254
|
-
assert_equal(content_length.to_i, page_as_string.length.to_i)
|
255
|
-
end
|
256
|
-
|
257
|
-
def test_transact
|
258
|
-
@agent.get("http://localhost/frame_test.html")
|
259
|
-
assert_equal(1, @agent.history.length)
|
260
|
-
@agent.transact { |a|
|
261
|
-
5.times {
|
262
|
-
@agent.get("http://localhost/frame_test.html")
|
263
|
-
}
|
264
|
-
assert_equal(6, @agent.history.length)
|
265
|
-
}
|
266
|
-
assert_equal(1, @agent.history.length)
|
267
|
-
end
|
268
|
-
|
269
|
-
def test_click2
|
270
|
-
@agent.get 'http://localhost/test_click.html'
|
271
|
-
@agent.click 'A Button'
|
272
|
-
assert_equal 'http://localhost/frame_test.html?words=nil',
|
273
|
-
@agent.page.uri.to_s
|
274
|
-
@agent.back
|
275
|
-
@agent.click 'A Link'
|
276
|
-
assert_equal 'http://localhost/index.html',
|
277
|
-
@agent.page.uri.to_s
|
278
|
-
@agent.back
|
279
|
-
@agent.click @agent.page.link_with(:text => 'A Link')
|
280
|
-
assert_equal 'http://localhost/index.html',
|
281
|
-
@agent.page.uri.to_s
|
282
|
-
end
|
283
|
-
end
|