From 58d4d0caf7d4fad5519033e9bfd0786146750a13 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Fri, 9 Apr 2010 14:01:17 +0200 Subject: [PATCH] Rack-test should use data-method if available in links. --- lib/capybara/driver/rack_test_driver.rb | 9 +++++++-- spec/session/rack_test_session_spec.rb | 8 ++++++++ spec/test_app.rb | 4 ++++ spec/views/with_html.erb | 1 + 4 files changed, 20 insertions(+), 2 deletions(-) diff --git a/lib/capybara/driver/rack_test_driver.rb b/lib/capybara/driver/rack_test_driver.rb index 2c44e156..03e48d2b 100644 --- a/lib/capybara/driver/rack_test_driver.rb +++ b/lib/capybara/driver/rack_test_driver.rb @@ -74,7 +74,8 @@ class Capybara::Driver::RackTest < Capybara::Driver::Base def click if tag_name == 'a' - driver.visit(self[:href].to_s) + method = self["data-method"] || :get + driver.process(method, self[:href].to_s) elsif (tag_name == 'input' or tag_name == 'button') and %w(submit image).include?(type) Form.new(driver, form).submit(self) end @@ -186,8 +187,12 @@ class Capybara::Driver::RackTest < Capybara::Driver::Base end def visit(path, attributes = {}) + process(:get, path, attributes) + end + + def process(method, path, attributes = {}) return if path.gsub(/^#{current_path}/, '') =~ /^#/ - get(path, attributes, env) + send(method, path, attributes, env) follow_redirects! end diff --git a/spec/session/rack_test_session_spec.rb b/spec/session/rack_test_session_spec.rb index 43395278..aca5533a 100644 --- a/spec/session/rack_test_session_spec.rb +++ b/spec/session/rack_test_session_spec.rb @@ -18,6 +18,14 @@ describe Capybara::Session do end end + describe '#click_link' do + it "should use data-method if available" do + @session.visit "/with_html" + @session.click_link "A link with data-method" + @session.body.should == 'The requested object was deleted' + end + end + it_should_behave_like "session" it_should_behave_like "session without javascript support" it_should_behave_like "session with headers support" diff --git a/spec/test_app.rb b/spec/test_app.rb index d88ee555..95e44a86 100644 --- a/spec/test_app.rb +++ b/spec/test_app.rb @@ -42,6 +42,10 @@ class TestApp < Sinatra::Base redirect '/redirect_again' end + delete "/delete" do + "The requested object was deleted" + end + get '/redirect_back' do redirect back end diff --git a/spec/views/with_html.erb b/spec/views/with_html.erb index 3ab2dd73..3b9f20da 100644 --- a/spec/views/with_html.erb +++ b/spec/views/with_html.erb @@ -23,6 +23,7 @@ BackToMyself A link came first A link + A link with data-method No Href Blank Href Blank Anchor