Skip to content

Commit

Permalink
Canonicalize all URLs (Shalon Wood)
Browse files Browse the repository at this point in the history
  • Loading branch information
brynary committed May 11, 2009
1 parent 481bfe0 commit 755cf6e
Show file tree
Hide file tree
Showing 22 changed files with 226 additions and 154 deletions.
19 changes: 10 additions & 9 deletions History.txt
@@ -1,19 +1,20 @@
== 0.4.5 / ?
== brynary/master (in git)

* Minor enhancements

* Added support for field_labeled_locators ending in non word characters
lh 148 (Zach Dennis)
* Filled in tests on click link lh 195 (diabolo)
* Canonicalize all URLs (Shalon Wood)

== 0.4.4 / 2009-04-06

* Major enhancements

* Make selenium process management code more robust and informative

* Minor enhancements

* Add support for Rails javascript post links (Mark Menard)
* Upgrade selenium-client dependency to 1.2.14, and update for new waiting
API (Balint Erdi)
Expand All @@ -24,7 +25,7 @@
* Don't create a new instance of WWW::Mechanize for each request
(Mark Menard)
* Select fields with duplicate selected options sent an incorrect value (Noah Davis)

== 0.4.3 / 2009-03-17

* Minor enhancements
Expand All @@ -34,7 +35,7 @@
* Initial Merb and Sinatra compatibility for Selenium mode (Corey Donohoe)
* When faced with a label with no for attribute, that contains a hidden field
and another field, as can be the case in Rails 2.3's checkbox view,
webrat now locates the non-hidden field. (Luke Melia)
webrat now locates the non-hidden field. (Luke Melia)
* Add application_framework config for Selenium mode to determine how to
start and stop the app server (Corey Donohoe)

Expand All @@ -50,7 +51,7 @@
attributes in a hash and :count and :content options. See
have_selector_spec.rb for more.
* Add the same functionality mentioned above to have_xpath

* Minor enhancements

* Squeeze extra whitespace out of failures messages from contain
Expand All @@ -77,7 +78,7 @@
redirected to (Adam Greene)
* Recognize input tags with type button (Lena Herrmann)
* Add uncheck method to Selenium mode (Lee Bankewitz)

* Bug fixes

* Make requests to a Rails app using a full URL instead of a relative path. This change
Expand All @@ -88,7 +89,7 @@
to behave correctly (Noah Davis)
* Switch to using selenium.click instead of .check when checking a checkbox
(Noah Davis)
* Create tmp/pids directory if directory does not exist. (Amos King and Mike Gaffney)
* Create tmp/pids directory if directory does not exist. (Amos King and Mike Gaffney)
* Setup deprecated writers for the selenium_environment= and selenium_port= config
* Ensure the previous pages params aren't passed through redirect (Daniel Lucraft and
Bryan Helmkamp)
Expand Down Expand Up @@ -164,7 +165,7 @@
* Added save_and_open_screengrab for Selenium mode (Luke Melia)

* Bug fixes

