From 88340500499ac5dc2897b4b7cb11f6512313cab5 Mon Sep 17 00:00:00 2001
From: "John Barton (joho)"
Date: Sun, 22 May 2011 18:37:02 +1000
Subject: [PATCH] Actually properly handling relative URLs for forms
(Where I subtly misunderstood how URIs work)
---
lib/capybara/rack_test/browser.rb | 8 ++++++--
lib/capybara/spec/session/click_button_spec.rb | 10 +++++++++-
lib/capybara/spec/test_app.rb | 2 +-
lib/capybara/spec/views/form.erb | 6 ++++++
4 files changed, 22 insertions(+), 4 deletions(-)
diff --git a/lib/capybara/rack_test/browser.rb b/lib/capybara/rack_test/browser.rb
index 896ead1e..e5d65746 100644
--- a/lib/capybara/rack_test/browser.rb
+++ b/lib/capybara/rack_test/browser.rb
@@ -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
diff --git a/lib/capybara/spec/session/click_button_spec.rb b/lib/capybara/spec/session/click_button_spec.rb
index ccb9445c..4c03b1c6 100644
--- a/lib/capybara/spec/session/click_button_spec.rb
+++ b/lib/capybara/spec/session/click_button_spec.rb
@@ -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
diff --git a/lib/capybara/spec/test_app.rb b/lib/capybara/spec/test_app.rb
index eafe7de9..124dfd44 100644
--- a/lib/capybara/spec/test_app.rb
+++ b/lib/capybara/spec/test_app.rb
@@ -47,7 +47,7 @@ class TestApp < Sinatra::Base
'
' + params[:form].to_yaml + '
'
end
- post '/form/relative' do
+ post '/relative' do
'
' + params[:form].to_yaml + '
'
end
diff --git a/lib/capybara/spec/views/form.erb b/lib/capybara/spec/views/form.erb
index 8e793e48..50b8c8e3 100644
--- a/lib/capybara/spec/views/form.erb
+++ b/lib/capybara/spec/views/form.erb
@@ -357,3 +357,9 @@