Actually properly handling relative URLs for forms

(Where I subtly misunderstood how URIs work)
This commit is contained in:
John Barton (joho) 2011-05-22 18:37:02 +10:00
parent 4d4eebe573
commit 8834050049
4 changed files with 22 additions and 4 deletions

View File

@ -39,9 +39,13 @@ class Capybara::RackTest::Browser
@current_host = new_uri.scheme + '://' + new_uri.host
end
unless new_uri.absolute?
if new_uri.relative?
path = request_path + path if path.start_with?('?')
path = request_path + '/' + path unless path.start_with?('/')
unless path.start_with?('/')
folders = request_path.split('/')
path = (folders[0, folders.size - 1] << path).join('/')
end
path = current_host + path
end

View File

@ -15,11 +15,19 @@ shared_examples_for "click_button" do
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'
@session.current_path.should == '/relative'
extract_results(@session)['relative'].should == 'Relative Action'
end
end
context "with a form that has no action specified" do
it "should post to the correct url" do
@session.click_button('No Action')
@session.current_path.should == '/form'
extract_results(@session)['no_action'].should == 'No Action'
end
end
context "with value given on a submit button" do
context "on a form with HTML5 fields" do
before do

View File

@ -47,7 +47,7 @@ class TestApp < Sinatra::Base
'<pre id="results">' + params[:form].to_yaml + '</pre>'
end
post '/form/relative' do
post '/relative' do
'<pre id="results">' + params[:form].to_yaml + '</pre>'
end

View File

@ -357,3 +357,9 @@
<input type="submit" name="form[relative]" value="Relative Action" />
</p>
</form>
<form method="post">
<p>
<input type="submit" name="form[no_action]" value="No Action" />
</p>
</form>