* field_labeled should disregard labels without matching fields (Kyle Hargraves)
* Fixed bug where Scope was creating a new DOM rather than re-using the existing DOM.
[#105] (Zach Dennis)
Expand Down
13 changes: 1 addition & 12 deletions lib/webrat/core/elements/area.rb
Expand Up @@ -8,24 +8,13 @@ def self.xpath_search
end

def click(method = nil, options = {})
@session.request_page(absolute_href, :get, {})
@session.request_page(href, :get, {})
end

protected

def href
Webrat::XML.attribute(@element, "href")
end

def absolute_href
if href =~ /^\?/
"#{@session.current_url}#{href}"
elsif href !~ %r{^https?://[\w|.]+(/.*)} && (href !~ /^\//)
"#{@session.current_url}/#{href}"
else
href
end
end

end
end
14 changes: 2 additions & 12 deletions lib/webrat/core/elements/link.rb
Expand Up @@ -16,9 +16,9 @@ def click(options = {})
options[:javascript] = true if options[:javascript].nil?

if options[:javascript]
@session.request_page(absolute_href, method, data)
@session.request_page(href, method, data)
else
@session.request_page(absolute_href, :get, {})
@session.request_page(href, :get, {})
end
end

Expand All @@ -40,16 +40,6 @@ def href
Webrat::XML.attribute(@element, "href")
end

def absolute_href
if href =~ /^\?/
"#{@session.current_url}#{href}"
elsif href !~ %r{^https?://} && (href !~ /^\//)
"#{@session.current_url}/#{href}"
else
href
end
end

def authenticity_token
return unless onclick && onclick.include?("s.setAttribute('name', 'authenticity_token');") &&
onclick =~ /s\.setAttribute\('value', '([a-f0-9]{40})'\);/
Expand Down
24 changes: 23 additions & 1 deletion lib/webrat/core/session.rb
Expand Up @@ -95,10 +95,32 @@ def headers #:nodoc:
@default_headers.dup.merge(@custom_headers.dup)
end

def canonicalize_url(href_url)
@current_url ||= "http://www.example.com/" # @current_url can't be blank, or things break
# Case one: relative url
if href_url !~ %r{^https?://} && (href_url !~ /^\//)
# If the relative url starts with # or ?, we need append it as is _if_ there are three slashes in the current url;
# otherwise, ensure there's a slash between it and the current
# url
if (href_url =~ /^\?/ or href_url =~ /^#/) && current_url.scan('/').length > 2
"#{current_url}#{href_url}"
else
"#{current_url.chomp('/')}/#{href_url}"
end
# Case two: absolute url without host
elsif href_url =~ /^\//
"http://#{current_host}#{href_url}"
# Case three: absolute url with scheme and host.
else
href_url
end
end

def request_page(url, http_method, data) #:nodoc:
h = headers
h['HTTP_REFERER'] = @current_url if @current_url

url = canonicalize_url(url)
debug_log "REQUESTING PAGE: #{http_method.to_s.upcase} #{url} with #{data.inspect} and HTTP headers #{h.inspect}"
if h.empty?
send "#{http_method}", url, data || {}
Expand Down Expand Up @@ -260,7 +282,7 @@ def automate
private

def response_location
response.headers["Location"]
canonicalize_url(response.headers["Location"])
end

def current_host
Expand Down
4 changes: 2 additions & 2 deletions spec/integration/rails/Rakefile
Expand Up @@ -16,13 +16,13 @@ namespace :test_unit do
desc "runs the test::unit based tests in webrat mode"
task :rails do
ENV['WEBRAT_INTEGRATION_MODE'] = 'rails'
Rake::Task['test:integration'].execute
Rake::Task['test:integration'].execute nil
end

desc "runs the test::unit based tests in selenium mode"
task :selenium do
ENV['WEBRAT_INTEGRATION_MODE'] = 'selenium'
Rake::Task['test:integration'].execute
Rake::Task['test:integration'].execute nil
end

desc "run both selenium and rails mode Test::Unit suites"
Expand Down
71 changes: 70 additions & 1 deletion spec/private/core/session_spec.rb
Expand Up @@ -118,7 +118,7 @@ def session.response_body

webrat_session.request_page("/oldurl", :get, {})

webrat_session.current_url.should == "/oldurl"
webrat_session.current_url.should == "http://www.example.com/oldurl"
end
end

Expand Down Expand Up @@ -195,4 +195,73 @@ def session.response_body

end

describe "#canonicalize_url"do

it "should turn 'http://www.example.com/' into 'http://www.example.com/'" do
webrat_session.stub!(:current_url => "http://www.example.com")
webrat_session.canonicalize_url("http://www.example.com/").should == "http://www.example.com/"
end

it "should turn '/login' into 'http://www.example.com/login' if current_url is 'http://www.example.com/'" do
webrat_session.stub!(:current_url => "http://www.example.com/")
webrat_session.canonicalize_url("/login").should == "http://www.example.com/login"
end

it "should turn '/login' into 'http://www.example.com/login' if current_url is 'http://www.example.com'" do
webrat_session.stub!(:current_url => "http://www.example.com")
webrat_session.canonicalize_url("/login").should == "http://www.example.com/login"
end

it "should turn 'login' into 'http://www.example.com/login' if current_url is 'http://www.example.com'" do
webrat_session.stub!(:current_url => "http://www.example.com")
webrat_session.canonicalize_url("login").should == "http://www.example.com/login"
end

it "should turn 'login' into 'http://www.example.com/login' if current_url is 'http://www.example.com/'" do
webrat_session.stub!(:current_url => "http://www.example.com/")
webrat_session.canonicalize_url("login").should == "http://www.example.com/login"
end

it "should turn '?login' into 'http://www.example.com/?login' if current_url is 'http://www.example.com'" do
webrat_session.stub!(:current_url => "http://www.example.com")
webrat_session.canonicalize_url("?login").should == "http://www.example.com/?login"
end

it "should turn '?login' into 'http://www.example.com/?login' if current_url is 'http://www.example.com/'" do
webrat_session.stub!(:current_url => "http://www.example.com/")
webrat_session.canonicalize_url("?login").should == "http://www.example.com/?login"
end

it "should turn '?login' into 'http://www.example.com/page?login' if current_url is 'http://www.example.com/page'" do
webrat_session.stub!(:current_url => "http://www.example.com/page")
webrat_session.canonicalize_url("?login").should == "http://www.example.com/page?login"
end

it "should turn '?login' into 'http://www.example.com/page/?login' if current_url is 'http://www.example.com/page/'" do
webrat_session.stub!(:current_url => "http://www.example.com/page/")
webrat_session.canonicalize_url("?login").should == "http://www.example.com/page/?login"
end

it "should turn '#anchor' into 'http://www.example.com/#anchor' if current_url is 'http://www.example.com'" do
webrat_session.stub!(:current_url => "http://www.example.com")
webrat_session.canonicalize_url("#anchor").should == "http://www.example.com/#anchor"
end

it "should turn '#anchor' into 'http://www.example.com/#anchor' if current_url is 'http://www.example.com/'" do
webrat_session.stub!(:current_url => "http://www.example.com/")
webrat_session.canonicalize_url("#anchor").should == "http://www.example.com/#anchor"
end

it "should turn '#anchor' into 'http://www.example.com/page#anchor' if current_url is 'http://www.example.com/page'" do
webrat_session.stub!(:current_url => "http://www.example.com/page")
webrat_session.canonicalize_url("#anchor").should == "http://www.example.com/page#anchor"
end

it "should turn '#anchor' into 'http://www.example.com/page/#anchor' if current_url is 'http://www.example.com/page/'" do
webrat_session.stub!(:current_url => "http://www.example.com/page/")
webrat_session.canonicalize_url("#anchor").should == "http://www.example.com/page/#anchor"
end

end

end
6 changes: 3 additions & 3 deletions spec/private/rails/attaches_file_spec.rb
Expand Up @@ -27,7 +27,7 @@
</form>
</html>
HTML
webrat_session.should_receive(:post).with("/widgets", { "widget" => { "file" => "" } })
webrat_session.should_receive(:post).with("http://www.example.com/widgets", { "widget" => { "file" => "" } })
click_button
end

Expand All @@ -41,7 +41,7 @@
</form>
</html>
HTML
webrat_session.should_receive(:post).with("/widgets", { "widget" => { "file" => @uploaded_file } })
webrat_session.should_receive(:post).with("http://www.example.com/widgets", { "widget" => { "file" => @uploaded_file } })
attach_file "Document", @filename
click_button
end
Expand All @@ -58,7 +58,7 @@
</form>
</html>
HTML
webrat_session.should_receive(:post).with("/widgets", { "widget" => { "files" => [@uploaded_file, @uploaded_file] } })
webrat_session.should_receive(:post).with("http://www.example.com/widgets", { "widget" => { "files" => [@uploaded_file, @uploaded_file] } })
attach_file "Document", @filename
attach_file "Spreadsheet", @filename
click_button
Expand Down
3 changes: 2 additions & 1 deletion spec/private/rails/rails_session_spec.rb
Expand Up @@ -96,7 +96,8 @@

response = mock("response", :body => body, :headers => {}, :code => 200)
@integration_session.stub!(:response => response)
@integration_session.should_receive(:get).with("/page2", {}, nil)
@integration_session.stub!(:https!)
@integration_session.should_receive(:get).with("http://www.example.com/page2", {}, nil)

rails_session = Webrat::RailsSession.new(@integration_session)

Expand Down
4 changes: 2 additions & 2 deletions spec/public/basic_auth_spec.rb
Expand Up @@ -6,7 +6,7 @@
end

it "should be present in visit" do
webrat_session.should_receive(:get).with("/", {}, {'HTTP_AUTHORIZATION' => "Basic dXNlcjpzZWNyZXQ=\n"})
webrat_session.should_receive(:get).with("http://www.example.com/", {}, {'HTTP_AUTHORIZATION' => "Basic dXNlcjpzZWNyZXQ=\n"})
visit("/")
end

Expand All @@ -18,7 +18,7 @@
</form>
</html>
HTML
webrat_session.should_receive(:post).with("/form1", {}, {'HTTP_AUTHORIZATION' => "Basic dXNlcjpzZWNyZXQ=\n"})
webrat_session.should_receive(:post).with("http://www.example.com/form1", {}, {'HTTP_AUTHORIZATION' => "Basic dXNlcjpzZWNyZXQ=\n"})
click_button
end
end
14 changes: 7 additions & 7 deletions spec/public/check_spec.rb
Expand Up @@ -36,7 +36,7 @@
</html>
HTML

webrat_session.should_receive(:get).with("/login", "user" => {"tos" => "1"})
webrat_session.should_receive(:get).with("http://www.example.com/login", "user" => {"tos" => "1"})
check "TOS"
click_button
end
Expand All @@ -51,7 +51,7 @@
</html>
HTML

webrat_session.should_receive(:post).with("/login", "remember_me" => "on")
webrat_session.should_receive(:post).with("http://www.example.com/login", "remember_me" => "on")
check "remember_me"
click_button
end
Expand Down Expand Up @@ -79,7 +79,7 @@
</html>
HTML

webrat_session.should_receive(:post).with("/login", "remember_me" => "yes")
webrat_session.should_receive(:post).with("http://www.example.com/login", "remember_me" => "yes")
check "remember_me"
click_button
end
Expand Down Expand Up @@ -132,7 +132,7 @@
</form>
</html>
HTML
webrat_session.should_receive(:get).with("/login", "user" => {"tos" => "0"})
webrat_session.should_receive(:get).with("http://www.example.com/login", "user" => {"tos" => "0"})
check "TOS"
uncheck "TOS"
click_button
Expand All @@ -147,7 +147,7 @@
</form>
</html>
HTML
webrat_session.should_receive(:post).with("/login", {})
webrat_session.should_receive(:post).with("http://www.example.com/login", {})
uncheck "remember_me"
click_button
end
Expand All @@ -164,7 +164,7 @@
</form>
</html>
HTML
webrat_session.should_receive(:post).with("/login", {"options" => ["1", "2"]})
webrat_session.should_receive(:post).with("http://www.example.com/login", {"options" => ["1", "2"]})
check 'Option 1'
check 'Option 2'
click_button
Expand All @@ -183,7 +183,7 @@
</form>
</html>
HTML
webrat_session.should_receive(:get).with("/login", "user" => {"tos" => "0"})
webrat_session.should_receive(:get).with("http://www.example.com/login", "user" => {"tos" => "0"})
uncheck "TOS"
click_button
end
Expand Down

0 comments on commit 755cf6e

Please sign in to comment.