Module: Merb::Test::RequestHelper
- Included in:
- ControllerHelper, RouteHelper
- Defined in:
- lib/merb-core/test/helpers/request_helper.rb
Defined Under Namespace
Classes: FakeRequest
Instance Method Summary collapse
-
#check_request_for_route(request) ⇒ Object
Checks to see that a request is routable.
-
#delete(path, params = {}, env = {}, &block) ⇒ Object
An HTTP DELETE request that operates through the router.
-
#dispatch_request(request, controller_klass, action) {|controller| ... } ⇒ Object
The workhorse for the dispatch*to helpers.
-
#dispatch_to(controller_klass, action, params = {}, env = {}, &blk) ⇒ Object
Dispatches an action to the given class.
-
#fake_request(env = {}, opt = {}) ⇒ Object
Parameters env<Hash>:: A hash of environment keys to be merged into the default list.
-
#get(path, params = {}, env = {}, &block) ⇒ Object
An HTTP GET request that operates through the router.
-
#post(path, params = {}, env = {}, &block) ⇒ Object
An HTTP POST request that operates through the router.
-
#put(path, params = {}, env = {}, &block) ⇒ Object
An HTTP PUT request that operates through the router.
-
#request(path, params = {}, env = {}, &block) ⇒ Object
A generic request that checks the router for the controller and action.
Instance Method Details
#check_request_for_route(request) ⇒ Object
Checks to see that a request is routable.
Parameters
- request<Merb::Test::FakeRequest, Merb::Request>
-
The request object to inspect.
Raises
- Merb::ControllerExceptions::BadRequest
-
No matching route was found.
Returns
- Hash
-
The parameters built based on the matching route.
247 248 249 250 251 252 253 254 |
# File 'lib/merb-core/test/helpers/request_helper.rb', line 247 def check_request_for_route(request) match = ::Merb::Router.match(request) if match[0].nil? raise ::Merb::ControllerExceptions::BadRequest, "No routes match the request" else match[1] end end |
#delete(path, params = {}, env = {}, &block) ⇒ Object
An HTTP DELETE request that operates through the router
Parameters
- path<String>
-
The path that should go to the router as the request uri.
- params<Hash>
-
An optional hash that will end up as params in the controller instance.
- env<Hash>
-
An optional hash that is passed to the fake request. Any request options should go here (see fake_request).
- &block
-
The block is executed in the context of the controller.
163 164 165 166 |
# File 'lib/merb-core/test/helpers/request_helper.rb', line 163 def delete(path, params = {}, env = {}, &block) env[:request_method] = "DELETE" request(path, params, env, &block) end |
#dispatch_request(request, controller_klass, action) {|controller| ... } ⇒ Object
The workhorse for the dispatch*to helpers.
Parameters
- request<Merb::Test::FakeRequest, Merb::Request>
-
A request object that has been setup for testing.
- controller_klass<Merb::Controller>
-
The class object off the controller to dispatch the action to.
- action<Symbol>
-
The action to dispatch the request to.
- blk<Proc>
-
The block will execute in the context of the controller itself.
Returns
An instance of controller_klass
based on the parameters.
Note
Does not use routes.
224 225 226 227 228 229 230 231 232 233 |
# File 'lib/merb-core/test/helpers/request_helper.rb', line 224 def dispatch_request(request, controller_klass, action, &blk) controller = controller_klass.new(request) yield controller if block_given? controller._dispatch(action) Merb.logger.info controller._benchmarks.inspect Merb.logger.flush controller end |
#dispatch_to(controller_klass, action, params = {}, env = {}, &blk) ⇒ Object
Dispatches an action to the given class. This bypasses the router and is suitable for unit testing of controllers.
Parameters
- controller_klass<Controller>
-
The controller class object that the action should be dispatched to.
- action<Symbol>
-
The action name, as a symbol.
- params<Hash>
-
An optional hash that will end up as params in the controller instance.
- env<Hash>
-
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.
Example
dispatch_to(MyController, :create, :name => 'Homer' ) do
self.stub!(:current_user).and_return(@user)
end
Note
Does not use routes.
99 100 101 102 103 104 105 106 |
# File 'lib/merb-core/test/helpers/request_helper.rb', line 99 def dispatch_to(controller_klass, action, params = {}, env = {}, &blk) action = action.to_s request_body = { :post_body => env[:post_body], :req => env[:req] } request = fake_request(env.merge( :query_string => Merb::Request.params_to_query_string(params)), request_body) dispatch_request(request, controller_klass, action, &blk) end |
#fake_request(env = {}, opt = {}) ⇒ Object
Parameters
- env<Hash>
-
A hash of environment keys to be merged into the default list.
- opt<Hash>
-
A hash of options (see below).
Options (opt)
- :post_body<String>
-
The post body for the request.
- :req<String>
-
The request string. This will only be used if :post_body is left out.
Returns
- FakeRequest
-
A Request object that is built based on the parameters.
Note
If you pass a post body, the content-type will be set to URL-encoded.
62 63 64 65 66 67 68 69 70 |
# File 'lib/merb-core/test/helpers/request_helper.rb', line 62 def fake_request(env = {}, opt = {}) if opt[:post_body] req = opt[:post_body] env[:content_type] ||= "application/x-www-form-urlencoded" else req = opt[:req] end FakeRequest.new(env, StringIO.new(req || '')) end |
#get(path, params = {}, env = {}, &block) ⇒ Object
An HTTP GET request that operates through the router.
Parameters
- path<String>
-
The path that should go to the router as the request uri.
- params<Hash>
-
An optional hash that will end up as params in the controller instance.
- env<Hash>
-
An optional hash that is passed to the fake request. Any request options should go here (see
fake_request
). - &block
-
The block is executed in the context of the controller.
118 119 120 121 |
# File 'lib/merb-core/test/helpers/request_helper.rb', line 118 def get(path, params = {}, env = {}, &block) env[:request_method] = "GET" request(path, params, env, &block) end |
#post(path, params = {}, env = {}, &block) ⇒ Object
An HTTP POST request that operates through the router.
Parameters
- path<String>
-
The path that should go to the router as the request uri.
- params<Hash>
-
An optional hash that will end up as params in the controller instance.
- env<Hash>
-
An optional hash that is passed to the fake request. Any request options should go here (see fake_request).
- &block
-
The block is executed in the context of the controller.
133 134 135 136 |
# File 'lib/merb-core/test/helpers/request_helper.rb', line 133 def post(path, params = {}, env = {}, &block) env[:request_method] = "POST" request(path, params, env, &block) end |
#put(path, params = {}, env = {}, &block) ⇒ Object
An HTTP PUT request that operates through the router.
Parameters
- path<String>
-
The path that should go to the router as the request uri.
- params<Hash>
-
An optional hash that will end up as params in the controller instance.
- env<Hash>
-
An optional hash that is passed to the fake request. Any request options should go here (see fake_request).
- &block
-
The block is executed in the context of the controller.
148 149 150 151 |
# File 'lib/merb-core/test/helpers/request_helper.rb', line 148 def put(path, params = {}, env = {}, &block) env[:request_method] = "PUT" request(path, params, env, &block) end |
#request(path, params = {}, env = {}, &block) ⇒ Object
A generic request that checks the router for the controller and action. This request goes through the Merb::Router and finishes at the controller.
Parameters
- path<String>
-
The path that should go to the router as the request uri.
- params<Hash>
-
An optional hash that will end up as params in the controller instance.
- env<Hash>
-
An optional hash that is passed to the fake request. Any request options should go here (see
fake_request
). - blk<Proc>
-
The block is executed in the context of the controller.
Example
request(path, :create, :name => 'Homer' ) do
self.stub!(:current_user).and_return(@user)
end
Note
Uses Routes.
@semi-public
190 191 192 193 194 195 196 197 198 199 200 201 202 203 |
# File 'lib/merb-core/test/helpers/request_helper.rb', line 190 def request(path, params = {}, env= {}, &block) env[:request_method] ||= "GET" env[:request_uri] = path multipart = env.delete(:test_with_multipart) request = fake_request(env) opts = check_request_for_route(request) # Check that the request will be routed correctly klass = Object.full_const_get(opts.delete(:controller).to_const_string) action = opts.delete(:action).to_s params.merge!(opts) multipart.nil? ? dispatch_to(klass, action, params, env, &block) : dispatch_multipart_to(klass, action, params, env, &block) end |