diff --git a/History.txt b/History.txt
index fcea11f..77af696 100644
--- a/History.txt
+++ b/History.txt
@@ -6,6 +6,7 @@
* Support button elements (Patch from Nick Sieger)
* Support matching select options by regexp (Patch from Kyle Hargraves)
* Support relative links, including href="?foo=bar" (Patch from Kyle Hargraves)
+ * Support links to fully qualified URLs starting with http:// or https:// (Luke Melia)
* Bug fixes
@@ -27,7 +28,7 @@
* Added reloads method to reload the page (Patch from Kamal Fariz Mahyuddi)
* Prevent making a request if clicking on local anchor link (Patch from Kamal Fariz Mahyuddi)
* Added clicks_link_within(selector, link_text), allowing restricting link search
- to within a given css selector (Path from Luke Melia)
+ to within a given css selector (Patch from Luke Melia)
* Allow specifying the input name/label when doing a select (Patch from David Chelimsky)
* Raise a specific exception if the developer tries to manipulate form elements before loading a page (Patch from James Deville)
* Add support for alternate POST, PUT and DELETE link clicking (Patch from Kyle Hargraves)
diff --git a/lib/webrat/link.rb b/lib/webrat/link.rb
index d9ff682..e13d21d 100644
--- a/lib/webrat/link.rb
+++ b/lib/webrat/link.rb
@@ -10,6 +10,7 @@ module Webrat
method ||= http_method
return if href =~ /^#/ && method == :get
+ update_protocol(href)
Page.new(@page.session, absolute_href, method, authenticity_token.blank? ? {} : {"authenticity_token" => authenticity_token})
end
@@ -26,9 +27,19 @@ module Webrat
def href
@element["href"]
end
+
+ def update_protocol(href)
+ if href =~ /^https:/
+ @page.session.https!(true)
+ elsif href =~ /^http:/
+ @page.session.https!(false)
+ end
+ end
def absolute_href
- if href =~ /^\?/
+ if href =~ %r{^https?://www.example.com(/.*)}
+ $LAST_MATCH_INFO.captures.first
+ elsif href =~ /^\?/
"#{@page.url}#{href}"
elsif href !~ /^\//
"#{@page.url}/#{href}"
diff --git a/spec/clicks_link_spec.rb b/spec/clicks_link_spec.rb
index 28a1ffe..26132c5 100644
--- a/spec/clicks_link_spec.rb
+++ b/spec/clicks_link_spec.rb
@@ -199,6 +199,23 @@ describe "clicks_link" do
@session.expects(:get_via_redirect).with("/page/sub", {})
@session.clicks_link "Jump to sub page"
end
+
+ it "should follow fully qualified local links" do
+ @response.stubs(:body).returns(<<-EOS)
+ Jump to sub page
+ EOS
+ @session.expects(:get_via_redirect).with("/page/sub", {})
+ @session.clicks_link "Jump to sub page"
+ end
+
+ it "should follow fully qualified secure local links" do
+ @response.stubs(:body).returns(<<-EOS)
+ Jump to sub page
+ EOS
+ @session.expects(:https!).with(true)
+ @session.expects(:get_via_redirect).with("/page/sub", {})
+ @session.clicks_link "Jump to sub page"
+ end
it "should follow query parameters" do
@session.current_page.stubs(:url).returns("/page")