From 755cf6e508794568c07f29a8a87efd4f8c4d3654 Mon Sep 17 00:00:00 2001 From: Bryan Helmkamp Date: Tue, 7 Apr 2009 20:37:26 -0400 Subject: [PATCH 1/5] Canonicalize all URLs (Shalon Wood) --- History.txt | 19 ++++--- lib/webrat/core/elements/area.rb | 13 +---- lib/webrat/core/elements/link.rb | 14 +---- lib/webrat/core/session.rb | 24 +++++++- spec/integration/rails/Rakefile | 4 +- spec/private/core/session_spec.rb | 71 +++++++++++++++++++++++- spec/private/rails/attaches_file_spec.rb | 6 +- spec/private/rails/rails_session_spec.rb | 3 +- spec/public/basic_auth_spec.rb | 4 +- spec/public/check_spec.rb | 14 ++--- spec/public/choose_spec.rb | 10 ++-- spec/public/click_area_spec.rb | 10 ++-- spec/public/click_button_spec.rb | 48 ++++++++-------- spec/public/click_link_spec.rb | 58 +++++++++---------- spec/public/fill_in_spec.rb | 20 +++---- spec/public/reload_spec.rb | 4 +- spec/public/select_date_spec.rb | 6 +- spec/public/select_datetime_spec.rb | 6 +- spec/public/select_spec.rb | 20 +++---- spec/public/select_time_spec.rb | 6 +- spec/public/visit_spec.rb | 6 +- spec/public/within_spec.rb | 14 ++--- 22 files changed, 226 insertions(+), 154 deletions(-) diff --git a/History.txt b/History.txt index 6c43aeb..9db842d 100644 --- a/History.txt +++ b/History.txt @@ -1,19 +1,20 @@ -== 0.4.5 / ? +== brynary/master (in git) * Minor enhancements * Added support for field_labeled_locators ending in non word characters lh 148 (Zach Dennis) * Filled in tests on click link lh 195 (diabolo) + * Canonicalize all URLs (Shalon Wood) == 0.4.4 / 2009-04-06 * Major enhancements * Make selenium process management code more robust and informative - + * Minor enhancements - + * Add support for Rails javascript post links (Mark Menard) * Upgrade selenium-client dependency to 1.2.14, and update for new waiting API (Balint Erdi) @@ -24,7 +25,7 @@ * Don't create a new instance of WWW::Mechanize for each request (Mark Menard) * Select fields with duplicate selected options sent an incorrect value (Noah Davis) - + == 0.4.3 / 2009-03-17 * Minor enhancements @@ -34,7 +35,7 @@ * Initial Merb and Sinatra compatibility for Selenium mode (Corey Donohoe) * When faced with a label with no for attribute, that contains a hidden field and another field, as can be the case in Rails 2.3's checkbox view, - webrat now locates the non-hidden field. (Luke Melia) + webrat now locates the non-hidden field. (Luke Melia) * Add application_framework config for Selenium mode to determine how to start and stop the app server (Corey Donohoe) @@ -50,7 +51,7 @@ attributes in a hash and :count and :content options. See have_selector_spec.rb for more. * Add the same functionality mentioned above to have_xpath - + * Minor enhancements * Squeeze extra whitespace out of failures messages from contain @@ -77,7 +78,7 @@ redirected to (Adam Greene) * Recognize input tags with type button (Lena Herrmann) * Add uncheck method to Selenium mode (Lee Bankewitz) - + * Bug fixes * Make requests to a Rails app using a full URL instead of a relative path. This change @@ -88,7 +89,7 @@ to behave correctly (Noah Davis) * Switch to using selenium.click instead of .check when checking a checkbox (Noah Davis) - * Create tmp/pids directory if directory does not exist. (Amos King and Mike Gaffney) + * Create tmp/pids directory if directory does not exist. (Amos King and Mike Gaffney) * Setup deprecated writers for the selenium_environment= and selenium_port= config * Ensure the previous pages params aren't passed through redirect (Daniel Lucraft and Bryan Helmkamp) @@ -164,7 +165,7 @@ * Added save_and_open_screengrab for Selenium mode (Luke Melia) * Bug fixes - + * field_labeled should disregard labels without matching fields (Kyle Hargraves) * Fixed bug where Scope was creating a new DOM rather than re-using the existing DOM. [#105] (Zach Dennis) diff --git a/lib/webrat/core/elements/area.rb b/lib/webrat/core/elements/area.rb index e2f08bb..03bea18 100644 --- a/lib/webrat/core/elements/area.rb +++ b/lib/webrat/core/elements/area.rb @@ -8,7 +8,7 @@ module Webrat end def click(method = nil, options = {}) - @session.request_page(absolute_href, :get, {}) + @session.request_page(href, :get, {}) end protected @@ -16,16 +16,5 @@ module Webrat def href Webrat::XML.attribute(@element, "href") end - - def absolute_href - if href =~ /^\?/ - "#{@session.current_url}#{href}" - elsif href !~ %r{^https?://[\w|.]+(/.*)} && (href !~ /^\//) - "#{@session.current_url}/#{href}" - else - href - end - end - end end diff --git a/lib/webrat/core/elements/link.rb b/lib/webrat/core/elements/link.rb index 5b83e36..607655b 100644 --- a/lib/webrat/core/elements/link.rb +++ b/lib/webrat/core/elements/link.rb @@ -16,9 +16,9 @@ module Webrat options[:javascript] = true if options[:javascript].nil? if options[:javascript] - @session.request_page(absolute_href, method, data) + @session.request_page(href, method, data) else - @session.request_page(absolute_href, :get, {}) + @session.request_page(href, :get, {}) end end @@ -40,16 +40,6 @@ module Webrat Webrat::XML.attribute(@element, "href") end - def absolute_href - if href =~ /^\?/ - "#{@session.current_url}#{href}" - elsif href !~ %r{^https?://} && (href !~ /^\//) - "#{@session.current_url}/#{href}" - else - href - end - end - def authenticity_token return unless onclick && onclick.include?("s.setAttribute('name', 'authenticity_token');") && onclick =~ /s\.setAttribute\('value', '([a-f0-9]{40})'\);/ diff --git a/lib/webrat/core/session.rb b/lib/webrat/core/session.rb index d006ce9..ba2e8bb 100644 --- a/lib/webrat/core/session.rb +++ b/lib/webrat/core/session.rb @@ -95,10 +95,32 @@ For example: @default_headers.dup.merge(@custom_headers.dup) end + def canonicalize_url(href_url) + @current_url ||= "http://www.example.com/" # @current_url can't be blank, or things break + # Case one: relative url + if href_url !~ %r{^https?://} && (href_url !~ /^\//) + # If the relative url starts with # or ?, we need append it as is _if_ there are three slashes in the current url; + # otherwise, ensure there's a slash between it and the current + # url + if (href_url =~ /^\?/ or href_url =~ /^#/) && current_url.scan('/').length > 2 + "#{current_url}#{href_url}" + else + "#{current_url.chomp('/')}/#{href_url}" + end + # Case two: absolute url without host + elsif href_url =~ /^\// + "http://#{current_host}#{href_url}" + # Case three: absolute url with scheme and host. + else + href_url + end + end + def request_page(url, http_method, data) #:nodoc: h = headers h['HTTP_REFERER'] = @current_url if @current_url + url = canonicalize_url(url) debug_log "REQUESTING PAGE: #{http_method.to_s.upcase} #{url} with #{data.inspect} and HTTP headers #{h.inspect}" if h.empty? send "#{http_method}", url, data || {} @@ -260,7 +282,7 @@ For example: private def response_location - response.headers["Location"] + canonicalize_url(response.headers["Location"]) end def current_host diff --git a/spec/integration/rails/Rakefile b/spec/integration/rails/Rakefile index e09ebc6..9f1361e 100644 --- a/spec/integration/rails/Rakefile +++ b/spec/integration/rails/Rakefile @@ -16,13 +16,13 @@ namespace :test_unit do desc "runs the test::unit based tests in webrat mode" task :rails do ENV['WEBRAT_INTEGRATION_MODE'] = 'rails' - Rake::Task['test:integration'].execute + Rake::Task['test:integration'].execute nil end desc "runs the test::unit based tests in selenium mode" task :selenium do ENV['WEBRAT_INTEGRATION_MODE'] = 'selenium' - Rake::Task['test:integration'].execute + Rake::Task['test:integration'].execute nil end desc "run both selenium and rails mode Test::Unit suites" diff --git a/spec/private/core/session_spec.rb b/spec/private/core/session_spec.rb index 7f20af8..cdda4f9 100644 --- a/spec/private/core/session_spec.rb +++ b/spec/private/core/session_spec.rb @@ -118,7 +118,7 @@ describe Webrat::Session do webrat_session.request_page("/oldurl", :get, {}) - webrat_session.current_url.should == "/oldurl" + webrat_session.current_url.should == "http://www.example.com/oldurl" end end @@ -195,4 +195,73 @@ describe Webrat::Session do end + describe "#canonicalize_url"do + + it "should turn 'http://www.example.com/' into 'http://www.example.com/'" do + webrat_session.stub!(:current_url => "http://www.example.com") + webrat_session.canonicalize_url("http://www.example.com/").should == "http://www.example.com/" + end + + it "should turn '/login' into 'http://www.example.com/login' if current_url is 'http://www.example.com/'" do + webrat_session.stub!(:current_url => "http://www.example.com/") + webrat_session.canonicalize_url("/login").should == "http://www.example.com/login" + end + + it "should turn '/login' into 'http://www.example.com/login' if current_url is 'http://www.example.com'" do + webrat_session.stub!(:current_url => "http://www.example.com") + webrat_session.canonicalize_url("/login").should == "http://www.example.com/login" + end + + it "should turn 'login' into 'http://www.example.com/login' if current_url is 'http://www.example.com'" do + webrat_session.stub!(:current_url => "http://www.example.com") + webrat_session.canonicalize_url("login").should == "http://www.example.com/login" + end + + it "should turn 'login' into 'http://www.example.com/login' if current_url is 'http://www.example.com/'" do + webrat_session.stub!(:current_url => "http://www.example.com/") + webrat_session.canonicalize_url("login").should == "http://www.example.com/login" + end + + it "should turn '?login' into 'http://www.example.com/?login' if current_url is 'http://www.example.com'" do + webrat_session.stub!(:current_url => "http://www.example.com") + webrat_session.canonicalize_url("?login").should == "http://www.example.com/?login" + end + + it "should turn '?login' into 'http://www.example.com/?login' if current_url is 'http://www.example.com/'" do + webrat_session.stub!(:current_url => "http://www.example.com/") + webrat_session.canonicalize_url("?login").should == "http://www.example.com/?login" + end + + it "should turn '?login' into 'http://www.example.com/page?login' if current_url is 'http://www.example.com/page'" do + webrat_session.stub!(:current_url => "http://www.example.com/page") + webrat_session.canonicalize_url("?login").should == "http://www.example.com/page?login" + end + + it "should turn '?login' into 'http://www.example.com/page/?login' if current_url is 'http://www.example.com/page/'" do + webrat_session.stub!(:current_url => "http://www.example.com/page/") + webrat_session.canonicalize_url("?login").should == "http://www.example.com/page/?login" + end + + it "should turn '#anchor' into 'http://www.example.com/#anchor' if current_url is 'http://www.example.com'" do + webrat_session.stub!(:current_url => "http://www.example.com") + webrat_session.canonicalize_url("#anchor").should == "http://www.example.com/#anchor" + end + + it "should turn '#anchor' into 'http://www.example.com/#anchor' if current_url is 'http://www.example.com/'" do + webrat_session.stub!(:current_url => "http://www.example.com/") + webrat_session.canonicalize_url("#anchor").should == "http://www.example.com/#anchor" + end + + it "should turn '#anchor' into 'http://www.example.com/page#anchor' if current_url is 'http://www.example.com/page'" do + webrat_session.stub!(:current_url => "http://www.example.com/page") + webrat_session.canonicalize_url("#anchor").should == "http://www.example.com/page#anchor" + end + + it "should turn '#anchor' into 'http://www.example.com/page/#anchor' if current_url is 'http://www.example.com/page/'" do + webrat_session.stub!(:current_url => "http://www.example.com/page/") + webrat_session.canonicalize_url("#anchor").should == "http://www.example.com/page/#anchor" + end + + end + end diff --git a/spec/private/rails/attaches_file_spec.rb b/spec/private/rails/attaches_file_spec.rb index e044db0..cd3a863 100644 --- a/spec/private/rails/attaches_file_spec.rb +++ b/spec/private/rails/attaches_file_spec.rb @@ -27,7 +27,7 @@ describe "attach_file" do HTML - webrat_session.should_receive(:post).with("/widgets", { "widget" => { "file" => "" } }) + webrat_session.should_receive(:post).with("http://www.example.com/widgets", { "widget" => { "file" => "" } }) click_button end @@ -41,7 +41,7 @@ describe "attach_file" do HTML - webrat_session.should_receive(:post).with("/widgets", { "widget" => { "file" => @uploaded_file } }) + webrat_session.should_receive(:post).with("http://www.example.com/widgets", { "widget" => { "file" => @uploaded_file } }) attach_file "Document", @filename click_button end @@ -58,7 +58,7 @@ describe "attach_file" do HTML - webrat_session.should_receive(:post).with("/widgets", { "widget" => { "files" => [@uploaded_file, @uploaded_file] } }) + webrat_session.should_receive(:post).with("http://www.example.com/widgets", { "widget" => { "files" => [@uploaded_file, @uploaded_file] } }) attach_file "Document", @filename attach_file "Spreadsheet", @filename click_button diff --git a/spec/private/rails/rails_session_spec.rb b/spec/private/rails/rails_session_spec.rb index 978e892..81eeed6 100644 --- a/spec/private/rails/rails_session_spec.rb +++ b/spec/private/rails/rails_session_spec.rb @@ -96,7 +96,8 @@ describe Webrat::RailsSession do response = mock("response", :body => body, :headers => {}, :code => 200) @integration_session.stub!(:response => response) - @integration_session.should_receive(:get).with("/page2", {}, nil) + @integration_session.stub!(:https!) + @integration_session.should_receive(:get).with("http://www.example.com/page2", {}, nil) rails_session = Webrat::RailsSession.new(@integration_session) diff --git a/spec/public/basic_auth_spec.rb b/spec/public/basic_auth_spec.rb index 1d6b590..caec284 100644 --- a/spec/public/basic_auth_spec.rb +++ b/spec/public/basic_auth_spec.rb @@ -6,7 +6,7 @@ describe "Basic Auth HTTP headers" do end it "should be present in visit" do - webrat_session.should_receive(:get).with("/", {}, {'HTTP_AUTHORIZATION' => "Basic dXNlcjpzZWNyZXQ=\n"}) + webrat_session.should_receive(:get).with("http://www.example.com/", {}, {'HTTP_AUTHORIZATION' => "Basic dXNlcjpzZWNyZXQ=\n"}) visit("/") end @@ -18,7 +18,7 @@ describe "Basic Auth HTTP headers" do HTML - webrat_session.should_receive(:post).with("/form1", {}, {'HTTP_AUTHORIZATION' => "Basic dXNlcjpzZWNyZXQ=\n"}) + webrat_session.should_receive(:post).with("http://www.example.com/form1", {}, {'HTTP_AUTHORIZATION' => "Basic dXNlcjpzZWNyZXQ=\n"}) click_button end end diff --git a/spec/public/check_spec.rb b/spec/public/check_spec.rb index 9376f88..ef2e56f 100644 --- a/spec/public/check_spec.rb +++ b/spec/public/check_spec.rb @@ -36,7 +36,7 @@ describe "check" do HTML - webrat_session.should_receive(:get).with("/login", "user" => {"tos" => "1"}) + webrat_session.should_receive(:get).with("http://www.example.com/login", "user" => {"tos" => "1"}) check "TOS" click_button end @@ -51,7 +51,7 @@ describe "check" do HTML - webrat_session.should_receive(:post).with("/login", "remember_me" => "on") + webrat_session.should_receive(:post).with("http://www.example.com/login", "remember_me" => "on") check "remember_me" click_button end @@ -79,7 +79,7 @@ describe "check" do HTML - webrat_session.should_receive(:post).with("/login", "remember_me" => "yes") + webrat_session.should_receive(:post).with("http://www.example.com/login", "remember_me" => "yes") check "remember_me" click_button end @@ -132,7 +132,7 @@ describe "uncheck" do HTML - webrat_session.should_receive(:get).with("/login", "user" => {"tos" => "0"}) + webrat_session.should_receive(:get).with("http://www.example.com/login", "user" => {"tos" => "0"}) check "TOS" uncheck "TOS" click_button @@ -147,7 +147,7 @@ describe "uncheck" do HTML - webrat_session.should_receive(:post).with("/login", {}) + webrat_session.should_receive(:post).with("http://www.example.com/login", {}) uncheck "remember_me" click_button end @@ -164,7 +164,7 @@ describe "uncheck" do HTML - webrat_session.should_receive(:post).with("/login", {"options" => ["1", "2"]}) + webrat_session.should_receive(:post).with("http://www.example.com/login", {"options" => ["1", "2"]}) check 'Option 1' check 'Option 2' click_button @@ -183,7 +183,7 @@ describe "uncheck" do HTML - webrat_session.should_receive(:get).with("/login", "user" => {"tos" => "0"}) + webrat_session.should_receive(:get).with("http://www.example.com/login", "user" => {"tos" => "0"}) uncheck "TOS" click_button end diff --git a/spec/public/choose_spec.rb b/spec/public/choose_spec.rb index 5a07c17..97fd97e 100644 --- a/spec/public/choose_spec.rb +++ b/spec/public/choose_spec.rb @@ -36,7 +36,7 @@ describe "choose" do HTML - webrat_session.should_receive(:get).with("/login", "user" => {"gender" => "M"}) + webrat_session.should_receive(:get).with("http://www.example.com/login", "user" => {"gender" => "M"}) choose "Male" click_button end @@ -53,7 +53,7 @@ describe "choose" do HTML - webrat_session.should_receive(:get).with("/login", "user" => {"gender" => "M"}) + webrat_session.should_receive(:get).with("http://www.example.com/login", "user" => {"gender" => "M"}) choose "Female" choose "Male" click_button @@ -81,7 +81,7 @@ describe "choose" do HTML - webrat_session.should_receive(:post).with("/login", "first_option" => "on") + webrat_session.should_receive(:post).with("http://www.example.com/login", "first_option" => "on") choose "first_option" click_button end @@ -95,7 +95,7 @@ describe "choose" do HTML - webrat_session.should_receive(:post).with("/login", "first_option" => "on") + webrat_session.should_receive(:post).with("http://www.example.com/login", "first_option" => "on") click_button end @@ -111,7 +111,7 @@ describe "choose" do HTML - webrat_session.should_receive(:post).with("/login", "user" => {"gender" => "M"}) + webrat_session.should_receive(:post).with("http://www.example.com/login", "user" => {"gender" => "M"}) choose "Male" click_button end diff --git a/spec/public/click_area_spec.rb b/spec/public/click_area_spec.rb index e039323..cb7c7df 100644 --- a/spec/public/click_area_spec.rb +++ b/spec/public/click_area_spec.rb @@ -9,7 +9,7 @@ describe "click_area" do HTML - webrat_session.should_receive(:get).with("/page", {}) + webrat_session.should_receive(:get).with("http://www.example.com/page", {}) click_area "Berlin" end @@ -62,13 +62,13 @@ describe "click_area" do HTML - webrat_session.should_receive(:get).with("/page", {}) + webrat_session.should_receive(:get).with("http://www.example.com/page", {}) click_area "berlin" end it "should follow relative links" do - webrat_session.stub!(:current_url => "/page") + webrat_session.stub!(:current_url => "http://www.example.com/page") with_html <<-HTML @@ -76,7 +76,7 @@ describe "click_area" do HTML - webrat_session.should_receive(:get).with("/page/sub", {}) + webrat_session.should_receive(:get).with("http://www.example.com/page/sub", {}) click_area "Berlin" end @@ -100,7 +100,7 @@ describe "click_area" do HTML - webrat_session.should_receive(:get).with("/page?foo=bar", {}) + webrat_session.should_receive(:get).with("http://www.example.com/page?foo=bar", {}) click_area "Berlin" end end diff --git a/spec/public/click_button_spec.rb b/spec/public/click_button_spec.rb index f0f9c80..3f561d1 100644 --- a/spec/public/click_button_spec.rb +++ b/spec/public/click_button_spec.rb @@ -86,7 +86,7 @@ describe "click_button" do HTML - webrat_session.should_receive(:get).with("/form1", {}) + webrat_session.should_receive(:get).with("http://www.example.com/form1", {}) click_button end @@ -113,7 +113,7 @@ describe "click_button" do HTML - webrat_session.should_receive(:get).with("/form2", {}) + webrat_session.should_receive(:get).with("http://www.example.com/form2", {}) click_button "Form2" end @@ -125,7 +125,7 @@ describe "click_button" do HTML - webrat_session.should_receive(:get).with("/login", {}) + webrat_session.should_receive(:get).with("http://www.example.com/login", {}) click_button end @@ -150,7 +150,7 @@ describe "click_button" do HTML - webrat_session.should_receive(:post).with("/login", "login" => "Login") + webrat_session.should_receive(:post).with("http://www.example.com/login", "login" => "Login") click_button("Login") end @@ -163,7 +163,7 @@ describe "click_button" do HTML - webrat_session.should_receive(:post).with("/login", {}) + webrat_session.should_receive(:post).with("http://www.example.com/login", {}) click_button("Login") end @@ -176,7 +176,7 @@ describe "click_button" do HTML - webrat_session.should_receive(:get).with("/login", "user" => {"password" => "mypass"}) + webrat_session.should_receive(:get).with("http://www.example.com/login", "user" => {"password" => "mypass"}) click_button end @@ -189,7 +189,7 @@ describe "click_button" do HTML - webrat_session.should_receive(:get).with("/login", "user" => {"email" => "test@example.com"}) + webrat_session.should_receive(:get).with("http://www.example.com/login", "user" => {"email" => "test@example.com"}) click_button end @@ -202,7 +202,7 @@ describe "click_button" do HTML - webrat_session.should_receive(:get).with("/login", "user" => {"email" => "test@example.com"}) + webrat_session.should_receive(:get).with("http://www.example.com/login", "user" => {"email" => "test@example.com"}) click_button end @@ -219,7 +219,7 @@ describe "click_button" do HTML - webrat_session.should_receive(:get).with("/login", {}) + webrat_session.should_receive(:get).with("http://www.example.com/login", {}) click_button end @@ -232,7 +232,7 @@ describe "click_button" do HTML - webrat_session.should_receive(:get).with("/login", "user" => {"tos" => "1"}) + webrat_session.should_receive(:get).with("http://www.example.com/login", "user" => {"tos" => "1"}) click_button end @@ -248,7 +248,7 @@ describe "click_button" do HTML - webrat_session.should_receive(:get).with("/login", "user" => {"gender" => "F"}) + webrat_session.should_receive(:get).with("http://www.example.com/login", "user" => {"gender" => "F"}) click_button end @@ -262,7 +262,7 @@ describe "click_button" do HTML - webrat_session.should_receive(:get).with("/login", "user" => {"tos" => "0"}) + webrat_session.should_receive(:get).with("http://www.example.com/login", "user" => {"tos" => "0"}) click_button end @@ -276,7 +276,7 @@ describe "click_button" do HTML - webrat_session.should_receive(:get).with("/login", "user" => {"tos" => "1"}) + webrat_session.should_receive(:get).with("http://www.example.com/login", "user" => {"tos" => "1"}) click_button end @@ -298,7 +298,7 @@ describe "click_button" do HTML - webrat_session.should_receive(:post).with("/login", + webrat_session.should_receive(:post).with("http://www.example.com/login", "options" => ["burger", "fries", "soda", "soda", "dessert"], "response" => { "choices" => [{"selected" => "one"}, {"selected" => "two"}, {"selected" => "two"}]}) click_button @@ -313,7 +313,7 @@ describe "click_button" do HTML - webrat_session.should_receive(:get).with("/login", {}) + webrat_session.should_receive(:get).with("http://www.example.com/login", {}) click_button end @@ -326,7 +326,7 @@ describe "click_button" do HTML - webrat_session.should_receive(:post).with("/posts", "post" => {"body" => "Post body here!"}) + webrat_session.should_receive(:post).with("http://www.example.com/posts", "post" => {"body" => "Post body here!"}) click_button end @@ -340,7 +340,7 @@ describe "click_button" do HTML - webrat_session.should_receive(:post).with("/posts", "post" => {"body" => "Peanut butter & jelly"}) + webrat_session.should_receive(:post).with("http://www.example.com/posts", "post" => {"body" => "Peanut butter & jelly"}) click_button end @@ -363,7 +363,7 @@ describe "click_button" do HTML - webrat_session.should_receive(:get).with("/login", "month" => "2") + webrat_session.should_receive(:get).with("http://www.example.com/login", "month" => "2") click_button end @@ -379,7 +379,7 @@ describe "click_button" do HTML - webrat_session.should_receive(:get).with("/login", "month" => "February") + webrat_session.should_receive(:get).with("http://www.example.com/login", "month" => "February") click_button end @@ -395,7 +395,7 @@ describe "click_button" do HTML - webrat_session.should_receive(:get).with("/login", "month" => "1") + webrat_session.should_receive(:get).with("http://www.example.com/login", "month" => "1") click_button end @@ -409,7 +409,7 @@ describe "click_button" do HTML - webrat_session.should_receive(:post).with("/login", "contestant" => {"scores" => {'1' => '2', '3' => '4'}}) + webrat_session.should_receive(:post).with("http://www.example.com/login", "contestant" => {"scores" => {'1' => '2', '3' => '4'}}) click_button end @@ -422,7 +422,7 @@ describe "click_button" do HTML - webrat_session.should_receive(:get).with("/login", "user" => {"email" => ""}) + webrat_session.should_receive(:get).with("http://www.example.com/login", "user" => {"email" => ""}) click_button end @@ -435,7 +435,7 @@ describe "click_button" do HTML - webrat_session.should_receive(:get).with("/login", "user" => {"email" => ""}) + webrat_session.should_receive(:get).with("http://www.example.com/login", "user" => {"email" => ""}) click_button end @@ -496,7 +496,7 @@ describe "click_button" do HTML - webrat_session.should_receive(:get).with("/login", "user" => {"email" => ""}) + webrat_session.should_receive(:get).with("http://www.example.com/login", "user" => {"email" => ""}) click_button "Login" end end diff --git a/spec/public/click_link_spec.rb b/spec/public/click_link_spec.rb index 63bd6a7..83dd43f 100644 --- a/spec/public/click_link_spec.rb +++ b/spec/public/click_link_spec.rb @@ -7,7 +7,7 @@ describe "click_link" do Save & go back HTML - webrat_session.should_receive(:get).with("/page", {}) + webrat_session.should_receive(:get).with("http://www.example.com/page", {}) click_link "Save & go back" end @@ -17,7 +17,7 @@ describe "click_link" do Link text HTML - webrat_session.should_receive(:get).with("/page", {}) + webrat_session.should_receive(:get).with("http://www.example.com/page", {}) click_link "Link text" end @@ -27,7 +27,7 @@ describe "click_link" do Link text HTML - webrat_session.should_receive(:get).with("/page", {}) + webrat_session.should_receive(:get).with("http://www.example.com/page", {}) click_link "Link text", :method => :get end @@ -37,7 +37,7 @@ describe "click_link" do Link text HTML - webrat_session.should_receive(:get).with("/page", {}) + webrat_session.should_receive(:get).with("http://www.example.com/page", {}) click_link "ink tex", :method => :get end @@ -47,7 +47,7 @@ describe "click_link" do Link text HTML - webrat_session.should_receive(:delete).with("/page", {}) + webrat_session.should_receive(:delete).with("http://www.example.com/page", {}) click_link "Link text", :method => :delete end @@ -58,7 +58,7 @@ describe "click_link" do Link text HTML - webrat_session.should_receive(:post).with("/page", {}) + webrat_session.should_receive(:post).with("http://www.example.com/page", {}) click_link "Link text", :method => :post end @@ -68,7 +68,7 @@ describe "click_link" do Link text HTML - webrat_session.should_receive(:put).with("/page", {}) + webrat_session.should_receive(:put).with("http://www.example.com/page", {}) click_link "Link text", :method => :put end @@ -78,7 +78,7 @@ describe "click_link" do Link text HTML - webrat_session.should_receive(:get).with("/page", {}) + webrat_session.should_receive(:get).with("http://www.example.com/page", {}) click_link /link [a-z]/i end @@ -88,7 +88,7 @@ describe "click_link" do Link text HTML - webrat_session.should_receive(:get).with("/page", {}) + webrat_session.should_receive(:get).with("http://www.example.com/page", {}) click_link "link_text_link" end @@ -98,7 +98,7 @@ describe "click_link" do Link text HTML - webrat_session.should_receive(:get).with("/page", {}) + webrat_session.should_receive(:get).with("http://www.example.com/page", {}) click_link /_text_/ end @@ -140,7 +140,7 @@ describe "click_link" do return false;">Posts HTML - webrat_session.should_receive(:post).with("/posts", "authenticity_token" => "aa79cb354597a60a3786e7e291ed4f74d77d3a62") + webrat_session.should_receive(:post).with("http://www.example.com/posts", "authenticity_token" => "aa79cb354597a60a3786e7e291ed4f74d77d3a62") click_link "Posts" end @@ -161,7 +161,7 @@ describe "click_link" do return false;">Delete HTML - webrat_session.should_receive(:delete).with("/posts/1", {}) + webrat_session.should_receive(:delete).with("http://www.example.com/posts/1", {}) click_link "Delete" end @@ -177,7 +177,7 @@ describe "click_link" do return false;">Posts HTML - webrat_session.should_receive(:post).with("/posts", {}) + webrat_session.should_receive(:post).with("http://www.example.com/posts", {}) click_link "Posts" end @@ -193,7 +193,7 @@ describe "click_link" do return false;">Posts HTML - webrat_session.should_receive(:get).with("/posts", {}) + webrat_session.should_receive(:get).with("http://www.example.com/posts", {}) click_link "Posts", :javascript => false end @@ -214,7 +214,7 @@ describe "click_link" do return false;">Post HTML - webrat_session.should_receive(:post).with("/posts", {}) + webrat_session.should_receive(:post).with("http://www.example.com/posts", {}) click_link "Post" end @@ -235,7 +235,7 @@ describe "click_link" do return false;">Put HTML - webrat_session.should_receive(:put).with("/posts", {}) + webrat_session.should_receive(:put).with("http://www.example.com/posts", {}) click_link "Put" end @@ -302,7 +302,7 @@ describe "click_link" do Link text HTML - webrat_session.should_receive(:get).with("/page", {}) + webrat_session.should_receive(:get).with("http://www.example.com/page", {}) click_link "LINK TEXT" end @@ -312,7 +312,7 @@ describe "click_link" do This is some cool link text, isn't it? HTML - webrat_session.should_receive(:get).with("/page", {}) + webrat_session.should_receive(:get).with("http://www.example.com/page", {}) click_link "Link text" end @@ -322,7 +322,7 @@ describe "click_link" do Link text HTML - webrat_session.should_receive(:get).with("/page", {}) + webrat_session.should_receive(:get).with("http://www.example.com/page", {}) click_link "Link text" end @@ -333,7 +333,7 @@ describe "click_link" do Link text HTML - webrat_session.should_receive(:get).with("/page1", {}) + webrat_session.should_receive(:get).with("http://www.example.com/page1", {}) click_link "Link text" end @@ -345,7 +345,7 @@ describe "click_link" do HTML - webrat_session.should_receive(:get).with("/page2", {}) + webrat_session.should_receive(:get).with("http://www.example.com/page2", {}) click_link "Link" end @@ -356,7 +356,7 @@ describe "click_link" do HTML - webrat_session.should_receive(:get).with("/page1", {}) + webrat_session.should_receive(:get).with("http://www.example.com/page1", {}) click_link "This is a link" end @@ -369,7 +369,7 @@ describe "click_link" do HTML - webrat_session.should_receive(:get).with("/page2", {}) + webrat_session.should_receive(:get).with("http://www.example.com/page2", {}) click_link "Location" end end @@ -384,7 +384,7 @@ describe "click_link" do HTML - webrat_session.should_receive(:get).with("/page2", {}) + webrat_session.should_receive(:get).with("http://www.example.com/page2", {}) click_link_within "#container", "Link" end @@ -400,18 +400,18 @@ describe "click_link" do end it "should follow relative links" do - webrat_session.stub!(:current_url => "/page") + webrat_session.stub!(:current_url => "http://www.example.com/page") with_html <<-HTML Jump to sub page HTML - webrat_session.should_receive(:get).with("/page/sub", {}) + webrat_session.should_receive(:get).with("http://www.example.com/page/sub", {}) click_link "Jump to sub page" end it "should follow fully qualified local links" do - webrat_session.stub!(:current_url => "/page") + webrat_session.stub!(:current_url => "http://www.example.com/page") with_html <<-HTML Jump to sub page @@ -432,13 +432,13 @@ describe "click_link" do end it "should follow query parameters" do - webrat_session.stub!(:current_url => "/page") + webrat_session.stub!(:current_url => "http://www.example.com/page") with_html <<-HTML Jump to foo bar HTML - webrat_session.should_receive(:get).with("/page?foo=bar", {}) + webrat_session.should_receive(:get).with("http://www.example.com/page?foo=bar", {}) click_link "Jump to foo bar" end diff --git a/spec/public/fill_in_spec.rb b/spec/public/fill_in_spec.rb index f6b8f1e..83f8de1 100644 --- a/spec/public/fill_in_spec.rb +++ b/spec/public/fill_in_spec.rb @@ -11,7 +11,7 @@ describe "fill_in" do HTML - webrat_session.should_receive(:post).with("/login", "user" => {"text" => "filling text area"}) + webrat_session.should_receive(:post).with("http://www.example.com/login", "user" => {"text" => "filling text area"}) fill_in "User Text", :with => "filling text area" click_button end @@ -25,7 +25,7 @@ describe "fill_in" do HTML - webrat_session.should_receive(:post).with("/login", "user" => {"text" => "pass"}) + webrat_session.should_receive(:post).with("http://www.example.com/login", "user" => {"text" => "pass"}) fill_in "user_text", :with => "pass" click_button end @@ -65,7 +65,7 @@ describe "fill_in" do HTML - webrat_session.should_receive(:post).with("/login", "user" => {"email" => "foo@example.com"}) + webrat_session.should_receive(:post).with("http://www.example.com/login", "user" => {"email" => "foo@example.com"}) fill_in "user[email]", :with => "foo@example.com" click_button end @@ -83,7 +83,7 @@ describe "fill_in" do HTML - webrat_session.should_receive(:post).with("/login", "user" => {"mail1" => "", "mail2" => "value"}) + webrat_session.should_receive(:post).with("http://www.example.com/login", "user" => {"mail1" => "", "mail2" => "value"}) fill_in "Some", :with => "value" click_button end @@ -101,7 +101,7 @@ describe "fill_in" do HTML - webrat_session.should_receive(:post).with("/login", "user" => {"mail1" => "value", "mail2" => ""}) + webrat_session.should_receive(:post).with("http://www.example.com/login", "user" => {"mail1" => "value", "mail2" => ""}) fill_in "Some mail", :with => "value" click_button end @@ -144,7 +144,7 @@ describe "fill_in" do HTML - webrat_session.should_receive(:post).with("/login", "user" => {"email" => "foo@example.com"}) + webrat_session.should_receive(:post).with("http://www.example.com/login", "user" => {"email" => "foo@example.com"}) fill_in "Email", :with => "foo@example.com" click_button end @@ -158,7 +158,7 @@ describe "fill_in" do HTML - webrat_session.should_receive(:post).with("/login", "user" => {"email" => "foo@example.com"}) + webrat_session.should_receive(:post).with("http://www.example.com/login", "user" => {"email" => "foo@example.com"}) fill_in "user[email]", :with => "foo@example.com" click_button end @@ -172,7 +172,7 @@ describe "fill_in" do HTML - webrat_session.should_receive(:post).with("/login", "user" => {"email" => "foo@example.com"}) + webrat_session.should_receive(:post).with("http://www.example.com/login", "user" => {"email" => "foo@example.com"}) fill_in "user[email]", :with => "foo@example.com" click_button end @@ -187,7 +187,7 @@ describe "fill_in" do HTML - webrat_session.should_receive(:post).with("/login", "user" => {"email" => "foo@example.com"}) + webrat_session.should_receive(:post).with("http://www.example.com/login", "user" => {"email" => "foo@example.com"}) fill_in :email, :with => "foo@example.com" click_button end @@ -202,7 +202,7 @@ describe "fill_in" do HTML - webrat_session.should_receive(:post).with("/users", "user" => {"phone" => "+1 22 33"}) + webrat_session.should_receive(:post).with("http://www.example.com/users", "user" => {"phone" => "+1 22 33"}) fill_in 'Phone', :with => "+1 22 33" click_button end diff --git a/spec/public/reload_spec.rb b/spec/public/reload_spec.rb index cce2bb5..5c9e344 100644 --- a/spec/public/reload_spec.rb +++ b/spec/public/reload_spec.rb @@ -2,9 +2,9 @@ require File.expand_path(File.dirname(__FILE__) + "/../spec_helper") describe "reloads" do it "should reload the page with http referer" do - webrat_session.should_receive(:get).with("/", {}) - webrat_session.should_receive(:get).with("/", {}, {"HTTP_REFERER"=>"/"}) + webrat_session.should_receive(:get).with("http://www.example.com/", {}) visit("/") + webrat_session.should_receive(:get).with("http://www.example.com/", {}, {"HTTP_REFERER"=>"http://www.example.com/"}) reload end end diff --git a/spec/public/select_date_spec.rb b/spec/public/select_date_spec.rb index 1649059..a6edee9 100644 --- a/spec/public/select_date_spec.rb +++ b/spec/public/select_date_spec.rb @@ -19,7 +19,7 @@ describe "select_date" do HTML - webrat_session.should_receive(:post).with("/appointments", + webrat_session.should_receive(:post).with("http://www.example.com/appointments", "appointment" => {"date(1i)" => '2003', "date(2i)" => "12", "date(3i)" => "25"}) select_date "December 25, 2003", :from => "Date" click_button @@ -43,7 +43,7 @@ describe "select_date" do HTML - webrat_session.should_receive(:post).with("/appointments", + webrat_session.should_receive(:post).with("http://www.example.com/appointments", "appointment" => {"date(1i)" => '2003', "date(2i)" => "12", "date(3i)" => "25"}) select_date Date.parse("December 25, 2003"), :from => "date" click_button @@ -66,7 +66,7 @@ describe "select_date" do HTML - webrat_session.should_receive(:post).with("/appointments", + webrat_session.should_receive(:post).with("http://www.example.com/appointments", "appointment" => {"date(1i)" => '2003', "date(2i)" => "12", "date(3i)" => "25"}) select_date "December 25, 2003" click_button diff --git a/spec/public/select_datetime_spec.rb b/spec/public/select_datetime_spec.rb index 0297177..5946ec7 100644 --- a/spec/public/select_datetime_spec.rb +++ b/spec/public/select_datetime_spec.rb @@ -25,7 +25,7 @@ describe "select_datetime" do HTML - webrat_session.should_receive(:post).with("/appointments", + webrat_session.should_receive(:post).with("http://www.example.com/appointments", "appointment" => {"time(1i)" => '2003', "time(2i)" => "12", "time(3i)" => "25", "time(4i)" => "09", "time(5i)" => "30"}) select_datetime "December 25, 2003 9:30", :from => "Time" click_button @@ -55,7 +55,7 @@ describe "select_datetime" do HTML - webrat_session.should_receive(:post).with("/appointments", + webrat_session.should_receive(:post).with("http://www.example.com/appointments", "appointment" => {"time(1i)" => '2003', "time(2i)" => "12", "time(3i)" => "25", "time(4i)" => "09", "time(5i)" => "30"}) select_datetime Time.parse("December 25, 2003 9:30"), :from => "Time" click_button @@ -115,7 +115,7 @@ describe "select_datetime" do HTML - webrat_session.should_receive(:post).with("/appointments", + webrat_session.should_receive(:post).with("http://www.example.com/appointments", "appointment" => {"time(1i)" => '2003', "time(2i)" => "12", "time(3i)" => "25", "time(4i)" => "09", "time(5i)" => "30"}) select_datetime "December 25, 2003 9:30" click_button diff --git a/spec/public/select_spec.rb b/spec/public/select_spec.rb index e5a5172..9b76cca 100644 --- a/spec/public/select_spec.rb +++ b/spec/public/select_spec.rb @@ -62,7 +62,7 @@ describe "select" do HTML - webrat_session.should_receive(:post).with("/login", "month" => "1") + webrat_session.should_receive(:post).with("http://www.example.com/login", "month" => "1") select "January", :from => "month" click_button end @@ -76,7 +76,7 @@ describe "select" do HTML - webrat_session.should_receive(:post).with("/login", "encoded" => "A & B") + webrat_session.should_receive(:post).with("http://www.example.com/login", "encoded" => "A & B") select "Encoded", :from => "encoded" click_button end @@ -90,7 +90,7 @@ describe "select" do HTML - webrat_session.should_receive(:post).with("/login", 'month' => '') + webrat_session.should_receive(:post).with("http://www.example.com/login", 'month' => '') click_button end @@ -103,7 +103,7 @@ describe "select" do HTML - webrat_session.should_receive(:post).with("/login", "month" => "1") + webrat_session.should_receive(:post).with("http://www.example.com/login", "month" => "1") select "January" click_button end @@ -118,7 +118,7 @@ describe "select" do HTML - webrat_session.should_receive(:post).with("/login", "start_month" => "s1", "end_month" => "e1") + webrat_session.should_receive(:post).with("http://www.example.com/login", "start_month" => "s1", "end_month" => "e1") select "January", :from => "end_month" click_button end @@ -135,7 +135,7 @@ describe "select" do HTML - webrat_session.should_receive(:post).with("/login", "start_month" => "s1", "end_month" => "e1") + webrat_session.should_receive(:post).with("http://www.example.com/login", "start_month" => "s1", "end_month" => "e1") select "January", :from => "End Month" click_button end @@ -149,7 +149,7 @@ describe "select" do HTML - webrat_session.should_receive(:post).with("/login", "month" => "January") + webrat_session.should_receive(:post).with("http://www.example.com/login", "month" => "January") select "January", :from => "month" click_button end @@ -163,7 +163,7 @@ describe "select" do HTML - webrat_session.should_receive(:post).with("/login", "month" => "January") + webrat_session.should_receive(:post).with("http://www.example.com/login", "month" => "January") select /jan/i click_button end @@ -195,7 +195,7 @@ describe "select" do HTML - webrat_session.should_receive(:post).with("/login", "start_month" => "s1", "end_month" => "e1") + webrat_session.should_receive(:post).with("http://www.example.com/login", "start_month" => "s1", "end_month" => "e1") select /jan/i, :from => "End Month" click_button end @@ -210,7 +210,7 @@ describe "select" do HTML - webrat_session.should_receive(:post).with("/login", "month" => "Peanut butter & jelly") + webrat_session.should_receive(:post).with("http://www.example.com/login", "month" => "Peanut butter & jelly") click_button end diff --git a/spec/public/select_time_spec.rb b/spec/public/select_time_spec.rb index 3eafbaa..534342c 100644 --- a/spec/public/select_time_spec.rb +++ b/spec/public/select_time_spec.rb @@ -16,7 +16,7 @@ describe "select_time" do HTML - webrat_session.should_receive(:post).with("/appointments", + webrat_session.should_receive(:post).with("http://www.example.com/appointments", "appointment" => {"time(4i)" => "09", "time(5i)" => "30"}) select_time "9:30AM", :from => "Time" click_button @@ -37,7 +37,7 @@ describe "select_time" do HTML - webrat_session.should_receive(:post).with("/appointments", + webrat_session.should_receive(:post).with("http://www.example.com/appointments", "appointment" => {"time(4i)" => "09", "time(5i)" => "30"}) select_time Time.parse("9:30AM"), :from => "Time" click_button @@ -78,7 +78,7 @@ describe "select_time" do HTML - webrat_session.should_receive(:post).with("/appointments", + webrat_session.should_receive(:post).with("http://www.example.com/appointments", "appointment" => {"time(4i)" => "09", "time(5i)" => "30"}) select_time "9:30" click_button diff --git a/spec/public/visit_spec.rb b/spec/public/visit_spec.rb index dc73fc6..37042f6 100644 --- a/spec/public/visit_spec.rb +++ b/spec/public/visit_spec.rb @@ -10,7 +10,7 @@ describe "visit" do end it "should use get" do - webrat_session.should_receive(:get).with("/", {}) + webrat_session.should_receive(:get).with("http://www.example.com/", {}) visit("/") end @@ -36,7 +36,7 @@ describe "visit" do visit("/oldurl") - current_url.should == "/oldurl" + current_url.should == "http://www.example.com/oldurl" end end @@ -51,7 +51,7 @@ describe "visit with referer" do end it "should use get with referer header" do - webrat_session.should_receive(:get).with("/", {}, {"HTTP_REFERER" => "/old_url"}) + webrat_session.should_receive(:get).with("http://www.example.com/", {}, {"HTTP_REFERER" => "/old_url"}) visit("/") end diff --git a/spec/public/within_spec.rb b/spec/public/within_spec.rb index ab083d2..6d3fd9c 100644 --- a/spec/public/within_spec.rb +++ b/spec/public/within_spec.rb @@ -13,7 +13,7 @@ describe "within" do HTML - webrat_session.should_receive(:get).with("/page2", {}) + webrat_session.should_receive(:get).with("http://www.example.com/page2", {}) within "#container" do within "div" do click_link "Link" @@ -31,7 +31,7 @@ describe "within" do HTML - webrat_session.should_receive(:get).with("/page2", {}) + webrat_session.should_receive(:get).with("http://www.example.com/page2", {}) within "#container" do click_link "Link" end @@ -51,7 +51,7 @@ describe "within" do HTML - webrat_session.should_receive(:get).with("/form2", "email" => "test@example.com") + webrat_session.should_receive(:get).with("http://www.example.com/form2", "email" => "test@example.com") within "#form2" do fill_in "Email", :with => "test@example.com" click_button @@ -70,7 +70,7 @@ describe "within" do HTML - webrat_session.should_receive(:get).with("/form2", "email" => "test@example.com") + webrat_session.should_receive(:get).with("http://www.example.com/form2", "email" => "test@example.com") within ".important" do fill_in "Email", :with => "test@example.com" end @@ -92,7 +92,7 @@ describe "within" do HTML - webrat_session.should_receive(:get).with("/form2", "email" => "test@example.com") + webrat_session.should_receive(:get).with("http://www.example.com/form2", "email" => "test@example.com") within "form[@action='/form2']" do fill_in "Email", :with => "test@example.com" click_button "Add" @@ -117,7 +117,7 @@ describe "within" do HTML - webrat_session.should_receive(:get).with("/form2", "email2" => "test@example.com") + webrat_session.should_receive(:get).with("http://www.example.com/form2", "email2" => "test@example.com") within "form[@action='/form2']" do fill_in "Email", :with => "test@example.com" click_button "Add" @@ -138,7 +138,7 @@ describe "within" do HTML - webrat_session.should_receive(:get).with("/form2", "email" => "test@example.com") + webrat_session.should_receive(:get).with("http://www.example.com/form2", "email" => "test@example.com") within "#form2" do fill_in "Email", :with => "test@example.com" click_button "Add" From b5254109f1762ce2ec582823f85bfcfa103d4fbc Mon Sep 17 00:00:00 2001 From: Bryan Helmkamp Date: Tue, 14 Apr 2009 22:19:57 -0400 Subject: [PATCH 2/5] Start on Rack::Test integration --- Rakefile | 12 +++- lib/webrat/core/methods.rb | 6 +- lib/webrat/core/session.rb | 17 +++-- lib/webrat/rack_test.rb | 32 +++++++++ spec/integration/rack/Rakefile | 5 ++ spec/integration/rack/rack_app.rb | 16 +++++ spec/integration/rack/test/test_helper.rb | 20 ++++++ .../integration/rack/test/webrat_rack_test.rb | 67 +++++++++++++++++++ 8 files changed, 167 insertions(+), 8 deletions(-) create mode 100644 lib/webrat/rack_test.rb create mode 100644 spec/integration/rack/Rakefile create mode 100644 spec/integration/rack/rack_app.rb create mode 100644 spec/integration/rack/test/test_helper.rb create mode 100644 spec/integration/rack/test/webrat_rack_test.rb diff --git a/Rakefile b/Rakefile index aa233cb..d974f59 100644 --- a/Rakefile +++ b/Rakefile @@ -106,7 +106,7 @@ end namespace :spec do desc "Run the integration specs" - task :integration => ["integration:rails", "integration:merb", "integration:sinatra"] + task :integration => ["integration:rails", "integration:merb", "integration:sinatra", "integration:rack"] namespace :integration do desc "Run the Rails integration specs" @@ -140,7 +140,15 @@ namespace :spec do task :sinatra do Dir.chdir "spec/integration/sinatra" do result = system "rake test" - raise "Sinatra tntegration tests failed" unless result + raise "Sinatra integration tests failed" unless result + end + end + + desc "Run the Sinatra integration specs" + task :rack do + Dir.chdir "spec/integration/rack" do + result = system "rake test" + raise "Rack integration tests failed" unless result end end end diff --git a/lib/webrat/core/methods.rb b/lib/webrat/core/methods.rb index 36efeb8..57c4085 100644 --- a/lib/webrat/core/methods.rb +++ b/lib/webrat/core/methods.rb @@ -16,7 +16,11 @@ module Webrat end def webrat_session - @_webrat_session ||= ::Webrat.session_class.new(self) + if Webrat.configuration.mode == :rack_test + @_webrat_session ||= ::Webrat::RackTestSession.new(rack_test_session) + else + @_webrat_session ||= ::Webrat.session_class.new(self) + end end # all of these methods delegate to the @session, which should diff --git a/lib/webrat/core/session.rb b/lib/webrat/core/session.rb index ba2e8bb..8551ae0 100644 --- a/lib/webrat/core/session.rb +++ b/lib/webrat/core/session.rb @@ -26,6 +26,8 @@ module Webrat SinatraSession when :mechanize MechanizeSession + when :rack_test + RackTestSession else raise WebratError.new(<<-STR) Unknown Webrat mode: #{Webrat.configuration.mode.inspect} @@ -122,11 +124,8 @@ For example: url = canonicalize_url(url) debug_log "REQUESTING PAGE: #{http_method.to_s.upcase} #{url} with #{data.inspect} and HTTP headers #{h.inspect}" - if h.empty? - send "#{http_method}", url, data || {} - else - send "#{http_method}", url, data || {}, h - end + + process_request(http_method, url, data, h) save_and_open_page if exception_caught? && Webrat.configuration.open_error_files? raise PageLoadError.new("Page load was not successful (Code: #{response_code.inspect}):\n#{formatted_error}") unless success_code? @@ -281,6 +280,14 @@ For example: private + def process_request(http_method, url, data, headers) + if headers.empty? + send "#{http_method}", url, data || {} + else + send "#{http_method}", url, data || {}, headers + end + end + def response_location canonicalize_url(response.headers["Location"]) end diff --git a/lib/webrat/rack_test.rb b/lib/webrat/rack_test.rb new file mode 100644 index 0000000..c1da047 --- /dev/null +++ b/lib/webrat/rack_test.rb @@ -0,0 +1,32 @@ +module Webrat + class RackTestSession < Session + + def initialize(rack_test_session) #:nodoc: + super() + @rack_test_session = rack_test_session + end + + def response_body + response.body + end + + def response_code + response.status + end + + def response + @rack_test_session.last_response + end + + protected + + def process_request(http_method, url, data = {}, headers = {}) + headers ||= {} + data ||= {} + + env = headers.merge(:params => data, :method => http_method.to_s.upcase) + @rack_test_session.request(url, env) + end + + end +end \ No newline at end of file diff --git a/spec/integration/rack/Rakefile b/spec/integration/rack/Rakefile new file mode 100644 index 0000000..cfba77d --- /dev/null +++ b/spec/integration/rack/Rakefile @@ -0,0 +1,5 @@ +require "rake/testtask" + +Rake::TestTask.new do |t| + t.test_files = FileList["test/*_test.rb"] +end diff --git a/spec/integration/rack/rack_app.rb b/spec/integration/rack/rack_app.rb new file mode 100644 index 0000000..978f734 --- /dev/null +++ b/spec/integration/rack/rack_app.rb @@ -0,0 +1,16 @@ +require "rubygems" +require "sinatra/base" + +class RackApp < Sinatra::Default + get "/" do + "Hello World" + end + + get "/redirect_absolute_url" do + redirect URI.join(request.url, "foo").to_s + end + + get "/foo" do + "spam" + end +end diff --git a/spec/integration/rack/test/test_helper.rb b/spec/integration/rack/test/test_helper.rb new file mode 100644 index 0000000..4aa659e --- /dev/null +++ b/spec/integration/rack/test/test_helper.rb @@ -0,0 +1,20 @@ +require "rubygems" +require "test/unit" +require "rack/test" +require "redgreen" + +require File.dirname(__FILE__) + "/../../../../lib/webrat" + +Webrat.configure do |config| + config.mode = :rack_test +end + +class Test::Unit::TestCase + include Rack::Test::Methods + include Webrat::Methods + include Webrat::Matchers + + def app + RackApp.new + end +end diff --git a/spec/integration/rack/test/webrat_rack_test.rb b/spec/integration/rack/test/webrat_rack_test.rb new file mode 100644 index 0000000..b75147a --- /dev/null +++ b/spec/integration/rack/test/webrat_rack_test.rb @@ -0,0 +1,67 @@ +require File.dirname(__FILE__) + "/test_helper" +require File.dirname(__FILE__) + "/../rack_app" + +class WebratRackTest < Test::Unit::TestCase + def test_visit_returns_response + response = visit "/" + assert response.ok? + end + + def test_last_response_is_available + visit "/" + assert last_response.ok? + end + + def test_last_request_is_available + visit "/" + assert_equal "/", last_request.env["PATH_INFO"] + end + + def test_redirects + visit "/redirect_absolute_url" + assert_equal "spam", response_body + end + + def test_assertions_after_visit + visit "/" + assert_contain "Hello World" + end + + def test_assertions_after_visit + get "/" + assert_contain "Hello World" + end + + # def test_visits_pages + # visit "/" + # assert response_body.include?("visit") + # + # click_link "there" + # assert response_body.include?('
') + # end + # + # def test_submits_form + # visit "/go" + # fill_in "Name", :with => "World" + # fill_in "Email", :with => "world@example.org" + # click_button "Submit" + # + # assert response_body.include?("Hello, World") + # assert response_body.include?("Your email is: world@example.org") + # end + # + # def test_check_value_of_field + # visit "/" + # assert field_labeled("Prefilled").value, "text" + # end + # + # def test_follows_internal_redirects + # visit "/internal_redirect" + # assert response_body.include?("visit") + # end + # + # def test_does_not_follow_external_redirects + # visit "/external_redirect" + # assert response_code == 302 + # end +end From 35cbfd9643d27c097051c0d0aaedb37afb99c614 Mon Sep 17 00:00:00 2001 From: Bryan Helmkamp Date: Mon, 11 May 2009 00:11:01 -0400 Subject: [PATCH 3/5] Use Nokogiri on JRuby -- ~2x faster than REXML on JRuby for me --- History.txt | 4 ++++ lib/webrat.rb | 13 +++++-------- lib/webrat/core/configuration.rb | 2 +- lib/webrat/core/logging.rb | 5 ++++- lib/webrat/core/scope.rb | 4 ++-- spec/private/core/configuration_spec.rb | 7 ------- spec/private/core/field_spec.rb | 20 +++++++++++--------- spec/public/click_button_spec.rb | 8 +------- spec/public/select_spec.rb | 16 ++-------------- 9 files changed, 30 insertions(+), 49 deletions(-) diff --git a/History.txt b/History.txt index 9db842d..e1fe167 100644 --- a/History.txt +++ b/History.txt @@ -1,5 +1,9 @@ == brynary/master (in git) +* Major enhancements + + * Improve performance (~2x) on JRuby by supporting Nokogiri + * Minor enhancements * Added support for field_labeled_locators ending in non word characters diff --git a/lib/webrat.rb b/lib/webrat.rb index 2dee9b9..271d1e1 100644 --- a/lib/webrat.rb +++ b/lib/webrat.rb @@ -10,17 +10,14 @@ module Webrat VERSION = '0.4.4' def self.require_xml - gem "nokogiri", ">= 1.0.6" - if on_java? - # We need Nokogiri's CSS to XPath support, even if using REXML and Hpricot for parsing and searching - require "nokogiri/css" - require "hpricot" - require "rexml/document" + gem "nokogiri", ">= 1.2.4" else - require "nokogiri" - require "webrat/core/xml/nokogiri" + gem "nokogiri", ">= 1.0.6" end + + require "nokogiri" + require "webrat/core/xml/nokogiri" end def self.on_java? diff --git a/lib/webrat/core/configuration.rb b/lib/webrat/core/configuration.rb index fcf2525..33d274f 100755 --- a/lib/webrat/core/configuration.rb +++ b/lib/webrat/core/configuration.rb @@ -60,7 +60,7 @@ module Webrat def initialize # :nodoc: self.open_error_files = true - self.parse_with_nokogiri = !Webrat.on_java? + self.parse_with_nokogiri = true self.application_environment = :test self.application_port = 3001 self.application_address = 'localhost' diff --git a/lib/webrat/core/logging.rb b/lib/webrat/core/logging.rb index dd2fb87..7a91c4d 100644 --- a/lib/webrat/core/logging.rb +++ b/lib/webrat/core/logging.rb @@ -13,7 +13,10 @@ module Webrat when :merb Merb.logger else - nil + @logger ||= begin + require "logger" + ::Logger.new("webrat.log") + end end end diff --git a/lib/webrat/core/scope.rb b/lib/webrat/core/scope.rb index f55dbaf..cc52ac7 100644 --- a/lib/webrat/core/scope.rb +++ b/lib/webrat/core/scope.rb @@ -209,8 +209,8 @@ module Webrat # along with the form. An optional content_type may be given. # # Example: - # attaches_file "Resume", "/path/to/the/resume.txt" - # attaches_file "Photo", "/path/to/the/image.png", "image/png" + # attach_file "Resume", "/path/to/the/resume.txt" + # attach_file "Photo", "/path/to/the/image.png", "image/png" def attach_file(field_locator, path, content_type = nil) locate_field(field_locator, FileField).set(path, content_type) end diff --git a/spec/private/core/configuration_spec.rb b/spec/private/core/configuration_spec.rb index 2022947..9180216 100755 --- a/spec/private/core/configuration_spec.rb +++ b/spec/private/core/configuration_spec.rb @@ -9,17 +9,10 @@ describe Webrat::Configuration do end it "should use Nokogiri as the parser by default" do - Webrat.stub!(:on_java? => false) config = Webrat::Configuration.new config.should parse_with_nokogiri end - it "should not use Nokogiri as the parser when on JRuby" do - Webrat.stub!(:on_java? => true) - config = Webrat::Configuration.new - config.should_not parse_with_nokogiri - end - it "should open error files by default" do config = Webrat::Configuration.new config.should open_error_files diff --git a/spec/private/core/field_spec.rb b/spec/private/core/field_spec.rb index e4bedfd..90b6227 100644 --- a/spec/private/core/field_spec.rb +++ b/spec/private/core/field_spec.rb @@ -2,16 +2,18 @@ require File.expand_path(File.dirname(__FILE__) + "/../../spec_helper") module Webrat describe Field do - it "should have nice inspect output" do - html = <<-HTML - - - - HTML + unless Webrat.on_java? + it "should have nice inspect output" do + html = <<-HTML + + + + HTML - element = Webrat::XML.css_search(Webrat::XML.document(html), "input").first - checkbox = CheckboxField.new(nil, element) - checkbox.inspect.should =~ /#>/ + element = Webrat::XML.css_search(Webrat::XML.document(html), "input").first + checkbox = CheckboxField.new(nil, element) + checkbox.inspect.should =~ /#>/ + end end end diff --git a/spec/public/click_button_spec.rb b/spec/public/click_button_spec.rb index 3f561d1..45a0d30 100644 --- a/spec/public/click_button_spec.rb +++ b/spec/public/click_button_spec.rb @@ -331,7 +331,7 @@ describe "click_button" do end it "should properly handle HTML entities in textarea default values" do - spec = lambda do + pending "needs bug fix" do with_html <<-HTML @@ -343,12 +343,6 @@ describe "click_button" do webrat_session.should_receive(:post).with("http://www.example.com/posts", "post" => {"body" => "Peanut butter & jelly"}) click_button end - - if Webrat.on_java? - spec.call - else - pending("needs bug fix", &spec) - end end it "should send default selected option value from select" do diff --git a/spec/public/select_spec.rb b/spec/public/select_spec.rb index 9b76cca..eb4032f 100644 --- a/spec/public/select_spec.rb +++ b/spec/public/select_spec.rb @@ -201,7 +201,7 @@ describe "select" do end it "should properly handle submitting HTML entities in select values" do - spec = lambda do + pending "needs bug fix" do with_html <<-HTML @@ -213,16 +213,10 @@ describe "select" do webrat_session.should_receive(:post).with("http://www.example.com/login", "month" => "Peanut butter & jelly") click_button end - - if Webrat.on_java? - spec.call - else - pending("needs bug fix", &spec) - end end it "should properly handle locating with HTML entities in select values" do - spec = lambda do + pending "needs bug fix" do with_html <<-HTML @@ -236,12 +230,6 @@ describe "select" do select "Peanut butter & jelly" }.should_not raise_error(Webrat::NotFoundError) end - - if Webrat.on_java? - spec.call - else - pending("needs bug fix", &spec) - end end it "should submit duplicates selected options as a single value" do From 3e71ae3733e56cc724f207e142404b5c9859b1a5 Mon Sep 17 00:00:00 2001 From: Bryan Helmkamp Date: Mon, 11 May 2009 00:16:38 -0400 Subject: [PATCH 4/5] Correct some specs that need full URLs --- .gitignore | 3 ++- spec/public/click_link_spec.rb | 4 ++-- spec/public/select_date_spec.rb | 2 +- spec/public/select_datetime_spec.rb | 2 +- spec/public/select_spec.rb | 2 +- spec/public/select_time_spec.rb | 2 +- 6 files changed, 8 insertions(+), 7 deletions(-) diff --git a/.gitignore b/.gitignore index ce1f77d..8c7320b 100644 --- a/.gitignore +++ b/.gitignore @@ -11,4 +11,5 @@ log *.swp results test_apps -*.tmproj \ No newline at end of file +*.tmproj +webrat.log diff --git a/spec/public/click_link_spec.rb b/spec/public/click_link_spec.rb index 83dd43f..a4a3b31 100644 --- a/spec/public/click_link_spec.rb +++ b/spec/public/click_link_spec.rb @@ -108,7 +108,7 @@ describe "click_link" do Link text HTML - webrat_session.should_receive(:get).with("/page", {}) + webrat_session.should_receive(:get).with("http://www.example.com/page", {}) click_link 'piddle' end @@ -118,7 +118,7 @@ describe "click_link" do Link text HTML - webrat_session.should_receive(:get).with("/page", {}) + webrat_session.should_receive(:get).with("http://www.example.com/page", {}) click_link /iddle/ end diff --git a/spec/public/select_date_spec.rb b/spec/public/select_date_spec.rb index a6edee9..9ccf0e5 100644 --- a/spec/public/select_date_spec.rb +++ b/spec/public/select_date_spec.rb @@ -90,7 +90,7 @@ describe "select_date" do HTML - webrat_session.should_receive(:post).with("/appointments", + webrat_session.should_receive(:post).with("http://www.example.com/appointments", "appointment" => {"date(1i)" => '2003', "date(2i)" => "12", "date(3i)" => "25"}) select_date Date.parse("December 25, 2003"), :from => "date ?" click_button diff --git a/spec/public/select_datetime_spec.rb b/spec/public/select_datetime_spec.rb index 5946ec7..575ad37 100644 --- a/spec/public/select_datetime_spec.rb +++ b/spec/public/select_datetime_spec.rb @@ -85,7 +85,7 @@ describe "select_datetime" do HTML - webrat_session.should_receive(:post).with("/appointments", + webrat_session.should_receive(:post).with("http://www.example.com/appointments", "appointment" => {"time(1i)" => '2003', "time(2i)" => "12", "time(3i)" => "25", "time(4i)" => "09", "time(5i)" => "30"}) select_datetime "December 25, 2003 9:30", :from => "Time ?" click_button diff --git a/spec/public/select_spec.rb b/spec/public/select_spec.rb index eb4032f..e35db85 100644 --- a/spec/public/select_spec.rb +++ b/spec/public/select_spec.rb @@ -242,7 +242,7 @@ describe "select" do HTML - webrat_session.should_receive(:post).with("/login", "clothes" => "pants") + webrat_session.should_receive(:post).with("http://www.example.com/login", "clothes" => "pants") click_button end diff --git a/spec/public/select_time_spec.rb b/spec/public/select_time_spec.rb index 534342c..fcc8678 100644 --- a/spec/public/select_time_spec.rb +++ b/spec/public/select_time_spec.rb @@ -58,7 +58,7 @@ describe "select_time" do HTML - webrat_session.should_receive(:post).with("/appointments", + webrat_session.should_receive(:post).with("http://www.example.com/appointments", "appointment" => {"time(4i)" => "09", "time(5i)" => "30"}) select_time "9:30AM", :from => "Time #" click_button From 453cb4b3ebd8837abd444f7333950d2c7b0d4a23 Mon Sep 17 00:00:00 2001 From: Bryan Helmkamp Date: Mon, 11 May 2009 01:27:04 -0400 Subject: [PATCH 5/5] Stripping whitespace --- lib/webrat.rb | 2 +- lib/webrat/core/session.rb | 2 +- lib/webrat/rack_test.rb | 18 +++++++-------- spec/integration/rack/test/test_helper.rb | 2 +- .../integration/rack/test/webrat_rack_test.rb | 22 +++++++++---------- spec/public/click_link_spec.rb | 6 ++--- spec/public/locators/field_labeled_spec.rb | 4 ++-- spec/public/select_date_spec.rb | 2 +- spec/public/select_spec.rb | 6 ++--- 9 files changed, 32 insertions(+), 32 deletions(-) diff --git a/lib/webrat.rb b/lib/webrat.rb index 271d1e1..dd4ba8f 100644 --- a/lib/webrat.rb +++ b/lib/webrat.rb @@ -15,7 +15,7 @@ module Webrat else gem "nokogiri", ">= 1.0.6" end - + require "nokogiri" require "webrat/core/xml/nokogiri" end diff --git a/lib/webrat/core/session.rb b/lib/webrat/core/session.rb index 8551ae0..e4bbaeb 100644 --- a/lib/webrat/core/session.rb +++ b/lib/webrat/core/session.rb @@ -287,7 +287,7 @@ For example: send "#{http_method}", url, data || {}, headers end end - + def response_location canonicalize_url(response.headers["Location"]) end diff --git a/lib/webrat/rack_test.rb b/lib/webrat/rack_test.rb index c1da047..b824cee 100644 --- a/lib/webrat/rack_test.rb +++ b/lib/webrat/rack_test.rb @@ -1,32 +1,32 @@ module Webrat class RackTestSession < Session - + def initialize(rack_test_session) #:nodoc: super() @rack_test_session = rack_test_session end - + def response_body response.body end - + def response_code response.status end - + def response @rack_test_session.last_response end - + protected - + def process_request(http_method, url, data = {}, headers = {}) headers ||= {} data ||= {} - + env = headers.merge(:params => data, :method => http_method.to_s.upcase) @rack_test_session.request(url, env) end - + end -end \ No newline at end of file +end diff --git a/spec/integration/rack/test/test_helper.rb b/spec/integration/rack/test/test_helper.rb index 4aa659e..56298ef 100644 --- a/spec/integration/rack/test/test_helper.rb +++ b/spec/integration/rack/test/test_helper.rb @@ -13,7 +13,7 @@ class Test::Unit::TestCase include Rack::Test::Methods include Webrat::Methods include Webrat::Matchers - + def app RackApp.new end diff --git a/spec/integration/rack/test/webrat_rack_test.rb b/spec/integration/rack/test/webrat_rack_test.rb index b75147a..4fdef09 100644 --- a/spec/integration/rack/test/webrat_rack_test.rb +++ b/spec/integration/rack/test/webrat_rack_test.rb @@ -6,12 +6,12 @@ class WebratRackTest < Test::Unit::TestCase response = visit "/" assert response.ok? end - + def test_last_response_is_available visit "/" assert last_response.ok? end - + def test_last_request_is_available visit "/" assert_equal "/", last_request.env["PATH_INFO"] @@ -21,45 +21,45 @@ class WebratRackTest < Test::Unit::TestCase visit "/redirect_absolute_url" assert_equal "spam", response_body end - + def test_assertions_after_visit visit "/" assert_contain "Hello World" end - + def test_assertions_after_visit get "/" assert_contain "Hello World" end - + # def test_visits_pages # visit "/" # assert response_body.include?("visit") - # + # # click_link "there" # assert response_body.include?('
') # end - # + # # def test_submits_form # visit "/go" # fill_in "Name", :with => "World" # fill_in "Email", :with => "world@example.org" # click_button "Submit" - # + # # assert response_body.include?("Hello, World") # assert response_body.include?("Your email is: world@example.org") # end - # + # # def test_check_value_of_field # visit "/" # assert field_labeled("Prefilled").value, "text" # end - # + # # def test_follows_internal_redirects # visit "/internal_redirect" # assert response_body.include?("visit") # end - # + # # def test_does_not_follow_external_redirects # visit "/external_redirect" # assert response_code == 302 diff --git a/spec/public/click_link_spec.rb b/spec/public/click_link_spec.rb index a4a3b31..82fcdfe 100644 --- a/spec/public/click_link_spec.rb +++ b/spec/public/click_link_spec.rb @@ -101,7 +101,7 @@ describe "click_link" do webrat_session.should_receive(:get).with("http://www.example.com/page", {}) click_link /_text_/ end - + it "should click links by title" do with_html <<-HTML @@ -111,7 +111,7 @@ describe "click_link" do webrat_session.should_receive(:get).with("http://www.example.com/page", {}) click_link 'piddle' end - + it "should click links by title regex" do with_html <<-HTML @@ -121,7 +121,7 @@ describe "click_link" do webrat_session.should_receive(:get).with("http://www.example.com/page", {}) click_link /iddle/ end - + it "should click rails javascript links with authenticity tokens" do with_html <<-HTML diff --git a/spec/public/locators/field_labeled_spec.rb b/spec/public/locators/field_labeled_spec.rb index 6828a9b..8d4578d 100644 --- a/spec/public/locators/field_labeled_spec.rb +++ b/spec/public/locators/field_labeled_spec.rb @@ -153,7 +153,7 @@ describe "field_labeled" do should_return_a Webrat::TextField, :for => "The Label" with_an_id_of "element_42", :for => "The Label" end - + describe "finding a field whose label ends with an non word character" do using_this_html <<-HTML @@ -163,7 +163,7 @@ describe "field_labeled" do
HTML - + should_return_a Webrat::TextField, :for => "License #" with_an_id_of "element_42", :for => "License #" should_raise_error_matching /Could not find .* "Other License #"/, :for => "Other License #" diff --git a/spec/public/select_date_spec.rb b/spec/public/select_date_spec.rb index 9ccf0e5..a5923a5 100644 --- a/spec/public/select_date_spec.rb +++ b/spec/public/select_date_spec.rb @@ -71,7 +71,7 @@ describe "select_date" do select_date "December 25, 2003" click_button end - + it "should work when the label ends in a non word character" do with_html <<-HTML diff --git a/spec/public/select_spec.rb b/spec/public/select_spec.rb index e35db85..34b6c31 100644 --- a/spec/public/select_spec.rb +++ b/spec/public/select_spec.rb @@ -231,7 +231,7 @@ describe "select" do }.should_not raise_error(Webrat::NotFoundError) end end - + it "should submit duplicates selected options as a single value" do with_html <<-HTML @@ -241,9 +241,9 @@ describe "select" do HTML - + webrat_session.should_receive(:post).with("http://www.example.com/login", "clothes" => "pants") click_button end - + end