Skip to content

Commit

Permalink
Rack-test should use data-method if available in links.
Browse files Browse the repository at this point in the history
  • Loading branch information
josevalim committed Apr 9, 2010
1 parent 5661d67 commit 58d4d0c
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 2 deletions.
9 changes: 7 additions & 2 deletions lib/capybara/driver/rack_test_driver.rb
Expand Up @@ -74,7 +74,8 @@ def unselect(option)

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
Expand Down Expand Up @@ -186,8 +187,12 @@ def initialize(app)
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

Expand Down
8 changes: 8 additions & 0 deletions spec/session/rack_test_session_spec.rb
Expand Up @@ -18,6 +18,14 @@
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"
Expand Down
4 changes: 4 additions & 0 deletions spec/test_app.rb
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions spec/views/with_html.erb
Expand Up @@ -23,6 +23,7 @@
<a href="/redirect_back">BackToMyself</a>
<a title="twas a fine link" href="/redirect">A link came first</a>
<a title="a fine link" href="/with_simple_html">A link</a>
<a title="a fine link with data method" data-method="delete" href="/delete">A link with data-method</a>
<a>No Href</a>
<a href="">Blank Href</a>
<a href="#">Blank Anchor</a>
Expand Down

0 comments on commit 58d4d0c

Please sign in to comment.