merged changes from notch8 repo

This commit is contained in:
Krzysztof Zylawy 2008-10-21 13:05:35 +01:00
parent 1a5db346c9
commit 06997fabb8
14 changed files with 128 additions and 31 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

@ -59,6 +59,13 @@ tests to break unnecessarily as your application evolves:
A test written with Webrat can handle these changes to these without any modifications. A test written with Webrat can handle these changes to these without any modifications.
=== Merb
To avoid losing sessions, you need this in environments/test.rb:
Merb::Config.use do |c|
c[:session_store] = 'memory'
end
=== Install === Install
To install the latest release: To install the latest release:

View File

@ -35,28 +35,39 @@ def remove_task(task_name)
Rake.application.remove_task(task_name) Rake.application.remove_task(task_name)
end end
def set_file_list
if ENV['TEST_MODE'] == "merb"
list = FileList['spec/**/*_spec.rb']
list = list.find_all do |file| !file.match("rails") end
return list
else
return FileList['spec/**/*_spec.rb']
end
end
remove_task "test" remove_task "test"
remove_task "test_deps" remove_task "test_deps"
desc "Run all specs in spec directory" desc "Run all specs in spec directory"
Spec::Rake::SpecTask.new do |t| Spec::Rake::SpecTask.new do |t|
t.spec_opts = ['--options', "\"#{File.dirname(__FILE__)}/spec/spec.opts\""] t.spec_opts = ['--options', "\"#{File.dirname(__FILE__)}/spec/spec.opts\""]
t.spec_files = FileList['spec/**/*_spec.rb'] t.spec_files = set_file_list
end end
desc "Run all specs in spec directory with RCov" desc "Run all specs in spec directory with RCov"
Spec::Rake::SpecTask.new(:rcov) do |t| Spec::Rake::SpecTask.new(:rcov) do |t|
t.spec_opts = ['--options', "\"#{File.dirname(__FILE__)}/spec/spec.opts\""] t.spec_opts = ['--options', "\"#{File.dirname(__FILE__)}/spec/spec.opts\""]
t.spec_files = FileList['spec/**/*_spec.rb'] t.spec_files = set_file_list
t.rcov = true t.rcov = true
t.rcov_opts = lambda do t.rcov_opts = lambda do
IO.readlines(File.dirname(__FILE__) + "/spec/rcov.opts").map {|l| l.chomp.split " "}.flatten IO.readlines(File.dirname(__FILE__) + "/spec/rcov.opts").map {|l| l.chomp.split " "}.flatten
end end
end end
require 'spec/rake/verify_rcov' require 'spec/rake/verify_rcov'
RCov::VerifyTask.new(:verify_rcov => :rcov) do |t| RCov::VerifyTask.new(:verify_rcov => :rcov) do |t|
t.threshold = 97.2 # Make sure you have rcov 0.7 or higher! t.threshold = 97.3 # Make sure you have rcov 0.7 or higher!
end end
remove_task "default" remove_task "default"

View File

@ -6,7 +6,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

@ -12,6 +12,7 @@ module Webrat
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

@ -5,6 +5,7 @@ module Webrat
attr_reader :response attr_reader :response
def get(url, data, headers = nil) def get(url, data, headers = nil)
<<<<<<< HEAD:lib/webrat/merb.rb
@response = request(url, :params => data, :headers => headers, :method => "GET") @response = request(url, :params => data, :headers => headers, :method => "GET")
end end
@ -22,12 +23,40 @@ module Webrat
def response_body def response_body
@response.body @response.body
=======
do_request(url, data, headers, "GET")
end
def post(url, data, headers = nil)
do_request(url, data, headers, "POST")
end
def put(url, data, headers = nil)
do_request(url, data, headers, "PUT")
end
def delete(url, data, headers = nil)
do_request(url, data, headers, "DELETE")
end
def response_body
@response.body.to_s
>>>>>>> 86879c13c05ad0448456575f148495bf700e1d4c:lib/webrat/merb.rb
end end
def response_code def response_code
@response.status @response.status
end end
<<<<<<< HEAD:lib/webrat/merb.rb
=======
protected
def do_request(url, data, headers, method)
@response = request(url, :params => data, :headers => headers, :method => method)
self.get(@response.headers['Location'], nil, @response.headers) if @response.status == 302
end
>>>>>>> 86879c13c05ad0448456575f148495bf700e1d4c:lib/webrat/merb.rb
end end
end end
@ -37,6 +66,7 @@ class Merb::Test::RspecStory
end end
end end
<<<<<<< HEAD:lib/webrat/merb.rb
# #
# class Application < Merb::Controller # class Application < Merb::Controller
@ -57,3 +87,5 @@ require File.join(File.dirname(__FILE__), "merb", "url_encoded_pair_parser.rb")
# #
# #
# #
=======
>>>>>>> 86879c13c05ad0448456575f148495bf700e1d4c:lib/webrat/merb.rb

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

View File

@ -5,18 +5,17 @@ require "spec/interop/test"
# gem install redgreen for colored test output # gem install redgreen for colored test output
begin require "redgreen" unless ENV['TM_CURRENT_LINE']; rescue LoadError; end begin require "redgreen" unless ENV['TM_CURRENT_LINE']; rescue LoadError; end
require File.expand_path(File.dirname(__FILE__) + "/../lib/webrat")
require File.dirname(__FILE__) + "/fakes/test_session"
if ["rails","merb"].include?(ENV["TEST_MODE"]) if ["rails","merb"].include?(ENV["TEST_MODE"])
require File.join(File.dirname(__FILE__), "webrat", "#{ENV["TEST_MODE"]}", "helper.rb") require File.join(File.dirname(__FILE__), "webrat", "#{ENV["TEST_MODE"]}", "helper.rb")
else else
puts "Please set the environment variable TEST_MODE to either 'rails' or 'merb'." puts "Assuming test mode is Rails... for Merb set TEST_MODE=merb and rerun."
$stdout.flush ENV["TEST_MODE"] = 'rails'
exit 1 require File.join(File.dirname(__FILE__), "webrat", "#{ENV["TEST_MODE"]}", "helper.rb")
end end
require File.expand_path(File.dirname(__FILE__) + "/../lib/webrat")
require File.expand_path(File.dirname(__FILE__) + "/../lib/webrat/rails")
require File.dirname(__FILE__) + "/fakes/test_session"
Spec::Runner.configure do |config| Spec::Runner.configure do |config|
# Nothing to configure yet # Nothing to configure yet
end end

View File

@ -1,10 +1,2 @@
require 'merb-core' require 'merb-core'
# module Merb require File.expand_path(File.dirname(__FILE__) + "/../../../lib/webrat/merb")
# module Test
# class RspecStory
# def flunk(message)
# raise message
# end
# end
# end
# end

View File

@ -4,3 +4,4 @@ silence_warnings do
require "action_controller" require "action_controller"
require "action_controller/integration" require "action_controller/integration"
end end
require File.expand_path(File.dirname(__FILE__) + "/../../../lib/webrat/rails")