Working on moving merb code to be functional with merbs request testing

This commit is contained in:
Rob Kaufman 2008-10-18 00:38:25 -07:00
parent b8e76a1af2
commit 9ed5c9013c
3 changed files with 86 additions and 140 deletions

View File

@ -1,41 +1,55 @@
.gitignore
History.txt
MIT-LICENSE.txt
Manifest.txt
README.txt
Rakefile
TODO.txt
coverage/index.html
coverage/lib-webrat-field_rb.html
coverage/lib-webrat-form_rb.html
coverage/lib-webrat-label_rb.html
coverage/lib-webrat-link_rb.html
coverage/lib-webrat-logging_rb.html
coverage/lib-webrat-page_rb.html
coverage/lib-webrat-redirect_actions_rb.html
coverage/lib-webrat-select_option_rb.html
coverage/lib-webrat_rb.html
init.rb
install.rb
lib/webrat.rb
lib/webrat/field.rb
lib/webrat/form.rb
lib/webrat/label.rb
lib/webrat/link.rb
lib/webrat/logging.rb
lib/webrat/page.rb
lib/webrat/redirect_actions.rb
lib/webrat/select_option.rb
spec/attaches_file_spec.rb
spec/checks_spec.rb
spec/chooses_spec.rb
spec/clicks_button_spec.rb
spec/clicks_link_spec.rb
spec/fills_in_spec.rb
lib/webrat/core.rb
lib/webrat/core/field.rb
lib/webrat/core/flunk.rb
lib/webrat/core/form.rb
lib/webrat/core/label.rb
lib/webrat/core/link.rb
lib/webrat/core/logging.rb
lib/webrat/core/scope.rb
lib/webrat/core/select_option.rb
lib/webrat/core/session.rb
lib/webrat/mechanize.rb
lib/webrat/mechanize/mechanize_session.rb
lib/webrat/merb.rb
lib/webrat/merb/indifferent_access.rb
lib/webrat/merb/param_parser.rb
lib/webrat/merb/support.rb
lib/webrat/merb/url_encoded_pair_parser.rb
lib/webrat/rails.rb
lib/webrat/rails/rails_session.rb
lib/webrat/rails/redirect_actions.rb
lib/webrat/rails/session.rb
lib/webrat/selenium.rb
lib/webrat/selenium/selenium_session.rb
mechanize_spike.rb
selenium_spike.rb
spec/api/attaches_file_spec.rb
spec/api/checks_spec.rb
spec/api/chooses_spec.rb
spec/api/clicks_button_spec.rb
spec/api/clicks_link_spec.rb
spec/api/fills_in_spec.rb
spec/api/reloads_spec.rb
spec/api/save_and_open_spec.rb
spec/api/selects_spec.rb
spec/api/visits_spec.rb
spec/api/within_spec.rb
spec/fakes/test_session.rb
spec/integration/rails_spec.rb
spec/rcov.opts
spec/reloads_spec.rb
spec/save_and_open_page_spec.rb
spec/selects_spec.rb
spec/spec.opts
spec/spec_helper.rb
spec/visits_spec.rb
spec/webrat/core/logging_spec.rb
spec/webrat/core/session_spec.rb
spec/webrat/merb/helper.rb
spec/webrat/rails/helper.rb
spec/webrat/rails/rails_session_spec.rb

View File

@ -174,7 +174,7 @@ module Webrat
else
checkbox_with_same_name = @form.find_field(name, CheckboxField)
if checkbox_with_same_name.to_param.nil?
if checkbox_with_same_name.to_param.blank?
super
else
nil

View File

