0
+ # FakeRequest sets up a default enviroment which can be overridden either
0
+ # by passing and env into initialize or using request['HTTP_VAR'] = 'foo'
0
+ class FakeRequest < Request
0
+ # env<Hash>:: Environment options that override the defaults.
0
+ # req<StringIO>:: The request to set as input for Rack.
0
+ def initialize(env = {}, req = StringIO.new)
0
+ env.environmentize_keys!
0
+ env['rack.input'] = req
0
+ super(DEFAULT_ENV.merge(env))
0
+ DEFAULT_ENV = Mash.new({
0
+ 'SERVER_NAME' => 'localhost',
0
+ 'HTTP_ACCEPT_ENCODING' => 'gzip,deflate',
0
+ 'HTTP_USER_AGENT' => 'Mozilla/5.0 (Macintosh; U; PPC Mac OS X Mach-O; en-US; rv:1.8.0.1) Gecko/20060214 Camino/1.0',
0
+ 'SERVER_PROTOCOL' => 'HTTP/1.1',
0
+ 'HTTP_CACHE_CONTROL' => 'max-age=0',
0
+ 'HTTP_ACCEPT_LANGUAGE' => 'en,ja;q=0.9,fr;q=0.9,de;q=0.8,es;q=0.7,it;q=0.7,nl;q=0.6,sv;q=0.5,nb;q=0.5,da;q=0.4,fi;q=0.3,pt;q=0.3,zh-Hans;q=0.2,zh-Hant;q=0.1,ko;q=0.1',
0
+ 'HTTP_HOST' => 'localhost',
0
+ 'REMOTE_ADDR' => '127.0.0.1',
0
+ 'SERVER_SOFTWARE' => 'Mongrel 1.1',
0
+ 'HTTP_KEEP_ALIVE' => '300',
0
+ 'HTTP_REFERER' => 'http://localhost/',
0
+ 'HTTP_ACCEPT_CHARSET' => 'ISO-8859-1,utf-8;q=0.7,*;q=0.7',
0
+ 'HTTP_VERSION' => 'HTTP/1.1',
0
+ 'SERVER_PORT' => '80',
0
+ 'GATEWAY_INTERFACE' => 'CGI/1.2',
0
+ 'HTTP_ACCEPT' => 'text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5',
0
+ 'HTTP_CONNECTION' => 'keep-alive',
0
+ 'REQUEST_METHOD' => 'GET'
0
+ }) unless defined?(DEFAULT_ENV)
0
+ # env<Hash>:: A hash of environment keys to be merged into the default list.
0
+ # opt<Hash>:: A hash of options (see below).
0
+ # :post_body<String>:: The post body for the request.
0
+ # The request string. This will only be used if :post_body is left out.
0
+ # FakeRequest:: A Request object that is built based on the parameters.
0
+ # If you pass a post body, the content-type will be set to URL-encoded.
0
+ def fake_request(env = {}, opt = {})
0
+ env[:content_type] ||= "application/x-www-form-urlencoded"
0
+ FakeRequest.new(env, req ? StringIO.new(req) : nil)
0
+ # Dispatches an action to the given class. This bypasses the router and is
0
+ # suitable for unit testing of controllers.
0
+ # controller_klass<Controller>::
0
+ # The controller class object that the action should be dispatched to.
0
+ # action<Symbol>:: The action name, as a symbol.
0
+ # An optional hash that will end up as params in the controller instance.
0
+ # An optional hash that is passed to the fake request. Any request options
0
+ # should go here (see +fake_request+).
0
+ # The controller is yielded to the block provided for actions *prior* to
0
+ # the action being dispatched.
0
+ # dispatch_to(MyController, :create, :name => 'Homer' ) do
0
+ # self.stub!(:current_user).and_return(@user)
0
+ # Does not use routes.
0
+ def dispatch_to(controller_klass, action, params = {}, env = {}, &blk)
0
+ request = fake_request(env.merge(
0
+ :query_string => Merb::Request.params_to_query_string(params)))
0
+ dispatch_request(request, controller_klass, action, &blk)
0
+ # An HTTP GET request that operates through the router.
0
+ # path<String>:: The path that should go to the router as the request uri.
0
+ # An optional hash that will end up as params in the controller instance.
0
+ # An optional hash that is passed to the fake request. Any request options
0
+ # should go here (see +fake_request+).
0
+ # &block:: The block is executed in the context of the controller.
0
+ def get(path, params = {}, env = {}, &block)
0
+ env[:request_method] = "GET"
0
+ request(path, params, env, &block)
0
+ # An HTTP POST request that operates through the router.
0
+ # path<String>:: The path that should go to the router as the request uri.
0
+ # An optional hash that will end up as params in the controller instance.
0
+ # An optional hash that is passed to the fake request. Any request options
0
+ # should go here (see fake_request).
0
+ # &block:: The block is executed in the context of the controller.
0
+ def post(path, params = {}, env = {}, &block)
0
+ env[:request_method] = "POST"
0
+ request(path, params, env, &block)
0
+ # An HTTP PUT request that operates through the router.
0
+ # path<String>:: The path that should go to the router as the request uri.
0
+ # An optional hash that will end up as params in the controller instance.
0
+ # An optional hash that is passed to the fake request. Any request options
0
+ # should go here (see fake_request).
0
+ # &block:: The block is executed in the context of the controller.
0
+ def put(path, params = {}, env = {}, &block)
0
+ env[:request_method] = "PUT"
0
+ request(path, params, env, &block)
0
+ # An HTTP DELETE request that operates through the router
0
+ # path<String>:: The path that should go to the router as the request uri.
0
+ # An optional hash that will end up as params in the controller instance.
0
+ # An optional hash that is passed to the fake request. Any request options
0
+ # should go here (see fake_request).
0
+ # &block:: The block is executed in the context of the controller.
0
+ def delete(path, params = {}, env = {}, &block)
0
+ env[:request_method] = "DELETE"
0
+ request(path, params, env, &block)
0
+ # A generic request that checks the router for the controller and action.
0
+ # This request goes through the Merb::Router and finishes at the controller.
0
+ # path<String>:: The path that should go to the router as the request uri.
0
+ # An optional hash that will end up as params in the controller instance.
0
+ # An optional hash that is passed to the fake request. Any request options
0
+ # should go here (see +fake_request+).
0
+ # blk<Proc>:: The block is executed in the context of the controller.
0
+ # request(path, :create, :name => 'Homer' ) do
0
+ # self.stub!(:current_user).and_return(@user)
0
+ def request(path, params = {}, env= {}, &block)
0
+ env[:request_method] ||= "GET"
0
+ env[:request_uri] = path
0
+ multipart = env.delete(:test_with_multipart)
0
+ request = fake_request(env)
0
+ opts = check_request_for_route(request) # Check that the request will be routed correctly
0
+ klass = Object.full_const_get(opts.delete(:controller).to_const_string)
0
+ action = opts.delete(:action).to_s
0
+ multipart.nil? ? dispatch_to(klass, action, params, env, &block) : dispatch_multipart_to(klass, action, params, env, &block)
0
+ # The workhorse for the dispatch*to helpers.
0
+ # request<Merb::Test::FakeRequest, Merb::Request>::
0
+ # A request object that has been setup for testing.
0
+ # controller_klass<Merb::Controller>::
0
+ # The class object off the controller to dispatch the action to.
0
+ # action<Symbol>:: The action to dispatch the request to.
0
+ # blk<Proc>:: The block will execute in the context of the controller itself.
0
+ # An instance of +controller_klass+ based on the parameters.
0
+ # Does not use routes.
0
+ def dispatch_request(request, controller_klass, action, &blk)
0
+ controller = controller_klass.new(request)
0
+ yield controller if block_given?
0
+ controller._dispatch(action)
0
+ Merb.logger.info controller._benchmarks.inspect
0
+ # Checks to see that a request is routable.
0
+ # request<Merb::Test::FakeRequest, Merb::Request>::
0
+ # The request object to inspect.
0
+ # Merb::ControllerExceptions::BadRequest::
0
+ # No matching route was found.
0
+ # Hash:: The parameters built based on the matching route.
0
+ def check_request_for_route(request)
0
+ match = ::Merb::Router.match(request)
0
+ raise ::Merb::ControllerExceptions::BadRequest, "No routes match the request"