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
* 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 should_see and should_not_see for verifying HTML response bodys
* 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.
=== Merb
To avoid losing sessions, you need this in environments/test.rb:
Merb::Config.use do |c|
c[:session_store] = 'memory'
end
=== Install
To install the latest release:

View File

@ -35,28 +35,39 @@ def remove_task(task_name)
Rake.application.remove_task(task_name)
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_deps"
desc "Run all specs in spec directory"
Spec::Rake::SpecTask.new do |t|
t.spec_opts = ['--options', "\"#{File.dirname(__FILE__)}/spec/spec.opts\""]
t.spec_files = FileList['spec/**/*_spec.rb']
t.spec_files = set_file_list
end
desc "Run all specs in spec directory with RCov"
Spec::Rake::SpecTask.new(:rcov) do |t|
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_opts = lambda do
IO.readlines(File.dirname(__FILE__) + "/spec/rcov.opts").map {|l| l.chomp.split " "}.flatten
end
end
require 'spec/rake/verify_rcov'
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
remove_task "default"

View File

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

View File

@ -86,11 +86,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

View File

@ -10,7 +10,7 @@ module Webrat
if defined? RAILS_DEFAULT_LOGGER
RAILS_DEFAULT_LOGGER
elsif defined? Merb
Merb.logger
Merb.logger
else
nil
end

View File

@ -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?

View File

@ -5,6 +5,7 @@ module Webrat
attr_reader :response
def get(url, data, headers = nil)
<<<<<<< HEAD:lib/webrat/merb.rb
@response = request(url, :params => data, :headers => headers, :method => "GET")
end
@ -22,12 +23,40 @@ module Webrat
def 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
def response_code
@response.status
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
@ -37,6 +66,7 @@ class Merb::Test::RspecStory
end
end
<<<<<<< HEAD:lib/webrat/merb.rb
#
# 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'
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

View File

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

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,17 +5,16 @@ require "spec/interop/test"
# gem install redgreen for colored test output
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"])
require File.join(File.dirname(__FILE__), "webrat", "#{ENV["TEST_MODE"]}", "helper.rb")
else
puts "Please set the environment variable TEST_MODE to either 'rails' or 'merb'."
$stdout.flush
exit 1
puts "Assuming test mode is Rails... for Merb set TEST_MODE=merb and rerun."
ENV["TEST_MODE"] = 'rails'
require File.join(File.dirname(__FILE__), "webrat", "#{ENV["TEST_MODE"]}", "helper.rb")
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|
# Nothing to configure yet

View File

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

View File

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