Prepares and returns a request suitable for dispatching with dispatch_request. If you don’t need to modify the request object before dispatching (e.g. to add cookies), you probably want to use dispatch_to instead.
params | An optional hash that will end up as params in the controller instance. |
env | An optional hash that is passed to the fake request. Any request options should go here (see fake_request), including :req or :post_body for setting the request body itself. |
req = build_request(:id => 1) req.cookies['app_cookie'] = "testing" dispatch_request(req, MyController, :edit)
Does not use routes.
:api: public @deprecated
# File lib/merb-core/test/helpers/mock_request_helper.rb, line 217 217: def build_request(params = {}, env = {}) 218: params = Merb::Parse.params_to_query_string(params) 219: 220: query_string = env[:query_string] || env['QUERY_STRING'] 221: env[:query_string] = query_string ? "#{query_string}&#{params}" : params 222: 223: post_body = env[:post_body] || env['POST_BODY'] 224: fake_request(env, { :post_body => post_body, :req => env[:req] }) 225: end
Checks to see that a request is routable.
request<Merb::Test::RequestHelper::FakeRequest, Merb::Request> | The request object to inspect. |
Merb::ControllerExceptions::BadRequest | No matching route was found. |
Hash | The parameters built based on the matching route. |
:api: plugin @deprecated
# File lib/merb-core/test/helpers/mock_request_helper.rb, line 395 395: def check_request_for_route(request) 396: match = ::Merb::Router.match(request) 397: if match[0].nil? && match[1].empty? 398: raise ::Merb::ControllerExceptions::BadRequest, "No routes match the request. Request uri: #{request.uri}" 399: else 400: match[1] 401: end 402: end
An HTTP DELETE request that operates through the router
path | The path that should go to the router as the request uri. |
params | An optional hash that will end up as params in the controller instance. |
env | An optional hash that is passed to the fake request. Any request options should go here (see fake_request). |
&blk | The controller is yielded to the block provided for actions prior to the action being dispatched. |
:api: public @deprecated
# File lib/merb-core/test/helpers/mock_request_helper.rb, line 301 301: def delete(path, params = {}, env = {}, &block) 302: env[:request_method] = "DELETE" 303: mock_request(path, params, env, &block) 304: end
# File lib/merb-core/test/helpers/request_helper.rb, line 76 76: def describe_input(input) 77: if input.respond_to?(:controller_name) 78: "#{input.controller_name}##{input.action_name}" 79: elsif input.respond_to?(:original_env) 80: describe_request(input) 81: else 82: input 83: end 84: end
# File lib/merb-core/test/helpers/request_helper.rb, line 72 72: def describe_request(rack) 73: "a #{rack.original_env[:method] || rack.original_env["REQUEST_METHOD"] || "GET"} to '#{rack.url}'" 74: end
The workhorse for the dispatch*to helpers.
request<Merb::Test::RequestHelper::FakeRequest, Merb::Request> | A request object that has been setup for testing. |
controller_klass | The class object off the controller to dispatch the action to. |
action | The action to dispatch the request to. |
&blk | The controller is yielded to the block provided for actions prior to the action being dispatched. |
An instance of controller_klass based on the parameters.
Does not use routes.
:api: public @deprecated
# File lib/merb-core/test/helpers/mock_request_helper.rb, line 369 369: def dispatch_request(request, controller_klass, action, &blk) 370: controller = controller_klass.new(request) 371: yield controller if block_given? 372: controller._dispatch(action) 373: 374: Merb.logger.info controller._benchmarks.inspect 375: Merb.logger.flush 376: 377: controller 378: end
Dispatches an action to the given class. This bypasses the router and is suitable for unit testing of controllers.
controller_klass | The controller class object that the action should be dispatched to. |
action | The action name, as a symbol. |
params | An optional hash that will end up as params in the controller instance. |
env | An optional hash that is passed to the fake request. Any request options should go here (see fake_request), including :req or :post_body for setting the request body itself. |
&blk | The controller is yielded to the block provided for actions prior to the action being dispatched. |
dispatch_to(MyController, :create, :name => 'Homer' ) do |controller| controller.stub!(:current_user).and_return(@user) end
Does not use routes.
:api: public @deprecated
# File lib/merb-core/test/helpers/mock_request_helper.rb, line 121 121: def dispatch_to(controller_klass, action, params = {}, env = {}, &blk) 122: params = merge_controller_and_action(controller_klass, action, params) 123: dispatch_request(build_request(params, env), controller_klass, action.to_s, &blk) 124: end
Dispatches an action to the given class and using HTTP Basic Authentication This bypasses the router and is suitable for unit testing of controllers.
controller_klass | The controller class object that the action should be dispatched to. |
action | The action name, as a symbol. |
username | The username. |
password | The password. |
params | An optional hash that will end up as params in the controller instance. |
env | An optional hash that is passed to the fake request. Any request options should go here (see fake_request), including :req or :post_body for setting the request body itself. |
&blk | The controller is yielded to the block provided for actions prior to the action being dispatched. |
dispatch_with_basic_authentication_to(MyController, :create, 'Fred', 'secret', :name => 'Homer' ) do |controller| controller.stub!(:current_user).and_return(@user) end
Does not use routes.
:api: public @deprecated
# File lib/merb-core/test/helpers/mock_request_helper.rb, line 179 179: def dispatch_with_basic_authentication_to(controller_klass, action, username, password, params = {}, env = {}, &blk) 180: env["X_HTTP_AUTHORIZATION"] = "Basic #{Base64.encode64("#{username}:#{password}")}" 181: 182: params = merge_controller_and_action(controller_klass, action, params) 183: dispatch_request(build_request(params, env), controller_klass, action.to_s, &blk) 184: end
env | A hash of environment keys to be merged into the default list. |
opt | A hash of options (see below). |
:post_body | The post body for the request. |
:req | The request string. This will only be used if :post_body is left out. |
FakeRequest | A Request object that is built based on the parameters. |
If you pass a post body, the content-type will be set to URL-encoded.
:api: public @deprecated
# File lib/merb-core/test/helpers/mock_request_helper.rb, line 84 84: def fake_request(env = {}, opt = {}) 85: if opt[:post_body] 86: req = opt[:post_body] 87: env[:content_type] ||= "application/x-www-form-urlencoded" 88: else 89: req = opt[:req] 90: end 91: FakeRequest.new(env, StringIO.new(req || '')) 92: end
An HTTP GET request that operates through the router.
path | The path that should go to the router as the request uri. |
params | An optional hash that will end up as params in the controller instance. |
env | An optional hash that is passed to the fake request. Any request options should go here (see fake_request). |
&blk | The controller is yielded to the block provided for actions prior to the action being dispatched. |
:api: public @deprecated
# File lib/merb-core/test/helpers/mock_request_helper.rb, line 242 242: def get(path, params = {}, env = {}, &block) 243: env[:request_method] = "GET" 244: mock_request(path, params, env, &block) 245: end
:api: private
# File lib/merb-core/test/helpers/mock_request_helper.rb, line 187 187: def merge_controller_and_action(controller_klass, action, params) 188: params[:controller] = controller_klass.name.to_const_path 189: params[:action] = action.to_s 190: 191: params 192: end
A generic request that checks the router for the controller and action. This request goes through the Merb::Router and finishes at the controller.
path | The path that should go to the router as the request uri. |
params | An optional hash that will end up as params in the controller instance. |
env | An optional hash that is passed to the fake request. Any request options should go here (see fake_request). |
&blk | The controller is yielded to the block provided for actions prior to the action being dispatched. |
request(path, { :name => 'Homer' }, { :request_method => "PUT" }) do |controller| controller.stub!(:current_user).and_return(@user) end
Uses Routes.
:api: plugin @deprecated
# File lib/merb-core/test/helpers/mock_request_helper.rb, line 330 330: def mock_request(path, params = {}, env= {}, &block) 331: env[:request_method] ||= "GET" 332: env[:request_uri], env[:query_string] = path.split('?') 333: 334: multipart = env.delete(:test_with_multipart) 335: 336: request = build_request(params, env) 337: 338: opts = check_request_for_route(request) # Check that the request will be routed correctly 339: controller_name = (opts[:namespace] ? opts.delete(:namespace) + '/' : '') + opts.delete(:controller) 340: klass = Object.full_const_get(controller_name.snake_case.to_const_string) 341: 342: action = opts.delete(:action).to_s 343: params.merge!(opts) 344: 345: multipart.nil? ? dispatch_to(klass, action, params, env, &block) : dispatch_multipart_to(klass, action, params, env, &block) 346: end
An HTTP POST request that operates through the router.
path | The path that should go to the router as the request uri. |
params | An optional hash that will end up as params in the controller instance. |
env | An optional hash that is passed to the fake request. Any request options should go here (see fake_request). |
&blk | The controller is yielded to the block provided for actions prior to the action being dispatched. |
:api: public @deprecated
# File lib/merb-core/test/helpers/mock_request_helper.rb, line 262 262: def post(path, params = {}, env = {}, &block) 263: env[:request_method] = "POST" 264: mock_request(path, params, env, &block) 265: end
An HTTP PUT request that operates through the router.
path | The path that should go to the router as the request uri. |
params | An optional hash that will end up as params in the controller instance. |
env | An optional hash that is passed to the fake request. Any request options should go here (see fake_request). |
&blk | The controller is yielded to the block provided for actions prior to the action being dispatched. |
:api: public
# File lib/merb-core/test/helpers/mock_request_helper.rb, line 281 281: def put(path, params = {}, env = {}, &block) 282: env[:request_method] = "PUT" 283: mock_request(path, params, env, &block) 284: end
# File lib/merb-core/test/helpers/request_helper.rb, line 90 90: def requesting(*args) request(*args) end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.