Skip to content

Commit

Permalink
Fix problem where capybara can't post to relative urls in form actions.
Browse files Browse the repository at this point in the history
  • Loading branch information
joho committed May 22, 2011
1 parent aeb62d6 commit 4d4eebe
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 4 deletions.
11 changes: 7 additions & 4 deletions lib/capybara/rack_test/browser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,16 @@ def process(method, path, attributes = {})
new_uri = URI.parse(path)
current_uri = URI.parse(current_url)

path = request_path + path if path.start_with?('?')
path = current_host + path if path.start_with?('/')

if new_uri.host
@current_host = new_uri.scheme + '://' + new_uri.host
end


unless new_uri.absolute?
path = request_path + path if path.start_with?('?')
path = request_path + '/' + path unless path.start_with?('/')
path = current_host + path
end

reset_cache!
send(method, path, attributes, env)
follow_redirects!
Expand Down
9 changes: 9 additions & 0 deletions lib/capybara/spec/session/click_button_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@
end
end

context "with a form that has a relative url as an action" do
it "should post to the correct url" do
@session.click_button('Relative Action')
@session.current_path.should == '/form/relative'
extract_results(@session)['relative'].should == 'Relative Action'
end
end

context "with value given on a submit button" do
context "on a form with HTML5 fields" do
before do
Expand Down Expand Up @@ -284,4 +292,5 @@
@session.body.should include('Postback')
end
end

end
4 changes: 4 additions & 0 deletions lib/capybara/spec/test_app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ class TestApp < Sinatra::Base
'<pre id="results">' + params[:form].to_yaml + '</pre>'
end

post '/form/relative' do
'<pre id="results">' + params[:form].to_yaml + '</pre>'
end

get '/favicon.ico' do
nil
end
Expand Down
6 changes: 6 additions & 0 deletions lib/capybara/spec/views/form.erb
Original file line number Diff line number Diff line change
Expand Up @@ -351,3 +351,9 @@
<input type="submit" name="form[button]" value="Just a button"/>
</p>
</form>

<form action="relative" method="post">
<p>
<input type="submit" name="form[relative]" value="Relative Action" />
</p>
</form>

0 comments on commit 4d4eebe

Please sign in to comment.