mechanize-2.7.7/ 0000755 0000041 0000041 00000000000 14006521455 013524 5 ustar www-data www-data mechanize-2.7.7/LICENSE.rdoc 0000644 0000041 0000041 00000002022 14006521455 015453 0 ustar www-data www-data (The MIT License)
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
'Software'), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
mechanize-2.7.7/test/ 0000755 0000041 0000041 00000000000 14006521455 014503 5 ustar www-data www-data mechanize-2.7.7/test/test_mechanize_image.rb 0000644 0000041 0000041 00000000162 14006521455 021173 0 ustar www-data www-data require 'mechanize/test_case'
class TestMechanizeImage < Mechanize::TestCase
# empty subclass, no tests
end
mechanize-2.7.7/test/test_mechanize_link.rb 0000644 0000041 0000041 00000011227 14006521455 021052 0 ustar www-data www-data require 'mechanize/test_case'
class TestMechanizeLink < Mechanize::TestCase
def test_search
page = @mech.get("http://localhost/find_link.html")
link = page.link_with(text: "Form Test")
assert_equal('Form Test', link.text)
link_with_search = page.link_with(search: "//*[text()='Form Test']")
assert_equal(link, link_with_search)
link_with_xpath = page.link_with(xpath: "//*[text()='Form Test']")
assert_equal(link, link_with_xpath)
link_with_css = page.link_with(css: ".formtest")
assert_equal(link, link_with_css)
link_with_class = page.link_with(class: "formtest")
assert_equal(link, link_with_class)
end
def test_click
page = @mech.get("http://localhost/frame_test.html")
link = page.link_with(:text => "Form Test")
assert_equal('Form Test', link.text)
page = link.click
assert_equal("http://localhost/form_test.html",
@mech.history.last.uri.to_s)
end unless RUBY_ENGINE == 'jruby' # NekoHTML does not parse body of NOFRAMES
def test_click_bang
page = @mech.get("http://localhost/frame_test.html")
link = page.link_with!(:text => "Form Test")
assert_equal('Form Test', link.text)
page = link.click
assert_equal("http://localhost/form_test.html",
@mech.history.last.uri.to_s)
end unless RUBY_ENGINE == 'jruby' # NekoHTML does not parse body of NOFRAMES
def test_click_base
page = @mech.get("http://google.com/tc_base_link.html")
page = page.links.first.click
assert @mech.visited?("http://localhost/index.html")
end
def test_click_unsupported_scheme
page = @mech.get("http://google.com/tc_links.html")
link = page.link_with(:text => 'javascript link')
assert_raises Mechanize::UnsupportedSchemeError do
begin
link.click
rescue Mechanize::UnsupportedSchemeError => error
assert_equal 'javascript', error.scheme
assert_equal "javascript:new_page('1')", error.uri.to_s
raise
end
end
@mech.scheme_handlers['javascript'] = lambda { |my_link, my_page|
URI.parse('http://localhost/tc_links.html')
}
link.click
# HACK no assertion
end
def test_click_unexiting_link
page = @mech.get("http://google.com/tc_links.html")
assert_raises NoMethodError do
page.link_with(:text => 'no link').click
end
begin
page.link_with!(:text => 'no link').click
rescue => e
assert_instance_of Mechanize::ElementNotFoundError, e
assert_kind_of Mechanize::Page, e.source
assert_equal :link, e.element
assert_kind_of Hash, e.conditions
assert_equal 'no link', e.conditions[:text]
end
end
def test_click_empty_href
page = @mech.get("http://google.com/tc_links.html?q=test#anchor")
link = page.link_with(:text => 'empty href')
new_page = link.click
assert_equal "http://google.com/tc_links.html?q=test", new_page.uri.to_s
end
def test_text_alt_text
page = @mech.get("http://localhost/alt_text.html")
assert_equal(5, page.links.length)
assert_equal(1, page.meta_refresh.length)
assert_equal '', page.meta_refresh.first.text
assert_equal 'alt text', page.link_with(:href => 'alt_text.html').text
assert_equal '', page.link_with(:href => 'no_alt_text.html').text
assert_equal 'no image', page.link_with(:href => 'no_image.html').text
assert_equal '', page.link_with(:href => 'no_text.html').text
assert_equal '', page.link_with(:href => 'nil_alt_text.html').text
end
def test_uri_escaped
doc = Nokogiri::HTML::Document.new
node = Nokogiri::XML::Node.new('foo', doc)
node['href'] = 'http://foo.bar/%20baz'
link = Mechanize::Page::Link.new(node, nil, nil)
assert_equal 'http://foo.bar/%20baz', link.uri.to_s
end
def test_uri_no_path
page = @mech.get("http://localhost/relative/tc_relative_links.html")
page = page.link_with(:text => 'just the query string').click
assert_equal('http://localhost/relative/tc_relative_links.html?a=b',
page.uri.to_s)
end unless RUBY_ENGINE == 'jruby' # NekoHTML does not parse IFRAME
def test_uri_weird
doc = Nokogiri::HTML::Document.new
node = Nokogiri::XML::Node.new('foo', doc)
node['href'] = 'http://foo.bar/ baz'
link = Mechanize::Page::Link.new(node, nil, nil)
assert_equal 'http://foo.bar/%20baz', link.uri.to_s
end
def test_resolving_full_uri
page = @mech.get("http://localhost/frame_test.html")
link = page.link_with(:text => "Form Test")
assert_equal "/form_test.html", link.uri.to_s
assert_equal "http://localhost/form_test.html", link.resolved_uri.to_s
end unless RUBY_ENGINE == 'jruby' # NekoHTML does not parse body of NOFRAMES
end
mechanize-2.7.7/test/test_mechanize_form_encoding.rb 0000644 0000041 0000041 00000006256 14006521455 022734 0 ustar www-data www-data # coding: utf-8
require 'mechanize/test_case'
class TestMechanizeFormEncoding < Mechanize::TestCase
# See also: tests of Util.from_native_charset
# Encoding test should do with non-utf-8 characters
INPUTTED_VALUE = "テスト" # "test" in Japanese UTF-8 encoding
CONTENT_ENCODING = 'Shift_JIS' # one of Japanese encoding
encoded_value = "\x83\x65\x83\x58\x83\x67".force_encoding(::Encoding::SHIFT_JIS) # "test" in Japanese Shift_JIS encoding
EXPECTED_QUERY = "first_name=#{CGI.escape(encoded_value)}&first_name=&gender=&green%5Beggs%5D="
ENCODING_ERRORS = [EncodingError, Encoding::ConverterNotFoundError] # and so on
ENCODING_LOG_MESSAGE = /INFO -- : form encoding: Shift_JIS/
INVALID_ENCODING = 'UTF-eight'
def set_form_with_encoding(enc)
page = @mech.get("http://localhost/form_set_fields.html")
form = page.forms.first
form.encoding = enc
form['first_name'] = INPUTTED_VALUE
form
end
def test_form_encoding_returns_accept_charset
page = @mech.get("http://localhost/rails_3_encoding_hack_form_test.html")
form = page.forms.first
accept_charset = form.form_node['accept-charset']
assert accept_charset
assert_equal accept_charset, form.encoding
refute_equal page.encoding, form.encoding
end
def test_form_encoding_returns_page_encoding_when_no_accept_charset
page = @mech.get("http://localhost/form_set_fields.html")
form = page.forms.first
accept_charset = form.form_node['accept-charset']
assert_nil accept_charset
refute_equal accept_charset, form.encoding
assert_equal page.encoding, form.encoding
end
def test_form_encoding_equals_sets_new_encoding
page = @mech.get("http://localhost/form_set_fields.html")
form = page.forms.first
refute_equal CONTENT_ENCODING, form.encoding
form.encoding = CONTENT_ENCODING
assert_equal CONTENT_ENCODING, form.encoding
end
def test_form_encoding_returns_nil_when_no_page_in_initialize
# this sequence is seen at Mechanize#post(url, query_hash)
node = {}
# Create a fake form
class << node
def search(*args); []; end
end
node['method'] = 'POST'
node['enctype'] = 'application/x-www-form-urlencoded'
form = Mechanize::Form.new(node)
assert_nil form.encoding
end
def test_post_form_with_form_encoding
form = set_form_with_encoding CONTENT_ENCODING
form.submit
# we can not use "links.find{|l| l.text == 'key:val'}" assertion here
# because the link text encoding is always UTF-8 regaredless of html encoding
assert EXPECTED_QUERY, @mech.page.at('div#query').inner_text
end
def test_post_form_with_problematic_encoding
form = set_form_with_encoding INVALID_ENCODING
assert_raises(*ENCODING_ERRORS){ form.submit }
end
def test_form_ignore_encoding_error_is_true
form = set_form_with_encoding INVALID_ENCODING
form.ignore_encoding_error = true
form.submit
# HACK no assertions
end
def test_post_form_logs_form_encoding
sio = StringIO.new
@mech.log = Logger.new(sio)
@mech.log.level = Logger::INFO
form = set_form_with_encoding CONTENT_ENCODING
form.submit
assert_match ENCODING_LOG_MESSAGE, sio.string
@mech.log = nil
end
end
mechanize-2.7.7/test/test_mechanize_element_not_found_error.rb 0000644 0000041 0000041 00000000466 14006521455 025035 0 ustar www-data www-data require 'mechanize/test_case'
class TestMechanizeRedirectLimitReachedError < Mechanize::TestCase
def test_to_s
page = fake_page
error = Mechanize::ElementNotFoundError.new(page, :element, :conditions)
assert_match(/element/, error.to_s)
assert_match(/conditions/, error.to_s)
end
end
mechanize-2.7.7/test/test_mechanize_xml_file.rb 0000644 0000041 0000041 00000001077 14006521455 021716 0 ustar www-data www-data require 'mechanize/test_case'
class TestMechanizeXmlFile < Mechanize::TestCase
def setup
super
uri = URI 'http://example.com/foo.xml'
@xml = Mechanize::XmlFile.new uri, nil, <<-XML
Ruby
Perl
XML
end
def test_xml
assert_kind_of Nokogiri::XML::Document, @xml.xml
end
def test_search
assert_equal ['Ruby', 'Perl'], @xml.search('language').map { |n| n.text }
end
def test_at
assert_equal 'Perl', @xml.at('//language[2]').text
end
end mechanize-2.7.7/test/test_mechanize_response_read_error.rb 0000644 0000041 0000041 00000001133 14006521455 024152 0 ustar www-data www-data require 'mechanize/test_case'
class TestMechanizeResponseReadError < Mechanize::TestCase
def setup
super
@error = 'error message'
@response = Response.new
@response['content-length'] = 3
@body_io = StringIO.new 'body'
end
def test_force_parse
@response['content-type'] = 'text/html'
uri = URI 'http://example/'
e = Mechanize::ResponseReadError.new @error, @response, @body_io, uri, @mech
page = e.force_parse
assert_kind_of Mechanize::Page, page
assert_equal 'body', page.body
assert_equal @mech, page.mech
end
end
mechanize-2.7.7/test/test_mechanize_util.rb 0000644 0000041 0000041 00000007645 14006521455 021103 0 ustar www-data www-data # coding: utf-8
require 'mechanize/test_case'
class TestMechanizeUtil < Mechanize::TestCase
INPUTTED_VALUE = "テスト" # "test" in Japanese UTF-8 encoding
CONTENT_ENCODING = 'Shift_JIS' # one of Japanese encoding
ENCODED_VALUE = "\x83\x65\x83\x58\x83\x67".force_encoding(::Encoding::SHIFT_JIS) # "test" in Japanese Shift_JIS encoding
ENCODING_ERRORS = [EncodingError, Encoding::ConverterNotFoundError] # and so on
ERROR_LOG_MESSAGE = /from_native_charset: Encoding::ConverterNotFoundError: form encoding: "UTF-eight"/
INVALID_ENCODING = 'UTF-eight'
def setup
super
@MU = Mechanize::Util
@result = "not set"
end
def test_from_native_charset
@result = @MU.from_native_charset(INPUTTED_VALUE, CONTENT_ENCODING)
assert_equal ENCODED_VALUE, @result
end
def test_from_native_charset_returns_nil_when_no_string
@result = @MU.from_native_charset(nil, CONTENT_ENCODING)
assert_nil @result
end
def test_from_native_charset_doesnot_convert_when_no_encoding
@result = @MU.from_native_charset(INPUTTED_VALUE, nil)
refute_equal ENCODED_VALUE, @result
assert_equal INPUTTED_VALUE, @result
end
def test_from_native_charset_doesnot_convert_when_not_nokogiri
parser = Mechanize.html_parser
Mechanize.html_parser = 'Another HTML Parser'
@result = @MU.from_native_charset(INPUTTED_VALUE, CONTENT_ENCODING)
refute_equal ENCODED_VALUE, @result
assert_equal INPUTTED_VALUE, @result
ensure
Mechanize.html_parser = parser
end
def test_from_native_charset_raises_error_with_bad_encoding
assert_raises(*ENCODING_ERRORS) do
@MU.from_native_charset(INPUTTED_VALUE, INVALID_ENCODING)
end
end
def test_from_native_charset_suppress_encoding_error_when_3rd_arg_is_true
@MU.from_native_charset(INPUTTED_VALUE, INVALID_ENCODING, true)
# HACK no assertion
end
def test_from_native_charset_doesnot_convert_when_encoding_error_raised_and_ignored
@result = @MU.from_native_charset(INPUTTED_VALUE, INVALID_ENCODING, true)
refute_equal ENCODED_VALUE, @result
assert_equal INPUTTED_VALUE, @result
end
def test_from_native_charset_logs_form_when_encoding_error_raised
sio = StringIO.new("")
log = Logger.new(sio)
log.level = Logger::DEBUG
assert_raises(*ENCODING_ERRORS) do
@MU.from_native_charset(INPUTTED_VALUE, INVALID_ENCODING, nil, log)
end
assert_match ERROR_LOG_MESSAGE, sio.string
end
def test_from_native_charset_logs_form_when_encoding_error_is_ignored
sio = StringIO.new("")
log = Logger.new(sio)
log.level = Logger::DEBUG
@MU.from_native_charset(INPUTTED_VALUE, INVALID_ENCODING, true, log)
assert_match ERROR_LOG_MESSAGE, sio.string
end
def test_self_html_unescape_entity
assert_equal '&', @MU::html_unescape('&')
assert_equal '&', @MU::html_unescape('&')
end
def test_uri_escape
assert_equal "%25", @MU.uri_escape("%")
assert_equal "%", @MU.uri_escape("%", /[^%]/)
end
def test_build_query_string_simple
input_params = [
[:ids, 1],
[:action, 'delete'],
[:ids, 5],
]
expected_params = [
['ids', '1'],
['action', 'delete'],
['ids', '5'],
]
query = @MU.build_query_string(input_params)
assert_equal expected_params, URI.decode_www_form(query)
end
def test_build_query_string_complex
input_params = {
number: 7,
name: "\u{6B66}\u{8005}",
"ids[]" => [1, 3, 5, 7],
words: ["Sing", "Now!"],
params: { x: "50%", y: "100%", t: [80, 160] },
}
expected_params = [
['number', '7'],
['name', "\u{6B66}\u{8005}"],
['ids[]', '1'], ['ids[]', '3'], ['ids[]', '5'], ['ids[]', '7'],
['words', 'Sing'], ['words', 'Now!'],
['params[x]', '50%'],
['params[y]', '100%'],
['params[t]', '80'], ['params[t]', '160'],
]
query = @MU.build_query_string(input_params)
assert_equal expected_params, URI.decode_www_form(query)
end
end
mechanize-2.7.7/test/test_mechanize_form_check_box.rb 0000644 0000041 0000041 00000002004 14006521455 023056 0 ustar www-data www-data require 'mechanize/test_case'
class TestMechanizeFormCheckBox < Mechanize::TestCase
def setup
super
@page = @mech.get('http://localhost/tc_checkboxes.html')
end
def test_search
form = @page.forms.first
checkbox = form.checkbox_with(name: 'green')
assert_equal('green', checkbox.name)
assert_equal(checkbox, form.checkbox_with('green'))
assert_equal(checkbox, form.checkbox_with(search: 'input[@type=checkbox][@name=green]'))
end
def test_check
form = @page.forms.first
form.checkbox_with(:name => 'green').check
assert(form.checkbox_with(:name => 'green').checked)
%w{ red blue yellow brown }.each do |color|
assert_equal(false, form.checkbox_with(:name => color).checked)
end
end
def test_uncheck
form = @page.forms.first
checkbox = form.checkbox_with(:name => 'green')
checkbox.check
assert form.checkbox_with(:name => 'green').checked
checkbox.uncheck
assert !form.checkbox_with(:name => 'green').checked
end
end
mechanize-2.7.7/test/test_mechanize_subclass.rb 0000644 0000041 0000041 00000000547 14006521455 021737 0 ustar www-data www-data require 'mechanize/test_case'
class TestMechanizeSubclass < Mechanize::TestCase
class Parent < Mechanize
@html_parser = :parser
@log = :log
end
class Child < Parent
end
def test_subclass_inherits_html_parser
assert_equal :parser, Child.html_parser
end
def test_subclass_inherits_log
assert_equal :log, Child.log
end
end
mechanize-2.7.7/test/test_mechanize_cookie.rb 0000644 0000041 0000041 00000040227 14006521455 021370 0 ustar www-data www-data require 'mechanize/test_case'
module Enumerable
def combine
masks = inject([[], 1]){|(ar, m), e| [ar << m, m << 1 ] }[0]
all = masks.inject(0){ |al, m| al|m }
result = []
for i in 1..all do
tmp = []
each_with_index do |e, idx|
tmp << e unless (masks[idx] & i) == 0
end
result << tmp
end
result
end
end
class TestMechanizeCookie < Mechanize::TestCase
def assert_cookie_parse url, cookie_text, &block
cookie = nil
block ||= proc { |p_cookie| cookie = p_cookie }
exp_re = /The call of Mechanize::Cookie.parse/
assert_output "", exp_re do
Mechanize::Cookie.parse(url, cookie_text, &block)
end
cookie
end
alias silently capture_io
def test_parse_dates
url = URI.parse('http://localhost/')
yesterday = Time.now - 86400
dates = [ "14 Apr 89 03:20:12",
"14 Apr 89 03:20 GMT",
"Fri, 17 Mar 89 4:01:33",
"Fri, 17 Mar 89 4:01 GMT",
"Mon Jan 16 16:12 PDT 1989",
#"Mon Jan 16 16:12 +0130 1989",
"6 May 1992 16:41-JST (Wednesday)",
#"22-AUG-1993 10:59:12.82",
"22-AUG-1993 10:59pm",
"22-AUG-1993 12:59am",
"22-AUG-1993 12:59 PM",
#"Friday, August 04, 1995 3:54 PM",
#"06/21/95 04:24:34 PM",
#"20/06/95 21:07",
#"95-06-08 19:32:48 EDT",
]
dates.each do |date|
cookie = "PREF=1; expires=#{date}"
silently do
Mechanize::Cookie.parse(url, cookie) { |c|
assert c.expires, "Tried parsing: #{date}"
assert_equal(true, c.expires < yesterday)
}
end
end
end
def test_parse_empty
cookie_str = 'a=b; ; c=d'
uri = URI.parse 'http://example'
assert_cookie_parse uri, cookie_str do |cookie|
assert_equal 'a', cookie.name
assert_equal 'b', cookie.value
end
end
def test_parse_no_space
cookie_str = "foo=bar;Expires=Sun, 06 Nov 2011 00:28:06 GMT;Path=/"
uri = URI.parse 'http://example'
assert_cookie_parse uri, cookie_str do |cookie|
assert_equal 'foo', cookie.name
assert_equal 'bar', cookie.value
assert_equal '/', cookie.path
assert_equal Time.at(1320539286), cookie.expires
end
end
def test_parse_quoted
cookie_str =
"quoted=\"value\"; Expires=Sun, 06 Nov 2011 00:11:18 GMT; Path=/"
uri = URI.parse 'http://example'
assert_cookie_parse uri, cookie_str do |cookie|
assert_equal 'quoted', cookie.name
assert_equal 'value', cookie.value
end
end
def test_parse_weird_cookie
cookie = 'n/a, ASPSESSIONIDCSRRQDQR=FBLDGHPBNDJCPCGNCPAENELB; path=/'
url = URI.parse('http://www.searchinnovation.com/')
assert_cookie_parse url, cookie do |c|
assert_equal('ASPSESSIONIDCSRRQDQR', c.name)
assert_equal('FBLDGHPBNDJCPCGNCPAENELB', c.value)
end
end
def test_double_semicolon
double_semi = 'WSIDC=WEST;; domain=.williams-sonoma.com; path=/'
url = URI.parse('http://williams-sonoma.com/')
assert_cookie_parse url, double_semi do |cookie|
assert_equal('WSIDC', cookie.name)
assert_equal('WEST', cookie.value)
end
end
def test_parse_bad_version
bad_cookie = 'PRETANET=TGIAqbFXtt; Name=/PRETANET; Path=/; Version=1.2; Content-type=text/html; Domain=192.168.6.196; expires=Friday, 13-November-2026 23:01:46 GMT;'
url = URI.parse('http://localhost/')
assert_cookie_parse url, bad_cookie do |cookie|
assert_nil(cookie.version)
end
end
def test_parse_bad_max_age
bad_cookie = 'PRETANET=TGIAqbFXtt; Name=/PRETANET; Path=/; Max-Age=1.2; Content-type=text/html; Domain=192.168.6.196; expires=Friday, 13-November-2026 23:01:46 GMT;'
url = URI.parse('http://localhost/')
assert_cookie_parse url, bad_cookie do |cookie|
assert_nil(cookie.max_age)
end
end
def test_parse_date_fail
url = URI.parse('http://localhost/')
dates = [
"20/06/95 21:07",
]
silently do
dates.each do |date|
cookie = "PREF=1; expires=#{date}"
Mechanize::Cookie.parse(url, cookie) { |c|
assert_equal(true, c.expires.nil?)
}
end
end
end
def test_parse_domain_dot
url = URI.parse('http://host.example.com/')
cookie_str = 'a=b; domain=.example.com'
cookie = assert_cookie_parse url, cookie_str
assert_equal 'example.com', cookie.domain
assert cookie.for_domain?
end
def test_parse_domain_no_dot
url = URI.parse('http://host.example.com/')
cookie_str = 'a=b; domain=example.com'
cookie = assert_cookie_parse url, cookie_str
assert_equal 'example.com', cookie.domain
assert cookie.for_domain?
end
def test_parse_domain_none
url = URI.parse('http://example.com/')
cookie_str = 'a=b;'
cookie = assert_cookie_parse url, cookie_str
assert_equal 'example.com', cookie.domain
assert !cookie.for_domain?
end
def test_parse_max_age
url = URI.parse('http://localhost/')
date = 'Mon, 19 Feb 2012 19:26:04 GMT'
cookie_text = "name=Akinori; expires=#{date}"
cookie = assert_cookie_parse url, cookie_text
assert_equal Time.at(1329679564), cookie.expires
cookie_text = 'name=Akinori; max-age=3600'
cookie = assert_cookie_parse url, cookie_text
assert_in_delta Time.now + 3600, cookie.expires, 1
# Max-Age has precedence over Expires
cookie_text = "name=Akinori; max-age=3600; expires=#{date}"
cookie = assert_cookie_parse url, cookie_text
assert_in_delta Time.now + 3600, cookie.expires, 1
cookie_text = "name=Akinori; expires=#{date}; max-age=3600"
cookie = assert_cookie_parse url, cookie_text
assert_in_delta Time.now + 3600, cookie.expires, 1
end
def test_parse_expires_session
url = URI.parse('http://localhost/')
[
'name=Akinori',
'name=Akinori; expires',
'name=Akinori; max-age',
'name=Akinori; expires=',
'name=Akinori; max-age=',
].each { |str|
cookie = assert_cookie_parse url, str
assert cookie.session, str
}
[
'name=Akinori; expires=Mon, 19 Feb 2012 19:26:04 GMT',
'name=Akinori; max-age=3600',
].each { |str|
cookie = assert_cookie_parse url, str
assert !cookie.session, str
}
end
def test_parse_many
url = URI 'http://localhost/'
cookie_str =
"name=Aaron; Domain=localhost; Expires=Sun, 06 Nov 2011 00:29:51 GMT; Path=/, " \
"name=Aaron; Domain=localhost; Expires=Sun, 06 Nov 2011 00:29:51 GMT; Path=/, " \
"name=Aaron; Domain=localhost; Expires=Sun, 06 Nov 2011 00:29:51 GMT; Path=/, " \
"name=Aaron; Domain=localhost; Expires=Sun, 06 Nov 2011 00:29:51 GMT; Path=/; HttpOnly, " \
"expired=doh; Expires=Fri, 04 Nov 2011 00:29:51 GMT; Path=/, " \
"a_path=some_path; Expires=Sun, 06 Nov 2011 00:29:51 GMT; Path=/some_path, " \
"no_path1=no_path; Expires=Sun, 06 Nov 2011 00:29:52 GMT, no_expires=nope; Path=/, " \
"no_path2=no_path; Expires=Sun, 06 Nov 2011 00:29:52 GMT; no_expires=nope; Path, " \
"no_path3=no_path; Expires=Sun, 06 Nov 2011 00:29:52 GMT; no_expires=nope; Path=, " \
"no_domain1=no_domain; Expires=Sun, 06 Nov 2011 00:29:53 GMT; no_expires=nope, " \
"no_domain2=no_domain; Expires=Sun, 06 Nov 2011 00:29:53 GMT; no_expires=nope; Domain, " \
"no_domain3=no_domain; Expires=Sun, 06 Nov 2011 00:29:53 GMT; no_expires=nope; Domain="
cookies = nil
silently { cookies = Mechanize::Cookie.parse url, cookie_str }
assert_equal 13, cookies.length
name = cookies.find { |c| c.name == 'name' }
assert_equal "Aaron", name.value
assert_equal "/", name.path
assert_equal Time.at(1320539391), name.expires
a_path = cookies.find { |c| c.name == 'a_path' }
assert_equal "some_path", a_path.value
assert_equal "/some_path", a_path.path
assert_equal Time.at(1320539391), a_path.expires
no_expires = cookies.find { |c| c.name == 'no_expires' }
assert_equal "nope", no_expires.value
assert_equal "/", no_expires.path
assert_nil no_expires.expires
no_path_cookies = cookies.select { |c| c.value == 'no_path' }
assert_equal 3, no_path_cookies.size
no_path_cookies.each { |c|
assert_equal "/", c.path, c.name
assert_equal Time.at(1320539392), c.expires, c.name
}
no_domain_cookies = cookies.select { |c| c.value == 'no_domain' }
assert_equal 3, no_domain_cookies.size
no_domain_cookies.each { |c|
assert !c.for_domain?, c.name
assert_equal c.domain, url.host, c.name
assert_equal Time.at(1320539393), c.expires, c.name
}
assert cookies.find { |c| c.name == 'expired' }
end
def test_parse_valid_cookie
url = URI.parse('http://rubygems.org/')
cookie_params = {}
cookie_params['expires'] = 'expires=Sun, 27-Sep-2037 00:00:00 GMT'
cookie_params['path'] = 'path=/'
cookie_params['domain'] = 'domain=.rubygems.org'
cookie_params['httponly'] = 'HttpOnly'
cookie_value = '12345%7D=ASDFWEE345%3DASda'
expires = Time.parse('Sun, 27-Sep-2037 00:00:00 GMT')
cookie_params.keys.combine.each do |c|
cookie_text = "#{cookie_value}; "
c.each_with_index do |key, idx|
if idx == (c.length - 1)
cookie_text << "#{cookie_params[key]}"
else
cookie_text << "#{cookie_params[key]}; "
end
end
cookie = assert_cookie_parse url, cookie_text
assert_equal('12345%7D=ASDFWEE345%3DASda', cookie.to_s)
assert_equal('/', cookie.path)
# if expires was set, make sure we parsed it
if c.find { |k| k == 'expires' }
assert_equal(expires, cookie.expires)
else
assert_nil(cookie.expires)
end
end
end
def test_parse_valid_cookie_empty_value
url = URI.parse('http://rubygems.org/')
cookie_params = {}
cookie_params['expires'] = 'expires=Sun, 27-Sep-2037 00:00:00 GMT'
cookie_params['path'] = 'path=/'
cookie_params['domain'] = 'domain=.rubygems.org'
cookie_params['httponly'] = 'HttpOnly'
cookie_value = '12345%7D='
expires = Time.parse('Sun, 27-Sep-2037 00:00:00 GMT')
cookie_params.keys.combine.each do |c|
cookie_text = "#{cookie_value}; "
c.each_with_index do |key, idx|
if idx == (c.length - 1)
cookie_text << "#{cookie_params[key]}"
else
cookie_text << "#{cookie_params[key]}; "
end
end
cookie = assert_cookie_parse url, cookie_text
assert_equal('12345%7D=', cookie.to_s)
assert_equal('', cookie.value)
assert_equal('/', cookie.path)
# if expires was set, make sure we parsed it
if c.find { |k| k == 'expires' }
assert_equal(expires, cookie.expires)
else
assert_nil(cookie.expires)
end
end
end
# If no path was given, use the one from the URL
def test_cookie_using_url_path
url = URI.parse('http://rubygems.org/login.php')
cookie_params = {}
cookie_params['expires'] = 'expires=Sun, 27-Sep-2037 00:00:00 GMT'
cookie_params['path'] = 'path=/'
cookie_params['domain'] = 'domain=.rubygems.org'
cookie_params['httponly'] = 'HttpOnly'
cookie_value = '12345%7D=ASDFWEE345%3DASda'
expires = Time.parse('Sun, 27-Sep-2037 00:00:00 GMT')
cookie_params.keys.combine.each do |c|
next if c.find { |k| k == 'path' }
cookie_text = "#{cookie_value}; "
c.each_with_index do |key, idx|
if idx == (c.length - 1)
cookie_text << "#{cookie_params[key]}"
else
cookie_text << "#{cookie_params[key]}; "
end
end
cookie = assert_cookie_parse url, cookie_text
assert_equal('12345%7D=ASDFWEE345%3DASda', cookie.to_s)
assert_equal('/', cookie.path)
# if expires was set, make sure we parsed it
if c.find { |k| k == 'expires' }
assert_equal(expires, cookie.expires)
else
assert_nil(cookie.expires)
end
end
end
# Test using secure cookies
def test_cookie_with_secure
url = URI.parse('http://rubygems.org/')
cookie_params = {}
cookie_params['expires'] = 'expires=Sun, 27-Sep-2037 00:00:00 GMT'
cookie_params['path'] = 'path=/'
cookie_params['domain'] = 'domain=.rubygems.org'
cookie_params['secure'] = 'secure'
cookie_value = '12345%7D=ASDFWEE345%3DASda'
expires = Time.parse('Sun, 27-Sep-2037 00:00:00 GMT')
cookie_params.keys.combine.each do |c|
next unless c.find { |k| k == 'secure' }
cookie_text = "#{cookie_value}; "
c.each_with_index do |key, idx|
if idx == (c.length - 1)
cookie_text << "#{cookie_params[key]}"
else
cookie_text << "#{cookie_params[key]}; "
end
end
cookie = assert_cookie_parse url, cookie_text
assert_equal('12345%7D=ASDFWEE345%3DASda', cookie.to_s)
assert_equal('/', cookie.path)
assert_equal(true, cookie.secure)
# if expires was set, make sure we parsed it
if c.find { |k| k == 'expires' }
assert_equal(expires, cookie.expires)
else
assert_nil(cookie.expires)
end
end
end
def test_parse_cookie_no_spaces
url = URI.parse('http://rubygems.org/')
cookie_params = {}
cookie_params['expires'] = 'expires=Sun, 27-Sep-2037 00:00:00 GMT'
cookie_params['path'] = 'path=/'
cookie_params['domain'] = 'domain=.rubygems.org'
cookie_params['httponly'] = 'HttpOnly'
cookie_value = '12345%7D=ASDFWEE345%3DASda'
expires = Time.parse('Sun, 27-Sep-2037 00:00:00 GMT')
cookie_params.keys.combine.each do |c|
cookie_text = "#{cookie_value};"
c.each_with_index do |key, idx|
if idx == (c.length - 1)
cookie_text << "#{cookie_params[key]}"
else
cookie_text << "#{cookie_params[key]};"
end
end
cookie = assert_cookie_parse url, cookie_text
assert_equal('12345%7D=ASDFWEE345%3DASda', cookie.to_s)
assert_equal('/', cookie.path)
# if expires was set, make sure we parsed it
if c.find { |k| k == 'expires' }
assert_equal(expires, cookie.expires)
else
assert_nil(cookie.expires)
end
end
end
def test_new
cookie = Mechanize::Cookie.new('key', 'value')
assert_equal 'key', cookie.name
assert_equal 'value', cookie.value
assert_nil cookie.expires
# Minimum unit for the expires attribute is second
expires = Time.at((Time.now + 3600).to_i)
cookie = Mechanize::Cookie.new('key', 'value', :expires => expires.dup)
assert_equal 'key', cookie.name
assert_equal 'value', cookie.value
assert_equal expires, cookie.expires
cookie = Mechanize::Cookie.new(:value => 'value', :name => 'key', :expires => expires.dup)
assert_equal 'key', cookie.name
assert_equal 'value', cookie.value
assert_equal expires, cookie.expires
end
def test_domain=
url = URI.parse('http://host.dom.example.com:8080/')
cookie_str = 'a=b; domain=Example.Com'
cookie = assert_cookie_parse url, cookie_str
assert 'example.com', cookie.domain
cookie.domain = DomainName(url.host)
assert 'host.dom.example.com', cookie.domain
cookie.domain = 'Dom.example.com'
assert 'dom.example.com', cookie.domain
cookie.domain = Object.new.tap { |o|
def o.to_str
'Example.com'
end
}
assert 'example.com', cookie.domain
end
def test_cookie_httponly
url = URI.parse('http://rubygems.org/')
cookie_params = {}
cookie_params['httponly'] = 'HttpOnly'
cookie_value = '12345%7D=ASDFWEE345%3DASda'
expires = Time.parse('Sun, 27-Sep-2037 00:00:00 GMT')
cookie_params.keys.combine.each do |c|
cookie_text = "#{cookie_value}; "
c.each_with_index do |key, idx|
if idx == (c.length - 1)
cookie_text << "#{cookie_params[key]}"
else
cookie_text << "#{cookie_params[key]}; "
end
end
cookie = assert_cookie_parse url, cookie_text
assert_equal(true, cookie.httponly)
# if expires was set, make sure we parsed it
if c.find { |k| k == 'expires' }
assert_equal(expires, cookie.expires)
else
assert_nil(cookie.expires)
end
end
end
end
mechanize-2.7.7/test/test_mechanize_page_frame.rb 0000644 0000041 0000041 00000000567 14006521455 022210 0 ustar www-data www-data require 'mechanize/test_case'
class TestMechanizePageFrame < Mechanize::TestCase
def test_content
page = page 'http://example/referer'
frame = node 'frame', 'name' => 'frame1', 'src' => 'http://example/'
frame = Mechanize::Page::Frame.new frame, @mech, page
frame.content
assert_equal 'http://example/referer', requests.first['Referer']
end
end
mechanize-2.7.7/test/test_mechanize_form_option.rb 0000644 0000041 0000041 00000002042 14006521455 022443 0 ustar www-data www-data require 'mechanize/test_case'
class TestMechanizeFormOption < Mechanize::TestCase
def setup
super
page = html_page <<-BODY
BODY
form = page.forms.first
@select = form.fields.first
@option1 = @select.options.first
@option2 = @select.options.last
end
def test_inspect
assert_match "value: 2", @select.inspect
end
def test_value_missing_value
option = node 'option'
option.inner_html = 'blah'
option = Mechanize::Form::Option.new option, nil
assert_equal 'blah', option.value
end
def test_click
@option1.click
assert @option1.selected?
end
def test_select
@option1.select
assert @option1.selected?
end
def test_unselect
@option2.unselect
refute @option2.selected?
end
def test_selected_eh
refute @option1.selected?
assert @option2.selected?
end
end
mechanize-2.7.7/test/data/ 0000755 0000041 0000041 00000000000 14006521455 015414 5 ustar www-data www-data mechanize-2.7.7/test/data/htpasswd 0000644 0000041 0000041 00000000023 14006521455 017167 0 ustar www-data www-data mech:44E/qORekFV0E
mechanize-2.7.7/test/data/server.csr 0000644 0000041 0000041 00000001304 14006521455 017431 0 ustar www-data www-data -----BEGIN CERTIFICATE REQUEST-----
MIIB0jCCATsCAQAwgZExCzAJBgNVBAYTAlVTMRMwEQYDVQQIEwpXYXNoaW5ndG9u
MRAwDgYDVQQHEwdTZWF0dGxlMRIwEAYDVQQKEwlNZWNoYW5pemUxEjAQBgNVBAsT
CU1lY2hhbml6ZTEOMAwGA1UEAxMFQWFyb24xIzAhBgkqhkiG9w0BCQEWFGFhcm9u
cEBydWJ5Zm9yZ2Uub3JnMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCqZ5iO
GOLQc90ibB0dvEKFK+yGMZKmw/Ko6oCDdC1zrJchcohszSnGuS59gLvAmS8groLf
77rY31fhKtG5dB8GynaOh4z+kcZhl+hCll+zTq7KH/rPeg4S5iWllm7b6j/HssvT
zSJyo1+p/+8LFXrULrY4Tcv3AJK4elDrI8ghrwIDAQABoAAwDQYJKoZIhvcNAQEE
BQADgYEAT7SPe71NQvT2BYGEmbWb7FlSQrPh+rDQMHt/Akb8+r91NLkxZtbD1e/F
iyI9JloPCEwJXxHBl0VVRpFCRuJNN0z0E/G4NUWu6n+ZkihtnmV6uazzAQmD4pTl
SjoiyVLWU+r4Q4yXWXtJ9GR8Attv32fL3PcP+GGLeurXJAn0MNU=
-----END CERTIFICATE REQUEST-----
mechanize-2.7.7/test/data/server.crt 0000644 0000041 0000041 00000001704 14006521455 017436 0 ustar www-data www-data -----BEGIN CERTIFICATE-----
MIICmzCCAgQCCQDq2kM3TCIM0DANBgkqhkiG9w0BAQQFADCBkTELMAkGA1UEBhMC
VVMxEzARBgNVBAgTCldhc2hpbmd0b24xEDAOBgNVBAcTB1NlYXR0bGUxEjAQBgNV
BAoTCU1lY2hhbml6ZTESMBAGA1UECxMJTWVjaGFuaXplMQ4wDAYDVQQDEwVBYXJv
bjEjMCEGCSqGSIb3DQEJARYUYWFyb25wQHJ1Ynlmb3JnZS5vcmcwHhcNMDYwODIz
MDU0NTMwWhcNMDcwODIzMDU0NTMwWjCBkTELMAkGA1UEBhMCVVMxEzARBgNVBAgT
Cldhc2hpbmd0b24xEDAOBgNVBAcTB1NlYXR0bGUxEjAQBgNVBAoTCU1lY2hhbml6
ZTESMBAGA1UECxMJTWVjaGFuaXplMQ4wDAYDVQQDEwVBYXJvbjEjMCEGCSqGSIb3
DQEJARYUYWFyb25wQHJ1Ynlmb3JnZS5vcmcwgZ8wDQYJKoZIhvcNAQEBBQADgY0A
MIGJAoGBAKpnmI4Y4tBz3SJsHR28QoUr7IYxkqbD8qjqgIN0LXOslyFyiGzNKca5
Ln2Au8CZLyCugt/vutjfV+Eq0bl0HwbKdo6HjP6RxmGX6EKWX7NOrsof+s96DhLm
JaWWbtvqP8eyy9PNInKjX6n/7wsVetQutjhNy/cAkrh6UOsjyCGvAgMBAAEwDQYJ
KoZIhvcNAQEEBQADgYEAGtqgxn1fh0X5MxDG1yMp5aGcZ6HhtEtlm5S0ZsRnMsqU
Hh6Bd57+zUQ66XnLCbQN2cwNeeSoqtI16Ccc1I5cAhQnIZESMsPG21i1BnpEhKph
HfNFNpWI/upT2EXNUM6Vx2Kk2aCw2ysrD2pHpsTo5bCOly00uK1ZkoJVQMTL4gU=
-----END CERTIFICATE-----
mechanize-2.7.7/test/data/server.pem 0000644 0000041 0000041 00000001567 14006521455 017436 0 ustar www-data www-data -----BEGIN RSA PRIVATE KEY-----
MIICXAIBAAKBgQCqZ5iOGOLQc90ibB0dvEKFK+yGMZKmw/Ko6oCDdC1zrJchcohs
zSnGuS59gLvAmS8groLf77rY31fhKtG5dB8GynaOh4z+kcZhl+hCll+zTq7KH/rP
eg4S5iWllm7b6j/HssvTzSJyo1+p/+8LFXrULrY4Tcv3AJK4elDrI8ghrwIDAQAB
AoGAC+iZfLS4hSDTv2gW0NErROtA6E/mk8j12GArAwTHeGIDXc8HQbNEzCJ84UBx
3o/V/06yzruOL0HMfmvjpDY9RLsH02xZb2F/lruw4MJLu50i/Zu8Sjmb1YPSfCh/
3+8lREA3Uznlq+wHC3yPxQzMBy5jaEdH4IKxT0Bq8TeF0AECQQDSpL47YpRVRsLn
sS00ndEgQQmT5AJWJJtPpbHk6AA0a+zdNeuDRbdF42zG483YEqU7meZbPKR8QbkK
ZQPEBuevAkEAzxjGcz6NZesmN/NQOtOpylewEs1bdIJyBIBmcnmkimLBtdxd0t34
wUKVHLDSj2aemuAHHwsyn/BNXs6F+obmAQJBALpbkAXAAFW1xefvo3vih8sOXyfd
WIfX2SRNBqbq7otyVFudQaChBDUrsOgBUPLyBAdH8DoV27wm9UuR9RPvu/cCQFRr
WgICXqtMFtE56tuACreD1S9k7MHqpsW0/Y3ujicnKKWUhd5+Q3esR5JhdgOkpkSl
y+FYtDNERpW+BBliwgECQA+Vc7pnxwDIOP8kFumdAUmRmhEZjuwArFcywPzrCUn9
4/KBOp5wDN7kanBwNGZCZ/eQtkb6thAS8C9pufHD1lw=
-----END RSA PRIVATE KEY-----
mechanize-2.7.7/test/data/server.key 0000644 0000041 0000041 00000001567 14006521455 017445 0 ustar www-data www-data -----BEGIN RSA PRIVATE KEY-----
MIICXAIBAAKBgQCqZ5iOGOLQc90ibB0dvEKFK+yGMZKmw/Ko6oCDdC1zrJchcohs
zSnGuS59gLvAmS8groLf77rY31fhKtG5dB8GynaOh4z+kcZhl+hCll+zTq7KH/rP
eg4S5iWllm7b6j/HssvTzSJyo1+p/+8LFXrULrY4Tcv3AJK4elDrI8ghrwIDAQAB
AoGAC+iZfLS4hSDTv2gW0NErROtA6E/mk8j12GArAwTHeGIDXc8HQbNEzCJ84UBx
3o/V/06yzruOL0HMfmvjpDY9RLsH02xZb2F/lruw4MJLu50i/Zu8Sjmb1YPSfCh/
3+8lREA3Uznlq+wHC3yPxQzMBy5jaEdH4IKxT0Bq8TeF0AECQQDSpL47YpRVRsLn
sS00ndEgQQmT5AJWJJtPpbHk6AA0a+zdNeuDRbdF42zG483YEqU7meZbPKR8QbkK
ZQPEBuevAkEAzxjGcz6NZesmN/NQOtOpylewEs1bdIJyBIBmcnmkimLBtdxd0t34
wUKVHLDSj2aemuAHHwsyn/BNXs6F+obmAQJBALpbkAXAAFW1xefvo3vih8sOXyfd
WIfX2SRNBqbq7otyVFudQaChBDUrsOgBUPLyBAdH8DoV27wm9UuR9RPvu/cCQFRr
WgICXqtMFtE56tuACreD1S9k7MHqpsW0/Y3ujicnKKWUhd5+Q3esR5JhdgOkpkSl
y+FYtDNERpW+BBliwgECQA+Vc7pnxwDIOP8kFumdAUmRmhEZjuwArFcywPzrCUn9
4/KBOp5wDN7kanBwNGZCZ/eQtkb6thAS8C9pufHD1lw=
-----END RSA PRIVATE KEY-----
mechanize-2.7.7/test/test_mechanize_page.rb 0000644 0000041 0000041 00000015121 14006521455 021026 0 ustar www-data www-data require 'mechanize/test_case'
class TestMechanizePage < Mechanize::TestCase
def setup
super
@uri = URI 'http://example/'
end
def test_selector_methods
page = html_page <<-BODY
Eamonn
/
Esther
/
Fletcher
BODY
# at(css_selector), % css_selector
assert_equal('Eamonn', page.at('#out').text)
assert_equal('Eamonn', (page % '#out').text)
# at(xpath_selector), % xpath_selector
assert_equal('Esther', page.at('//span[@id="bloody"]').text)
assert_equal('Esther', (page % '//span[@id="bloody"]').text)
# at_css()
assert_equal('Eamonn', page.at_css('#out').text)
# css()
assert_equal('Fletcher', page.css('.name')[2].text)
# at_xpath()
assert_equal('Esther', page.at_xpath('//span[@id="bloody"]').text)
# xpath()
assert_equal('Fletcher', page.xpath('//*[@class="name"]')[2].text)
end
def test_initialize_good_content_type
page = Mechanize::Page.new
assert_equal('text/html', page.content_type)
[
'text/html',
'Text/HTML',
'text/html; charset=UTF-8',
'text/html ; charset=US-ASCII',
'application/xhtml+xml',
'Application/XHTML+XML',
'application/xhtml+xml; charset=UTF-8',
'application/xhtml+xml ; charset=US-ASCII',
].each { |content_type|
page = Mechanize::Page.new(URI('http://example/'),
{ 'content-type' => content_type }, 'hello', '200')
assert_equal(content_type, page.content_type, content_type)
}
end
def test_initialize_bad_content_type
[
'text/xml',
'text/xhtml',
'text/htmlfu',
'footext/html',
'application/xhtml+xmlfu',
'fooapplication/xhtml+xml',
].each { |content_type|
page = Mechanize::Page.new(URI('http://example/'),
{ 'content-type' => content_type }, 'hello', '200')
assert_equal(content_type, page.content_type, content_type)
}
end
def test_frames
page = html_page <<-BODY
A simple frameset document
BODY
assert_equal 3, page.frames.size
assert_equal "frame1", page.frames[0].name
assert_equal "/google.html", page.frames[0].src
assert_equal "Google", page.frames[0].content.title
assert_equal "frame2", page.frames[1].name
assert_equal "/form_test.html", page.frames[1].src
assert_equal "Page Title", page.frames[1].content.title
assert_equal "frame3", page.frames[2].name
assert_equal "/file_upload.html", page.frames[2].src
assert_equal "File Upload Form", page.frames[2].content.title
assert_equal %w[/google.html /file_upload.html], page.frames_with(search: '*[name=frame1], *[name=frame3]').map(&:src)
end
def test_iframes
page = html_page <<-BODY
A simple frameset document
BODY
assert_equal 1, page.iframes.size
assert_equal "frame4", page.iframes.first.name
assert_equal "/file_upload.html", page.iframes.first.src
assert_equal "File Upload Form", page.iframes.first.content.title
end unless RUBY_ENGINE == 'jruby' # NekoHTML does not parse IFRAME
def test_image_with
page = html_page <<-BODY
BODY
assert_equal "http://example/b.jpg",
page.image_with(:src => 'b.jpg').url.to_s
end
def test_images_with
page = html_page <<-BODY
BODY
images = page.images_with(:src => /jpg\Z/).map { |img| img.url.to_s }
assert_equal %w[http://example/a.jpg http://example/b.jpg], images
end
def test_links
page = html_page <<-BODY
BODY
assert_equal page.links.first.href, "foo.html"
end
def test_parser_no_attributes
page = html_page <<-BODY
Hello
BODY
# HACK weak assertion
assert_kind_of Nokogiri::HTML::Document, page.root
end
def test_search_links
page = html_page <<-BODY
b
a
6
BODY
links = page.links_with(:search => "#spany a")
assert_equal 2, links.size
assert_equal "b.html", links[0].href
assert_equal "b", links[0].text
assert_equal "a.html", links[1].href
assert_equal "a", links[1].text
end
def test_search_images
page = html_page <<-BODY
BODY
{
:search => "//img[@class='pretty']",
:xpath => "//img[@class='pretty']",
:css => "img.pretty",
:class => "pretty",
:dom_class => "pretty",
}.each { |key, expr|
images = page.images_with(key => expr)
message = "selecting with #{key.inspect}"
assert_equal 2, images.size
assert_equal "pretty", images[0].dom_class, message
assert_equal "a.jpg", images[0].src, message
assert_equal "pretty", images[1].dom_class, message
assert_equal "c.png", images[1].src, message
}
end
def test_search_bad_selectors
page = html_page <<-BODY
foo
BODY
assert_empty page.images_with(:search => '//a')
assert_empty page.links_with(:search => '//img')
end
def test_multiple_titles
page = html_page <<-BODY
HTML>TITLE
BODY
assert_equal page.title, "HTML>TITLE"
end
end
mechanize-2.7.7/test/test_mechanize_http_www_authenticate_parser.rb 0000644 0000041 0000041 00000011116 14006521455 026107 0 ustar www-data www-data require 'mechanize/test_case'
class TestMechanizeHttpWwwAuthenticateParser < Mechanize::TestCase
def setup
super
@parser = Mechanize::HTTP::WWWAuthenticateParser.new
end
def test_auth_param
@parser.scanner = StringScanner.new 'realm=here'
param = @parser.auth_param
assert_equal %w[realm here], param
end
def test_auth_param_bad_no_value
@parser.scanner = StringScanner.new 'realm='
assert_nil @parser.auth_param
end
def test_auth_param_bad_token
@parser.scanner = StringScanner.new 'realm'
assert_nil @parser.auth_param
end
def test_auth_param_bad_value
@parser.scanner = StringScanner.new 'realm="this '
assert_nil @parser.auth_param
end
def test_auth_param_quoted
@parser.scanner = StringScanner.new 'realm="this site"'
param = @parser.auth_param
assert_equal ['realm', 'this site'], param
end
def test_parse
expected = [
challenge('Basic', { 'realm' => 'foo', 'qop' => 'auth,auth-int' }, 'Basic realm=foo, qop="auth,auth-int"'),
]
assert_equal expected, @parser.parse('Basic realm=foo, qop="auth,auth-int"')
end
def test_parse_without_comma_delimiter
expected = [
challenge('Basic', { 'realm' => 'foo', 'qop' => 'auth,auth-int' }, 'Basic realm=foo qop="auth,auth-int"'),
]
assert_equal expected, @parser.parse('Basic realm=foo qop="auth,auth-int"')
end
def test_parse_multiple
expected = [
challenge('Basic', { 'realm' => 'foo' }, 'Basic realm=foo'),
challenge('Digest', { 'realm' => 'bar' }, 'Digest realm=bar'),
]
assert_equal expected, @parser.parse('Basic realm=foo, Digest realm=bar')
end
def test_parse_multiple_without_comma_delimiter
expected = [
challenge('Basic', { 'realm' => 'foo' }, 'Basic realm=foo'),
challenge('Digest', { 'realm' => 'bar' }, 'Digest realm=bar'),
]
assert_equal expected, @parser.parse('Basic realm=foo Digest realm=bar')
end
def test_parse_multiple_blank
expected = [
challenge('Basic', { 'realm' => 'foo' }, 'Basic realm=foo'),
challenge('Digest', { 'realm' => 'bar' }, 'Digest realm=bar'),
]
assert_equal expected, @parser.parse('Basic realm=foo,, Digest realm=bar')
end
def test_parse_ntlm_init
expected = [
challenge('NTLM', nil, 'NTLM'),
]
assert_equal expected, @parser.parse('NTLM')
end
def test_parse_ntlm_type_2_3
expected = [
challenge('NTLM', 'foo=', 'NTLM foo='),
]
assert_equal expected, @parser.parse('NTLM foo=')
end
def test_parse_realm_uppercase
expected = [
challenge('Basic', { 'realm' => 'foo' }, 'Basic ReAlM=foo'),
]
assert_equal expected, @parser.parse('Basic ReAlM=foo')
end
def test_parse_realm_value_case
expected = [
challenge('Basic', { 'realm' => 'Foo' }, 'Basic realm=Foo'),
]
assert_equal expected, @parser.parse('Basic realm=Foo')
end
def test_parse_scheme_uppercase
expected = [
challenge('Basic', { 'realm' => 'foo' }, 'BaSiC realm=foo'),
]
assert_equal expected, @parser.parse('BaSiC realm=foo')
end
def test_parse_bad_whitespace_around_auth_param
expected = [
challenge('Basic', { 'realm' => 'foo' }, 'Basic realm = "foo"'),
]
assert_equal expected, @parser.parse('Basic realm = "foo"')
end
def test_parse_bad_single_quote
expected = [
challenge('Basic', { 'realm' => "'foo" }, "Basic realm='foo"),
]
assert_equal expected, @parser.parse("Basic realm='foo bar', qop='baz'")
end
def test_quoted_string
@parser.scanner = StringScanner.new '"text"'
string = @parser.quoted_string
assert_equal 'text', string
end
def test_quoted_string_bad
@parser.scanner = StringScanner.new '"text'
assert_nil @parser.quoted_string
end
def test_quoted_string_quote
@parser.scanner = StringScanner.new '"escaped \\" here"'
string = @parser.quoted_string
assert_equal 'escaped \\" here', string
end
def test_quoted_string_quote_end
@parser.scanner = StringScanner.new '"end \""'
string = @parser.quoted_string
assert_equal 'end \"', string
end
def test_token
@parser.scanner = StringScanner.new 'text'
string = @parser.token
assert_equal 'text', string
end
def test_token_space
@parser.scanner = StringScanner.new 't ext'
string = @parser.token
assert_equal 't', string
end
def test_token_special
@parser.scanner = StringScanner.new "t\text"
string = @parser.token
assert_equal 't', string
end
def challenge scheme, params, raw
Mechanize::HTTP::AuthChallenge.new scheme, params, raw
end
end
mechanize-2.7.7/test/test_mechanize_page_image.rb 0000644 0000041 0000041 00000013147 14006521455 022176 0 ustar www-data www-data require 'mechanize/test_case'
class TestMechanizePageImage < Mechanize::TestCase
def setup
super
@uri = URI 'http://example/'
@src = (@uri + 'a.jpg').to_s
@empty_page = Mechanize::Page.new(@uri, nil, '', 200, @mech)
end
def img attributes
img = node 'img', attributes
Mechanize::Page::Image.new img, @empty_page
end
def test_initialize
image = img("src" => "a.jpg", "alt" => "alt", "width" => "100",
"height" => "200", "title" => "title", "id" => "id",
"class" => "class")
assert_equal "a.jpg", image.src
assert_equal "alt", image.alt
assert_equal "100", image.width
assert_equal "200", image.height
assert_equal "title", image.title
assert_equal "id", image.dom_id
assert_equal "class", image.dom_class
end
def test_initialize_no_attributes
image = img({})
assert_nil image.src
assert_nil image.alt
assert_nil image.width
assert_nil image.height
assert_nil image.title
assert_nil image.dom_id
assert_nil image.dom_class
end
def test_caption
assert_equal "", img("src" => @src).caption
assert_equal "alt", img("src" => @src, "alt" => "alt").caption
assert_equal "title", img("src" => @src, "title" => "title").caption
assert_equal "title", img("src" => @src,
"alt" => "alt", "title" => "title").caption
end
def test_url
assert_equal ".jpg", img('src' => @src).extname
assert_equal "http://example/a.jpg", img('src' => @src).url.to_s
assert_equal "http://example/a%20.jpg", img('src' => 'http://example/a .jpg' ).url.to_s
end
def test_url_base
page = html_page <<-BODY
BODY
assert_equal "http://other.example/a.jpg", page.images.first.url
end
def test_extname
assert_equal ".jpg", img("src" => "a.jpg").extname
assert_equal ".PNG", img("src" => "a.PNG").extname
assert_equal ".aaa", img("src" => "unknown.aaa").extname
assert_equal "", img("src" => "nosuffiximage").extname
assert_nil img("width" => "1", "height" => "1").extname
assert_equal ".jpg", img("src" => "a.jpg?cache_buster").extname
end
def test_mime_type
assert_equal "image/jpeg", img("src" => "a.jpg").mime_type
assert_equal "image/png", img("src" => "a.PNG").mime_type
assert_nil img("src" => "unknown.aaa").mime_type
assert_nil img("src" => "nosuffiximage").mime_type
end
def test_fetch
image = img "src" => "http://localhost/button.jpg"
fetched = image.fetch
assert_equal fetched, @mech.page
assert_equal "http://localhost/button.jpg", fetched.uri.to_s
assert_equal "http://example/", requests.first['Referer']
assert @mech.visited? "http://localhost/button.jpg"
end
def test_fetch_referer_http_page_rel_src
# | rel-src http-src https-src
# http page | *page* page page
# https page | page empty empty
page = html_page '
'
page.images.first.fetch
assert_equal 'http', page.uri.scheme
assert_equal true, page.images.first.relative?
assert_equal "http://example/", requests.first['Referer']
end
def test_fetch_referer_http_page_abs_src
# | rel-src http-src https-src
# http page | page *page* *page*
# https page | page empty empty
page = html_page '
'
page.images.first.fetch
assert_equal 'http', page.uri.scheme
assert_equal false, page.images.first.relative?
assert_equal "http://example/", requests.first['Referer']
end
def test_fetch_referer_https_page_rel_src
# | rel-src http-src https-src
# http page | page page page
# https page | *page* empty empty
page = html_page '
'
page.uri = URI 'https://example/'
page.images.first.fetch
assert_equal 'https', page.uri.scheme
assert_equal true, page.images.first.relative?
assert_equal "https://example/", requests.first['Referer']
end
def test_fetch_referer_https_page_abs_src
# | rel-src http-src https-src
# http page | page page page
# https page | page *empty* *empty*
page = html_page '
'
page.uri = URI 'https://example/'
page.images.first.fetch
assert_equal 'https', page.uri.scheme
assert_equal false, page.images.first.relative?
assert_nil requests.first['Referer']
end
def test_image_referer_http_page_abs_src
page = html_page '
'
assert_equal 'http', page.uri.scheme
assert_equal @uri, page.images.first.image_referer.uri
end
def test_image_referer_http_page_rel_src
page = html_page '
'
assert_equal 'http', page.uri.scheme
assert_equal @uri, page.images.first.image_referer.uri
end
def test_image_referer_https_page_abs_src
page = html_page '
'
page.uri = URI 'https://example/'
assert_equal 'https', page.uri.scheme
assert_nil page.images.first.image_referer.uri
end
def test_image_referer_https_page_rel_src
page = html_page '
'
page.uri = URI 'https://example/'
assert_equal 'https', page.uri.scheme
assert_equal URI('https://example/'), page.images.first.image_referer.uri
end
def test_no_src_attribute
page = html_page '
'
page.uri = URI 'https://example/'
assert_equal URI('https://example/'), page.images.first.url
end
end
mechanize-2.7.7/test/test_mechanize_cookie_jar.rb 0000644 0000041 0000041 00000044640 14006521455 022227 0 ustar www-data www-data require 'mechanize/test_case'
require 'fileutils'
class TestMechanizeCookieJar < Mechanize::TestCase
def setup
super
@jar = Mechanize::CookieJar.new
@jar.extend Minitest::Assertions
def @jar.add(*args)
capture_io { super }
end
def @jar.jar(*args)
result = nil
capture_io { result = super }
result
end
def @jar.save_as(*args)
result = nil
capture_io { result = super }
result
end
def @jar.clear!(*args)
result = nil
capture_io { result = super }
result
end
end
def cookie_values(options = {})
{
:name => 'Foo',
:value => 'Bar',
:path => '/',
:expires => Time.now + (10 * 86400),
:for_domain => true,
:domain => 'rubygems.org'
}.merge(options)
end
def test_two_cookies_same_domain_and_name_different_paths
url = URI 'http://rubygems.org/'
cookie = Mechanize::Cookie.new(cookie_values)
@jar.add(url, cookie)
@jar.add(url, Mechanize::Cookie.new(cookie_values(:path => '/onetwo')))
assert_equal(1, @jar.cookies(url).length)
assert_equal 2, @jar.cookies(URI('http://rubygems.org/onetwo')).length
end
def test_domain_case
url = URI 'http://rubygems.org/'
# Add one cookie with an expiration date in the future
cookie = Mechanize::Cookie.new(cookie_values)
@jar.add(url, cookie)
assert_equal(1, @jar.cookies(url).length)
@jar.add(url, Mechanize::Cookie.new(
cookie_values(:domain => 'rubygems.Org', :name => 'aaron')))
assert_equal(2, @jar.cookies(url).length)
url2 = URI 'http://rubygems.oRg/'
assert_equal(2, @jar.cookies(url2).length)
end
def test_host_only
url = URI.parse('http://rubygems.org/')
@jar.add(url, Mechanize::Cookie.new(
cookie_values(:domain => 'rubygems.org', :for_domain => false)))
assert_equal(1, @jar.cookies(url).length)
assert_equal(1, @jar.cookies(URI('http://rubygems.org/')).length)
assert_equal(1, @jar.cookies(URI('https://rubygems.org/')).length)
assert_equal(0, @jar.cookies(URI('http://www.rubygems.org/')).length)
end
def test_empty_value
values = cookie_values(:value => "")
url = URI 'http://rubygems.org/'
# Add one cookie with an expiration date in the future
cookie = Mechanize::Cookie.new(values)
@jar.add(url, cookie)
assert_equal(1, @jar.cookies(url).length)
@jar.add url, Mechanize::Cookie.new(values.merge(:domain => 'rubygems.Org',
:name => 'aaron'))
assert_equal(2, @jar.cookies(url).length)
url2 = URI 'http://rubygems.oRg/'
assert_equal(2, @jar.cookies(url2).length)
end
def test_add_future_cookies
url = URI 'http://rubygems.org/'
# Add one cookie with an expiration date in the future
cookie = Mechanize::Cookie.new(cookie_values)
@jar.add(url, cookie)
assert_equal(1, @jar.cookies(url).length)
# Add the same cookie, and we should still only have one
@jar.add(url, Mechanize::Cookie.new(cookie_values))
assert_equal(1, @jar.cookies(url).length)
# Make sure we can get the cookie from different paths
assert_equal(1, @jar.cookies(URI('http://rubygems.org/login')).length)
# Make sure we can't get the cookie from different domains
assert_equal(0, @jar.cookies(URI('http://google.com/')).length)
end
def test_add_multiple_cookies
url = URI 'http://rubygems.org/'
# Add one cookie with an expiration date in the future
cookie = Mechanize::Cookie.new(cookie_values)
@jar.add(url, cookie)
assert_equal(1, @jar.cookies(url).length)
# Add the same cookie, and we should still only have one
@jar.add(url, Mechanize::Cookie.new(cookie_values(:name => 'Baz')))
assert_equal(2, @jar.cookies(url).length)
# Make sure we can get the cookie from different paths
assert_equal(2, @jar.cookies(URI('http://rubygems.org/login')).length)
# Make sure we can't get the cookie from different domains
assert_equal(0, @jar.cookies(URI('http://google.com/')).length)
end
def test_add_rejects_cookies_that_do_not_contain_an_embedded_dot
url = URI 'http://rubygems.org/'
tld_cookie = Mechanize::Cookie.new(cookie_values(:domain => '.org'))
@jar.add(url, tld_cookie)
# single dot domain is now treated as no domain
# single_dot_cookie = Mechanize::Cookie.new(cookie_values(:domain => '.'))
# @jar.add(url, single_dot_cookie)
assert_equal(0, @jar.cookies(url).length)
end
def test_fall_back_rules_for_local_domains
url = URI 'http://www.example.local'
tld_cookie = Mechanize::Cookie.new(cookie_values(:domain => '.local'))
@jar.add(url, tld_cookie)
assert_equal(0, @jar.cookies(url).length)
sld_cookie = Mechanize::Cookie.new(cookie_values(:domain => '.example.local'))
@jar.add(url, sld_cookie)
assert_equal(1, @jar.cookies(url).length)
end
def test_add_makes_exception_for_localhost
url = URI 'http://localhost'
tld_cookie = Mechanize::Cookie.new(cookie_values(:domain => 'localhost'))
@jar.add(url, tld_cookie)
assert_equal(1, @jar.cookies(url).length)
end
def test_add_cookie_for_the_parent_domain
url = URI 'http://x.foo.com'
cookie = Mechanize::Cookie.new(cookie_values(:domain => '.foo.com'))
@jar.add(url, cookie)
assert_equal(1, @jar.cookies(url).length)
end
def test_add_does_not_reject_cookies_from_a_nested_subdomain
url = URI 'http://y.x.foo.com'
cookie = Mechanize::Cookie.new(cookie_values(:domain => '.foo.com'))
@jar.add(url, cookie)
assert_equal(1, @jar.cookies(url).length)
end
def test_cookie_without_leading_dot_does_not_cause_substring_match
url = URI 'http://arubygems.org/'
cookie = Mechanize::Cookie.new(cookie_values(:domain => 'rubygems.org'))
@jar.add(url, cookie)
assert_equal(0, @jar.cookies(url).length)
end
def test_cookie_without_leading_dot_matches_subdomains
url = URI 'http://admin.rubygems.org/'
cookie = Mechanize::Cookie.new(cookie_values(:domain => 'rubygems.org'))
@jar.add(url, cookie)
assert_equal(1, @jar.cookies(url).length)
end
def test_cookies_with_leading_dot_match_subdomains
url = URI 'http://admin.rubygems.org/'
@jar.add(url, Mechanize::Cookie.new(cookie_values(:domain => '.rubygems.org')))
assert_equal(1, @jar.cookies(url).length)
end
def test_cookies_with_leading_dot_match_parent_domains
url = URI 'http://rubygems.org/'
@jar.add(url, Mechanize::Cookie.new(cookie_values(:domain => '.rubygems.org')))
assert_equal(1, @jar.cookies(url).length)
end
def test_cookies_with_leading_dot_match_parent_domains_exactly
url = URI 'http://arubygems.org/'
@jar.add(url, Mechanize::Cookie.new(cookie_values(:domain => '.rubygems.org')))
assert_equal(0, @jar.cookies(url).length)
end
def test_cookie_for_ipv4_address_matches_the_exact_ipaddress
url = URI 'http://192.168.0.1/'
cookie = Mechanize::Cookie.new(cookie_values(:domain => '192.168.0.1'))
@jar.add(url, cookie)
assert_equal(1, @jar.cookies(url).length)
end
def test_cookie_for_ipv4_address_does_not_cause_subdomain_match
url = URI 'http://192.168.0.1/'
cookie = Mechanize::Cookie.new(cookie_values(:domain => '.0.1'))
@jar.add(url, cookie)
assert_equal(0, @jar.cookies(url).length)
end
def test_cookie_for_ipv6_address_matches_the_exact_ipaddress
url = URI 'http://[fe80::0123:4567:89ab:cdef]/'
cookie = Mechanize::Cookie.new(cookie_values(:domain => '[fe80::0123:4567:89ab:cdef]'))
@jar.add(url, cookie)
assert_equal(1, @jar.cookies(url).length)
end
def test_cookies_dot
url = URI 'http://www.host.example/'
@jar.add(url,
Mechanize::Cookie.new(cookie_values(:domain => 'www.host.example')))
url = URI 'http://wwwxhost.example/'
assert_equal(0, @jar.cookies(url).length)
end
def test_clear_bang
url = URI 'http://rubygems.org/'
# Add one cookie with an expiration date in the future
cookie = Mechanize::Cookie.new(cookie_values)
@jar.add(url, cookie)
@jar.add(url, Mechanize::Cookie.new(cookie_values(:name => 'Baz')))
assert_equal(2, @jar.cookies(url).length)
@jar.clear!
assert_equal(0, @jar.cookies(url).length)
end
def test_save_cookies_yaml
url = URI 'http://rubygems.org/'
# Add one cookie with an expiration date in the future
cookie = Mechanize::Cookie.new(cookie_values)
s_cookie = Mechanize::Cookie.new(cookie_values(:name => 'Bar',
:expires => nil))
@jar.add(url, cookie)
@jar.add(url, s_cookie)
@jar.add(url, Mechanize::Cookie.new(cookie_values(:name => 'Baz')))
assert_equal(3, @jar.cookies(url).length)
in_tmpdir do
value = @jar.save_as("cookies.yml")
assert_same @jar, value
jar = Mechanize::CookieJar.new
jar.load("cookies.yml")
assert_equal(2, jar.cookies(url).length)
end
assert_equal(3, @jar.cookies(url).length)
end
def test_save_session_cookies_yaml
url = URI 'http://rubygems.org/'
# Add one cookie with an expiration date in the future
cookie = Mechanize::Cookie.new(cookie_values)
s_cookie = Mechanize::Cookie.new(cookie_values(:name => 'Bar',
:expires => nil))
@jar.add(url, cookie)
@jar.add(url, s_cookie)
@jar.add(url, Mechanize::Cookie.new(cookie_values(:name => 'Baz')))
assert_equal(3, @jar.cookies(url).length)
in_tmpdir do
@jar.save_as("cookies.yml", :format => :yaml, :session => true)
jar = Mechanize::CookieJar.new
jar.load("cookies.yml")
assert_equal(3, jar.cookies(url).length)
end
assert_equal(3, @jar.cookies(url).length)
end
def test_save_cookies_cookiestxt
url = URI 'http://rubygems.org/'
# Add one cookie with an expiration date in the future
cookie = Mechanize::Cookie.new(cookie_values)
s_cookie = Mechanize::Cookie.new(cookie_values(:name => 'Bar',
:expires => nil))
@jar.add(url, cookie)
@jar.add(url, s_cookie)
@jar.add(url, Mechanize::Cookie.new(cookie_values(:name => 'Baz')))
assert_equal(3, @jar.cookies(url).length)
in_tmpdir do
@jar.save_as("cookies.txt", :cookiestxt)
assert_match(/\A# (?:Netscape )?HTTP Cookie File$/, File.read("cookies.txt"))
jar = Mechanize::CookieJar.new
jar.load("cookies.txt", :cookiestxt)
assert_equal(2, jar.cookies(url).length)
end
in_tmpdir do
@jar.save_as("cookies.txt", :cookiestxt, :session => true)
assert_match(/\A# (?:Netscape )?HTTP Cookie File$/, File.read("cookies.txt"))
jar = Mechanize::CookieJar.new
jar.load("cookies.txt", :cookiestxt)
assert_equal(3, jar.cookies(url).length)
end
assert_equal(3, @jar.cookies(url).length)
end
def test_expire_cookies
url = URI 'http://rubygems.org/'
# Add one cookie with an expiration date in the future
cookie = Mechanize::Cookie.new(cookie_values)
@jar.add(url, cookie)
assert_equal(1, @jar.cookies(url).length)
# Add a second cookie
@jar.add(url, Mechanize::Cookie.new(cookie_values(:name => 'Baz')))
assert_equal(2, @jar.cookies(url).length)
# Make sure we can get the cookie from different paths
assert_equal(2, @jar.cookies(URI('http://rubygems.org/login')).length)
# Expire the first cookie
@jar.add(url, Mechanize::Cookie.new(
cookie_values(:expires => Time.now - (10 * 86400))))
assert_equal(1, @jar.cookies(url).length)
# Expire the second cookie
@jar.add(url, Mechanize::Cookie.new(
cookie_values( :name => 'Baz', :expires => Time.now - (10 * 86400))))
assert_equal(0, @jar.cookies(url).length)
end
def test_session_cookies
values = cookie_values(:expires => nil)
url = URI 'http://rubygems.org/'
# Add one cookie with an expiration date in the future
cookie = Mechanize::Cookie.new(values)
@jar.add(url, cookie)
assert_equal(1, @jar.cookies(url).length)
# Add a second cookie
@jar.add(url, Mechanize::Cookie.new(values.merge(:name => 'Baz')))
assert_equal(2, @jar.cookies(url).length)
# Make sure we can get the cookie from different paths
assert_equal(2, @jar.cookies(URI('http://rubygems.org/login')).length)
# Expire the first cookie
@jar.add(url, Mechanize::Cookie.new(values.merge(:expires => Time.now - (10 * 86400))))
assert_equal(1, @jar.cookies(url).length)
# Expire the second cookie
@jar.add(url, Mechanize::Cookie.new(
values.merge(:name => 'Baz', :expires => Time.now - (10 * 86400))))
assert_equal(0, @jar.cookies(url).length)
# When given a URI with a blank path, CookieJar#cookies should return
# cookies with the path '/':
url = URI 'http://rubygems.org'
assert_equal '', url.path
assert_equal(0, @jar.cookies(url).length)
# Now add a cookie with the path set to '/':
@jar.add(url, Mechanize::Cookie.new(values.merge( :name => 'has_root_path',
:path => '/')))
assert_equal(1, @jar.cookies(url).length)
end
def test_paths
values = cookie_values(:path => "/login", :expires => nil)
url = URI 'http://rubygems.org/login'
# Add one cookie with an expiration date in the future
cookie = Mechanize::Cookie.new(values)
@jar.add(url, cookie)
assert_equal(1, @jar.cookies(url).length)
# Add a second cookie
@jar.add(url, Mechanize::Cookie.new(values.merge( :name => 'Baz' )))
assert_equal(2, @jar.cookies(url).length)
# Make sure we don't get the cookie in a different path
assert_equal(0, @jar.cookies(URI('http://rubygems.org/hello')).length)
assert_equal(0, @jar.cookies(URI('http://rubygems.org/')).length)
# Expire the first cookie
@jar.add(url, Mechanize::Cookie.new(values.merge( :expires => Time.now - (10 * 86400))))
assert_equal(1, @jar.cookies(url).length)
# Expire the second cookie
@jar.add(url, Mechanize::Cookie.new(values.merge( :name => 'Baz',
:expires => Time.now - (10 * 86400))))
assert_equal(0, @jar.cookies(url).length)
end
def test_save_and_read_cookiestxt
url = URI 'http://rubygems.org/'
# Add one cookie with an expiration date in the future
cookie = Mechanize::Cookie.new(cookie_values)
@jar.add(url, cookie)
@jar.add(url, Mechanize::Cookie.new(cookie_values(:name => 'Baz')))
assert_equal(2, @jar.cookies(url).length)
in_tmpdir do
@jar.save_as("cookies.txt", :cookiestxt)
@jar.clear!
@jar.load("cookies.txt", :cookiestxt)
end
assert_equal(2, @jar.cookies(url).length)
end
def test_save_and_read_cookiestxt_with_session_cookies
url = URI 'http://rubygems.org/'
@jar.add(url, Mechanize::Cookie.new(cookie_values(:expires => nil)))
in_tmpdir do
@jar.save_as("cookies.txt", :cookiestxt)
@jar.clear!
@jar.load("cookies.txt", :cookiestxt)
end
assert_equal(0, @jar.cookies(url).length)
end
def test_prevent_command_injection_when_saving
url = URI 'http://rubygems.org/'
path = '| ruby -rfileutils -e \'FileUtils.touch("vul.txt")\''
@jar.add(url, Mechanize::Cookie.new(cookie_values))
in_tmpdir do
@jar.save_as(path, :cookiestxt)
assert_equal(false, File.exist?('vul.txt'))
end
end
def test_prevent_command_injection_when_loading
url = URI 'http://rubygems.org/'
path = '| ruby -rfileutils -e \'FileUtils.touch("vul.txt")\''
@jar.add(url, Mechanize::Cookie.new(cookie_values))
in_tmpdir do
@jar.save_as("cookies.txt", :cookiestxt)
@jar.clear!
assert_raises Errno::ENOENT do
@jar.load(path, :cookiestxt)
end
assert_equal(false, File.exist?('vul.txt'))
end
end
def test_save_and_read_expired_cookies
url = URI 'http://rubygems.org/'
@jar.jar['rubygems.org'] = {}
@jar.add url, Mechanize::Cookie.new(cookie_values)
# HACK no asertion
end
def test_ssl_cookies
# thanks to michal "ocher" ochman for reporting the bug responsible for this test.
values = cookie_values(:expires => nil)
values_ssl = values.merge(:name => 'Baz', :domain => "#{values[:domain]}:443")
url = URI 'https://rubygems.org/login'
cookie = Mechanize::Cookie.new(values)
@jar.add(url, cookie)
assert_equal(1, @jar.cookies(url).length, "did not handle SSL cookie")
cookie = Mechanize::Cookie.new(values_ssl)
@jar.add(url, cookie)
assert_equal(2, @jar.cookies(url).length, "did not handle SSL cookie with :443")
end
def test_secure_cookie
nurl = URI 'http://rubygems.org/login'
surl = URI 'https://rubygems.org/login'
ncookie = Mechanize::Cookie.new(cookie_values(:name => 'Foo1'))
scookie = Mechanize::Cookie.new(cookie_values(:name => 'Foo2', :secure => true))
@jar.add(nurl, ncookie)
@jar.add(nurl, scookie)
@jar.add(surl, ncookie)
@jar.add(surl, scookie)
assert_equal('Foo1', @jar.cookies(nurl).map { |c| c.name }.sort.join(' ') )
assert_equal('Foo1 Foo2', @jar.cookies(surl).map { |c| c.name }.sort.join(' ') )
end
def test_save_cookies_cookiestxt_subdomain
top_url = URI 'http://rubygems.org/'
subdomain_url = URI 'http://admin.rubygems.org/'
# cookie1 is for *.rubygems.org; cookie2 is only for rubygems.org, no subdomains
cookie1 = Mechanize::Cookie.new(cookie_values)
cookie2 = Mechanize::Cookie.new(cookie_values(:name => 'Boo', :for_domain => false))
@jar.add(top_url, cookie1)
@jar.add(top_url, cookie2)
assert_equal(2, @jar.cookies(top_url).length)
assert_equal(1, @jar.cookies(subdomain_url).length)
in_tmpdir do
@jar.save_as("cookies.txt", :cookiestxt)
jar = Mechanize::CookieJar.new
jar.load("cookies.txt", :cookiestxt) # HACK test the format
assert_equal(2, jar.cookies(top_url).length)
assert_equal(1, jar.cookies(subdomain_url).length)
# Check that we actually wrote the file correctly (not just that we were
# able to read what we wrote):
#
# * Cookies that only match exactly the domain specified must not have a
# leading dot, and must have FALSE as the second field.
# * Cookies that match subdomains may have a leading dot, and must have
# TRUE as the second field.
cookies_txt = File.readlines("cookies.txt")
assert_equal(1, cookies_txt.grep( /^rubygems\.org\tFALSE/ ).length)
assert_equal(1, cookies_txt.grep( /^\.rubygems\.org\tTRUE/ ).length)
end
assert_equal(2, @jar.cookies(top_url).length)
assert_equal(1, @jar.cookies(subdomain_url).length)
end
end
mechanize-2.7.7/test/test_mechanize_file_request.rb 0000644 0000041 0000041 00000000746 14006521455 022610 0 ustar www-data www-data require 'mechanize/test_case'
class TestMechanizeFileRequest < Mechanize::TestCase
def setup
@uri = URI.parse 'file:///nonexistent'
@r = Mechanize::FileRequest.new @uri
end
def test_initialize
assert_equal @uri, @r.uri
assert_equal '/nonexistent', @r.path
assert_respond_to @r, :[]=
assert_respond_to @r, :add_field
assert_respond_to @r, :each_header
end
def test_response_body_permitted_eh
assert @r.response_body_permitted?
end
end
mechanize-2.7.7/test/test_mechanize_page_meta_refresh.rb 0000644 0000041 0000041 00000007723 14006521455 023563 0 ustar www-data www-data require 'mechanize/test_case'
class TestMechanizePageMetaRefresh < Mechanize::TestCase
def setup
super
@MR = Mechanize::Page::MetaRefresh
@uri = URI 'http://example/here/'
end
def util_page delay, uri
body = <<-BODY
BODY
Mechanize::Page.new(@uri, nil, body, 200, @mech)
end
def util_meta_refresh page
node = page.search('meta').first
@MR.from_node node, page
end
def test_class_parse
delay, uri, link_self = @MR.parse "0; url=http://localhost:8080/path", @uri
assert_equal "0", delay
assert_equal "http://localhost:8080/path", uri.to_s
refute link_self
delay, uri, link_self =
@MR.parse "100.001; url=http://localhost:8080/path", @uri
assert_equal "100.001", delay
assert_equal "http://localhost:8080/path", uri.to_s
refute link_self
delay, uri, link_self =
@MR.parse "0; url='http://localhost:8080/path'", @uri
assert_equal "0", delay
assert_equal "http://localhost:8080/path", uri.to_s
refute link_self
delay, uri, link_self =
@MR.parse "0; url=\"http://localhost:8080/path\"", @uri
assert_equal "0", delay
assert_equal "http://localhost:8080/path", uri.to_s
refute link_self
delay, uri, link_self = @MR.parse "0; url=", @uri
assert_equal "0", delay
assert_equal "http://example/here/", uri.to_s
assert link_self
delay, uri, link_self = @MR.parse "0", @uri
assert_equal "0", delay
assert_equal "http://example/here/", uri.to_s
assert link_self
delay, uri, link_self = @MR.parse " 0; ", @uri
assert_equal "0", delay
assert_equal "http://example/here/", uri.to_s
assert link_self
delay, uri, link_self = @MR.parse " 0 ; ", @uri
assert_equal "0", delay
assert_equal "http://example/here/", uri.to_s
assert link_self
delay, uri, link_self = @MR.parse "0; UrL=http://localhost:8080/path", @uri
assert_equal "0", delay
assert_equal "http://localhost:8080/path", uri.to_s
refute link_self
delay, uri, link_self = @MR.parse "0 ; UrL = http://localhost:8080/path", @uri
assert_equal "0", delay
assert_equal "http://localhost:8080/path", uri.to_s
refute link_self
end
def test_class_parse_funky
delay, uri, link_self = @MR.parse "0; url=/funky?Welcome<%2Fb>", @uri
assert_equal "0", delay
assert_equal "http://example/funky?%3Cb%3EWelcome%3C%2Fb%3E",
uri.to_s
refute link_self
end
def test_class_from_node
page = util_page 5, 'http://b.example'
link = util_meta_refresh page
assert_equal 5, link.delay
assert_equal 'http://b.example', link.href
page = util_page 5, 'http://example/a'
link = util_meta_refresh page
assert_equal 5, link.delay
assert_equal 'http://example/a', link.href
page = util_page 5, 'test'
link = util_meta_refresh page
assert_equal 5, link.delay
assert_equal 'test', link.href
page = util_page 5, '/test'
link = util_meta_refresh page
assert_equal 5, link.delay
assert_equal '/test', link.href
page = util_page 5, nil
link = util_meta_refresh page
assert_equal 5, link.delay
assert_nil link.href
page = util_page 5, @uri
link = util_meta_refresh page
assert_equal 5, link.delay
assert_equal 'http://example/here/', link.href
end
def test_class_from_node_no_content
body = <<-BODY
BODY
page = Mechanize::Page.new(@uri, nil, body, 200, @mech)
assert_nil util_meta_refresh page
end
def test_class_from_node_not_refresh
body = <<-BODY
BODY
page = Mechanize::Page.new(@uri, nil, body, 200, @mech)
assert_nil util_meta_refresh page
end
def test_meta_refresh_click_sends_no_referer
page = util_page 0, '/referer'
link = util_meta_refresh page
refreshed = link.click
assert_equal '', refreshed.body
end
end
mechanize-2.7.7/test/test_mechanize_redirect_not_get_or_head_error.rb 0000644 0000041 0000041 00000000370 14006521455 026324 0 ustar www-data www-data require 'mechanize/test_case'
class TestMechanizeRedirectNotGetOrHead < Mechanize::TestCase
def test_to_s
page = fake_page
error = Mechanize::RedirectNotGetOrHeadError.new(page, :put)
assert_match(/ PUT /, error.to_s)
end
end
mechanize-2.7.7/test/test_mechanize_http_auth_store.rb 0000644 0000041 0000041 00000012477 14006521455 023341 0 ustar www-data www-data # coding: utf-8
require 'mechanize/test_case'
class TestMechanizeHttpAuthStore < Mechanize::TestCase
def setup
super
@store = Mechanize::HTTP::AuthStore.new
@uri = URI.parse 'http://example/'
end
def test_add_auth
@store.add_auth @uri + '/path', 'user', 'pass'
expected = {
@uri => {
nil => ['user', 'pass', nil],
}
}
assert_equal expected, @store.auth_accounts
end
def test_add_auth_domain
@store.add_auth @uri + '/path', 'user1', 'pass', nil, 'domain'
expected = {
@uri => {
nil => %w[user1 pass domain],
}
}
assert_equal expected, @store.auth_accounts
e = assert_raises ArgumentError do
@store.add_auth @uri, 'user3', 'pass', 'realm', 'domain'
end
assert_equal 'NTLM domain given with realm which NTLM does not use',
e.message
end
def test_add_auth_realm
@store.add_auth @uri, 'user1', 'pass'
@store.add_auth @uri, 'user2', 'pass', 'realm'
expected = {
@uri => {
nil => ['user1', 'pass', nil],
'realm' => ['user2', 'pass', nil],
}
}
assert_equal expected, @store.auth_accounts
end
def test_add_auth_realm_case
@store.add_auth @uri, 'user1', 'pass', 'realm'
@store.add_auth @uri, 'user2', 'pass', 'Realm'
expected = {
@uri => {
'realm' => ['user1', 'pass', nil],
'Realm' => ['user2', 'pass', nil],
}
}
assert_equal expected, @store.auth_accounts
end
def test_add_auth_string
@store.add_auth "#{@uri}/path", 'user', 'pass'
expected = {
@uri => {
nil => ['user', 'pass', nil],
}
}
assert_equal expected, @store.auth_accounts
end
def test_add_default_auth
_, err = capture_io do
@store.add_default_auth 'user', 'pass'
end
expected = ['user', 'pass', nil]
assert_equal expected, @store.default_auth
assert_match 'DISCLOSURE WITHOUT YOUR KNOWLEDGE', err
capture_io do
@store.add_default_auth 'user', 'pass', 'realm'
end
expected = %w[user pass realm]
assert_equal expected, @store.default_auth
end
def test_credentials_eh
challenges = [
Mechanize::HTTP::AuthChallenge.new('Basic', 'realm' => 'r'),
Mechanize::HTTP::AuthChallenge.new('Digest', 'realm' => 'r'),
]
refute @store.credentials? @uri, challenges
@store.add_auth @uri, 'user', 'pass'
assert @store.credentials? @uri, challenges
assert @store.credentials? "#{@uri}/path", challenges
end
def test_credentials_for
assert_nil @store.credentials_for(@uri, 'realm')
@store.add_auth @uri, 'user', 'pass', 'realm'
assert_equal ['user', 'pass', nil], @store.credentials_for(@uri, 'realm')
assert_equal ['user', 'pass', nil],
@store.credentials_for(@uri.to_s, 'realm')
assert_nil @store.credentials_for(@uri, 'other')
end
def test_credentials_for_default
assert_nil @store.credentials_for(@uri, 'realm')
capture_io do
@store.add_default_auth 'user1', 'pass'
end
assert_equal ['user1', 'pass', nil], @store.credentials_for(@uri, 'realm')
@store.add_auth @uri, 'user2', 'pass'
assert_equal ['user2', 'pass', nil], @store.credentials_for(@uri, 'realm')
assert_equal ['user2', 'pass', nil], @store.credentials_for(@uri, 'other')
end
def test_credentials_for_no_realm
@store.add_auth @uri, 'user', 'pass' # no realm set
assert_equal ['user', 'pass', nil], @store.credentials_for(@uri, 'realm')
end
def test_credentials_for_realm
@store.add_auth @uri, 'user1', 'pass'
@store.add_auth @uri, 'user2', 'pass', 'realm'
assert_equal ['user2', 'pass', nil], @store.credentials_for(@uri, 'realm')
assert_equal ['user1', 'pass', nil], @store.credentials_for(@uri, 'other')
end
def test_credentials_for_realm_case
@store.add_auth @uri, 'user1', 'pass', 'realm'
@store.add_auth @uri, 'user2', 'pass', 'Realm'
assert_equal ['user1', 'pass', nil], @store.credentials_for(@uri, 'realm')
assert_equal ['user2', 'pass', nil], @store.credentials_for(@uri, 'Realm')
end
def test_credentials_for_path
@store.add_auth @uri, 'user', 'pass', 'realm'
uri = @uri + '/path'
assert_equal ['user', 'pass', nil], @store.credentials_for(uri, 'realm')
end
def test_remove_auth
@store.remove_auth @uri
assert_empty @store.auth_accounts
end
def test_remove_auth_both
@store.add_auth @uri, 'user1', 'pass'
@store.add_auth @uri, 'user2', 'pass', 'realm'
uri = @uri + '/path'
@store.remove_auth uri
assert_empty @store.auth_accounts
end
def test_remove_auth_realm
@store.add_auth @uri, 'user1', 'pass'
@store.add_auth @uri, 'user2', 'pass', 'realm'
@store.remove_auth @uri, 'realm'
expected = {
@uri => {
nil => ['user1', 'pass', nil]
}
}
assert_equal expected, @store.auth_accounts
end
def test_remove_auth_realm_case
@store.add_auth @uri, 'user1', 'pass', 'realm'
@store.add_auth @uri, 'user2', 'pass', 'Realm'
@store.remove_auth @uri, 'Realm'
expected = {
@uri => {
'realm' => ['user1', 'pass', nil]
}
}
assert_equal expected, @store.auth_accounts
end
def test_remove_auth_string
@store.add_auth @uri, 'user1', 'pass'
@store.remove_auth "#{@uri}/path"
assert_empty @store.auth_accounts
end
end
mechanize-2.7.7/test/test_mechanize_form_textarea.rb 0000644 0000041 0000041 00000003047 14006521455 022756 0 ustar www-data www-data require 'mechanize/test_case'
class TestMechanizeFormTextarea < Mechanize::TestCase
def setup
super
@page = @mech.get("http://localhost/tc_textarea.html")
end
def test_empty_text_area
form = @page.forms_with(:name => 'form1').first
assert_equal('', form.field_with(:name => 'text1').value)
form.text1 = 'Hello World'
assert_equal('Hello World', form.field_with(:name => 'text1').value)
page = @mech.submit(form)
assert_equal(1, page.links.length)
assert_equal('text1:Hello World', page.links[0].text)
end
def test_non_empty_textfield
form = @page.forms_with(:name => 'form2').first
assert_equal('sample text', form.field_with(:name => 'text1').value)
page = @mech.submit(form)
assert_equal(1, page.links.length)
assert_equal('text1:sample text', page.links[0].text)
end
def test_multi_textfield
form = @page.form_with(:name => 'form3')
assert_equal(2, form.fields_with(:name => 'text1').length)
assert_equal('', form.fields_with(:name => 'text1')[0].value)
assert_equal('sample text', form.fields_with(:name => 'text1')[1].value)
form.text1 = 'Hello World'
assert_equal('Hello World', form.fields_with(:name => 'text1')[0].value)
assert_equal('sample text', form.fields_with(:name => 'text1')[1].value)
page = @mech.submit(form)
assert_equal(2, page.links.length)
link = page.links_with(:text => 'text1:sample text')
assert_equal(1, link.length)
link = page.links_with(:text => 'text1:Hello World')
assert_equal(1, link.length)
end
end
mechanize-2.7.7/test/test_mechanize_directory_saver.rb 0000644 0000041 0000041 00000002420 14006521455 023314 0 ustar www-data www-data require 'mechanize/test_case'
class TestMechanizeDirectorySaver < Mechanize::TestCase
def setup
super
@uri = URI 'http://example/relative/tc_relative_links.html'
@io = StringIO.new 'hello world'
end
def test_self_save_to
in_tmpdir do
saver = Mechanize::DirectorySaver.save_to 'dir'
saver.new @uri, nil, @io, 200
assert File.exist? 'dir/tc_relative_links.html'
refute File.exist? 'dir/relative'
end
end
def test_self_save_to_cd
in_tmpdir do
saver = Mechanize::DirectorySaver.save_to 'dir'
FileUtils.mkdir 'other'
Dir.chdir 'other' do
saver.new @uri, nil, @io, 200
end
assert File.exist? 'dir/tc_relative_links.html'
refute File.exist? 'dir/relative'
end
end
def test_with_decode_filename
in_tmpdir do
saver = Mechanize::DirectorySaver.save_to 'dir', :decode_filename => true
uri = URI 'http://example.com/foo+bar.html'
saver.new uri, nil, @io, 200
assert File.exist? 'dir/foo bar.html'
end
end
def test_initialize_no_save_dir
in_tmpdir do
e = assert_raises Mechanize::Error do
Mechanize::DirectorySaver.new @uri, nil, @io, 200
end
assert_match %r%no save directory specified%, e.message
end
end
end
mechanize-2.7.7/test/test_multi_select.rb 0000644 0000041 0000041 00000010214 14006521455 020556 0 ustar www-data www-data require 'mechanize/test_case'
class MultiSelectTest < Mechanize::TestCase
def setup
super
@page = @mech.get("http://localhost/form_multi_select.html")
@form = @page.forms.first
end
def test_option_with
o = @form.field_with(:name => 'list').option_with(:value => '1')
assert_equal '1', o.value
end
def test_options_with
os = @form.field_with(:name => 'list').options_with(:value => /1|2/)
assert_equal ['1', '2'].sort, os.map { |x| x.value }.sort
end
def test_select_none
page = @mech.get("http://localhost/form_multi_select.html")
form = page.forms.first
form.field_with(:name => 'list').select_none
page = @mech.submit(form)
assert_equal(0, page.links.length)
end
def test_select_all
page = @mech.get("http://localhost/form_multi_select.html")
form = page.forms.first
form.field_with(:name => 'list').select_all
page = @mech.submit(form)
assert_equal(6, page.links.length)
assert_equal(1, page.links_with(:text => 'list:1').length)
assert_equal(1, page.links_with(:text => 'list:2').length)
assert_equal(1, page.links_with(:text => 'list:3').length)
assert_equal(1, page.links_with(:text => 'list:4').length)
assert_equal(1, page.links_with(:text => 'list:5').length)
assert_equal(1, page.links_with(:text => 'list:6').length)
end
def test_click_all
page = @mech.get("http://localhost/form_multi_select.html")
form = page.forms.first
form.field_with(:name => 'list').options.each { |o| o.click }
page = @mech.submit(form)
assert_equal(5, page.links.length)
assert_equal(1, page.links_with(:text => 'list:1').length)
assert_equal(1, page.links_with(:text => 'list:3').length)
assert_equal(1, page.links_with(:text => 'list:4').length)
assert_equal(1, page.links_with(:text => 'list:5').length)
assert_equal(1, page.links_with(:text => 'list:6').length)
end
def test_select_default
page = @mech.get("http://localhost/form_multi_select.html")
form = page.forms.first
page = @mech.submit(form)
assert_equal(1, page.links.length)
assert_equal(1, page.links_with(:text => 'list:2').length)
end
def test_select_one
page = @mech.get("http://localhost/form_multi_select.html")
form = page.forms.first
form.list = 'Aaron'
assert_equal(['Aaron'], form.list)
page = @mech.submit(form)
assert_equal(1, page.links.length)
assert_equal('list:Aaron', page.links.first.text)
end
def test_select_two
page = @mech.get("http://localhost/form_multi_select.html")
form = page.forms.first
form.list = ['1', 'Aaron']
page = @mech.submit(form)
assert_equal(2, page.links.length)
assert_equal(1, page.links_with(:text => 'list:1').length)
assert_equal(1, page.links_with(:text => 'list:Aaron').length)
end
def test_select_three
page = @mech.get("http://localhost/form_multi_select.html")
form = page.forms.first
form.list = ['1', '2', '3']
page = @mech.submit(form)
assert_equal(3, page.links.length)
assert_equal(1, page.links_with(:text => 'list:1').length)
assert_equal(1, page.links_with(:text => 'list:2').length)
assert_equal(1, page.links_with(:text => 'list:3').length)
end
def test_select_three_twice
page = @mech.get("http://localhost/form_multi_select.html")
form = page.forms.first
form.list = ['1', '2', '3']
form.list = ['1', '2', '3']
page = @mech.submit(form)
assert_equal(3, page.links.length)
assert_equal(1, page.links_with(:text => 'list:1').length)
assert_equal(1, page.links_with(:text => 'list:2').length)
assert_equal(1, page.links_with(:text => 'list:3').length)
end
def test_select_with_click
page = @mech.get("http://localhost/form_multi_select.html")
form = page.forms.first
form.list = ['1', 'Aaron']
form.field_with(:name => 'list').options[3].tick
assert_equal(['1', 'Aaron', '4'].sort, form.list.sort)
page = @mech.submit(form)
assert_equal(3, page.links.length)
assert_equal(1, page.links_with(:text => 'list:1').length)
assert_equal(1, page.links_with(:text => 'list:Aaron').length)
assert_equal(1, page.links_with(:text => 'list:4').length)
end
end
mechanize-2.7.7/test/test_mechanize_headers.rb 0000644 0000041 0000041 00000001442 14006521455 021526 0 ustar www-data www-data require 'mechanize/test_case'
class TestMechanizeHeaders < Mechanize::TestCase
def setup
super
@headers = Mechanize::Headers.new
@headers['content-type'] = 'text/html'
@headers['Content-encoding'] = 'gzip'
@headers['SERVER'] = 'Apache/2.2'
end
def test_aref
assert_equal('Apache/2.2', @headers['server'])
assert_equal('text/html', @headers['Content-Type'])
end
def test_key?
assert_equal(true, @headers.key?('content-Encoding'))
end
def test_canonical_each
all_keys = ['Content-Type', 'Content-Encoding', 'Server']
keys = all_keys.dup
@headers.canonical_each { |key, value|
case keys.delete(key)
when *all_keys
# ok
else
flunk "unexpected key: #{key}"
end
}
assert_equal([], keys)
end
end
mechanize-2.7.7/test/test_mechanize_form_keygen.rb 0000644 0000041 0000041 00000001626 14006521455 022424 0 ustar www-data www-data require 'mechanize/test_case'
class TestMechanizeFormKeygen < Mechanize::TestCase
def setup
super
keygen = node('keygen',
'name' => 'userkey',
'challenge' => 'f4832e1d200df3df8c5c859edcabe52f')
@keygen = Mechanize::Form::Keygen.new keygen
end
def test_challenge
assert_equal "f4832e1d200df3df8c5c859edcabe52f", @keygen.challenge
end
def test_key
assert @keygen.key.kind_of?(OpenSSL::PKey::PKey), "Not an OpenSSL key"
assert @keygen.key.private?, "Not a private key"
end
def test_spki_signature
skip("JRuby PKI doesn't handle this for reasons I've been unable to understand") if RUBY_ENGINE=~/jruby/
spki = OpenSSL::Netscape::SPKI.new @keygen.value
assert_equal @keygen.challenge, spki.challenge
assert_equal @keygen.key.public_key.to_pem, spki.public_key.to_pem
assert spki.verify(@keygen.key.public_key)
end
end
mechanize-2.7.7/test/test_mechanize_form_file_upload.rb 0000644 0000041 0000041 00000000626 14006521455 023424 0 ustar www-data www-data require 'mechanize/test_case'
class TestMechanizeFormFileUpload < Mechanize::TestCase
def test_file_name
field = node 'input'
field = Mechanize::Form::FileUpload.new field, 'a&b'
assert_equal 'a&b', field.file_name
end
def test_file_name_entity
field = node 'input'
field = Mechanize::Form::FileUpload.new field, 'a&b'
assert_equal 'a&b', field.file_name
end
end
mechanize-2.7.7/test/test_mechanize_parser.rb 0000644 0000041 0000041 00000015352 14006521455 021414 0 ustar www-data www-data require 'mechanize/test_case'
class TestMechanizeParser < Mechanize::TestCase
class P
include Mechanize::Parser
attr_accessor :filename
attr_accessor :response
attr_accessor :uri
def initialize
@uri = URI 'http://example'
@full_path = false
end
end
def setup
super
@parser = P.new
end
def test_extract_filename
@parser.response = {}
assert_equal 'index.html', @parser.extract_filename
end
def test_extract_filename_content_disposition
@parser.uri = URI 'http://example/foo'
@parser.response = {
'content-disposition' => 'attachment; filename=genome.jpeg'
}
assert_equal 'genome.jpeg', @parser.extract_filename
end
def test_extract_filename_content_disposition_bad
@parser.uri = URI 'http://example/foo'
@parser.response = {
'content-disposition' => "inline; filename*=UTF-8''X%20Y.jpg"
}
assert_equal 'foo.html', @parser.extract_filename
@parser.response = {
'content-disposition' => "inline; filename=\"\""
}
assert_equal 'foo.html', @parser.extract_filename
end
def test_extract_filename_content_disposition_path
@parser.uri = URI 'http://example'
@parser.response = {
'content-disposition' => 'attachment; filename="../genome.jpeg"'
}
assert_equal 'example/genome.jpeg', @parser.extract_filename(true)
@parser.response = {
'content-disposition' => 'attachment; filename="foo/genome.jpeg"'
}
assert_equal 'example/genome.jpeg', @parser.extract_filename(true)
end
def test_extract_filename_content_disposition_path_windows
@parser.uri = URI 'http://example'
@parser.response = {
'content-disposition' => 'attachment; filename="..\\\\genome.jpeg"'
}
assert_equal 'example/genome.jpeg', @parser.extract_filename(true)
@parser.response = {
'content-disposition' => 'attachment; filename="foo\\\\genome.jpeg"'
}
assert_equal 'example/genome.jpeg', @parser.extract_filename(true)
end
def test_extract_filename_content_disposition_full_path
@parser.uri = URI 'http://example/foo'
@parser.response = {
'content-disposition' => 'attachment; filename=genome.jpeg'
}
assert_equal 'example/genome.jpeg', @parser.extract_filename(true)
end
def test_extract_filename_content_disposition_quoted
@parser.uri = URI 'http://example'
@parser.response = {
'content-disposition' => 'attachment; filename="\"some \"file\""'
}
assert_equal '_some__file_', @parser.extract_filename
end
def test_extract_filename_content_disposition_special
@parser.uri = URI 'http://example/foo'
@parser.response = {
'content-disposition' => 'attachment; filename="/\\\\<>:\\"|?*"'
}
assert_equal '_______', @parser.extract_filename
chars = (0..12).map { |c| c.chr }.join
chars += "\\\r"
chars += (14..31).map { |c| c.chr }.join
@parser.response = {
'content-disposition' => "attachment; filename=\"#{chars}\""
}
assert_equal '_' * 32, @parser.extract_filename
end
def test_extract_filename_content_disposition_windows_special
@parser.uri = URI 'http://example'
windows_special = %w[
AUX
COM1
COM2
COM3
COM4
COM5
COM6
COM7
COM8
COM9
CON
LPT1
LPT2
LPT3
LPT4
LPT5
LPT6
LPT7
LPT8
LPT9
NUL
PRN
]
windows_special.each do |special|
@parser.response = {
'content-disposition' => "attachment; filename=#{special}"
}
assert_equal "_#{special}", @parser.extract_filename
end
end
def test_extract_filename_content_disposition_empty
@parser.uri = URI 'http://example'
@parser.response = {
'content-disposition' => 'inline; filename="/"'
}
assert_equal '', @parser.extract_filename
end
def test_extract_filename_host
@parser.response = {}
@parser.uri = URI 'http://example'
assert_equal 'example/index.html', @parser.extract_filename(true)
end
def test_extract_filename_special_character
@parser.response = {}
invisible = "\t\n\v\f\r"
invisible.chars.each do |char|
begin
@parser.uri = URI "http://example/#{char}"
assert_equal 'index.html', @parser.extract_filename, char.inspect
rescue URI::InvalidURIError
# ignore
end
end
escaped = "<>\"\\|"
escaped.chars.each do |char|
escaped_char = CGI.escape char
@parser.uri = URI "http://example/#{escaped_char}"
assert_equal "#{escaped_char}.html", @parser.extract_filename, char
end
@parser.uri = URI "http://example/?"
assert_equal 'index.html_', @parser.extract_filename, 'empty query'
@parser.uri = URI "http://example/:"
assert_equal '_.html', @parser.extract_filename, 'colon'
@parser.uri = URI "http://example/*"
assert_equal '_.html', @parser.extract_filename, 'asterisk'
end
def test_extract_filename_uri
@parser.response = {}
@parser.uri = URI 'http://example/foo'
assert_equal 'foo.html', @parser.extract_filename
@parser.uri += '/foo.jpg'
assert_equal 'foo.jpg', @parser.extract_filename
end
def test_extract_filename_uri_full_path
@parser.response = {}
@parser.uri = URI 'http://example/foo'
assert_equal 'example/foo.html', @parser.extract_filename(true)
@parser.uri += '/foo.jpg'
assert_equal 'example/foo.jpg', @parser.extract_filename(true)
end
def test_extract_filename_uri_query
@parser.response = {}
@parser.uri = URI 'http://example/?id=5'
assert_equal 'index.html_id=5', @parser.extract_filename
@parser.uri += '/foo.html?id=5'
assert_equal 'foo.html_id=5', @parser.extract_filename
end
def test_extract_filename_uri_slash
@parser.response = {}
@parser.uri = URI 'http://example/foo/'
assert_equal 'example/foo/index.html', @parser.extract_filename(true)
@parser.uri += '/foo///'
assert_equal 'example/foo/index.html', @parser.extract_filename(true)
end
def test_extract_filename_windows_special
@parser.uri = URI 'http://example'
@parser.response = {}
windows_special = %w[
AUX
COM1
COM2
COM3
COM4
COM5
COM6
COM7
COM8
COM9
CON
LPT1
LPT2
LPT3
LPT4
LPT5
LPT6
LPT7
LPT8
LPT9
NUL
PRN
]
windows_special.each do |special|
@parser.uri += "/#{special}"
assert_equal "_#{special}.html", @parser.extract_filename
end
end
def test_fill_header
@parser.fill_header 'a' => 'b'
expected = { 'a' => 'b' }
assert_equal expected, @parser.response
end
def test_fill_header_nil
@parser.fill_header nil
assert_empty @parser.response
end
end
mechanize-2.7.7/test/test_mechanize_http_auth_realm.rb 0000644 0000041 0000041 00000002063 14006521455 023273 0 ustar www-data www-data require 'mechanize/test_case'
class TestMechanizeHttpAuthRealm < Mechanize::TestCase
def setup
super
@uri = URI 'http://example/'
@AR = Mechanize::HTTP::AuthRealm
@realm = @AR.new 'Digest', @uri, 'r'
end
def test_initialize
assert_equal 'r', @realm.realm
realm = @AR.new 'Digest', @uri, 'R'
refute_equal 'r', realm.realm
realm = @AR.new 'Digest', @uri, 'R'
assert_equal 'R', realm.realm
realm = @AR.new 'Digest', @uri, nil
assert_nil realm.realm
end
def test_equals2
other = @realm.dup
assert_equal @realm, other
other = @AR.new 'Basic', @uri, 'r'
refute_equal @realm, other
other = @AR.new 'Digest', URI('http://other.example/'), 'r'
refute_equal @realm, other
other = @AR.new 'Digest', @uri, 'R'
refute_equal @realm, other
other = @AR.new 'Digest', @uri, 's'
refute_equal @realm, other
end
def test_hash
h = {}
h[@realm] = 1
other = @realm.dup
assert_equal 1, h[other]
other = @AR.new 'Basic', @uri, 'r'
assert_nil h[other]
end
end
mechanize-2.7.7/test/htdocs/ 0000755 0000041 0000041 00000000000 14006521455 015767 5 ustar www-data www-data mechanize-2.7.7/test/htdocs/tc_charset.html 0000644 0000041 0000041 00000000170 14006521455 020772 0 ustar www-data www-data
mechanize-2.7.7/test/htdocs/tc_referer.html 0000644 0000041 0000041 00000001261 14006521455 020775 0 ustar www-data www-data
Referer Servlet
Referer Servlet forced to http
Referer Servlet forced to https
Referer Servlet (noreferrer)
Referer Servlet forced to http (noreferrer)
Referer Servlet forced to https (noreferrer)
mechanize-2.7.7/test/htdocs/test_click.html 0000644 0000041 0000041 00000000521 14006521455 020777 0 ustar www-data www-data
Page Title
This link is not called "A Link"
A Link