Skip to content

Commit

Permalink
Merge branch 'relative-links' of git://github.com/pd/webrat into pd/r…
Browse files Browse the repository at this point in the history
…elative-links
  • Loading branch information
brynary committed Apr 13, 2008
2 parents c2ace0e + 32dc365 commit e5630a5
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
4 changes: 4 additions & 0 deletions History.txt
@@ -1,5 +1,9 @@
== Trunk

* Enhancements

* Support relative links, including href="?foo=bar" (Patch from Kyle Hargraves)

* Bug fixes

* Fix regression of not sending default values in password fields
Expand Down
12 changes: 11 additions & 1 deletion lib/webrat/link.rb
Expand Up @@ -10,7 +10,7 @@ def click(method = nil)
method ||= http_method
return if href =~ /^#/ && method == :get

Page.new(@page.session, href, method, authenticity_token.blank? ? {} : {"authenticity_token" => authenticity_token})
Page.new(@page.session, absolute_href, method, authenticity_token.blank? ? {} : {"authenticity_token" => authenticity_token})
end

def matches_text?(link_text)
Expand All @@ -26,6 +26,16 @@ def text
def href
@element["href"]
end

def absolute_href
if href =~ /^\?/
"#{@page.url}#{href}"
elsif href !~ /^\//
"#{@page.url}/#{href}"
else
href
end
end

def authenticity_token
return unless onclick && onclick.include?("s.setAttribute('name', 'authenticity_token');") &&
Expand Down
18 changes: 18 additions & 0 deletions test/clicks_link_test.rb
Expand Up @@ -190,4 +190,22 @@ def test_should_not_make_request_when_link_is_local_anchor
@session.expects(:send).with('get_via_redirect', '#section-1', {}).never
@session.clicks_link "Jump to Section 1"
end

def test_should_follow_relative_links
@session.current_page.stubs(:url).returns("/page")
@response.stubs(:body).returns(<<-EOS)
<a href="sub">Jump to sub page</a>
EOS
@session.expects(:get_via_redirect).with("/page/sub", {})
@session.clicks_link "Jump to sub page"
end

def test_should_follow_query_parameters
@session.current_page.stubs(:url).returns("/page")
@response.stubs(:body).returns(<<-EOS)
<a href="?foo=bar">Jump to foo bar</a>
EOS
@session.expects(:get_via_redirect).with("/page?foo=bar", {})
@session.clicks_link "Jump to foo bar"
end
end

0 comments on commit e5630a5

Please sign in to comment.