@ -1,127 +1,59 @@
module Webrat
module MerbTest
#Our own redirect actions defined below, to deal with the fact that we need to store
#a controller reference.
def current_page
@current_page ||= Webrat::Page.new(self)
class Session
include Merb::Test::RequestHelper
attr_reader :response
def get(url, data, headers = nil)
@response = request(url, :params => data, :headers => headers, :method => "GET")
end
def current_page=(new_page)
@current_page = new_page
end
# Issues a GET request for a page, follows any redirects, and verifies the final page
# load was successful.
#
# Example:
# visits "/"
def visits(*args)
@current_page = Webrat::Page.new(self, *args)
end
def save_and_open_page
current_page.save_and_open
end
[:reloads, :fills_in, :clicks_button, :selects, :chooses, :checks, :unchecks, :clicks_link, :clicks_link_within, :clicks_put_link, :clicks_get_link, :clicks_post_link, :clicks_delete_link].each do |method_name|
define_method(method_name) do |*args|
current_page.send(method_name, *args)
end
end
#Session defines the following (used by webrat), but RspecStory doesn't. Merb's get/put/delete return a controller,
#which is where we get our status and response from.
#
#We have to preserve cookies like this, or the session is lost.
#
#While (in a web application) a PUT is modelled as a POST with a parameter _method,
#this close to the metal we need to make sure that we actually hit the underlying 'put' method,
#so we rewrite 'method'.
def request_via_redirect(method,path,parameters={},headers={})
method = parameters["_method"] if !parameters["_method"].blank?
mycookies = defined?(@controller) ? @controller.cookies : nil #will be nil if no requests yet
begin
@controller=self.send(method, path, parameters, headers) do |new_controller|
new_controller.cookies = mycookies
end
rescue => exception
raise unless exception.kind_of?(Merb::ControllerExceptions::Base)
#Now we want to go one level below 'post' to build the request ourselves, then send it to the controller
exception_klass = exception.class
klass = ::Exceptions rescue Merb::Controller
request = fake_request
request.params[:exception] = exception
request.params[:action] = exception_klass.name
@controller=dispatch_request(request, klass, exception_klass.name)
end
follow_redirect! while redirect?
status
def post(url, data, headers = nil)
@response = request(url, :params => data, :headers => headers, :method => "POST")
end
def get_via_redirect(path, parameters = {}, headers = {})
request_via_redirect(:get,path,parameters,headers)
def put(url, data, headers = nil)
@response = request(url, :params => data, :headers => headers, :method => "PUT")
end
def put_via_redirect(path, parameters = {}, headers = {})
request_via_redirect(:put,path,parameters,headers)
def delete(url, data, headers = nil)
@response = request(url, :params => data, :headers => headers, :method => "DELETE")
end
def post_via_redirect(path, parameters = {}, headers = {})
request_via_redirect(:post,path,parameters,headers)
end
def delete_via_redirect(path, parameters = {}, headers = {})
request_via_redirect(:delete,path,parameters,headers)
end
def follow_redirect!
mycookies = @controller.cookies rescue nil
@controller=get @controller.headers["Location"] do |new_controller|
new_controller.cookies=mycookies
end
def response_body
@response.body
end
def redirect?
[307, *(300..305)].include?(status)
def response_code
@response.status
end
def status
@controller.status
end
def response
@controller #things like @controller.body will work.
end
def assert_response(resp)
if resp == :success
response.should be_successful
else
raise "assert_response #{resp.inspect} is not supported"
end
end
end
end
class Application < Merb::Controller
def cookies=(newcookies)
@_cookies = newcookies
class Merb::Test::RspecStory
def browser
@browser ||= Webrat::Session.new
end
end
#Other utilities used by Webrat that are present in Rails but not Merb. We can require heavy dependencies
#here because we're only loaded in Test mode.
require 'strscan'
require 'cgi'
#
# class Application < Merb::Controller
# def cookies=(newcookies)
# @_cookies = newcookies
# end
# end
#
#
# #Other utilities used by Webrat that are present in Rails but not Merb. We can require heavy dependencies
# #here because we're only loaded in Test mode.
# require 'strscan'
# require 'cgi'
require File.join(File.dirname(__FILE__), "merb", "param_parser.rb")
require File.join(File.dirname(__FILE__), "merb", "url_encoded_pair_parser.rb")
require File.join(File.dirname(__FILE__), "merb", "indifferent_access.rb")
require File.join(File.dirname(__FILE__), "merb", "support.rb")
# require File.join(File.dirname(__FILE__), "merb", "indifferent_access.rb")
# require File.join(File.dirname(__FILE__), "merb", "support.rb")
#
#
#