Merge branch 'aslakhellesoy/master'

Conflicts:
	lib/webrat/rack/rack_session.rb
	lib/webrat/sinatra/sinatra_session.rb
This commit is contained in:
Bryan Helmkamp 2008-10-17 14:30:07 -04:00
commit 6d8c346762
7 changed files with 66 additions and 11 deletions

View File

@ -2,6 +2,8 @@
* Major enhancements * 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 * Added #within for manipulating the current page within a selector scope
* Add should_see and should_not_see for verifying HTML response bodys * Add should_see and should_not_see for verifying HTML response bodys
* Add support for simulating SSL requests (Luke Melia) * Add support for simulating SSL requests (Luke Melia)

View File

@ -3,7 +3,6 @@ module Webrat
end end
require "rubygems" require "rubygems"
require "active_support"
require File.dirname(__FILE__) + "/webrat/core" require File.dirname(__FILE__) + "/webrat/core"
require File.dirname(__FILE__) + "/webrat/rails" if defined?(RAILS_ENV) require File.dirname(__FILE__) + "/webrat/rails" if defined?(RAILS_ENV)

View File

@ -87,10 +87,12 @@ module Webrat
@element["action"].blank? ? @session.current_url : @element["action"] @element["action"].blank? ? @session.current_url : @element["action"]
end end
HASH = [Hash, HashWithIndifferentAccess] rescue [Hash]
def merge(all_params, new_param) def merge(all_params, new_param)
new_param.each do |key, value| new_param.each do |key, value|
case all_params[key] case all_params[key]
when Hash, HashWithIndifferentAccess when *HASH
merge_hash_values(all_params[key], value) merge_hash_values(all_params[key], value)
when Array when Array
all_params[key] += value all_params[key] += value

View File

@ -10,8 +10,9 @@ module Webrat
attr_reader :current_url attr_reader :current_url
def initialize def initialize
@http_method = :get @http_method = :get
@data = {} @data = {}
@default_headers = {}
end end
# Saves the page out to RAILS_ROOT/tmp/ and opens it in the default # Saves the page out to RAILS_ROOT/tmp/ and opens it in the default
@ -52,12 +53,23 @@ module Webrat
File.expand_path(".") File.expand_path(".")
end 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) def request_page(url, http_method, data)
debug_log "REQUESTING PAGE: #{http_method.to_s.upcase} #{url} with #{data.inspect}" h = headers
if @current_url h['HTTP_REFERER'] = @current_url if @current_url
send "#{http_method}", url, data || {}, {"HTTP_REFERER" => @current_url}
else 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 || {} send "#{http_method}", url, data || {}
else
send "#{http_method}", url, data || {}, h
end end
save_and_open_page if exception_caught? save_and_open_page if exception_caught?

View File

@ -1,5 +1,16 @@
require 'webrat' 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 module Webrat
class RackSession < Session class RackSession < Session
def response_body def response_body

View File

@ -6,8 +6,14 @@ module Webrat
class SinatraSession < RackSession class SinatraSession < RackSession
include Sinatra::Test::Methods include Sinatra::Test::Methods
def get(*args) %w(get head post put delete).each do |verb|
get_it(*args) 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 end
end end
end end

View File

@ -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
<form method="post" action="/form1">
<input type="submit" />
</form>
EOS
@session.should_receive(:post).with("/form1", {}, {'HTTP_AUTHORIZATION' => "Basic dXNlcjpzZWNyZXQ=\n"})
@session.clicks_button
end
end