From 0b9fd99bc01bfc9a83d5788032329df6bb2766a7 Mon Sep 17 00:00:00 2001 From: Noah Davis Date: Fri, 19 Dec 2008 14:32:02 -0500 Subject: [PATCH] stripping anchor tags from URIs before passing to rails integration session --- lib/webrat/rails.rb | 15 +++++++++------ spec/webrat/rails/rails_session_spec.rb | 9 +++++++++ 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/lib/webrat/rails.rb b/lib/webrat/rails.rb index f312db9..5da0cff 100644 --- a/lib/webrat/rails.rb +++ b/lib/webrat/rails.rb @@ -48,15 +48,18 @@ module Webrat def do_request(http_method, url, data, headers) #:nodoc: update_protocol(url) - integration_session.request_via_redirect(http_method, remove_protocol(url), data, headers) + url = normalize_url(url) + integration_session.request_via_redirect(http_method, url, data, headers) end - def remove_protocol(href) #:nodoc: - if href =~ %r{^https?://www.example.com(/.*)} - $LAST_MATCH_INFO.captures.first - else - href + # remove protocol, host and anchor + def normalize_url(href) #:nodoc: + uri = URI.parse(href) + normalized_url = uri.path + if uri.query + normalized_url += "?" + uri.query end + normalized_url end def update_protocol(href) #:nodoc: diff --git a/spec/webrat/rails/rails_session_spec.rb b/spec/webrat/rails/rails_session_spec.rb index 4e00802..e8ea061 100644 --- a/spec/webrat/rails/rails_session_spec.rb +++ b/spec/webrat/rails/rails_session_spec.rb @@ -69,6 +69,15 @@ describe Webrat::RailsSession do end end + context "the URL include an anchor" do + it "should strip out the anchor" do + integration_session = mock("integration session", :https! => false) + rails_session = Webrat::RailsSession.new(integration_session) + integration_session.should_receive(:request_via_redirect).with(:get, "/url", "data", "headers") + rails_session.get("http://www.example.com/url#foo", "data", "headers") + end + end + it "should provide a saved_page_dir" do Webrat::RailsSession.new(mock("integration session")).should respond_to(:saved_page_dir) end