lib/rackbox/rackbox.rb in remi-rackbox-1.1.0 vs lib/rackbox/rackbox.rb in remi-rackbox-1.1.1

- old
+ new

@@ -1,15 +1,85 @@ + # To add blackbox testing to a Rails app, # in your spec_helper.rb # # require 'rackbox' # # Spec::Runner.configure do |config| # config.use_blackbox = true # end # class RackBox + + # i am an rdoc comment on RackBox's eigenclass class << self + + # to turn on some verbosity / logging, set: + # RackBox.verbose = true + attr_accessor :verbose + + # A port of Merb's request() method, used in tests + # + # At the moment, we're using #req instead because #request conflicts + # with an existing RSpec-Rails method + # + # Usage: + # + # req '/' + # req login_path + # req url_for(:controller => 'login') + # + # req '/', :method => :post, :params => { 'chunky' => 'bacon' } + # + # req '/', :data => "some XML data to POST" + # + # TODO take any additional options and pass them along to the environment, so we can say + # req '/', :user_agent => 'some custom user agent' + # + def req app_or_request, url, options = {} + puts "RackBox#request url:#{ url.inspect }, options:#{ options.inspect }" if RackBox.verbose + + # need to find the request or app + mock_request = nil + if app_or_request.is_a? Rack::MockRequest + mock_request = app_or_request + elsif app_or_request.nil? + if RackBox.app.nil? + raise "Not sure howto to execute a request against app or request: #{ app_or_request.inspect }" + else + mock_request = Rack::MockRequest.new(RackBox.app) # default to RackBox.app if nil + end + elsif app_or_request.respond_to? :call + mock_request = Rack::MockRequest.new(app_or_request) + else + raise "Not sure howto to execute a request against app or request: #{ app_or_request.inspect }" + end + + options[:method] ||= ( options[:params] || options[:data] ) ? :post : :get # if params, default to POST, else default to GET + options[:params] ||= { } + + if options[:data] + # input should be the data we're likely POSTing ... this overrides any params + input = options[:data] + else + # input should be params, if any + input = RackBox.build_query options[:params] + end + + headers = options.dup + headers.delete :data if headers[:data] + headers.delete :params if headers[:params] + headers.delete :method if headers[:method] + + # merge input + headers[:input] = input + + puts " requesting #{ options[:method].to_s.upcase } #{ url.inspect } #{ headers.inspect }" if RackBox.verbose + mock_request.send options[:method], url, headers + end + + alias request req unless defined? request + # the Rack appliction to do 'Black Box' testing against # # To set, in your spec_helper.rb or someplace: # RackBox.app = Rack::Adapter::Rails.new :root => '/root/directory/of/rails/app', :environment => 'test' #