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 History.txt
MIT-LICENSE.txt MIT-LICENSE.txt
Manifest.txt Manifest.txt
README.txt README.txt
Rakefile Rakefile
TODO.txt 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 init.rb
install.rb install.rb
lib/webrat.rb lib/webrat.rb
lib/webrat/field.rb lib/webrat/core.rb
lib/webrat/form.rb lib/webrat/core/field.rb
lib/webrat/label.rb lib/webrat/core/flunk.rb
lib/webrat/link.rb lib/webrat/core/form.rb
lib/webrat/logging.rb lib/webrat/core/label.rb
lib/webrat/page.rb lib/webrat/core/link.rb
lib/webrat/redirect_actions.rb lib/webrat/core/logging.rb
lib/webrat/select_option.rb lib/webrat/core/scope.rb
spec/attaches_file_spec.rb lib/webrat/core/select_option.rb
spec/checks_spec.rb lib/webrat/core/session.rb
spec/chooses_spec.rb lib/webrat/mechanize.rb
spec/clicks_button_spec.rb lib/webrat/mechanize/mechanize_session.rb
spec/clicks_link_spec.rb lib/webrat/merb.rb
spec/fills_in_spec.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/rcov.opts
spec/reloads_spec.rb
spec/save_and_open_page_spec.rb
spec/selects_spec.rb
spec/spec.opts spec/spec.opts
spec/spec_helper.rb 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 else
checkbox_with_same_name = @form.find_field(name, CheckboxField) 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 super
else else
nil nil

View File

@ -1,127 +1,59 @@
module Webrat module Webrat
module MerbTest class Session
include Merb::Test::RequestHelper
#Our own redirect actions defined below, to deal with the fact that we need to store attr_reader :response
#a controller reference.
def current_page def get(url, data, headers = nil)
@current_page ||= Webrat::Page.new(self) @response = request(url, :params => data, :headers => headers, :method => "GET")
end end
def current_page=(new_page) def post(url, data, headers = nil)
@current_page = new_page @response = request(url, :params => data, :headers => headers, :method => "POST")
end end
# Issues a GET request for a page, follows any redirects, and verifies the final page def put(url, data, headers = nil)
# load was successful. @response = request(url, :params => data, :headers => headers, :method => "PUT")
end
def delete(url, data, headers = nil)
@response = request(url, :params => data, :headers => headers, :method => "DELETE")
end
def response_body
@response.body
end
def response_code
@response.status
end
end
end
class Merb::Test::RspecStory
def browser
@browser ||= Webrat::Session.new
end
end
# #
# Example: # class Application < Merb::Controller
# visits "/" # def cookies=(newcookies)
def visits(*args) # @_cookies = newcookies
@current_page = Webrat::Page.new(self, *args) # end
end # 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, # #Other utilities used by Webrat that are present in Rails but not Merb. We can require heavy dependencies
#this close to the metal we need to make sure that we actually hit the underlying 'put' method, # #here because we're only loaded in Test mode.
#so we rewrite 'method'. # require 'strscan'
def request_via_redirect(method,path,parameters={},headers={}) # require 'cgi'
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
end
def get_via_redirect(path, parameters = {}, headers = {})
request_via_redirect(:get,path,parameters,headers)
end
def put_via_redirect(path, parameters = {}, headers = {})
request_via_redirect(:put,path,parameters,headers)
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
end
def redirect?
[307, *(300..305)].include?(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
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", "param_parser.rb")
require File.join(File.dirname(__FILE__), "merb", "url_encoded_pair_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", "indifferent_access.rb")
require File.join(File.dirname(__FILE__), "merb", "support.rb") # require File.join(File.dirname(__FILE__), "merb", "support.rb")
#
#
#