mechanize 2.0.pre.2 → 2.0
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 +0 -0
- data/CHANGELOG.rdoc +22 -0
- data/Manifest.txt +11 -8
- data/Rakefile +2 -2
- data/examples/flickr_upload.rb +6 -7
- data/examples/mech-dump.rb +0 -2
- data/examples/proxy_req.rb +0 -2
- data/examples/rubyforge.rb +1 -3
- data/examples/spider.rb +2 -3
- data/lib/mechanize.rb +228 -680
- data/lib/mechanize/form/field.rb +1 -1
- data/lib/mechanize/history.rb +23 -5
- data/lib/mechanize/http.rb +3 -0
- data/lib/mechanize/http/agent.rb +738 -0
- data/lib/mechanize/inspect.rb +2 -2
- data/lib/mechanize/page.rb +101 -42
- data/lib/mechanize/page/frame.rb +24 -17
- data/lib/mechanize/page/link.rb +72 -54
- data/lib/mechanize/page/meta_refresh.rb +56 -0
- data/lib/mechanize/response_read_error.rb +27 -0
- data/test/htdocs/frame_referer_test.html +10 -0
- data/test/htdocs/tc_referer.html +4 -0
- data/test/test_frames.rb +9 -0
- data/test/test_history.rb +74 -98
- data/test/test_mechanize.rb +334 -812
- data/test/test_mechanize_form.rb +32 -3
- data/test/{test_textarea.rb → test_mechanize_form_textarea.rb} +1 -1
- data/test/test_mechanize_http_agent.rb +697 -0
- data/test/test_mechanize_link.rb +83 -0
- data/test/test_mechanize_page_encoding.rb +147 -0
- data/test/test_mechanize_page_link.rb +379 -0
- data/test/test_mechanize_page_meta_refresh.rb +115 -0
- data/test/test_pretty_print.rb +1 -1
- data/test/test_referer.rb +29 -5
- data/test/test_response_code.rb +21 -20
- data/test/test_robots.rb +13 -17
- data/test/test_scheme.rb +1 -1
- metadata +30 -31
- metadata.gz.sig +0 -0
- data/lib/mechanize/page/meta.rb +0 -48
- data/test/test_form_no_inputname.rb +0 -15
- data/test/test_links.rb +0 -146
- data/test/test_mechanize_page.rb +0 -224
- data/test/test_meta.rb +0 -67
- data/test/test_upload.rb +0 -109
- data/test/test_verbs.rb +0 -25
@@ -0,0 +1,27 @@
|
|
1
|
+
##
|
2
|
+
# Raised when Mechanize encounters an error while reading the response body
|
3
|
+
# from the server. Contains the response headers and the response body up to
|
4
|
+
# the error along with the initial error.
|
5
|
+
|
6
|
+
class Mechanize::ResponseReadError < Mechanize::Error
|
7
|
+
|
8
|
+
attr_reader :body_io
|
9
|
+
attr_reader :error
|
10
|
+
attr_reader :response
|
11
|
+
|
12
|
+
##
|
13
|
+
# Creates a new ResponseReadError with the +error+ raised, the +response+
|
14
|
+
# and the +body_io+ for content read so far.
|
15
|
+
|
16
|
+
def initialize error, response, body_io
|
17
|
+
@error = error
|
18
|
+
@response = response
|
19
|
+
@body_io = body_io
|
20
|
+
end
|
21
|
+
|
22
|
+
def message # :nodoc:
|
23
|
+
"#{@error.message} (#{self.class})"
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
27
|
+
|
@@ -0,0 +1,10 @@
|
|
1
|
+
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN"
|
2
|
+
"http://www.w3.org/TR/html4/frameset.dtd">
|
3
|
+
<HTML>
|
4
|
+
<HEAD>
|
5
|
+
<TITLE>A simple frameset document</TITLE>
|
6
|
+
</HEAD>
|
7
|
+
<FRAMESET>
|
8
|
+
<FRAME name="frame1" src="http://localhost/referer">
|
9
|
+
</FRAMESET>
|
10
|
+
</HTML>
|
data/test/htdocs/tc_referer.html
CHANGED
@@ -4,6 +4,10 @@
|
|
4
4
|
<a href="http://localhost/referer">Referer Servlet forced to http</a>
|
5
5
|
<a href="https://localhost/referer">Referer Servlet forced to https</a>
|
6
6
|
<br />
|
7
|
+
<a href="/referer" rel="noreferrer">Referer Servlet (noreferrer)</a>
|
8
|
+
<a href="http://localhost/referer" rel="noreferrer">Referer Servlet forced to http (noreferrer)</a>
|
9
|
+
<a href="https://localhost/referer" rel="noreferrer">Referer Servlet forced to https (noreferrer)</a>
|
10
|
+
<br />
|
7
11
|
<form method="post" action="/referer">
|
8
12
|
<input type="text" name="first" /></br>
|
9
13
|
<input type="submit" value="Submit" />
|
data/test/test_frames.rb
CHANGED
@@ -14,6 +14,9 @@ class FramesMechTest < Test::Unit::TestCase
|
|
14
14
|
assert_equal("/google.html", page.frames[0].src)
|
15
15
|
assert_equal("/form_test.html", page.frames[1].src)
|
16
16
|
assert_equal("/file_upload.html", page.frames[2].src)
|
17
|
+
assert_equal("Google", page.frames[0].content.title)
|
18
|
+
assert_equal("Page Title", page.frames[1].content.title)
|
19
|
+
assert_equal("File Upload Form", page.frames[2].content.title)
|
17
20
|
end
|
18
21
|
|
19
22
|
def test_iframes
|
@@ -21,5 +24,11 @@ class FramesMechTest < Test::Unit::TestCase
|
|
21
24
|
assert_equal(1, page.iframes.size)
|
22
25
|
assert_equal("frame4", page.iframes.first.name)
|
23
26
|
assert_equal("/file_upload.html", page.iframes.first.src)
|
27
|
+
assert_equal("File Upload Form", page.iframes.first.content.title)
|
28
|
+
end
|
29
|
+
|
30
|
+
def test_frame_referer
|
31
|
+
page = @agent.get("http://localhost/frame_referer_test.html")
|
32
|
+
assert_equal("http://localhost/frame_referer_test.html", page.frames.first.content.body)
|
24
33
|
end
|
25
34
|
end
|
data/test/test_history.rb
CHANGED
@@ -1,142 +1,118 @@
|
|
1
1
|
require "helper"
|
2
2
|
|
3
3
|
class TestHistory < Test::Unit::TestCase
|
4
|
+
|
5
|
+
Node = Struct.new :href, :inner_text
|
6
|
+
|
4
7
|
def setup
|
5
|
-
@
|
6
|
-
@history
|
8
|
+
@mech = Mechanize.new
|
9
|
+
@history = Mechanize::History.new
|
10
|
+
@uri = URI 'http://example'
|
11
|
+
end
|
12
|
+
|
13
|
+
def test_initialize
|
14
|
+
assert_equal 0, @history.length
|
7
15
|
end
|
8
16
|
|
9
17
|
def test_push
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
assert
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
@history.
|
24
|
-
assert_equal(2, @history.length)
|
18
|
+
response = { 'content-type' => 'text/html' }
|
19
|
+
page = Mechanize::Page.new @uri, response, '', 200, @mech
|
20
|
+
|
21
|
+
obj = @history.push page
|
22
|
+
|
23
|
+
assert_same @history, obj
|
24
|
+
assert_equal 1, @history.length
|
25
|
+
assert @history.visited? @uri
|
26
|
+
|
27
|
+
page = Mechanize::Page.new @uri + '/a', response, '', 200, @mech
|
28
|
+
|
29
|
+
@history.push page
|
30
|
+
|
31
|
+
assert_equal 2, @history.length
|
25
32
|
end
|
26
33
|
|
27
|
-
def
|
28
|
-
|
29
|
-
page = @agent.get("http://localhost/tc_bad_links.html")
|
30
|
-
@history.push(page)
|
31
|
-
assert_equal(1, @history.length)
|
34
|
+
def test_push_uri
|
35
|
+
obj = @history.push :page, @uri
|
32
36
|
|
33
|
-
@history
|
34
|
-
assert_equal
|
37
|
+
assert_same @history, obj
|
38
|
+
assert_equal 1, @history.length
|
35
39
|
|
36
|
-
@history.
|
37
|
-
assert_equal(3, @history.length)
|
40
|
+
assert @history.visited? @uri
|
38
41
|
|
39
|
-
page2
|
40
|
-
assert_equal(page, page2)
|
41
|
-
assert_equal(2, @history.length)
|
42
|
+
@history.push :page2, @uri
|
42
43
|
|
43
|
-
@history.
|
44
|
-
|
45
|
-
|
44
|
+
assert_equal 2, @history.length
|
45
|
+
end
|
46
|
+
|
47
|
+
def test_shift
|
48
|
+
assert_nil @history.shift
|
49
|
+
|
50
|
+
@history.push(:page1, @uri)
|
51
|
+
@history.push(:page2, @uri + '/a')
|
52
|
+
|
53
|
+
page = @history.shift
|
54
|
+
|
55
|
+
assert_equal :page1, page
|
56
|
+
assert_equal 1, @history.length
|
57
|
+
assert !@history.visited?(@uri)
|
46
58
|
|
47
59
|
@history.shift
|
48
|
-
assert_equal(0, @history.length)
|
49
60
|
|
50
|
-
|
51
|
-
assert_equal(0, @history.length)
|
61
|
+
assert_equal 0, @history.length
|
52
62
|
end
|
53
63
|
|
54
64
|
def test_pop
|
55
|
-
|
56
|
-
|
57
|
-
@history.
|
58
|
-
assert_equal
|
59
|
-
|
60
|
-
|
61
|
-
@history.
|
62
|
-
assert_equal(2, @history.length)
|
63
|
-
assert_equal(page2, @history.pop)
|
64
|
-
assert_equal(1, @history.length)
|
65
|
-
assert_equal(true, @history.visited?(page))
|
66
|
-
assert_equal(false, @history.visited?(page2))
|
67
|
-
assert_equal(page, @history.pop)
|
68
|
-
assert_equal(0, @history.length)
|
69
|
-
assert_equal(false, @history.visited?(page))
|
70
|
-
assert_equal(false, @history.visited?(page2))
|
71
|
-
assert_nil(@history.pop)
|
65
|
+
@history.push(:page, @uri)
|
66
|
+
|
67
|
+
assert_equal(:page, @history.pop)
|
68
|
+
assert_equal 0, @history.length
|
69
|
+
assert !@history.visited?(@uri)
|
70
|
+
|
71
|
+
assert_nil @history.pop
|
72
72
|
end
|
73
73
|
|
74
74
|
def test_max_size
|
75
|
-
@history
|
76
|
-
1.upto(20) do |i|
|
77
|
-
page = @agent.get('http://localhost/index.html')
|
78
|
-
@history.push page
|
79
|
-
assert_equal(true, @history.visited?(page))
|
80
|
-
if i < 10
|
81
|
-
assert_equal(i, @history.length)
|
82
|
-
else
|
83
|
-
assert_equal(10, @history.length)
|
84
|
-
end
|
85
|
-
end
|
75
|
+
@history = Mechanize::History.new 2
|
86
76
|
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
assert_equal(true, @history.visited?(page))
|
93
|
-
if i < 5
|
94
|
-
assert_equal(i, @history.length)
|
77
|
+
1.upto(3) do |i|
|
78
|
+
@history.push :page, @uri
|
79
|
+
|
80
|
+
if i >= 2
|
81
|
+
assert_equal 2, @history.length
|
95
82
|
else
|
96
|
-
assert_equal
|
83
|
+
assert_equal i, @history.length
|
97
84
|
end
|
98
85
|
end
|
99
|
-
|
100
|
-
@history.max_size = 0
|
101
|
-
1.upto(20) do |i|
|
102
|
-
page = @agent.get('http://localhost/index.html')
|
103
|
-
@history.push page
|
104
|
-
assert_equal(false, @history.visited?(page))
|
105
|
-
assert_equal(0, @history.length)
|
106
|
-
end
|
107
86
|
end
|
108
87
|
|
109
|
-
def
|
110
|
-
@
|
88
|
+
def test_visited_eh
|
89
|
+
@mech.get('http://localhost/')
|
111
90
|
|
112
91
|
node = Struct.new(:href, :inner_text).new('http://localhost/', 'blah')
|
113
92
|
link = Mechanize::Page::Link.new(node, nil, nil)
|
114
|
-
assert(@
|
93
|
+
assert(@mech.visited?(link))
|
115
94
|
|
116
95
|
node = Struct.new(:href, :inner_text).new('http://localhost', 'blah')
|
117
96
|
link = Mechanize::Page::Link.new(node, nil, nil)
|
118
|
-
assert(@
|
97
|
+
assert(@mech.visited?(link))
|
119
98
|
end
|
120
99
|
|
121
|
-
def
|
122
|
-
|
100
|
+
def test_visited_eh_no_slash
|
101
|
+
slash = URI 'http://example/'
|
102
|
+
no_slash = URI 'http://example'
|
123
103
|
|
124
|
-
|
125
|
-
link = Mechanize::Page::Link.new(node, nil, nil)
|
126
|
-
assert(@agent.visited?(link))
|
104
|
+
@history.push :page, slash
|
127
105
|
|
128
|
-
|
129
|
-
|
130
|
-
assert(@agent.visited?(link))
|
106
|
+
assert @history.visited?(slash), 'slash'
|
107
|
+
assert @history.visited?(no_slash), 'no slash'
|
131
108
|
end
|
132
109
|
|
133
110
|
def test_clear
|
134
|
-
page
|
135
|
-
|
136
|
-
assert_equal(20, @history.length)
|
137
|
-
assert_equal(true, @history.visited?(page))
|
111
|
+
@history.push :page, @uri
|
112
|
+
|
138
113
|
@history.clear
|
139
|
-
|
140
|
-
assert_equal
|
114
|
+
|
115
|
+
assert_equal 0, @history.length
|
116
|
+
assert !@history.visited?(@uri)
|
141
117
|
end
|
142
118
|
end
|
data/test/test_mechanize.rb
CHANGED
@@ -17,7 +17,8 @@ class TestMechanize < Test::Unit::TestCase
|
|
17
17
|
CERT.sign KEY, OpenSSL::Digest::SHA1.new
|
18
18
|
|
19
19
|
def setup
|
20
|
-
@
|
20
|
+
@mech = Mechanize.new
|
21
|
+
@mech.log = nil
|
21
22
|
@uri = URI.parse 'http://example/'
|
22
23
|
@req = Net::HTTP::Get.new '/'
|
23
24
|
|
@@ -34,26 +35,26 @@ class TestMechanize < Test::Unit::TestCase
|
|
34
35
|
|
35
36
|
def test_back
|
36
37
|
0.upto(5) do |i|
|
37
|
-
assert_equal(i, @
|
38
|
-
@
|
38
|
+
assert_equal(i, @mech.history.size)
|
39
|
+
@mech.get("http://localhost/")
|
39
40
|
end
|
40
|
-
@
|
41
|
+
@mech.get("http://localhost/form_test.html")
|
41
42
|
|
42
43
|
assert_equal("http://localhost/form_test.html",
|
43
|
-
@
|
44
|
+
@mech.history.last.uri.to_s)
|
44
45
|
assert_equal("http://localhost/",
|
45
|
-
@
|
46
|
+
@mech.history[-2].uri.to_s)
|
46
47
|
|
47
|
-
assert_equal(7, @
|
48
|
-
@
|
49
|
-
assert_equal(6, @
|
48
|
+
assert_equal(7, @mech.history.size)
|
49
|
+
@mech.back
|
50
|
+
assert_equal(6, @mech.history.size)
|
50
51
|
assert_equal("http://localhost/",
|
51
|
-
@
|
52
|
+
@mech.history.last.uri.to_s)
|
52
53
|
end
|
53
54
|
|
54
55
|
def test_basic_auth
|
55
|
-
@
|
56
|
-
page = @
|
56
|
+
@mech.basic_auth('user', 'pass')
|
57
|
+
page = @mech.get("http://localhost/basic_auth")
|
57
58
|
assert_equal('You are authenticated', page.body)
|
58
59
|
end
|
59
60
|
|
@@ -66,115 +67,120 @@ class TestMechanize < Test::Unit::TestCase
|
|
66
67
|
cert.write CERT.to_pem
|
67
68
|
cert.rewind
|
68
69
|
|
69
|
-
|
70
|
+
mech = Mechanize.new do |a|
|
70
71
|
a.cert = cert.path
|
71
72
|
a.key = key.path
|
72
73
|
end
|
73
74
|
|
74
75
|
# Certificate#== seems broken
|
75
|
-
assert_equal CERT.to_pem,
|
76
|
+
assert_equal CERT.to_pem, mech.certificate.to_pem
|
76
77
|
end
|
77
78
|
end
|
78
79
|
end
|
79
80
|
|
80
81
|
def test_cert_key_object
|
81
|
-
|
82
|
+
mech = Mechanize.new do |a|
|
82
83
|
a.cert = CERT
|
83
84
|
a.key = KEY
|
84
85
|
end
|
85
86
|
|
86
|
-
assert_equal CERT,
|
87
|
+
assert_equal CERT, mech.certificate
|
87
88
|
end
|
88
89
|
|
89
90
|
def test_click
|
90
|
-
@
|
91
|
-
page = @
|
91
|
+
@mech.user_agent_alias = 'Mac Safari'
|
92
|
+
page = @mech.get("http://localhost/frame_test.html")
|
92
93
|
link = page.link_with(:text => "Form Test")
|
93
94
|
assert_not_nil(link)
|
94
|
-
page = @
|
95
|
+
page = @mech.click(link)
|
95
96
|
assert_equal("http://localhost/form_test.html",
|
96
|
-
@
|
97
|
+
@mech.history.last.uri.to_s)
|
97
98
|
end
|
98
99
|
|
99
100
|
def test_click_frame_hpricot_style
|
100
|
-
page = @
|
101
|
+
page = @mech.get("http://localhost/frame_test.html")
|
101
102
|
|
102
103
|
link = (page/"//frame[@name='frame2']").first
|
103
104
|
assert_not_nil(link)
|
104
|
-
page = @
|
105
|
+
page = @mech.click(link)
|
105
106
|
assert_equal("http://localhost/form_test.html",
|
106
|
-
@
|
107
|
+
@mech.history.last.uri.to_s)
|
107
108
|
end
|
108
109
|
|
109
110
|
def test_click_hpricot_style # HACK move to test_divide in Page
|
110
|
-
page = @
|
111
|
+
page = @mech.get("http://localhost/frame_test.html")
|
111
112
|
|
112
113
|
link = (page/"//a[@class='bar']").first
|
113
114
|
assert_not_nil(link)
|
114
115
|
|
115
|
-
page = @
|
116
|
+
page = @mech.click(link)
|
116
117
|
|
117
118
|
assert_equal("http://localhost/form_test.html",
|
118
|
-
@
|
119
|
+
@mech.history.last.uri.to_s)
|
120
|
+
end
|
121
|
+
|
122
|
+
def test_click_link
|
123
|
+
agent = Mechanize.new
|
124
|
+
agent.user_agent_alias = 'Mac Safari'
|
125
|
+
page = agent.get("http://localhost/frame_test.html")
|
126
|
+
link = page.link_with(:text => "Form Test")
|
127
|
+
|
128
|
+
agent.click link
|
129
|
+
|
130
|
+
assert_equal("http://localhost/form_test.html",
|
131
|
+
agent.history.last.uri.to_s)
|
119
132
|
end
|
120
133
|
|
121
134
|
def test_click_link_hpricot_style # HACK move to test_search in Page
|
122
|
-
page = @
|
135
|
+
page = @mech.get("http://localhost/tc_encoded_links.html")
|
123
136
|
|
124
|
-
page = @
|
137
|
+
page = @mech.click(page.search('a').first)
|
125
138
|
|
126
139
|
assert_equal("http://localhost/form_post?a=b&b=c", page.uri.to_s)
|
127
140
|
end
|
128
141
|
|
129
142
|
def test_click_link_query
|
130
|
-
page = @
|
143
|
+
page = @mech.get("http://localhost/tc_encoded_links.html")
|
131
144
|
link = page.links.first
|
132
145
|
assert_equal('/form_post?a=b&b=c', link.href)
|
133
146
|
|
134
|
-
page = @
|
147
|
+
page = @mech.click(link)
|
135
148
|
|
136
149
|
assert_equal("http://localhost/form_post?a=b&b=c", page.uri.to_s)
|
137
150
|
end
|
138
151
|
|
139
152
|
def test_click_link_space
|
140
|
-
page = @
|
153
|
+
page = @mech.get("http://localhost/tc_bad_links.html")
|
141
154
|
|
142
|
-
@
|
155
|
+
@mech.click page.links.first
|
143
156
|
|
144
|
-
assert_match(/alt_text.html$/, @
|
145
|
-
assert_equal(2, @
|
157
|
+
assert_match(/alt_text.html$/, @mech.history.last.uri.to_s)
|
158
|
+
assert_equal(2, @mech.history.length)
|
146
159
|
end
|
147
160
|
|
148
161
|
def test_click_more
|
149
|
-
@
|
150
|
-
@
|
162
|
+
@mech.get 'http://localhost/test_click.html'
|
163
|
+
@mech.click 'A Button'
|
151
164
|
assert_equal 'http://localhost/frame_test.html?words=nil',
|
152
|
-
@
|
153
|
-
@
|
154
|
-
@
|
165
|
+
@mech.page.uri.to_s
|
166
|
+
@mech.back
|
167
|
+
@mech.click 'A Link'
|
155
168
|
assert_equal 'http://localhost/index.html',
|
156
|
-
@
|
157
|
-
@
|
158
|
-
@
|
169
|
+
@mech.page.uri.to_s
|
170
|
+
@mech.back
|
171
|
+
@mech.click @mech.page.link_with(:text => 'A Link')
|
159
172
|
assert_equal 'http://localhost/index.html',
|
160
|
-
@
|
173
|
+
@mech.page.uri.to_s
|
161
174
|
end
|
162
175
|
|
163
|
-
def
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
assert_equal Mechanize::FileConnection.new, conn
|
168
|
-
end
|
169
|
-
|
170
|
-
def test_connection_for_http
|
171
|
-
conn = @agent.connection_for @uri
|
172
|
-
|
173
|
-
assert_equal @agent.http, conn
|
176
|
+
def test_delete
|
177
|
+
page = @mech.delete('http://localhost/verb', { 'q' => 'foo' })
|
178
|
+
assert_equal 1, @mech.history.length
|
179
|
+
assert_equal 'DELETE', page.header['X-Request-Method']
|
174
180
|
end
|
175
181
|
|
176
182
|
def test_delete_redirect
|
177
|
-
page = @
|
183
|
+
page = @mech.delete('http://localhost/redirect')
|
178
184
|
|
179
185
|
assert_equal(page.uri.to_s, 'http://localhost/verb')
|
180
186
|
|
@@ -185,101 +191,41 @@ class TestMechanize < Test::Unit::TestCase
|
|
185
191
|
# Dir.mktmpdir do |dir|
|
186
192
|
# file = "#{dir}/download"
|
187
193
|
# open file, 'w' do |io|
|
188
|
-
# @
|
194
|
+
# @mech.download 'http://example', io
|
189
195
|
# end
|
190
196
|
|
191
197
|
# assert_equal 1, File.stat(file).size
|
192
198
|
# end
|
193
199
|
#end
|
194
200
|
|
195
|
-
def test_enable_gzip
|
196
|
-
@agent.enable_gzip @req
|
197
|
-
|
198
|
-
assert_equal 'gzip,deflate,identity', @req['accept-encoding']
|
199
|
-
end
|
200
|
-
|
201
|
-
def test_enable_gzip_no
|
202
|
-
@agent.gzip_enabled = false
|
203
|
-
|
204
|
-
@agent.enable_gzip @req
|
205
|
-
|
206
|
-
assert_equal 'identity', @req['accept-encoding']
|
207
|
-
end
|
208
|
-
|
209
|
-
def test_fetch_page_file_plus
|
210
|
-
Tempfile.open '++plus++' do |io|
|
211
|
-
content = 'plusses +++'
|
212
|
-
io.write content
|
213
|
-
io.rewind
|
214
|
-
|
215
|
-
uri = URI.parse "file://#{Mechanize::Util.uri_escape io.path}"
|
216
|
-
|
217
|
-
page = @agent.send :fetch_page, uri
|
218
|
-
|
219
|
-
assert_equal content, page.body
|
220
|
-
assert_kind_of Mechanize::File, page
|
221
|
-
end
|
222
|
-
end
|
223
|
-
|
224
|
-
def test_fetch_page_file_space
|
225
|
-
foo = File.expand_path("../htdocs/dir with spaces/foo.html", __FILE__)
|
226
|
-
|
227
|
-
uri = URI.parse "file://#{Mechanize::Util.uri_escape foo}"
|
228
|
-
|
229
|
-
page = @agent.send :fetch_page, uri
|
230
|
-
|
231
|
-
assert_equal File.read(foo), page.body
|
232
|
-
assert_kind_of Mechanize::Page, page
|
233
|
-
end
|
234
|
-
|
235
|
-
def test_fetch_page_file_nonexistent
|
236
|
-
uri = URI.parse 'file:///nonexistent'
|
237
|
-
|
238
|
-
e = assert_raises Mechanize::ResponseCodeError do
|
239
|
-
@agent.send :fetch_page, uri
|
240
|
-
end
|
241
|
-
|
242
|
-
assert_equal '404 => Net::HTTPNotFound', e.message
|
243
|
-
end
|
244
|
-
|
245
|
-
def test_fetch_page_post_connect_hook
|
246
|
-
response = nil
|
247
|
-
@agent.post_connect_hooks << lambda { |_, res|
|
248
|
-
response = res
|
249
|
-
}
|
250
|
-
|
251
|
-
@agent.get('http://localhost/')
|
252
|
-
assert(response)
|
253
|
-
end
|
254
|
-
|
255
201
|
def test_get
|
256
|
-
page = @
|
202
|
+
page = @mech.get('http://localhost', { :q => 'h' }, 'http://example',
|
257
203
|
{ 'X-H' => 'v' })
|
258
204
|
|
259
205
|
assert_equal 'http://localhost/?q=h', page.uri.to_s
|
260
206
|
end
|
261
207
|
|
262
208
|
def test_get_HTTP
|
263
|
-
page = @
|
209
|
+
page = @mech.get('HTTP://localhost/', { :q => 'hello' })
|
264
210
|
assert_equal('HTTP://localhost/?q=hello', page.uri.to_s)
|
265
211
|
end
|
266
212
|
|
267
213
|
def test_get_anchor
|
268
|
-
page = @
|
214
|
+
page = @mech.get('http://localhost/?foo=bar"')
|
269
215
|
assert_equal('http://localhost/?foo=bar%22', page.uri.to_s)
|
270
216
|
end
|
271
217
|
|
272
218
|
def test_get_bad_url
|
273
219
|
assert_raise ArgumentError do
|
274
|
-
@
|
220
|
+
@mech.get('/foo.html')
|
275
221
|
end
|
276
222
|
end
|
277
223
|
|
278
224
|
def test_get_basic_auth_bad
|
279
|
-
@
|
225
|
+
@mech.basic_auth('aaron', 'aaron')
|
280
226
|
|
281
227
|
e = assert_raises Mechanize::ResponseCodeError do
|
282
|
-
@
|
228
|
+
@mech.get("http://localhost/basic_auth")
|
283
229
|
end
|
284
230
|
|
285
231
|
assert_equal("401", e.response_code)
|
@@ -287,7 +233,7 @@ class TestMechanize < Test::Unit::TestCase
|
|
287
233
|
|
288
234
|
def test_get_basic_auth_none
|
289
235
|
e = assert_raises Mechanize::ResponseCodeError do
|
290
|
-
@
|
236
|
+
@mech.get("http://localhost/basic_auth")
|
291
237
|
end
|
292
238
|
|
293
239
|
assert_equal("401", e.response_code)
|
@@ -296,102 +242,113 @@ class TestMechanize < Test::Unit::TestCase
|
|
296
242
|
def test_get_digest_auth
|
297
243
|
block_called = false
|
298
244
|
|
299
|
-
@
|
245
|
+
@mech.basic_auth('user', 'pass')
|
300
246
|
|
301
|
-
@
|
247
|
+
@mech.pre_connect_hooks << lambda { |_, request|
|
302
248
|
block_called = true
|
303
249
|
request.to_hash.each do |k,v|
|
304
250
|
assert_equal(1, v.length)
|
305
251
|
end
|
306
252
|
}
|
307
253
|
|
308
|
-
page = @
|
254
|
+
page = @mech.get("http://localhost/digest_auth")
|
309
255
|
assert_equal('You are authenticated', page.body)
|
310
256
|
assert block_called
|
311
257
|
end
|
312
258
|
|
313
259
|
def test_get_file
|
314
|
-
page = @
|
260
|
+
page = @mech.get("http://localhost/frame_test.html")
|
315
261
|
content_length = page.header['Content-Length']
|
316
|
-
page_as_string = @
|
262
|
+
page_as_string = @mech.get_file("http://localhost/frame_test.html")
|
317
263
|
assert_equal(content_length.to_i, page_as_string.length.to_i)
|
318
264
|
end
|
319
265
|
|
320
266
|
def test_get_follow_meta_refresh
|
321
|
-
@
|
267
|
+
@mech.follow_meta_refresh = true
|
322
268
|
|
323
|
-
page = @
|
269
|
+
page = @mech.get('http://localhost/tc_follow_meta.html')
|
324
270
|
|
325
|
-
assert_equal(2, @
|
271
|
+
assert_equal(2, @mech.history.length)
|
326
272
|
|
327
273
|
assert_equal('http://localhost/tc_follow_meta.html',
|
328
|
-
@
|
274
|
+
@mech.history.first.uri.to_s)
|
329
275
|
assert_equal('http://localhost/index.html', page.uri.to_s)
|
330
|
-
assert_equal('http://localhost/index.html', @
|
276
|
+
assert_equal('http://localhost/index.html', @mech.history.last.uri.to_s)
|
277
|
+
end
|
278
|
+
|
279
|
+
def test_get_follow_meta_refresh_anywhere
|
280
|
+
@mech.follow_meta_refresh = :anywhere
|
281
|
+
requests = []
|
282
|
+
@mech.pre_connect_hooks << lambda { |_, request|
|
283
|
+
requests << request
|
284
|
+
}
|
285
|
+
|
286
|
+
@mech.get('http://localhost/tc_meta_in_body.html')
|
287
|
+
assert_equal 2, requests.length
|
331
288
|
end
|
332
289
|
|
333
290
|
def test_get_follow_meta_refresh_disabled
|
334
|
-
page = @
|
291
|
+
page = @mech.get('http://localhost/tc_follow_meta.html')
|
335
292
|
assert_equal('http://localhost/tc_follow_meta.html', page.uri.to_s)
|
336
|
-
assert_equal(1, page.
|
293
|
+
assert_equal(1, page.meta_refresh.length)
|
337
294
|
end
|
338
295
|
|
339
296
|
def test_get_follow_meta_refresh_empty_url
|
340
|
-
@
|
297
|
+
@mech.follow_meta_refresh = true
|
341
298
|
|
342
|
-
page = @
|
299
|
+
page = @mech.get('http://localhost/refresh_with_empty_url')
|
343
300
|
|
344
|
-
assert_equal(3, @
|
301
|
+
assert_equal(3, @mech.history.length)
|
345
302
|
assert_equal('http://localhost/refresh_with_empty_url',
|
346
|
-
@
|
303
|
+
@mech.history[0].uri.to_s)
|
347
304
|
assert_equal('http://localhost/refresh_with_empty_url',
|
348
|
-
@
|
305
|
+
@mech.history[1].uri.to_s)
|
349
306
|
assert_equal('http://localhost/index.html', page.uri.to_s)
|
350
|
-
assert_equal('http://localhost/index.html', @
|
307
|
+
assert_equal('http://localhost/index.html', @mech.history.last.uri.to_s)
|
351
308
|
end
|
352
309
|
|
353
310
|
def test_get_follow_meta_refresh_in_body
|
354
|
-
@
|
311
|
+
@mech.follow_meta_refresh = true
|
355
312
|
requests = []
|
356
|
-
@
|
313
|
+
@mech.pre_connect_hooks << lambda { |_, request|
|
357
314
|
requests << request
|
358
315
|
}
|
359
316
|
|
360
|
-
@
|
317
|
+
@mech.get('http://localhost/tc_meta_in_body.html')
|
361
318
|
assert_equal 1, requests.length
|
362
319
|
end
|
363
320
|
|
364
321
|
def test_get_follow_meta_refresh_no_url
|
365
|
-
@
|
322
|
+
@mech.follow_meta_refresh = true
|
366
323
|
|
367
|
-
page = @
|
324
|
+
page = @mech.get('http://localhost/refresh_without_url')
|
368
325
|
|
369
|
-
assert_equal(3, @
|
326
|
+
assert_equal(3, @mech.history.length)
|
370
327
|
assert_equal('http://localhost/refresh_without_url',
|
371
|
-
@
|
328
|
+
@mech.history[0].uri.to_s)
|
372
329
|
assert_equal('http://localhost/refresh_without_url',
|
373
|
-
@
|
330
|
+
@mech.history[1].uri.to_s)
|
374
331
|
assert_equal('http://localhost/index.html', page.uri.to_s)
|
375
|
-
assert_equal('http://localhost/index.html', @
|
332
|
+
assert_equal('http://localhost/index.html', @mech.history.last.uri.to_s)
|
376
333
|
end
|
377
334
|
|
378
335
|
def test_get_follow_meta_refresh_referer_not_sent
|
379
|
-
@
|
336
|
+
@mech.follow_meta_refresh = true
|
380
337
|
|
381
338
|
requests = []
|
382
339
|
|
383
|
-
@
|
340
|
+
@mech.pre_connect_hooks << lambda { |_, request|
|
384
341
|
requests << request
|
385
342
|
}
|
386
343
|
|
387
|
-
@
|
344
|
+
@mech.get('http://localhost/tc_follow_meta.html')
|
388
345
|
|
389
|
-
assert_equal 2, @
|
346
|
+
assert_equal 2, @mech.history.length
|
390
347
|
assert_nil requests.last['referer']
|
391
348
|
end
|
392
349
|
|
393
350
|
def test_get_gzip
|
394
|
-
page = @
|
351
|
+
page = @mech.get("http://localhost/gzip?file=index.html")
|
395
352
|
|
396
353
|
assert_kind_of(Mechanize::Page, page)
|
397
354
|
|
@@ -399,45 +356,45 @@ class TestMechanize < Test::Unit::TestCase
|
|
399
356
|
end
|
400
357
|
|
401
358
|
def test_get_http_refresh
|
402
|
-
@
|
403
|
-
page = @
|
359
|
+
@mech.follow_meta_refresh = true
|
360
|
+
page = @mech.get('http://localhost/http_refresh?refresh_time=0')
|
404
361
|
assert_equal('http://localhost/index.html', page.uri.to_s)
|
405
|
-
assert_equal(2, @
|
362
|
+
assert_equal(2, @mech.history.length)
|
406
363
|
end
|
407
364
|
|
408
365
|
def test_get_http_refresh_delay
|
409
|
-
@
|
410
|
-
class << @agent
|
366
|
+
@mech.follow_meta_refresh = true
|
367
|
+
class << @mech.agent
|
411
368
|
attr_accessor :slept
|
412
369
|
def sleep *args
|
413
370
|
@slept = args
|
414
371
|
end
|
415
372
|
end
|
416
373
|
|
417
|
-
@
|
418
|
-
assert_equal [1], @agent.slept
|
374
|
+
@mech.get('http://localhost/http_refresh?refresh_time=1')
|
375
|
+
assert_equal [1], @mech.agent.slept
|
419
376
|
end
|
420
377
|
|
421
378
|
def test_get_http_refresh_disabled
|
422
|
-
page = @
|
379
|
+
page = @mech.get('http://localhost/http_refresh?refresh_time=0')
|
423
380
|
assert_equal('http://localhost/http_refresh?refresh_time=0', page.uri.to_s)
|
424
381
|
end
|
425
382
|
|
426
383
|
def test_get_kcode
|
427
384
|
$KCODE = 'u'
|
428
|
-
page = @
|
385
|
+
page = @mech.get("http://localhost/?a=#{[0xd6].pack('U')}")
|
429
386
|
assert_not_nil(page)
|
430
387
|
assert_equal('http://localhost/?a=%D6', page.uri.to_s)
|
431
388
|
$KCODE = 'NONE'
|
432
389
|
end unless RUBY_VERSION >= '1.9.0'
|
433
390
|
|
434
391
|
def test_get_query
|
435
|
-
page = @
|
392
|
+
page = @mech.get('http://localhost/', { :q => 'hello' })
|
436
393
|
assert_equal('http://localhost/?q=hello', page.uri.to_s)
|
437
394
|
end
|
438
395
|
|
439
396
|
def test_get_redirect
|
440
|
-
page = @
|
397
|
+
page = @mech.get('http://localhost/redirect')
|
441
398
|
|
442
399
|
assert_equal(page.uri.to_s, 'http://localhost/verb')
|
443
400
|
|
@@ -445,32 +402,32 @@ class TestMechanize < Test::Unit::TestCase
|
|
445
402
|
end
|
446
403
|
|
447
404
|
def test_get_redirect_found
|
448
|
-
page = @
|
405
|
+
page = @mech.get('http://localhost/response_code?code=302&ct=test/xml')
|
449
406
|
|
450
407
|
assert_equal('http://localhost/index.html', page.uri.to_s)
|
451
408
|
|
452
|
-
assert_equal(2, @
|
409
|
+
assert_equal(2, @mech.history.length)
|
453
410
|
end
|
454
411
|
|
455
412
|
def test_get_redirect_infinite
|
456
413
|
assert_raises(Mechanize::RedirectLimitReachedError) {
|
457
|
-
@
|
414
|
+
@mech.get('http://localhost/infinite_refresh')
|
458
415
|
}
|
459
416
|
end
|
460
417
|
|
461
418
|
def test_get_referer
|
462
419
|
request = nil
|
463
|
-
@
|
420
|
+
@mech.pre_connect_hooks << lambda { |_, req|
|
464
421
|
request = req
|
465
422
|
}
|
466
423
|
|
467
|
-
@
|
424
|
+
@mech.get('http://localhost/', [], 'http://tenderlovemaking.com/')
|
468
425
|
assert_equal 'http://tenderlovemaking.com/', request['Referer']
|
469
426
|
end
|
470
427
|
|
471
428
|
def test_get_referer_file
|
472
429
|
assert_nothing_raised do
|
473
|
-
@
|
430
|
+
@mech.get('http://localhost', [], Mechanize::File.new(URI.parse('http://tenderlovemaking.com/crossdomain.xml')))
|
474
431
|
end
|
475
432
|
|
476
433
|
# HACK no assertion of behavior
|
@@ -478,12 +435,12 @@ class TestMechanize < Test::Unit::TestCase
|
|
478
435
|
|
479
436
|
def test_get_referer_none
|
480
437
|
requests = []
|
481
|
-
@
|
438
|
+
@mech.pre_connect_hooks << lambda { |_, request|
|
482
439
|
requests << request
|
483
440
|
}
|
484
441
|
|
485
|
-
@
|
486
|
-
@
|
442
|
+
@mech.get('http://localhost/')
|
443
|
+
@mech.get('http://localhost/')
|
487
444
|
assert_equal(2, requests.length)
|
488
445
|
requests.each do |request|
|
489
446
|
assert_nil request['referer']
|
@@ -492,41 +449,42 @@ class TestMechanize < Test::Unit::TestCase
|
|
492
449
|
|
493
450
|
def test_get_scheme_unsupported
|
494
451
|
assert_raise(Mechanize::UnsupportedSchemeError) {
|
495
|
-
@
|
452
|
+
@mech.get('ftp://server.com/foo.html')
|
496
453
|
}
|
497
454
|
end
|
498
455
|
|
499
456
|
def test_get_space
|
500
457
|
page = nil
|
501
458
|
|
502
|
-
page = @
|
459
|
+
page = @mech.get("http://localhost/tc_bad_links.html ")
|
503
460
|
|
504
|
-
assert_match(/tc_bad_links.html$/, @
|
461
|
+
assert_match(/tc_bad_links.html$/, @mech.history.last.uri.to_s)
|
505
462
|
|
506
|
-
assert_equal(1, @
|
463
|
+
assert_equal(1, @mech.history.length)
|
507
464
|
end
|
508
465
|
|
509
466
|
def test_get_tilde
|
510
|
-
page = @
|
467
|
+
page = @mech.get('http://localhost/?foo=~2')
|
468
|
+
|
511
469
|
assert_equal('http://localhost/?foo=~2', page.uri.to_s)
|
512
470
|
end
|
513
471
|
|
514
472
|
def test_get_weird
|
515
473
|
assert_nothing_raised {
|
516
|
-
@
|
474
|
+
@mech.get('http://localhost/?action=bing&bang=boom=1|a=|b=|c=')
|
517
475
|
}
|
518
476
|
assert_nothing_raised {
|
519
|
-
@
|
477
|
+
@mech.get('http://localhost/?a=b&b=c&c=d')
|
520
478
|
}
|
521
479
|
assert_nothing_raised {
|
522
|
-
@
|
480
|
+
@mech.get("http://localhost/?a=#{[0xd6].pack('U')}")
|
523
481
|
}
|
524
482
|
end
|
525
483
|
|
526
484
|
def test_get_yield
|
527
485
|
pages = nil
|
528
486
|
|
529
|
-
@
|
487
|
+
@mech.get("http://localhost/file_upload.html") { |page|
|
530
488
|
pages = page
|
531
489
|
}
|
532
490
|
|
@@ -534,8 +492,14 @@ class TestMechanize < Test::Unit::TestCase
|
|
534
492
|
assert_equal('File Upload Form', pages.title)
|
535
493
|
end
|
536
494
|
|
495
|
+
def test_head
|
496
|
+
page = @mech.head('http://localhost/verb', { 'q' => 'foo' })
|
497
|
+
assert_equal 0, @mech.history.length
|
498
|
+
assert_equal 'HEAD', page.header['X-Request-Method']
|
499
|
+
end
|
500
|
+
|
537
501
|
def test_head_redirect
|
538
|
-
page = @
|
502
|
+
page = @mech.head('http://localhost/redirect')
|
539
503
|
|
540
504
|
assert_equal(page.uri.to_s, 'http://localhost/verb')
|
541
505
|
|
@@ -543,747 +507,305 @@ class TestMechanize < Test::Unit::TestCase
|
|
543
507
|
end
|
544
508
|
|
545
509
|
def test_history
|
546
|
-
|
547
|
-
assert_equal(i, @
|
548
|
-
|
510
|
+
2.times do |i|
|
511
|
+
assert_equal(i, @mech.history.size)
|
512
|
+
|
513
|
+
@mech.get("http://localhost/")
|
549
514
|
end
|
550
|
-
|
515
|
+
|
516
|
+
page = @mech.get("http://localhost/form_test.html")
|
551
517
|
|
552
518
|
assert_equal("http://localhost/form_test.html",
|
553
|
-
@
|
554
|
-
assert_equal("http://localhost/",
|
555
|
-
@agent.history[-2].uri.to_s)
|
519
|
+
@mech.history.last.uri.to_s)
|
556
520
|
assert_equal("http://localhost/",
|
557
|
-
@
|
558
|
-
|
559
|
-
assert_equal(true, @agent.visited?("http://localhost/"))
|
560
|
-
assert_equal(true, @agent.visited?("/form_test.html"))
|
561
|
-
assert_equal(false, @agent.visited?("http://google.com/"))
|
562
|
-
assert_equal(true, @agent.visited?(page.links.first))
|
521
|
+
@mech.history[-2].uri.to_s)
|
563
522
|
|
523
|
+
assert @mech.visited?("http://localhost/")
|
524
|
+
assert @mech.visited?("/form_test.html"), 'relative'
|
525
|
+
assert !@mech.visited?("http://google.com/")
|
526
|
+
assert @mech.visited?(page.links.first)
|
564
527
|
end
|
565
528
|
|
566
529
|
def test_history_order
|
567
|
-
@
|
568
|
-
assert_equal(0, @
|
530
|
+
@mech.max_history = 2
|
531
|
+
assert_equal(0, @mech.history.length)
|
569
532
|
|
570
|
-
@
|
571
|
-
assert_equal(1, @
|
533
|
+
@mech.get('http://localhost/form_test.html')
|
534
|
+
assert_equal(1, @mech.history.length)
|
572
535
|
|
573
|
-
@
|
574
|
-
assert_equal(2, @
|
536
|
+
@mech.get('http://localhost/empty_form.html')
|
537
|
+
assert_equal(2, @mech.history.length)
|
575
538
|
|
576
|
-
@
|
577
|
-
assert_equal(2, @
|
578
|
-
assert_equal('http://localhost/empty_form.html', @
|
539
|
+
@mech.get('http://localhost/tc_checkboxes.html')
|
540
|
+
assert_equal(2, @mech.history.length)
|
541
|
+
assert_equal('http://localhost/empty_form.html', @mech.history[0].uri.to_s)
|
579
542
|
assert_equal('http://localhost/tc_checkboxes.html',
|
580
|
-
@
|
543
|
+
@mech.history[1].uri.to_s)
|
581
544
|
end
|
582
545
|
|
583
546
|
def test_html_parser_equals
|
584
|
-
@
|
547
|
+
@mech.html_parser = {}
|
585
548
|
assert_raises(NoMethodError) {
|
586
|
-
@
|
549
|
+
@mech.get('http://localhost/?foo=~2').links
|
587
550
|
}
|
588
551
|
end
|
589
552
|
|
590
|
-
def test_http_request_file
|
591
|
-
uri = URI.parse 'file:///nonexistent'
|
592
|
-
request = @agent.http_request uri, :get
|
593
|
-
|
594
|
-
assert_kind_of Mechanize::FileRequest, request
|
595
|
-
assert_equal '/nonexistent', request.path
|
596
|
-
end
|
597
|
-
|
598
|
-
def test_http_request_get
|
599
|
-
request = @agent.http_request @uri, :get
|
600
|
-
|
601
|
-
assert_kind_of Net::HTTP::Get, request
|
602
|
-
assert_equal '/', request.path
|
603
|
-
end
|
604
|
-
|
605
|
-
def test_http_request_post
|
606
|
-
request = @agent.http_request @uri, :post
|
607
|
-
|
608
|
-
assert_kind_of Net::HTTP::Post, request
|
609
|
-
assert_equal '/', request.path
|
610
|
-
end
|
611
|
-
|
612
553
|
def test_max_history_equals
|
613
|
-
@
|
554
|
+
@mech.max_history = 10
|
614
555
|
0.upto(10) do |i|
|
615
|
-
assert_equal(i, @
|
616
|
-
@
|
556
|
+
assert_equal(i, @mech.history.size)
|
557
|
+
@mech.get("http://localhost/")
|
617
558
|
end
|
618
559
|
|
619
560
|
0.upto(10) do |i|
|
620
|
-
assert_equal(10, @
|
621
|
-
@
|
561
|
+
assert_equal(10, @mech.history.size)
|
562
|
+
@mech.get("http://localhost/")
|
622
563
|
end
|
623
564
|
end
|
624
565
|
|
625
|
-
def
|
626
|
-
|
627
|
-
alias :old_fetch_page :fetch_page
|
628
|
-
attr_accessor :requests
|
629
|
-
def fetch_page(uri, method, *args)
|
630
|
-
@requests ||= []
|
631
|
-
x = old_fetch_page(uri, method, *args)
|
632
|
-
@requests << method
|
633
|
-
x
|
634
|
-
end
|
635
|
-
end
|
636
|
-
@agent.basic_auth('user', 'pass')
|
637
|
-
page = @agent.post("http://localhost/basic_auth")
|
638
|
-
assert_equal('You are authenticated', page.body)
|
639
|
-
assert_equal(2, @agent.requests.length)
|
640
|
-
r1 = @agent.requests[0]
|
641
|
-
r2 = @agent.requests[1]
|
642
|
-
assert_equal(r1, r2)
|
643
|
-
end
|
566
|
+
def test_open_timeout_equals
|
567
|
+
@mech.open_timeout = 5
|
644
568
|
|
645
|
-
|
646
|
-
page = @agent.post('http://localhost/redirect')
|
647
|
-
|
648
|
-
assert_equal(page.uri.to_s, 'http://localhost/verb')
|
649
|
-
|
650
|
-
assert_equal 'GET', page.header['X-Request-Method']
|
569
|
+
assert_equal 5, @mech.open_timeout
|
651
570
|
end
|
652
571
|
|
653
|
-
def
|
654
|
-
|
655
|
-
|
656
|
-
|
657
|
-
|
572
|
+
def test_post_basic_auth
|
573
|
+
requests = []
|
574
|
+
|
575
|
+
@mech.pre_connect_hooks << proc { |agent, request|
|
576
|
+
requests << request.class
|
658
577
|
}
|
659
578
|
|
660
|
-
|
661
|
-
|
662
|
-
|
579
|
+
@mech.basic_auth('user', 'pass')
|
580
|
+
page = @mech.post("http://localhost/basic_auth")
|
581
|
+
assert_equal('You are authenticated', page.body)
|
582
|
+
assert_equal(2, requests.length)
|
583
|
+
r1 = requests[0]
|
584
|
+
r2 = requests[1]
|
585
|
+
assert_equal(r1, r2)
|
663
586
|
end
|
664
587
|
|
665
|
-
def
|
666
|
-
@
|
667
|
-
|
668
|
-
|
669
|
-
|
670
|
-
}
|
588
|
+
def test_post_multipart
|
589
|
+
page = @mech.post('http://localhost/file_upload', {
|
590
|
+
:name => 'Some file',
|
591
|
+
:userfile1 => File.open(__FILE__)
|
592
|
+
})
|
671
593
|
|
672
|
-
|
673
|
-
|
674
|
-
|
594
|
+
name = File.basename __FILE__
|
595
|
+
assert_match(
|
596
|
+
"Content-Disposition: form-data; name=\"userfile1\"; filename=\"#{name}\"",
|
597
|
+
page.body
|
598
|
+
)
|
599
|
+
assert page.body.length > File.read(__FILE__).length
|
675
600
|
end
|
676
601
|
|
677
|
-
def
|
678
|
-
page = @
|
602
|
+
def test_post_redirect
|
603
|
+
page = @mech.post('http://localhost/redirect')
|
679
604
|
|
680
605
|
assert_equal(page.uri.to_s, 'http://localhost/verb')
|
681
606
|
|
682
607
|
assert_equal 'GET', page.header['X-Request-Method']
|
683
608
|
end
|
684
609
|
|
685
|
-
def
|
686
|
-
|
687
|
-
|
688
|
-
|
689
|
-
end
|
690
|
-
|
691
|
-
@agent.request_cookies @req, uri
|
692
|
-
|
693
|
-
assert_equal 'hello=world domain=.example.com', @req['Cookie']
|
694
|
-
end
|
695
|
-
|
696
|
-
def test_request_cookies_none
|
697
|
-
@agent.request_cookies @req, @uri
|
698
|
-
|
699
|
-
assert_nil @req['Cookie']
|
700
|
-
end
|
701
|
-
|
702
|
-
def test_request_cookies_many
|
703
|
-
uri = URI.parse 'http://host.example.com'
|
704
|
-
cookie_str = 'a=b domain=.example.com, c=d domain=.example.com'
|
705
|
-
Mechanize::Cookie.parse uri, cookie_str do |cookie|
|
706
|
-
@agent.cookie_jar.add uri, cookie
|
707
|
-
end
|
708
|
-
|
709
|
-
@agent.request_cookies @req, uri
|
710
|
-
|
711
|
-
expected = cookie_str.sub ', ', '; '
|
712
|
-
|
713
|
-
assert_equal expected, @req['Cookie']
|
714
|
-
end
|
715
|
-
|
716
|
-
def test_request_cookies_wrong_domain
|
717
|
-
uri = URI.parse 'http://host.example.com'
|
718
|
-
Mechanize::Cookie.parse uri, 'hello=world domain=.example.com' do |cookie|
|
719
|
-
@agent.cookie_jar.add uri, cookie
|
720
|
-
end
|
721
|
-
|
722
|
-
@agent.request_cookies @req, @uri
|
723
|
-
|
724
|
-
assert_nil @req['Cookie']
|
725
|
-
end
|
726
|
-
|
727
|
-
def test_request_host
|
728
|
-
@agent.request_host @req, @uri
|
729
|
-
|
730
|
-
assert_equal 'example', @req['host']
|
731
|
-
end
|
732
|
-
|
733
|
-
def test_request_host_nonstandard
|
734
|
-
@uri.port = 81
|
735
|
-
|
736
|
-
@agent.request_host @req, @uri
|
737
|
-
|
738
|
-
assert_equal 'example:81', @req['host']
|
739
|
-
end
|
740
|
-
|
741
|
-
def test_request_language_charset
|
742
|
-
@agent.request_language_charset @req
|
743
|
-
|
744
|
-
assert_equal 'en-us,en;q=0.5', @req['accept-language']
|
745
|
-
assert_equal 'ISO-8859-1,utf-8;q=0.7,*;q=0.7', @req['accept-charset']
|
746
|
-
end
|
747
|
-
|
748
|
-
def test_request_add_headers
|
749
|
-
@agent.request_add_headers @req, 'Content-Length' => 300
|
750
|
-
|
751
|
-
assert_equal '300', @req['content-length']
|
752
|
-
end
|
753
|
-
|
754
|
-
def test_request_add_headers_etag
|
755
|
-
@agent.request_add_headers @req, :etag => '300'
|
756
|
-
|
757
|
-
assert_equal '300', @req['etag']
|
758
|
-
end
|
759
|
-
|
760
|
-
def test_request_add_headers_if_modified_since
|
761
|
-
@agent.request_add_headers @req, :if_modified_since => 'some_date'
|
762
|
-
|
763
|
-
assert_equal 'some_date', @req['if-modified-since']
|
764
|
-
end
|
765
|
-
|
766
|
-
def test_request_add_headers_none
|
767
|
-
@agent.request_add_headers @req
|
768
|
-
|
769
|
-
assert_equal @headers, @req.to_hash.keys.sort
|
770
|
-
end
|
771
|
-
|
772
|
-
def test_request_add_headers_request_headers
|
773
|
-
@agent.request_headers['X-Foo'] = 'bar'
|
774
|
-
|
775
|
-
@agent.request_add_headers @req
|
776
|
-
|
777
|
-
assert_equal @headers + %w[x-foo], @req.to_hash.keys.sort
|
778
|
-
end
|
779
|
-
|
780
|
-
def test_request_add_headers_symbol
|
781
|
-
e = assert_raises ArgumentError do
|
782
|
-
@agent.request_add_headers @req, :content_length => 300
|
783
|
-
end
|
784
|
-
|
785
|
-
assert_equal 'unknown header symbol content_length', e.message
|
786
|
-
end
|
787
|
-
|
788
|
-
def test_request_referer
|
789
|
-
referer = URI.parse 'http://old.example'
|
790
|
-
|
791
|
-
@agent.request_referer @req, @uri, referer
|
792
|
-
|
793
|
-
assert_equal 'http://old.example', @req['referer']
|
794
|
-
end
|
795
|
-
|
796
|
-
def test_request_referer_https
|
797
|
-
uri = URI.parse 'https://example'
|
798
|
-
referer = URI.parse 'https://old.example'
|
799
|
-
|
800
|
-
@agent.request_referer @req, uri, referer
|
801
|
-
|
802
|
-
assert_equal 'https://old.example', @req['referer']
|
803
|
-
end
|
804
|
-
|
805
|
-
def test_request_referer_https_downgrade
|
806
|
-
referer = URI.parse 'https://old.example'
|
807
|
-
|
808
|
-
@agent.request_referer @req, @uri, referer
|
809
|
-
|
810
|
-
assert_nil @req['referer']
|
811
|
-
end
|
812
|
-
|
813
|
-
def test_request_referer_https_downgrade_case
|
814
|
-
uri = URI.parse 'http://example'
|
815
|
-
referer = URI.parse 'httpS://old.example'
|
816
|
-
|
817
|
-
@agent.request_referer @req, uri, referer
|
818
|
-
|
819
|
-
assert_nil @req['referer']
|
820
|
-
end
|
821
|
-
|
822
|
-
def test_request_referer_none
|
823
|
-
@agent.request_referer @req, @uri, nil
|
824
|
-
|
825
|
-
assert_nil @req['referer']
|
826
|
-
end
|
827
|
-
|
828
|
-
def test_request_user_agent
|
829
|
-
@agent.request_user_agent @req
|
830
|
-
|
831
|
-
assert_match %r%^Mechanize/#{Mechanize::VERSION}%, @req['user-agent']
|
832
|
-
|
833
|
-
ruby_version = if RUBY_PATCHLEVEL >= 0 then
|
834
|
-
"#{RUBY_VERSION}p#{RUBY_PATCHLEVEL}"
|
835
|
-
else
|
836
|
-
"#{RUBY_VERSION}dev#{RUBY_REVISION}"
|
837
|
-
end
|
838
|
-
|
839
|
-
assert_match %r%Ruby/#{ruby_version}%, @req['user-agent']
|
840
|
-
end
|
841
|
-
|
842
|
-
def test_resolve_bad_uri
|
843
|
-
e = assert_raises ArgumentError do
|
844
|
-
@agent.resolve 'google'
|
845
|
-
end
|
846
|
-
|
847
|
-
assert_equal 'absolute URL needed (not google)', e.message
|
848
|
-
end
|
849
|
-
|
850
|
-
def test_resolve_utf8
|
851
|
-
uri = 'http://example?q=ü'
|
852
|
-
|
853
|
-
resolved = @agent.resolve uri
|
854
|
-
|
855
|
-
assert_equal '/?q=%C3%BC', resolved.request_uri
|
856
|
-
end
|
857
|
-
|
858
|
-
def test_resolve_parameters_body
|
859
|
-
input_params = { :q => 'hello' }
|
860
|
-
|
861
|
-
uri, params = @agent.resolve_parameters @uri, :post, input_params
|
862
|
-
|
863
|
-
assert_equal 'http://example/', uri.to_s
|
864
|
-
assert_equal input_params, params
|
865
|
-
end
|
866
|
-
|
867
|
-
def test_resolve_parameters_query
|
868
|
-
uri, params = @agent.resolve_parameters @uri, :get, :q => 'hello'
|
869
|
-
|
870
|
-
assert_equal 'http://example/?q=hello', uri.to_s
|
871
|
-
assert_nil params
|
872
|
-
end
|
873
|
-
|
874
|
-
def test_resolve_parameters_query_append
|
875
|
-
input_params = { :q => 'hello' }
|
876
|
-
@uri.query = 'a=b'
|
877
|
-
|
878
|
-
uri, params = @agent.resolve_parameters @uri, :get, input_params
|
879
|
-
|
880
|
-
assert_equal 'http://example/?a=b&q=hello', uri.to_s
|
881
|
-
assert_nil params
|
882
|
-
end
|
883
|
-
|
884
|
-
def test_response_cookies
|
885
|
-
uri = URI.parse 'http://host.example.com'
|
886
|
-
cookie_str = 'a=b domain=.example.com'
|
887
|
-
@res.instance_variable_set(:@header,
|
888
|
-
'set-cookie' => [cookie_str],
|
889
|
-
'content-type' => %w[text/html])
|
890
|
-
page = Mechanize::Page.new uri, @res, '', 200, @agent
|
891
|
-
|
892
|
-
@agent.response_cookies @res, uri, page
|
893
|
-
|
894
|
-
assert_equal ['a=b domain=.example.com'],
|
895
|
-
@agent.cookie_jar.cookies(uri).map { |c| c.to_s }
|
896
|
-
end
|
897
|
-
|
898
|
-
def test_response_cookies_meta
|
899
|
-
uri = URI.parse 'http://host.example.com'
|
900
|
-
cookie_str = 'a=b domain=.example.com'
|
901
|
-
|
902
|
-
body = <<-BODY
|
903
|
-
<head>
|
904
|
-
<meta http-equiv="Set-Cookie" content="#{cookie_str}">
|
905
|
-
</head>"
|
906
|
-
BODY
|
907
|
-
|
908
|
-
@res.instance_variable_set(:@header,
|
909
|
-
'content-type' => %w[text/html])
|
910
|
-
page = Mechanize::Page.new uri, @res, body, 200, @agent
|
911
|
-
|
912
|
-
@agent.response_cookies @res, uri, page
|
913
|
-
|
914
|
-
assert_equal ['a=b domain=.example.com'],
|
915
|
-
@agent.cookie_jar.cookies(uri).map { |c| c.to_s }
|
916
|
-
end
|
917
|
-
|
918
|
-
def test_response_follow_meta_refresh
|
919
|
-
uri = URI.parse 'http://example/#id+1'
|
920
|
-
|
921
|
-
body = <<-BODY
|
922
|
-
<title></title>
|
923
|
-
<meta http-equiv="refresh" content="0">
|
924
|
-
BODY
|
925
|
-
|
926
|
-
page = Mechanize::Page.new(uri, {'content-type' => 'text/html'}, body,
|
927
|
-
200, @agent)
|
928
|
-
|
929
|
-
@agent.follow_meta_refresh = true
|
930
|
-
|
931
|
-
page = @agent.response_follow_meta_refresh @res, uri, page, 0
|
932
|
-
|
933
|
-
assert_equal uri, page.uri
|
934
|
-
end
|
935
|
-
|
936
|
-
def test_response_read
|
937
|
-
def @res.read_body() yield 'part' end
|
938
|
-
def @res.content_length() 4 end
|
939
|
-
|
940
|
-
body = @agent.response_read @res, @req
|
941
|
-
|
942
|
-
assert_equal 'part', body
|
943
|
-
end
|
944
|
-
|
945
|
-
def test_response_read_content_length_head
|
946
|
-
req = Net::HTTP::Head.new '/'
|
947
|
-
|
948
|
-
def @res.content_length() end
|
949
|
-
def @res.read_body() end
|
950
|
-
|
951
|
-
body = @agent.response_read @res, req
|
952
|
-
|
953
|
-
assert_equal '', body
|
954
|
-
end
|
955
|
-
|
956
|
-
def test_response_read_content_length_mismatch
|
957
|
-
def @res.content_length() 5 end
|
958
|
-
def @res.read_body() yield 'part' end
|
959
|
-
|
960
|
-
e = assert_raises EOFError do
|
961
|
-
@agent.response_read @res, @req
|
962
|
-
end
|
963
|
-
|
964
|
-
assert_equal 'Content-Length (5) does not match response body length (4)',
|
965
|
-
e.message
|
966
|
-
end
|
967
|
-
|
968
|
-
def test_response_read_content_length_redirect
|
969
|
-
res = Net::HTTPFound.allocate
|
970
|
-
def res.content_length() 5 end
|
971
|
-
def res.code() 302 end
|
972
|
-
def res.read_body() yield 'part' end
|
973
|
-
res.instance_variable_set :@header, {}
|
974
|
-
|
975
|
-
body = @agent.response_read res, @req
|
976
|
-
|
977
|
-
assert_equal 'part', body
|
978
|
-
end
|
979
|
-
|
980
|
-
def test_response_read_encoding_7_bit
|
981
|
-
def @res.read_body() yield 'part' end
|
982
|
-
def @res.content_length() 4 end
|
983
|
-
@res.instance_variable_set :@header, 'content-encoding' => %w[7bit]
|
984
|
-
|
985
|
-
body = @agent.response_read @res, @req
|
986
|
-
|
987
|
-
assert_equal 'part', body
|
988
|
-
end
|
989
|
-
|
990
|
-
def test_response_read_encoding_deflate
|
991
|
-
def @res.read_body()
|
992
|
-
yield "x\x9C+H,*\x01\x00\x04?\x01\xB8"
|
993
|
-
end
|
994
|
-
def @res.content_length() 12 end
|
995
|
-
@res.instance_variable_set :@header, 'content-encoding' => %w[deflate]
|
996
|
-
|
997
|
-
body = @agent.response_read @res, @req
|
998
|
-
|
999
|
-
assert_equal 'part', body
|
1000
|
-
end
|
1001
|
-
|
1002
|
-
# IIS/6.0 ASP.NET/2.0.50727 does not wrap deflate with zlib, WTF?
|
1003
|
-
def test_response_read_encoding_deflate_no_zlib
|
1004
|
-
def @res.read_body()
|
1005
|
-
yield "+H,*\001\000"
|
1006
|
-
end
|
1007
|
-
def @res.content_length() 6 end
|
1008
|
-
@res.instance_variable_set :@header, 'content-encoding' => %w[deflate]
|
1009
|
-
|
1010
|
-
body = @agent.response_read @res, @req
|
1011
|
-
|
1012
|
-
assert_equal 'part', body
|
1013
|
-
end
|
1014
|
-
|
1015
|
-
def test_response_read_encoding_gzip
|
1016
|
-
def @res.read_body
|
1017
|
-
yield "\037\213\b\0002\002\225M\000\003"
|
1018
|
-
yield "+H,*\001\000\306p\017I\004\000\000\000"
|
1019
|
-
end
|
1020
|
-
def @res.content_length() 24 end
|
1021
|
-
@res.instance_variable_set :@header, 'content-encoding' => %w[gzip]
|
1022
|
-
|
1023
|
-
body = @agent.response_read @res, @req
|
1024
|
-
|
1025
|
-
assert_equal 'part', body
|
1026
|
-
end
|
1027
|
-
|
1028
|
-
def test_response_read_encoding_none
|
1029
|
-
def @res.read_body() yield 'part' end
|
1030
|
-
def @res.content_length() 4 end
|
1031
|
-
@res.instance_variable_set :@header, 'content-encoding' => %w[none]
|
1032
|
-
|
1033
|
-
body = @agent.response_read @res, @req
|
1034
|
-
|
1035
|
-
assert_equal 'part', body
|
610
|
+
def test_put
|
611
|
+
page = @mech.put('http://localhost/verb', 'foo')
|
612
|
+
assert_equal 1, @mech.history.length
|
613
|
+
assert_equal 'PUT', page.header['X-Request-Method']
|
1036
614
|
end
|
1037
615
|
|
1038
|
-
def
|
1039
|
-
|
1040
|
-
yield "\037\213\b\0002\002\225M\000\003"
|
1041
|
-
yield "+H,*\001\000\306p\017I\004\000\000\000"
|
1042
|
-
end
|
1043
|
-
def @res.content_length() 24 end
|
1044
|
-
@res.instance_variable_set :@header, 'content-encoding' => %w[x-gzip]
|
616
|
+
def test_put_redirect
|
617
|
+
page = @mech.put('http://localhost/redirect', 'foo')
|
1045
618
|
|
1046
|
-
|
619
|
+
assert_equal(page.uri.to_s, 'http://localhost/verb')
|
1047
620
|
|
1048
|
-
assert_equal '
|
621
|
+
assert_equal 'GET', page.header['X-Request-Method']
|
1049
622
|
end
|
1050
623
|
|
1051
|
-
def
|
1052
|
-
|
1053
|
-
def @res.content_length() 4 end
|
1054
|
-
@res.instance_variable_set :@header, 'content-encoding' => %w[unknown]
|
624
|
+
def test_read_timeout_equals
|
625
|
+
@mech.read_timeout = 5
|
1055
626
|
|
1056
|
-
|
1057
|
-
@agent.response_read @res, @req
|
1058
|
-
end
|
1059
|
-
|
1060
|
-
assert_equal 'Unsupported Content-Encoding: unknown', e.message
|
627
|
+
assert_equal 5, @mech.read_timeout
|
1061
628
|
end
|
1062
629
|
|
1063
|
-
def
|
1064
|
-
|
1065
|
-
|
1066
|
-
|
1067
|
-
tempfile.rewind
|
1068
|
-
|
1069
|
-
uri = URI.parse "file://#{tempfile.path}"
|
1070
|
-
req = Mechanize::FileRequest.new uri
|
1071
|
-
res = Mechanize::FileResponse.new tempfile.path
|
1072
|
-
|
1073
|
-
body = @agent.response_read res, req
|
1074
|
-
|
1075
|
-
expected = "π\n"
|
1076
|
-
expected.force_encoding Encoding::BINARY if expected.respond_to? :encoding
|
1077
|
-
|
1078
|
-
assert_equal expected, body
|
1079
|
-
assert_equal Encoding::BINARY, body.encoding if body.respond_to? :encoding
|
630
|
+
def test_submit_bad_form_method
|
631
|
+
page = @mech.get("http://localhost/bad_form_test.html")
|
632
|
+
assert_raise ArgumentError do
|
633
|
+
@mech.submit(page.forms.first)
|
1080
634
|
end
|
1081
635
|
end
|
1082
636
|
|
1083
|
-
def
|
1084
|
-
|
1085
|
-
|
1086
|
-
|
1087
|
-
def @res.read_body() end
|
1088
|
-
|
1089
|
-
body = @agent.response_read @res, req
|
1090
|
-
|
1091
|
-
assert_equal '', body
|
1092
|
-
end
|
1093
|
-
|
1094
|
-
def test_response_read_unknown_code
|
1095
|
-
res = Net::HTTPUnknownResponse.allocate
|
1096
|
-
res.instance_variable_set :@code, 9999
|
1097
|
-
def res.read_body() yield 'part' end
|
637
|
+
def test_submit_check_one
|
638
|
+
page = @mech.get('http://localhost/tc_checkboxes.html')
|
639
|
+
form = page.forms.first
|
640
|
+
form.checkboxes_with(:name => 'green')[1].check
|
1098
641
|
|
1099
|
-
|
1100
|
-
@agent.response_read res, @req
|
1101
|
-
end
|
642
|
+
page = @mech.submit(form)
|
1102
643
|
|
1103
|
-
assert_equal
|
644
|
+
assert_equal(1, page.links.length)
|
645
|
+
assert_equal('green:on', page.links.first.text)
|
1104
646
|
end
|
1105
647
|
|
1106
|
-
def
|
1107
|
-
|
1108
|
-
|
648
|
+
def test_submit_check_two
|
649
|
+
page = @mech.get('http://localhost/tc_checkboxes.html')
|
650
|
+
form = page.forms.first
|
651
|
+
form.checkboxes_with(:name => 'green')[0].check
|
652
|
+
form.checkboxes_with(:name => 'green')[1].check
|
1109
653
|
|
1110
|
-
page = @
|
654
|
+
page = @mech.submit(form)
|
1111
655
|
|
1112
|
-
|
1113
|
-
assert_equal
|
656
|
+
assert_equal(2, page.links.length)
|
657
|
+
assert_equal('green:on', page.links[0].text)
|
658
|
+
assert_equal('green:on', page.links[1].text)
|
1114
659
|
end
|
1115
660
|
|
1116
|
-
def
|
1117
|
-
|
1118
|
-
|
661
|
+
def test_submit_enctype
|
662
|
+
page = @mech.get("http://localhost/file_upload.html")
|
663
|
+
assert_equal('multipart/form-data', page.forms[0].enctype)
|
1119
664
|
|
1120
|
-
|
1121
|
-
|
1122
|
-
|
1123
|
-
|
1124
|
-
|
665
|
+
form = page.forms.first
|
666
|
+
form.file_uploads.first.file_name = "#{BASE_DIR}/helper.rb"
|
667
|
+
form.file_uploads.first.mime_type = "text/plain"
|
668
|
+
form.file_uploads.first.file_data = "Hello World\n\n"
|
669
|
+
|
670
|
+
page = @mech.submit(form)
|
671
|
+
|
672
|
+
assert_match(
|
673
|
+
"Content-Disposition: form-data; name=\"userfile1\"; filename=\"helper.rb\"",
|
674
|
+
page.body
|
675
|
+
)
|
676
|
+
assert_match(
|
677
|
+
"Content-Disposition: form-data; name=\"name\"",
|
678
|
+
page.body
|
679
|
+
)
|
680
|
+
assert_match('Content-Type: text/plain', page.body)
|
681
|
+
assert_match('Hello World', page.body)
|
682
|
+
assert_match('foo[aaron]', page.body)
|
1125
683
|
end
|
1126
684
|
|
1127
|
-
def
|
1128
|
-
|
1129
|
-
|
1130
|
-
'content-type' =>
|
1131
|
-
%w[text/html;charset=ISO-8859-1])
|
685
|
+
def test_submit_file_data
|
686
|
+
page = @mech.get("http://localhost/file_upload.html")
|
687
|
+
assert_equal('multipart/form-data', page.forms[1].enctype)
|
1132
688
|
|
1133
|
-
|
689
|
+
form = page.forms[1]
|
690
|
+
form.file_uploads.first.file_name = "#{BASE_DIR}/helper.rb"
|
691
|
+
form.file_uploads.first.file_data = File.open("#{BASE_DIR}/helper.rb", 'rb')
|
1134
692
|
|
1135
|
-
|
1136
|
-
assert_equal @agent, page.mech
|
693
|
+
page = @mech.submit(form)
|
1137
694
|
|
1138
|
-
|
1139
|
-
|
695
|
+
contents = File.open("#{BASE_DIR}/helper.rb", 'rb') { |f| f.read }
|
696
|
+
assert_match(
|
697
|
+
"Content-Disposition: form-data; name=\"green[eggs]\"; filename=\"helper.rb\"",
|
698
|
+
page.body
|
699
|
+
)
|
700
|
+
assert_match(contents, page.body)
|
1140
701
|
end
|
1141
702
|
|
1142
|
-
def
|
1143
|
-
|
1144
|
-
|
1145
|
-
'content-type' =>
|
1146
|
-
%w[text/html; charset=garbage_charset])
|
703
|
+
def test_submit_file_name
|
704
|
+
page = @mech.get("http://localhost/file_upload.html")
|
705
|
+
assert_equal('multipart/form-data', page.forms[1].enctype)
|
1147
706
|
|
1148
|
-
|
707
|
+
form = page.forms[1]
|
708
|
+
form.file_uploads.first.file_name = "#{BASE_DIR}/helper.rb"
|
1149
709
|
|
1150
|
-
|
1151
|
-
assert_equal @agent, page.mech
|
1152
|
-
end
|
1153
|
-
|
1154
|
-
def test_response_parse_content_type_encoding_broken_iso_8859_1
|
1155
|
-
body = '<title>hi</title>'
|
1156
|
-
@res.instance_variable_set(:@header,
|
1157
|
-
'content-type' =>
|
1158
|
-
%w[text/html; charset=ISO_8859-1])
|
1159
|
-
|
1160
|
-
page = @agent.response_parse @res, body, @uri
|
710
|
+
page = @mech.submit(form)
|
1161
711
|
|
1162
|
-
|
1163
|
-
|
712
|
+
contents = File.open("#{BASE_DIR}/helper.rb", 'rb') { |f| f.read }
|
713
|
+
assert_match(
|
714
|
+
"Content-Disposition: form-data; name=\"green[eggs]\"; filename=\"helper.rb\"",
|
715
|
+
page.body
|
716
|
+
)
|
717
|
+
assert_match(contents, page.body)
|
1164
718
|
end
|
1165
719
|
|
1166
|
-
def
|
1167
|
-
|
1168
|
-
@res.instance_variable_set(:@header,
|
1169
|
-
'content-type' =>
|
1170
|
-
%w[text/html; charset=UTF8])
|
1171
|
-
|
1172
|
-
page = @agent.response_parse @res, body, @uri
|
1173
|
-
|
1174
|
-
assert_instance_of Mechanize::Page, page
|
1175
|
-
assert_equal 'UTF8', page.encoding
|
1176
|
-
assert_equal 'UTF8', page.parser.encoding
|
1177
|
-
end
|
720
|
+
def test_submit_headers
|
721
|
+
page = @mech.get 'http://localhost:2000/form_no_action.html'
|
1178
722
|
|
1179
|
-
|
1180
|
-
|
1181
|
-
@res.instance_variable_set(:@header,
|
1182
|
-
'content-type' =>
|
1183
|
-
%w[text/html;charset=UTF-8;])
|
723
|
+
assert form = page.forms.first
|
724
|
+
form.action = '/http_headers'
|
1184
725
|
|
1185
|
-
page = @
|
726
|
+
page = @mech.submit form, nil, 'foo' => 'bar'
|
1186
727
|
|
1187
|
-
|
728
|
+
headers = page.body.split("\n").map { |x| x.split('|', 2) }.flatten
|
729
|
+
headers = Hash[*headers]
|
1188
730
|
|
1189
|
-
assert_equal '
|
731
|
+
assert_equal 'bar', headers['foo']
|
1190
732
|
end
|
1191
733
|
|
1192
|
-
def
|
1193
|
-
@
|
734
|
+
def test_submit_multipart
|
735
|
+
page = @mech.get("http://localhost/file_upload.html")
|
1194
736
|
|
1195
|
-
assert_equal(
|
1196
|
-
assert_equal(@agent.http.proxy_uri.port, 9001)
|
1197
|
-
assert_equal(@agent.http.proxy_uri.user, 'joe')
|
1198
|
-
assert_equal(@agent.http.proxy_uri.password, 'lol')
|
1199
|
-
end
|
737
|
+
assert_equal('multipart/form-data', page.forms[1].enctype)
|
1200
738
|
|
1201
|
-
|
1202
|
-
|
1203
|
-
|
1204
|
-
|
1205
|
-
end
|
1206
|
-
end
|
1207
|
-
|
1208
|
-
def test_submit_check_one
|
1209
|
-
page = @agent.get('http://localhost/tc_checkboxes.html')
|
1210
|
-
form = page.forms.first
|
1211
|
-
form.checkboxes_with(:name => 'green')[1].check
|
739
|
+
form = page.forms[1]
|
740
|
+
form.file_uploads.first.file_name = "#{BASE_DIR}/helper.rb"
|
741
|
+
form.file_uploads.first.mime_type = "text/plain"
|
742
|
+
form.file_uploads.first.file_data = "Hello World\n\n"
|
1212
743
|
|
1213
|
-
page = @
|
744
|
+
page = @mech.submit(form)
|
1214
745
|
|
1215
|
-
|
1216
|
-
|
746
|
+
assert_match(
|
747
|
+
"Content-Disposition: form-data; name=\"green[eggs]\"; filename=\"helper.rb\"",
|
748
|
+
page.body
|
749
|
+
)
|
1217
750
|
end
|
1218
751
|
|
1219
|
-
def
|
1220
|
-
page = @
|
752
|
+
def test_submit_no_file
|
753
|
+
page = @mech.get("http://localhost/file_upload.html")
|
1221
754
|
form = page.forms.first
|
1222
|
-
form.
|
1223
|
-
|
1224
|
-
|
1225
|
-
|
1226
|
-
|
1227
|
-
|
1228
|
-
|
1229
|
-
assert_equal('green:on', page.links[1].text)
|
1230
|
-
end
|
1231
|
-
|
1232
|
-
def test_submit_headers
|
1233
|
-
page = @agent.get('http://localhost:2000/form_no_action.html')
|
1234
|
-
assert form = page.forms.first
|
1235
|
-
form.action = '/http_headers'
|
1236
|
-
page = @agent.submit(form, nil, { 'foo' => 'bar' })
|
1237
|
-
headers = Hash[*(
|
1238
|
-
page.body.split("\n").map { |x| x.split('|') }.flatten
|
1239
|
-
)]
|
1240
|
-
assert_equal 'bar', headers['foo']
|
755
|
+
form.field_with(:name => 'name').value = 'Aaron'
|
756
|
+
@page = @mech.submit(form)
|
757
|
+
assert_match('Aaron', @page.body)
|
758
|
+
assert_match(
|
759
|
+
"Content-Disposition: form-data; name=\"userfile1\"; filename=\"\"",
|
760
|
+
@page.body
|
761
|
+
)
|
1241
762
|
end
|
1242
763
|
|
1243
764
|
def test_submit_too_many_radiobuttons
|
1244
|
-
page = @
|
765
|
+
page = @mech.get("http://localhost/form_test.html")
|
1245
766
|
form = page.form_with(:name => 'post_form1')
|
1246
767
|
form.radiobuttons.each { |r| r.checked = true }
|
1247
768
|
|
1248
769
|
assert_raises Mechanize::Error do
|
1249
|
-
@
|
770
|
+
@mech.submit(form)
|
1250
771
|
end
|
1251
772
|
end
|
1252
773
|
|
1253
774
|
def test_transact
|
1254
|
-
@
|
1255
|
-
assert_equal(1, @
|
1256
|
-
@
|
775
|
+
@mech.get("http://localhost/frame_test.html")
|
776
|
+
assert_equal(1, @mech.history.length)
|
777
|
+
@mech.transact { |a|
|
1257
778
|
5.times {
|
1258
|
-
@
|
779
|
+
@mech.get("http://localhost/frame_test.html")
|
1259
780
|
}
|
1260
|
-
assert_equal(6, @
|
781
|
+
assert_equal(6, @mech.history.length)
|
1261
782
|
}
|
1262
|
-
assert_equal(1, @
|
783
|
+
assert_equal(1, @mech.history.length)
|
1263
784
|
end
|
1264
785
|
|
1265
786
|
def test_user_agent_alias_equals_unknown
|
1266
787
|
assert_raises ArgumentError do
|
1267
|
-
@
|
788
|
+
@mech.user_agent_alias = "Aaron's Browser"
|
1268
789
|
end
|
1269
790
|
end
|
1270
791
|
|
1271
792
|
def test_visited_eh
|
1272
|
-
@
|
1273
|
-
|
1274
|
-
|
1275
|
-
|
1276
|
-
|
1277
|
-
|
1278
|
-
|
793
|
+
@mech.get("http://localhost/content_type_test?ct=application/pdf")
|
794
|
+
|
795
|
+
assert \
|
796
|
+
@mech.visited?("http://localhost/content_type_test?ct=application/pdf")
|
797
|
+
assert \
|
798
|
+
!@mech.visited?("http://localhost/content_type_test")
|
799
|
+
assert \
|
800
|
+
!@mech.visited?("http://localhost/content_type_test?ct=text/html")
|
1279
801
|
end
|
1280
802
|
|
1281
803
|
def test_visited_eh_redirect
|
1282
|
-
@
|
1283
|
-
|
1284
|
-
|
1285
|
-
|
1286
|
-
|
804
|
+
@mech.get("http://localhost/response_code?code=302")
|
805
|
+
|
806
|
+
assert_equal("http://localhost/index.html", @mech.current_page.uri.to_s)
|
807
|
+
|
808
|
+
assert @mech.visited?('http://localhost/response_code?code=302')
|
1287
809
|
end
|
1288
810
|
|
1289
811
|
def assert_header(page, header)
|