From 89d56fb5da15ef58805e77e002284c7a6e10eb5d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aslak=20Helles=C3=B8y?= Date: Mon, 13 Oct 2008 14:56:13 +0200 Subject: [PATCH 1/5] Start adding support for rack and sinatra --- lib/webrat/rack/rack_session.rb | 13 +++++++++++++ lib/webrat/sinatra/sinatra_session.rb | 13 +++++++++++++ 2 files changed, 26 insertions(+) create mode 100644 lib/webrat/rack/rack_session.rb create mode 100644 lib/webrat/sinatra/sinatra_session.rb diff --git a/lib/webrat/rack/rack_session.rb b/lib/webrat/rack/rack_session.rb new file mode 100644 index 0000000..b283294 --- /dev/null +++ b/lib/webrat/rack/rack_session.rb @@ -0,0 +1,13 @@ +require 'webrat' + +module Webrat + class RackSession < Session + def response_body + @response.body + end + + def response_code + @response.status + end + end +end \ No newline at end of file diff --git a/lib/webrat/sinatra/sinatra_session.rb b/lib/webrat/sinatra/sinatra_session.rb new file mode 100644 index 0000000..597c3b8 --- /dev/null +++ b/lib/webrat/sinatra/sinatra_session.rb @@ -0,0 +1,13 @@ +require 'webrat/rack/rack_session' +require 'sinatra' +require 'sinatra/test/methods' + +module Webrat + class SinatraSession < RackSession + include Sinatra::Test::Methods + + def get(*args) + get_it(*args) + end + end +end \ No newline at end of file From 682f4d19f92eb906c891b1f0764f8954f1174155 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aslak=20Helles=C3=B8y?= Date: Thu, 16 Oct 2008 15:50:11 +0200 Subject: [PATCH 2/5] Decouple from Rails so we can use Webrat with Sinatra --- lib/webrat.rb | 1 - lib/webrat/core/form.rb | 4 +++- lib/webrat/rack/rack_session.rb | 11 +++++++++++ lib/webrat/sinatra/sinatra_session.rb | 8 ++++++-- 4 files changed, 20 insertions(+), 4 deletions(-) diff --git a/lib/webrat.rb b/lib/webrat.rb index dffddac..8f11582 100644 --- a/lib/webrat.rb +++ b/lib/webrat.rb @@ -3,7 +3,6 @@ module Webrat end require "rubygems" -require "active_support" require File.dirname(__FILE__) + "/webrat/core" require File.dirname(__FILE__) + "/webrat/rails" if defined?(RAILS_ENV) diff --git a/lib/webrat/core/form.rb b/lib/webrat/core/form.rb index 60a5c07..9e55a95 100644 --- a/lib/webrat/core/form.rb +++ b/lib/webrat/core/form.rb @@ -106,11 +106,13 @@ module Webrat def form_action @element["action"].blank? ? @session.current_url : @element["action"] end + + HASH = [Hash, HashWithIndifferentAccess] rescue [Hash] def merge(all_params, new_param) new_param.each do |key, value| case all_params[key] - when Hash, HashWithIndifferentAccess + when *HASH merge_hash_values(all_params[key], value) when Array all_params[key] += value diff --git a/lib/webrat/rack/rack_session.rb b/lib/webrat/rack/rack_session.rb index b283294..e3c2e24 100644 --- a/lib/webrat/rack/rack_session.rb +++ b/lib/webrat/rack/rack_session.rb @@ -1,5 +1,16 @@ require 'webrat' +class CGIMethods + def self.parse_query_parameters(params) + hash = {} + params.split('&').each do |p| + pair = p.split('=') + hash[pair[0]] = pair[1] + end + hash + end +end + module Webrat class RackSession < Session def response_body diff --git a/lib/webrat/sinatra/sinatra_session.rb b/lib/webrat/sinatra/sinatra_session.rb index 597c3b8..f770554 100644 --- a/lib/webrat/sinatra/sinatra_session.rb +++ b/lib/webrat/sinatra/sinatra_session.rb @@ -6,8 +6,12 @@ module Webrat class SinatraSession < RackSession include Sinatra::Test::Methods - def get(*args) - get_it(*args) + def get(url, data, headers = nil) + get_it(url, data) + end + + def post(url, data, headers = nil) + post_it(url, data) end end end \ No newline at end of file From a885a80a7b2c3622990c1f4c649c222460b3f541 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aslak=20Helles=C3=B8y?= Date: Fri, 17 Oct 2008 12:38:59 +0200 Subject: [PATCH 3/5] More complete Sinatra support --- lib/webrat/sinatra/sinatra_session.rb | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/webrat/sinatra/sinatra_session.rb b/lib/webrat/sinatra/sinatra_session.rb index f770554..2647f6c 100644 --- a/lib/webrat/sinatra/sinatra_session.rb +++ b/lib/webrat/sinatra/sinatra_session.rb @@ -6,12 +6,12 @@ module Webrat class SinatraSession < RackSession include Sinatra::Test::Methods - def get(url, data, headers = nil) - get_it(url, data) - end - - def post(url, data, headers = nil) - post_it(url, data) + %w(get head post put delete).each do |verb| + define_method(verb) do |*args| + url, data, headers = *args + self.__send__("#{verb}_it", url, data) + follow! while @response.redirect? + end end end end \ No newline at end of file From e2c70b8e919d860857061aab698a64a7fb9c6c1b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aslak=20Helles=C3=B8y?= Date: Fri, 17 Oct 2008 13:39:34 +0200 Subject: [PATCH 4/5] Update History with note about Sinatra and Rack --- History.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/History.txt b/History.txt index 2e82518..3a41df6 100644 --- a/History.txt +++ b/History.txt @@ -2,6 +2,7 @@ * Major enhancements + * Added support for Sinatra and Rack (Aslak Hellesøy) * Added #within for manipulating the current page within a selector scope * Add support for simulating SSL requests (Luke Melia) * Add support for file fields via #attaches_file method (Patch from Kyle Hargraves) From 17cf56eb5e9f3872b842a43a3181caca4f005a28 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aslak=20Helles=C3=B8y?= Date: Fri, 17 Oct 2008 17:43:14 +0200 Subject: [PATCH 5/5] Add support for Basic Auth --- History.txt | 1 + lib/webrat/core/session.rb | 26 +++++++++++++++++++------- lib/webrat/sinatra/sinatra_session.rb | 7 ++++--- spec/api/basic_auth_spec.rb | 23 +++++++++++++++++++++++ 4 files changed, 47 insertions(+), 10 deletions(-) create mode 100644 spec/api/basic_auth_spec.rb diff --git a/History.txt b/History.txt index 3a41df6..d9e65c0 100644 --- a/History.txt +++ b/History.txt @@ -2,6 +2,7 @@ * Major enhancements + * Added #basic_auth(user, pass) to support HTTP Basic Auth (Aslak Hellesøy) * Added support for Sinatra and Rack (Aslak Hellesøy) * Added #within for manipulating the current page within a selector scope * Add support for simulating SSL requests (Luke Melia) diff --git a/lib/webrat/core/session.rb b/lib/webrat/core/session.rb index ecf243c..65396df 100644 --- a/lib/webrat/core/session.rb +++ b/lib/webrat/core/session.rb @@ -10,8 +10,9 @@ module Webrat attr_reader :current_url def initialize - @http_method = :get - @data = {} + @http_method = :get + @data = {} + @default_headers = {} end # Saves the page out to RAILS_ROOT/tmp/ and opens it in the default @@ -52,14 +53,25 @@ module Webrat File.expand_path(".") end + def basic_auth(user, pass) + @default_headers['HTTP_AUTHORIZATION'] = "Basic " + ["#{user}:#{pass}"].pack("m*") + end + + def headers + @default_headers.dup + end + def request_page(url, http_method, data) - debug_log "REQUESTING PAGE: #{http_method.to_s.upcase} #{url} with #{data.inspect}" - if @current_url - send "#{http_method}", url, data || {}, {"HTTP_REFERER" => @current_url} - else + h = headers + h['HTTP_REFERER'] = @current_url if @current_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 || {} + else + send "#{http_method}", url, data || {}, h end - + save_and_open_page if exception_caught? flunk("Page load was not successful (Code: #{response_code.inspect})") unless success_code? diff --git a/lib/webrat/sinatra/sinatra_session.rb b/lib/webrat/sinatra/sinatra_session.rb index 2647f6c..3152c6c 100644 --- a/lib/webrat/sinatra/sinatra_session.rb +++ b/lib/webrat/sinatra/sinatra_session.rb @@ -7,9 +7,10 @@ module Webrat include Sinatra::Test::Methods %w(get head post put delete).each do |verb| - define_method(verb) do |*args| - url, data, headers = *args - self.__send__("#{verb}_it", url, data) + define_method(verb) do |*args| # (path, data, headers = nil) + path, data, headers = *args + params = data.merge({:env => headers || {}}) + self.__send__("#{verb}_it", path, params) follow! while @response.redirect? end end diff --git a/spec/api/basic_auth_spec.rb b/spec/api/basic_auth_spec.rb new file mode 100644 index 0000000..07b9cb1 --- /dev/null +++ b/spec/api/basic_auth_spec.rb @@ -0,0 +1,23 @@ +require File.expand_path(File.dirname(__FILE__) + "/../spec_helper") + +describe "Basic Auth HTTP headers" do + before do + @session = Webrat::TestSession.new + @session.basic_auth('user', 'secret') + end + + it "should be present in visits" do + @session.should_receive(:get).with("/", {}, {'HTTP_AUTHORIZATION' => "Basic dXNlcjpzZWNyZXQ=\n"}) + @session.visits("/") + end + + it "should be present in form submits" do + @session.response_body = <<-EOS +
+ +
+ EOS + @session.should_receive(:post).with("/form1", {}, {'HTTP_AUTHORIZATION' => "Basic dXNlcjpzZWNyZXQ=\n"}) + @session.clicks_button + end +end