mechanize-ntlm 0.9.1
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.rdoc +480 -0
- data/EXAMPLES.rdoc +171 -0
- data/FAQ.rdoc +11 -0
- data/GUIDE.rdoc +122 -0
- data/LICENSE.rdoc +340 -0
- data/Manifest.txt +169 -0
- data/README.rdoc +60 -0
- data/Rakefile +44 -0
- data/examples/flickr_upload.rb +23 -0
- data/examples/mech-dump.rb +7 -0
- data/examples/proxy_req.rb +9 -0
- data/examples/rubyforge.rb +21 -0
- data/examples/spider.rb +11 -0
- data/lib/mechanize-ntlm.rb +7 -0
- data/lib/www/mechanize.rb +582 -0
- data/lib/www/mechanize/chain.rb +34 -0
- data/lib/www/mechanize/chain/auth_headers.rb +82 -0
- data/lib/www/mechanize/chain/body_decoding_handler.rb +43 -0
- data/lib/www/mechanize/chain/connection_resolver.rb +78 -0
- data/lib/www/mechanize/chain/custom_headers.rb +23 -0
- data/lib/www/mechanize/chain/handler.rb +9 -0
- data/lib/www/mechanize/chain/header_resolver.rb +48 -0
- data/lib/www/mechanize/chain/parameter_resolver.rb +23 -0
- data/lib/www/mechanize/chain/post_connect_hook.rb +0 -0
- data/lib/www/mechanize/chain/pre_connect_hook.rb +22 -0
- data/lib/www/mechanize/chain/request_resolver.rb +32 -0
- data/lib/www/mechanize/chain/response_body_parser.rb +40 -0
- data/lib/www/mechanize/chain/response_header_handler.rb +51 -0
- data/lib/www/mechanize/chain/response_reader.rb +41 -0
- data/lib/www/mechanize/chain/ssl_resolver.rb +36 -0
- data/lib/www/mechanize/chain/uri_resolver.rb +73 -0
- data/lib/www/mechanize/content_type_error.rb +16 -0
- data/lib/www/mechanize/cookie.rb +72 -0
- data/lib/www/mechanize/cookie_jar.rb +191 -0
- data/lib/www/mechanize/file.rb +73 -0
- data/lib/www/mechanize/file_response.rb +62 -0
- data/lib/www/mechanize/file_saver.rb +39 -0
- data/lib/www/mechanize/form.rb +359 -0
- data/lib/www/mechanize/form/button.rb +8 -0
- data/lib/www/mechanize/form/check_box.rb +13 -0
- data/lib/www/mechanize/form/field.rb +28 -0
- data/lib/www/mechanize/form/file_upload.rb +24 -0
- data/lib/www/mechanize/form/image_button.rb +23 -0
- data/lib/www/mechanize/form/multi_select_list.rb +69 -0
- data/lib/www/mechanize/form/option.rb +51 -0
- data/lib/www/mechanize/form/radio_button.rb +38 -0
- data/lib/www/mechanize/form/select_list.rb +45 -0
- data/lib/www/mechanize/headers.rb +12 -0
- data/lib/www/mechanize/history.rb +67 -0
- data/lib/www/mechanize/inspect.rb +90 -0
- data/lib/www/mechanize/monkey_patch.rb +37 -0
- data/lib/www/mechanize/page.rb +145 -0
- data/lib/www/mechanize/page/base.rb +10 -0
- data/lib/www/mechanize/page/frame.rb +22 -0
- data/lib/www/mechanize/page/link.rb +50 -0
- data/lib/www/mechanize/page/meta.rb +10 -0
- data/lib/www/mechanize/pluggable_parsers.rb +103 -0
- data/lib/www/mechanize/redirect_limit_reached_error.rb +18 -0
- data/lib/www/mechanize/redirect_not_get_or_head_error.rb +20 -0
- data/lib/www/mechanize/response_code_error.rb +25 -0
- data/lib/www/mechanize/unsupported_scheme_error.rb +10 -0
- data/lib/www/mechanize/util.rb +76 -0
- data/lib/www/ntlm-http/lib/net/ntlm_http.rb +854 -0
- data/mechanize.gemspec +24 -0
- data/test/chain/test_argument_validator.rb +14 -0
- data/test/chain/test_custom_headers.rb +18 -0
- data/test/chain/test_parameter_resolver.rb +35 -0
- data/test/chain/test_request_resolver.rb +29 -0
- data/test/chain/test_response_reader.rb +24 -0
- data/test/data/htpasswd +1 -0
- data/test/data/server.crt +16 -0
- data/test/data/server.csr +12 -0
- data/test/data/server.key +15 -0
- data/test/data/server.pem +15 -0
- data/test/helper.rb +127 -0
- data/test/htdocs/alt_text.html +10 -0
- data/test/htdocs/bad_form_test.html +9 -0
- data/test/htdocs/button.jpg +0 -0
- data/test/htdocs/empty_form.html +6 -0
- data/test/htdocs/file_upload.html +26 -0
- data/test/htdocs/find_link.html +41 -0
- data/test/htdocs/form_multi_select.html +16 -0
- data/test/htdocs/form_multival.html +37 -0
- data/test/htdocs/form_no_action.html +18 -0
- data/test/htdocs/form_no_input_name.html +16 -0
- data/test/htdocs/form_select.html +16 -0
- data/test/htdocs/form_select_all.html +16 -0
- data/test/htdocs/form_select_none.html +17 -0
- data/test/htdocs/form_select_noopts.html +10 -0
- data/test/htdocs/form_set_fields.html +14 -0
- data/test/htdocs/form_test.html +188 -0
- data/test/htdocs/frame_test.html +30 -0
- data/test/htdocs/google.html +13 -0
- data/test/htdocs/iframe_test.html +16 -0
- data/test/htdocs/index.html +6 -0
- data/test/htdocs/link with space.html +5 -0
- data/test/htdocs/meta_cookie.html +11 -0
- data/test/htdocs/no_title_test.html +6 -0
- data/test/htdocs/relative/tc_relative_links.html +21 -0
- data/test/htdocs/tc_bad_links.html +5 -0
- data/test/htdocs/tc_base_link.html +8 -0
- data/test/htdocs/tc_blank_form.html +11 -0
- data/test/htdocs/tc_checkboxes.html +19 -0
- data/test/htdocs/tc_encoded_links.html +5 -0
- data/test/htdocs/tc_follow_meta.html +8 -0
- data/test/htdocs/tc_form_action.html +48 -0
- data/test/htdocs/tc_links.html +18 -0
- data/test/htdocs/tc_no_attributes.html +16 -0
- data/test/htdocs/tc_pretty_print.html +17 -0
- data/test/htdocs/tc_radiobuttons.html +17 -0
- data/test/htdocs/tc_referer.html +10 -0
- data/test/htdocs/tc_relative_links.html +19 -0
- data/test/htdocs/tc_textarea.html +23 -0
- data/test/htdocs/unusual______.html +5 -0
- data/test/servlets.rb +339 -0
- data/test/ssl_server.rb +48 -0
- data/test/test_authenticate.rb +71 -0
- data/test/test_bad_links.rb +25 -0
- data/test/test_blank_form.rb +16 -0
- data/test/test_checkboxes.rb +61 -0
- data/test/test_content_type.rb +13 -0
- data/test/test_cookie_class.rb +338 -0
- data/test/test_cookie_jar.rb +343 -0
- data/test/test_cookies.rb +123 -0
- data/test/test_encoded_links.rb +20 -0
- data/test/test_errors.rb +49 -0
- data/test/test_follow_meta.rb +69 -0
- data/test/test_form_action.rb +44 -0
- data/test/test_form_as_hash.rb +61 -0
- data/test/test_form_button.rb +38 -0
- data/test/test_form_no_inputname.rb +15 -0
- data/test/test_forms.rb +575 -0
- data/test/test_frames.rb +25 -0
- data/test/test_get_headers.rb +52 -0
- data/test/test_gzipping.rb +22 -0
- data/test/test_hash_api.rb +45 -0
- data/test/test_history.rb +142 -0
- data/test/test_history_added.rb +16 -0
- data/test/test_html_unscape_forms.rb +39 -0
- data/test/test_if_modified_since.rb +20 -0
- data/test/test_keep_alive.rb +31 -0
- data/test/test_links.rb +120 -0
- data/test/test_mech.rb +259 -0
- data/test/test_mechanize_file.rb +47 -0
- data/test/test_multi_select.rb +106 -0
- data/test/test_no_attributes.rb +13 -0
- data/test/test_option.rb +18 -0
- data/test/test_page.rb +67 -0
- data/test/test_pluggable_parser.rb +145 -0
- data/test/test_post_form.rb +34 -0
- data/test/test_pretty_print.rb +22 -0
- data/test/test_radiobutton.rb +75 -0
- data/test/test_redirect_limit_reached.rb +41 -0
- data/test/test_redirect_verb_handling.rb +45 -0
- data/test/test_referer.rb +39 -0
- data/test/test_relative_links.rb +40 -0
- data/test/test_request.rb +13 -0
- data/test/test_response_code.rb +52 -0
- data/test/test_save_file.rb +48 -0
- data/test/test_scheme.rb +48 -0
- data/test/test_select.rb +106 -0
- data/test/test_select_all.rb +15 -0
- data/test/test_select_none.rb +15 -0
- data/test/test_select_noopts.rb +16 -0
- data/test/test_set_fields.rb +44 -0
- data/test/test_ssl_server.rb +20 -0
- data/test/test_subclass.rb +14 -0
- data/test/test_textarea.rb +45 -0
- data/test/test_upload.rb +109 -0
- data/test/test_verbs.rb +25 -0
- metadata +284 -0
data/test/test_frames.rb
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
require File.expand_path(File.join(File.dirname(__FILE__), "helper"))
|
2
|
+
|
3
|
+
class FramesMechTest < Test::Unit::TestCase
|
4
|
+
def setup
|
5
|
+
@agent = WWW::Mechanize.new
|
6
|
+
end
|
7
|
+
|
8
|
+
def test_frames
|
9
|
+
page = @agent.get("http://localhost/frame_test.html")
|
10
|
+
assert_equal(3, page.frames.size)
|
11
|
+
assert_equal("frame1", page.frames[0].name)
|
12
|
+
assert_equal("frame2", page.frames[1].name)
|
13
|
+
assert_equal("frame3", page.frames[2].name)
|
14
|
+
assert_equal("/google.html", page.frames[0].src)
|
15
|
+
assert_equal("/form_test.html", page.frames[1].src)
|
16
|
+
assert_equal("/file_upload.html", page.frames[2].src)
|
17
|
+
end
|
18
|
+
|
19
|
+
def test_iframes
|
20
|
+
page = @agent.get("http://localhost/iframe_test.html")
|
21
|
+
assert_equal(1, page.iframes.size)
|
22
|
+
assert_equal("frame4", page.iframes.first.name)
|
23
|
+
assert_equal("/file_upload.html", page.iframes.first.src)
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
require File.expand_path(File.join(File.dirname(__FILE__), "helper"))
|
2
|
+
|
3
|
+
class TestGetHeaders < Test::Unit::TestCase
|
4
|
+
def setup
|
5
|
+
@agent = WWW::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
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require File.expand_path(File.join(File.dirname(__FILE__), "helper"))
|
2
|
+
|
3
|
+
class TestGzip < Test::Unit::TestCase
|
4
|
+
def setup
|
5
|
+
@agent = WWW::Mechanize.new
|
6
|
+
end
|
7
|
+
|
8
|
+
def test_request_empty_gzip
|
9
|
+
assert_nothing_raised do
|
10
|
+
page = @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(WWW::Mechanize::Page, page)
|
20
|
+
assert_match('Hello World', page.body)
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
require File.expand_path(File.join(File.dirname(__FILE__), "helper"))
|
2
|
+
|
3
|
+
class TestHashApi < Test::Unit::TestCase
|
4
|
+
def setup
|
5
|
+
@agent = WWW::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 { |params|
|
30
|
+
request = params[:request]
|
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
|
@@ -0,0 +1,142 @@
|
|
1
|
+
require File.expand_path(File.join(File.dirname(__FILE__), "helper"))
|
2
|
+
|
3
|
+
class TestHistory < Test::Unit::TestCase
|
4
|
+
def setup
|
5
|
+
@agent = WWW::Mechanize.new
|
6
|
+
@history = WWW::Mechanize::History.new
|
7
|
+
end
|
8
|
+
|
9
|
+
def test_push
|
10
|
+
assert_equal(0, @history.length)
|
11
|
+
|
12
|
+
page = @agent.get("http://localhost/tc_bad_links.html")
|
13
|
+
x = @history.push(page)
|
14
|
+
assert_equal(x, @history)
|
15
|
+
assert_equal(1, @history.length)
|
16
|
+
assert(@history.visited?(page))
|
17
|
+
assert(@history.visited?(page.uri))
|
18
|
+
assert(@history.visited?(page.uri.to_s))
|
19
|
+
assert_equal(page, @history.visited_page(page))
|
20
|
+
assert_equal(page, @history.visited_page(page.uri))
|
21
|
+
assert_equal(page, @history.visited_page(page.uri.to_s))
|
22
|
+
|
23
|
+
@history.push(@agent.get("/tc_bad_links.html"))
|
24
|
+
assert_equal(2, @history.length)
|
25
|
+
end
|
26
|
+
|
27
|
+
def test_shift
|
28
|
+
assert_equal(0, @history.length)
|
29
|
+
page = @agent.get("http://localhost/tc_bad_links.html")
|
30
|
+
@history.push(page)
|
31
|
+
assert_equal(1, @history.length)
|
32
|
+
|
33
|
+
@history.push(@agent.get("/tc_bad_links.html"))
|
34
|
+
assert_equal(2, @history.length)
|
35
|
+
|
36
|
+
@history.push(@agent.get("/index.html"))
|
37
|
+
assert_equal(3, @history.length)
|
38
|
+
|
39
|
+
page2 = @history.shift
|
40
|
+
assert_equal(page, page2)
|
41
|
+
assert_equal(2, @history.length)
|
42
|
+
|
43
|
+
@history.shift
|
44
|
+
assert_equal(1, @history.length)
|
45
|
+
assert_equal(false, @history.visited?(page))
|
46
|
+
|
47
|
+
@history.shift
|
48
|
+
assert_equal(0, @history.length)
|
49
|
+
|
50
|
+
assert_nil(@history.shift)
|
51
|
+
assert_equal(0, @history.length)
|
52
|
+
end
|
53
|
+
|
54
|
+
def test_pop
|
55
|
+
assert_equal(0, @history.length)
|
56
|
+
page = @agent.get("http://localhost/tc_bad_links.html")
|
57
|
+
@history.push(page)
|
58
|
+
assert_equal(1, @history.length)
|
59
|
+
|
60
|
+
page2 = @agent.get("/index.html")
|
61
|
+
@history.push(page2)
|
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)
|
72
|
+
end
|
73
|
+
|
74
|
+
def test_max_size
|
75
|
+
@history = WWW::Mechanize::History.new(10)
|
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
|
86
|
+
|
87
|
+
@history.clear
|
88
|
+
@history.max_size = 5
|
89
|
+
1.upto(20) do |i|
|
90
|
+
page = @agent.get('http://localhost/index.html')
|
91
|
+
@history.push page
|
92
|
+
assert_equal(true, @history.visited?(page))
|
93
|
+
if i < 5
|
94
|
+
assert_equal(i, @history.length)
|
95
|
+
else
|
96
|
+
assert_equal(5, @history.length)
|
97
|
+
end
|
98
|
+
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
|
+
end
|
108
|
+
|
109
|
+
def test_no_slash
|
110
|
+
page = @agent.get('http://localhost')
|
111
|
+
|
112
|
+
node = Struct.new(:href, :inner_text).new('http://localhost/', 'blah')
|
113
|
+
link = WWW::Mechanize::Page::Link.new(node, nil, nil)
|
114
|
+
assert(@agent.visited?(link))
|
115
|
+
|
116
|
+
node = Struct.new(:href, :inner_text).new('http://localhost', 'blah')
|
117
|
+
link = WWW::Mechanize::Page::Link.new(node, nil, nil)
|
118
|
+
assert(@agent.visited?(link))
|
119
|
+
end
|
120
|
+
|
121
|
+
def test_with_slash
|
122
|
+
page = @agent.get('http://localhost/')
|
123
|
+
|
124
|
+
node = Struct.new(:href, :inner_text).new('http://localhost/', 'blah')
|
125
|
+
link = WWW::Mechanize::Page::Link.new(node, nil, nil)
|
126
|
+
assert(@agent.visited?(link))
|
127
|
+
|
128
|
+
node = Struct.new(:href, :inner_text).new('http://localhost', 'blah')
|
129
|
+
link = WWW::Mechanize::Page::Link.new(node, nil, nil)
|
130
|
+
assert(@agent.visited?(link))
|
131
|
+
end
|
132
|
+
|
133
|
+
def test_clear
|
134
|
+
page = nil
|
135
|
+
20.times { @history.push(page = @agent.get('http://localhost/index.html')) }
|
136
|
+
assert_equal(20, @history.length)
|
137
|
+
assert_equal(true, @history.visited?(page))
|
138
|
+
@history.clear
|
139
|
+
assert_equal(0, @history.length)
|
140
|
+
assert_equal(false, @history.visited?(page))
|
141
|
+
end
|
142
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
require File.expand_path(File.join(File.dirname(__FILE__), "helper"))
|
2
|
+
|
3
|
+
class HistoryAddedTest < Test::Unit::TestCase
|
4
|
+
def setup
|
5
|
+
@agent = WWW::Mechanize.new
|
6
|
+
end
|
7
|
+
|
8
|
+
def test_history_added_gets_called
|
9
|
+
onload = 0
|
10
|
+
@agent.history_added = lambda { |page|
|
11
|
+
onload += 1
|
12
|
+
}
|
13
|
+
page = @agent.get('http://localhost/tc_blank_form.html')
|
14
|
+
assert_equal(1, onload)
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
require File.expand_path(File.join(File.dirname(__FILE__), "helper"))
|
2
|
+
|
3
|
+
class TestCheckBoxes < Test::Unit::TestCase
|
4
|
+
def test_field
|
5
|
+
f = WWW::Mechanize::Form::Field.new('a&b', 'a&b')
|
6
|
+
assert_equal('a&b', f.name)
|
7
|
+
assert_equal('a&b', f.value)
|
8
|
+
|
9
|
+
f = WWW::Mechanize::Form::Field.new('a&b', 'a&b')
|
10
|
+
assert_equal('a&b', f.name)
|
11
|
+
assert_equal('a&b', f.value)
|
12
|
+
|
13
|
+
f = WWW::Mechanize::Form::Field.new('a&b', 'a&b')
|
14
|
+
assert_equal('a&b', f.name)
|
15
|
+
assert_equal('a&b', f.value)
|
16
|
+
end
|
17
|
+
|
18
|
+
def test_file_upload
|
19
|
+
f = WWW::Mechanize::Form::FileUpload.new('a&b', 'a&b')
|
20
|
+
assert_equal('a&b', f.name)
|
21
|
+
assert_equal('a&b', f.file_name)
|
22
|
+
|
23
|
+
f = WWW::Mechanize::Form::FileUpload.new('a&b', 'a&b')
|
24
|
+
assert_equal('a&b', f.name)
|
25
|
+
assert_equal('a&b', f.file_name)
|
26
|
+
end
|
27
|
+
|
28
|
+
def test_image_button
|
29
|
+
f = WWW::Mechanize::Form::ImageButton.new('a&b', 'a&b')
|
30
|
+
assert_equal('a&b', f.name)
|
31
|
+
assert_equal('a&b', f.value)
|
32
|
+
end
|
33
|
+
|
34
|
+
def test_radio_button
|
35
|
+
f = WWW::Mechanize::Form::RadioButton.new('a&b', 'a&b', nil, nil)
|
36
|
+
assert_equal('a&b', f.name)
|
37
|
+
assert_equal('a&b', f.value)
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require File.expand_path(File.join(File.dirname(__FILE__), "helper"))
|
2
|
+
|
3
|
+
class TestIfModifiedSince < Test::Unit::TestCase
|
4
|
+
def setup
|
5
|
+
@agent = WWW::Mechanize.new
|
6
|
+
end
|
7
|
+
|
8
|
+
def test_get_twice
|
9
|
+
assert_equal(0, @agent.history.length)
|
10
|
+
page = @agent.get('http://localhost/if_modified_since')
|
11
|
+
assert_match(/You did not send/, page.body)
|
12
|
+
|
13
|
+
assert_equal(1, @agent.history.length)
|
14
|
+
page2 = @agent.get('http://localhost/if_modified_since')
|
15
|
+
|
16
|
+
assert_equal(2, @agent.history.length)
|
17
|
+
assert_equal(page.object_id, page2.object_id)
|
18
|
+
assert_match(/You did not send/, page.body)
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
require File.expand_path(File.join(File.dirname(__FILE__), "helper"))
|
2
|
+
|
3
|
+
class TestKeepAlive < Test::Unit::TestCase
|
4
|
+
def setup
|
5
|
+
@agent = WWW::Mechanize.new
|
6
|
+
end
|
7
|
+
|
8
|
+
def test_keep_alive
|
9
|
+
page = @agent.get('http://localhost/http_headers')
|
10
|
+
headers = {}
|
11
|
+
page.body.split(/[\r\n]+/).each do |header|
|
12
|
+
headers.[]=(*header.chomp.split(/\|/))
|
13
|
+
end
|
14
|
+
assert(headers.has_key?('connection'))
|
15
|
+
assert_equal('keep-alive', headers['connection'])
|
16
|
+
assert(headers.has_key?('keep-alive'))
|
17
|
+
assert_equal('300', headers['keep-alive'])
|
18
|
+
end
|
19
|
+
|
20
|
+
def test_close_connection
|
21
|
+
@agent.keep_alive = false
|
22
|
+
page = @agent.get('http://localhost/http_headers')
|
23
|
+
headers = {}
|
24
|
+
page.body.split(/[\r\n]+/).each do |header|
|
25
|
+
headers.[]=(*header.chomp.split(/\|/))
|
26
|
+
end
|
27
|
+
assert(headers.has_key?('connection'))
|
28
|
+
assert_equal('close', headers['connection'])
|
29
|
+
assert(!headers.has_key?('keep-alive'))
|
30
|
+
end
|
31
|
+
end
|
data/test/test_links.rb
ADDED
@@ -0,0 +1,120 @@
|
|
1
|
+
require File.expand_path(File.join(File.dirname(__FILE__), "helper"))
|
2
|
+
|
3
|
+
class LinksMechTest < Test::Unit::TestCase
|
4
|
+
def setup
|
5
|
+
@agent = WWW::Mechanize.new
|
6
|
+
end
|
7
|
+
|
8
|
+
def test_unsupported_link_types
|
9
|
+
page = @agent.get("http://google.com/tc_links.html")
|
10
|
+
link = page.link_with(:text => 'javascript link')
|
11
|
+
assert_raise(WWW::Mechanize::UnsupportedSchemeError) {
|
12
|
+
link.click
|
13
|
+
}
|
14
|
+
|
15
|
+
@agent.scheme_handlers['javascript'] = lambda { |my_link, my_page|
|
16
|
+
URI.parse('http://localhost/tc_links.html')
|
17
|
+
}
|
18
|
+
assert_nothing_raised {
|
19
|
+
link.click
|
20
|
+
}
|
21
|
+
end
|
22
|
+
|
23
|
+
def test_link_with_no_path
|
24
|
+
page = @agent.get("http://localhost/relative/tc_relative_links.html")
|
25
|
+
page = page.link_with(:text => 'just the query string').click
|
26
|
+
assert_equal('http://localhost/relative/tc_relative_links.html?a=b', page.uri.to_s)
|
27
|
+
end
|
28
|
+
|
29
|
+
def test_base
|
30
|
+
page = @agent.get("http://google.com/tc_base_link.html")
|
31
|
+
page = page.links.first.click
|
32
|
+
assert @agent.visited?("http://localhost/index.html")
|
33
|
+
end
|
34
|
+
|
35
|
+
def test_find_meta
|
36
|
+
page = @agent.get("http://localhost/find_link.html")
|
37
|
+
assert_equal(3, page.meta.length)
|
38
|
+
assert_equal(%w{
|
39
|
+
http://www.drphil.com/
|
40
|
+
http://www.upcase.com/
|
41
|
+
http://tenderlovemaking.com/ }.sort,
|
42
|
+
page.meta.map { |x| x.href.downcase }.sort)
|
43
|
+
end
|
44
|
+
|
45
|
+
def test_find_link
|
46
|
+
page = @agent.get("http://localhost/find_link.html")
|
47
|
+
assert_equal(18, page.links.length)
|
48
|
+
end
|
49
|
+
|
50
|
+
def test_alt_text
|
51
|
+
page = @agent.get("http://localhost/alt_text.html")
|
52
|
+
assert_equal(5, page.links.length)
|
53
|
+
assert_equal(1, page.meta.length)
|
54
|
+
|
55
|
+
assert_equal('', page.meta.first.text)
|
56
|
+
assert_equal('alt text', page.link_with(:href => 'alt_text.html').text)
|
57
|
+
assert_equal('', page.link_with(:href => 'no_alt_text.html').text)
|
58
|
+
assert_equal('no image', page.link_with(:href => 'no_image.html').text)
|
59
|
+
assert_equal('', page.link_with(:href => 'no_text.html').text)
|
60
|
+
assert_equal('', page.link_with(:href => 'nil_alt_text.html').text)
|
61
|
+
end
|
62
|
+
|
63
|
+
def test_click_link
|
64
|
+
@agent.user_agent_alias = 'Mac Safari'
|
65
|
+
page = @agent.get("http://localhost/frame_test.html")
|
66
|
+
link = page.link_with(:text => "Form Test")
|
67
|
+
assert_not_nil(link)
|
68
|
+
assert_equal('Form Test', link.text)
|
69
|
+
page = @agent.click(link)
|
70
|
+
assert_equal("http://localhost/form_test.html",
|
71
|
+
@agent.history.last.uri.to_s)
|
72
|
+
end
|
73
|
+
|
74
|
+
def test_click_method
|
75
|
+
page = @agent.get("http://localhost/frame_test.html")
|
76
|
+
link = page.link_with(:text => "Form Test")
|
77
|
+
assert_not_nil(link)
|
78
|
+
assert_equal('Form Test', link.text)
|
79
|
+
page = link.click
|
80
|
+
assert_equal("http://localhost/form_test.html",
|
81
|
+
@agent.history.last.uri.to_s)
|
82
|
+
end
|
83
|
+
|
84
|
+
def test_find_bold_link
|
85
|
+
page = @agent.get("http://localhost/tc_links.html")
|
86
|
+
link = page.links_with(:text => /Bold Dude/)
|
87
|
+
assert_equal(1, link.length)
|
88
|
+
assert_equal('Bold Dude', link.first.text)
|
89
|
+
|
90
|
+
link = page.links_with(:text => 'Aaron James Patterson')
|
91
|
+
assert_equal(1, link.length)
|
92
|
+
assert_equal('Aaron James Patterson', link.first.text)
|
93
|
+
|
94
|
+
link = page.links_with(:text => 'Aaron Patterson')
|
95
|
+
assert_equal(1, link.length)
|
96
|
+
assert_equal('Aaron Patterson', link.first.text)
|
97
|
+
|
98
|
+
link = page.links_with(:text => 'Ruby Rocks!')
|
99
|
+
assert_equal(1, link.length)
|
100
|
+
assert_equal('Ruby Rocks!', link.first.text)
|
101
|
+
end
|
102
|
+
|
103
|
+
def test_link_with_encoded_space
|
104
|
+
page = @agent.get("http://localhost/tc_links.html")
|
105
|
+
link = page.link_with(:text => 'encoded space')
|
106
|
+
page = @agent.click link
|
107
|
+
end
|
108
|
+
|
109
|
+
def test_link_with_space
|
110
|
+
page = @agent.get("http://localhost/tc_links.html")
|
111
|
+
link = page.link_with(:text => 'not encoded space')
|
112
|
+
page = @agent.click link
|
113
|
+
end
|
114
|
+
|
115
|
+
def test_link_with_unusual_characters
|
116
|
+
page = @agent.get("http://localhost/tc_links.html")
|
117
|
+
link = page.link_with(:text => 'unusual characters')
|
118
|
+
assert_nothing_raised { @agent.click link }
|
119
|
+
end
|
120
|
+
end
|