mechanize-2.7.2/0000755000004100000410000000000012222372210013506 5ustar www-datawww-datamechanize-2.7.2/.travis.yml0000644000004100000410000000067012222372210015622 0ustar www-datawww-data--- after_script: - rake travis:after -t before_script: - gem install hoe-travis --no-rdoc --no-ri - rake travis:before -t language: ruby notifications: email: - drbrain@segment7.net - ljjarvis@gmail.com - knu@idaemons.org rvm: - 1.9.3 - 2.0.0 - ruby-head - jruby-19mode - jruby-head - rbx-19mode script: rake travis matrix: allow_failures: - rvm: ruby-head - rvm: jruby-19mode - rvm: jruby-head - rvm: rbx-19mode mechanize-2.7.2/test/0000755000004100000410000000000012222372210014465 5ustar www-datawww-datamechanize-2.7.2/test/test_mechanize_page_image.rb0000644000004100000410000001315612222372210022160 0ustar www-datawww-datarequire '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_equal 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.2/test/test_mechanize_xml_file.rb0000644000004100000410000000107712222372210021700 0ustar www-datawww-datarequire '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 endmechanize-2.7.2/test/test_mechanize_redirect_limit_reached_error.rb0000644000004100000410000000066112222372210025762 0ustar www-datawww-datarequire 'mechanize/test_case' class TestMechanizeRedirectLimitReachedError < Mechanize::TestCase def setup super @error = Mechanize::RedirectLimitReachedError.new fake_page, 10 end def test_message assert_equal 'Redirect limit of 10 reached', @error.message end def test_redirects assert_equal 10, @error.redirects end def test_response_code assert_equal 200, @error.response_code end end mechanize-2.7.2/test/test_mechanize_util.rb0000644000004100000410000000627012222372210021056 0ustar www-datawww-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" # "test" in Japanese Shift_JIS encoding if Mechanize::Util::NEW_RUBY_ENCODING ENCODING_ERRORS = [EncodingError, Encoding::ConverterNotFoundError] # and so on ERROR_LOG_MESSAGE = /from_native_charset: Encoding::ConverterNotFoundError: form encoding: "UTF-eight"/ ENCODED_VALUE.force_encoding(::Encoding::SHIFT_JIS) else ENCODING_ERRORS = [Iconv::InvalidEncoding, Iconv::IllegalSequence] ERROR_LOG_MESSAGE = /from_native_charset: Iconv::InvalidEncoding: form encoding: "UTF-eight"/ end 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_equal 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 end mechanize-2.7.2/test/test_mechanize_http_www_authenticate_parser.rb0000644000004100000410000000774312222372210026104 0ustar www-datawww-datarequire '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_scheme_uppercase expected = [ challenge('Basic', { 'realm' => 'foo' }, 'BaSiC realm=foo'), ] assert_equal expected, @parser.parse('BaSiC realm=foo') 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.2/test/test_multi_select.rb0000644000004100000410000001021412222372210020540 0ustar www-datawww-datarequire '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.2/test/test_mechanize_form.rb0000644000004100000410000007364112222372210021052 0ustar www-datawww-datarequire 'mechanize/test_case' class TestMechanizeForm < Mechanize::TestCase def setup super @uri = URI 'http://example' @page = page @uri @form = Mechanize::Form.new node('form', 'name' => @NAME), @mech, @page end def test_action form = Mechanize::Form.new node('form', 'action' => '?a=b&b=c') assert_equal '?a=b&b=c', form.action end def test_add_button_to_query button = Mechanize::Form::Button.new node('input', 'type' => 'submit') e = assert_raises ArgumentError do @form.add_button_to_query button end assert_equal "#{button.inspect} does not belong to the same page " \ "as the form \"#{@NAME}\" in #{@uri}", e.message end def test_aset assert_empty @form.keys @form['intarweb'] = 'Aaron' assert_equal 'Aaron', @form['intarweb'] end def test_aset_exists page = html_page <<-BODY Page Title
BODY form = page.form_with(:name => 'post_form') assert_equal %w[first first], form.keys form['first'] = 'Aaron' assert_equal 'Aaron', form['first'] assert_equal ['Aaron', ''], form.values end def test_build_query_blank_form page = @mech.get('http://localhost/tc_blank_form.html') form = page.forms.first query = form.build_query assert(query.length > 0) assert query.all? { |x| x[1] == '' } end def test_build_query_radio_button_duplicate html = Nokogiri::HTML <<-HTML
HTML form = Mechanize::Form.new html.at('form'), @mech, @page query = form.build_query assert_equal [%w[name a]], query end def test_build_query_radio_button_multiple_checked html = Nokogiri::HTML <<-HTML
HTML form = Mechanize::Form.new html.at('form'), @mech, @page e = assert_raises Mechanize::Error do form.build_query end assert_equal 'radiobuttons "a, b" are checked in the "name" group, ' \ 'only one is allowed', e.message end def test_method_missing_get page = html_page <<-BODY
BODY form = page.forms.first assert_equal 'some value', form.not_a_method end def test_method_missing_set page = html_page <<-BODY
BODY form = page.forms.first form.not_a_method = 'some value' assert_equal [%w[not_a_method some\ value]], form.build_query end def test_parse_buttons page = html_page <<-BODY