### Home Classes Methods

### In Files

- lib/merb-core/test/helpers/mock_request_helper.rb
- lib/merb-core/test/helpers/request_helper.rb

### Namespace

- CLASS Merb::Test::RequestHelper::CookieJar
- CLASS Merb::Test::RequestHelper::FakeRequest

### Methods

- #build_request
- #check_request_for_route
- #delete
- #describe_input
- #describe_request
- #dispatch_request
- #dispatch_to
- #dispatch_with_basic_authentication_to
- #fake_request
- #get
- #merge_controller_and_action
- #mock_request
- #post
- #put
- #requesting
- #response_for
- #status_code
- #with_cookies

### Included Modules

- Merb::Test::MakeRequest

### Files

- CHANGELOG
- LICENSE
- README
- TODO

### Class/Module Index

Quicksearch

- Merb
- Merb::AbstractController
- Merb::AcceptType
- Merb::AuthenticationMixin
- Merb::AuthenticationMixin::BasicAuthentication
- Merb::BootLoader
- Merb::BootLoader::AfterAppLoads
- Merb::BootLoader::BackgroundServices
- Merb::BootLoader::BeforeAppLoads
- Merb::BootLoader::BuildFramework
- Merb::BootLoader::ChooseAdapter
- Merb::BootLoader::Cookies
- Merb::BootLoader::Defaults
- Merb::BootLoader::Dependencies
- Merb::BootLoader::DropPidFile
- Merb::BootLoader::LoadClasses
- Merb::BootLoader::Logger
- Merb::BootLoader::MimeTypes
- Merb::BootLoader::MixinSession
- Merb::BootLoader::RackUpApplication
- Merb::BootLoader::ReloadClasses
- Merb::BootLoader::ReloadClasses::TimedExecutor
- Merb::BootLoader::Router
- Merb::BootLoader::SetupSession
- Merb::BootLoader::SetupStubClasses
- Merb::BootLoader::Templates
- Merb::ConditionalGetMixin
- Merb::Config
- Merb::Config::ConfigBlock
- Merb::Const
- Merb::Controller
- Merb::ControllerExceptions
- Merb::ControllerExceptions::Accepted
- Merb::ControllerExceptions::ActionNotFound
- Merb::ControllerExceptions::BadGateway
- Merb::ControllerExceptions::BadRequest
- Merb::ControllerExceptions::Base
- Merb::ControllerExceptions::ClientError
- Merb::ControllerExceptions::Conflict
- Merb::ControllerExceptions::Continue
- Merb::ControllerExceptions::Created
- Merb::ControllerExceptions::ExpectationFailed
- Merb::ControllerExceptions::Forbidden
- Merb::ControllerExceptions::GatewayTimeout
- Merb::ControllerExceptions::Gone
- Merb::ControllerExceptions::HTTPVersionNotSupported
- Merb::ControllerExceptions::Informational
- Merb::ControllerExceptions::InternalServerError
- Merb::ControllerExceptions::LayoutNotFound
- Merb::ControllerExceptions::LengthRequired
- Merb::ControllerExceptions::MethodNotAllowed
- Merb::ControllerExceptions::MovedPermanently
- Merb::ControllerExceptions::MovedTemporarily
- Merb::ControllerExceptions::MultiPartParseError
- Merb::ControllerExceptions::MultipleChoices
- Merb::ControllerExceptions::NoContent
- Merb::ControllerExceptions::NonAuthoritativeInformation
- Merb::ControllerExceptions::NotAcceptable
- Merb::ControllerExceptions::NotFound
- Merb::ControllerExceptions::NotImplemented
- Merb::ControllerExceptions::NotModified
- Merb::ControllerExceptions::OK
- Merb::ControllerExceptions::PartialContent
- Merb::ControllerExceptions::PaymentRequired
- Merb::ControllerExceptions::PreconditionFailed
- Merb::ControllerExceptions::ProxyAuthenticationRequired
- Merb::ControllerExceptions::Redirection
- Merb::ControllerExceptions::RequestEntityTooLarge
- Merb::ControllerExceptions::RequestRangeNotSatisfiable
- Merb::ControllerExceptions::RequestTimeout
- Merb::ControllerExceptions::RequestURITooLarge
- Merb::ControllerExceptions::ResetContent
- Merb::ControllerExceptions::SeeOther
- Merb::ControllerExceptions::ServerError
- Merb::ControllerExceptions::ServiceUnavailable
- Merb::ControllerExceptions::Successful
- Merb::ControllerExceptions::SwitchingProtocols
- Merb::ControllerExceptions::TemplateNotFound
- Merb::ControllerExceptions::TemporaryRedirect
- Merb::ControllerExceptions::Unauthorized
- Merb::ControllerExceptions::UnsupportedMediaType
- Merb::ControllerExceptions::UseProxy
- Merb::ControllerMixin
- Merb::CookieSession
- Merb::CookieSession::CookieOverflow
- Merb::CookieSession::TamperedWithCookie
- Merb::Cookies
- Merb::CookiesMixin
- Merb::CookiesMixin::RequestMixin
- Merb::Counter
- Merb::Dispatcher
- Merb::Dispatcher::DefaultException
- Merb::Dispatcher::DefaultExceptionHelper
- Merb::GlobalHelpers
- Merb::InlineTemplates
- Merb::Logger
- Merb::MemcacheSession
- Merb::MemcacheStore
- Merb::MemorySession
- Merb::MemorySessionStore
- Merb::Parse
- Merb::Plugins
- Merb::Rack
- Merb::Rack::AbstractAdapter
- Merb::Rack::Adapter
- Merb::Rack::Application
- Merb::Rack::ConditionalGet
- Merb::Rack::Console
- Merb::Rack::ContentLength
- Merb::Rack::DeferrableMiddleware
- Merb::Rack::Ebb
- Merb::Rack::EventedMongrel
- Merb::Rack::FastCGI
- Merb::Rack::Handler
- Merb::Rack::Handler::Mongrel
- Merb::Rack::Head
- Merb::Rack::Helpers
- Merb::Rack::Irb
- Merb::Rack::Middleware
- Merb::Rack::Mongrel
- Merb::Rack::PathPrefix
- Merb::Rack::Profiler
- Merb::Rack::Runner
- Merb::Rack::Static
- Merb::Rack::StreamWrapper
- Merb::Rack::SwiftipliedMongrel
- Merb::Rack::Thin
- Merb::Rack::ThinTurbo
- Merb::Rack::Tracer
- Merb::Rack::WEBrick
- Merb::RenderMixin
- Merb::RenderMixin::ClassMethods
- Merb::Request
- Merb::ReservedError
- Merb::Responder
- Merb::ResponderMixin
- Merb::ResponderMixin::ClassMethods
- Merb::ResponderMixin::ContentTypeAlreadySet
- Merb::Router
- Merb::Router::Behavior
- Merb::Router::Behavior::Error
- Merb::Router::Behavior::Proxy
- Merb::Router::CachedProc
- Merb::Router::GenerationError
- Merb::Router::NotCompiledError
- Merb::Router::Resources
- Merb::Router::RouteNotFound
- Merb::Server
- Merb::Session
- Merb::SessionContainer
- Merb::SessionMixin
- Merb::SessionMixin::NoSessionContainer
- Merb::SessionMixin::RequestMixin
- Merb::SessionMixin::RequestMixin::ClassMethods
- Merb::SessionMixin::SessionOverflow
- Merb::SessionStoreContainer
- Merb::System
- Merb::System::PortablePoller
- Merb::Template
- Merb::Template::Erubis
- Merb::Template::Erubis::Mixin
- Merb::Test
- Merb::Test::ControllerHelper
- Merb::Test::Cookie
- Merb::Test::CookieJar
- Merb::Test::ExampleGroup
- Merb::Test::Helpers
- Merb::Test::MakeRequest
- Merb::Test::Matchers
- Merb::Test::MultipartRequestHelper
- Merb::Test::MultipartRequestHelper::FileParam
- Merb::Test::MultipartRequestHelper::Param
- Merb::Test::MultipartRequestHelper::Post
- Merb::Test::RequestHelper
- Merb::Test::RequestHelper::CookieJar
- Merb::Test::RequestHelper::FakeRequest
- Merb::Test::RouteHelper
- Merb::Test::Rspec
- Merb::Test::Rspec::ControllerMatchers
- Merb::Test::Rspec::ControllerMatchers::BeError
- Merb::Test::Rspec::ControllerMatchers::BeRedirect
- Merb::Test::Rspec::ControllerMatchers::Provide
- Merb::Test::Rspec::RouteMatchers
- Merb::Test::Rspec::RouteMatchers::ParameterMatcher
- Merb::Test::Rspec::RouteMatchers::RouteToMatcher
- Merb::Test::ViewHelper
- Merb::Test::WebratHelper
- Merb::Worker
- Erubis
- Erubis::Basic
- Erubis::Basic::Converter
- Erubis::BlockAwareEnhancer
- Erubis::BlockAwareEruby
- Erubis::MEruby
- Spec
- Spec::Matchers
- Spec::Matchers::MatcherDSL
- Spec::Runner
- Spec::Runner::Formatter
- Spec::Runner::Formatter::BaseTextFormatter
- Gem
- Gem::Dependency
- Hpricot
- Hpricot::Elem
- Mongrel
- Mongrel::HttpResponse
- Class
- Exception
- Hash
- Kernel
- MemCache
- Memcached
- Object
- String
- Time

No matching classes.

# Merb::Test::RequestHelper

### Public Instance Methods

build_request(params = {}, env = {}) click to toggle source

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.

#### Parameters

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.

#### Example

    req = build_request(:id => 1)
    req.cookies['app_cookie'] = "testing"
    dispatch_request(req, MyController, :edit)

#### Notes

Does not use routes.

:api: public @deprecated

    # File lib/merb-core/test/helpers/mock_request_helper.rb, line 217
    def build_request(params = {}, env = {})
      params             = Merb::Parse.params_to_query_string(params)

      query_string = env[:query_string] || env['QUERY_STRING']
      env[:query_string] = query_string ? "#{query_string}&#{params}" : params

      post_body = env[:post_body] || env['POST_BODY']
      fake_request(env, { :post_body => post_body, :req => env[:req] })
    end

check_request_for_route(request) click to toggle source

Checks to see that a request is routable.

#### Parameters

request<Merb::Test::RequestHelper::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.

:api: plugin @deprecated

    # File lib/merb-core/test/helpers/mock_request_helper.rb, line 395
    def check_request_for_route(request)
      match =  ::Merb::Router.match(request)
      if match[0].nil? && match[1].empty?
        raise ::Merb::ControllerExceptions::BadRequest, "No routes match the request. Request uri: #{request.uri}"
      else
        match[1]
      end
    end

delete(path, params = {}, env = {}, &block) click to toggle source

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).
&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
    def delete(path, params = {}, env = {}, &block)
      env[:request_method] = "DELETE"
      mock_request(path, params, env, &block)
    end

describe_input(input) click to toggle source

    # File lib/merb-core/test/helpers/request_helper.rb, line 76
    def describe_input(input)
      if input.respond_to?(:controller_name)
        "#{input.controller_name}##{input.action_name}"
      elsif input.respond_to?(:original_env)
        describe_request(input)
      else
        input
      end
    end

describe_request(rack) click to toggle source

    # File lib/merb-core/test/helpers/request_helper.rb, line 72
    def describe_request(rack)
      "a #{rack.original_env[:method] || rack.original_env["REQUEST_METHOD"] || "GET"} to '#{rack.url}'"
    end

dispatch_request(request, controller_klass, action, &blk) click to toggle source

The workhorse for the dispatch*to helpers.

#### Parameters

request<Merb::Test::RequestHelper::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 | The controller is yielded to the block provided for actions prior to the action being dispatched.

#### Returns

An instance of `controller_klass` based on the parameters.

#### Notes

Does not use routes.

:api: public @deprecated

    # File lib/merb-core/test/helpers/mock_request_helper.rb, line 369
    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) click to toggle source

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 |controller|
      controller.stub!(:current_user).and_return(@user)
    end

#### Notes

Does not use routes.

:api: public @deprecated

    # File lib/merb-core/test/helpers/mock_request_helper.rb, line 121
    def dispatch_to(controller_klass, action, params = {}, env = {}, &blk)
      params = merge_controller_and_action(controller_klass, action, params)
      dispatch_request(build_request(params, env), controller_klass, action.to_s, &blk)
    end

dispatch_with_basic_authentication_to(controller_klass, action, username, password, params = {}, env = {}, &blk) click to toggle source

Dispatches an action to the given class and using HTTP Basic Authentication 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.
username<String> | The username.
password<String> | The password.
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_with_basic_authentication_to(MyController, :create, 'Fred', 'secret', :name => 'Homer' ) do |controller|
      controller.stub!(:current_user).and_return(@user)
    end

#### Notes

Does not use routes.

:api: public @deprecated

    # File lib/merb-core/test/helpers/mock_request_helper.rb, line 179
    def dispatch_with_basic_authentication_to(controller_klass, action, username, password, params = {}, env = {}, &blk)
      env["X_HTTP_AUTHORIZATION"] = "Basic #{Base64.encode64("#{username}:#{password}")}"

      params = merge_controller_and_action(controller_klass, action, params)
      dispatch_request(build_request(params, env), controller_klass, action.to_s, &blk)
    end

fake_request(env = {}, opt = {}) click to toggle source

#### 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.

#### Notes

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
    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) click to toggle source

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`).
&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
    def get(path, params = {}, env = {}, &block)
      env[:request_method] = "GET"
      mock_request(path, params, env, &block)
    end

merge_controller_and_action(controller_klass, action, params) click to toggle source

:api: private

    # File lib/merb-core/test/helpers/mock_request_helper.rb, line 187
    def merge_controller_and_action(controller_klass, action, params)
      params[:controller] = controller_klass.name.to_const_path
      params[:action]     = action.to_s

      params
    end

mock_request(path, params = {}, env= {}, &block) click to toggle source

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 | The controller is yielded to the block provided for actions prior to the action being dispatched.

#### Example

    request(path, { :name => 'Homer' }, { :request_method => "PUT" }) do |controller|
      controller.stub!(:current_user).and_return(@user)
    end

#### Notes

Uses Routes.

:api: plugin @deprecated

    # File lib/merb-core/test/helpers/mock_request_helper.rb, line 330
    def mock_request(path, params = {}, env= {}, &block)
      env[:request_method] ||= "GET"
      env[:request_uri], env[:query_string] = path.split('?')

      multipart = env.delete(:test_with_multipart)

      request = build_request(params, env)

      opts = check_request_for_route(request) # Check that the request will be routed correctly
      controller_name = (opts[:namespace] ? opts.delete(:namespace) + '/' : '') + opts.delete(:controller)
      klass = Object.full_const_get(controller_name.snake_case.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

post(path, params = {}, env = {}, &block) click to toggle source

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).
&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
    def post(path, params = {}, env = {}, &block)
      env[:request_method] = "POST"
      mock_request(path, params, env, &block)
    end

put(path, params = {}, env = {}, &block) click to toggle source

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).
&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
    def put(path, params = {}, env = {}, &block)
      env[:request_method] = "PUT"
      mock_request(path, params, env, &block)
    end

requesting(*args) click to toggle source

    # File lib/merb-core/test/helpers/request_helper.rb, line 90
    def requesting(*args)   request(*args) end

response_for(*args) click to toggle source

    # File lib/merb-core/test/helpers/request_helper.rb, line 91
    def response_for(*args) request(*args) end

status_code(input) click to toggle source

    # File lib/merb-core/test/helpers/request_helper.rb, line 86
    def status_code(input)
      input.respond_to?(:status) ? input.status : input
    end

with_cookies(*controller_classes, &blk) click to toggle source

Keep track of cookie values in CookieJar within the context of the block; you need to set this up for secific controllers.

#### Parameters

*controller_classes | Controller classes to operate on in the context of the block.
&blk | The context to operate on; optionally accepts the cookie jar as an argument.

:api: public @deprecated

    # File lib/merb-core/test/helpers/mock_request_helper.rb, line 135
    def with_cookies(*controller_classes, &blk)
      cookie_jar = CookieJar.new
      before_cb = lambda { |c| c.cookies.update(cookie_jar) }
      after_cb  = lambda { |c| cookie_jar.update_from_request(c.request) }
      controller_classes.each do |klass|
        klass._before_dispatch_callbacks << before_cb
        klass._after_dispatch_callbacks  << after_cb
      end
      blk.arity == 1 ? blk.call(cookie_jar) : blk.call
      controller_classes.each do |klass|
        klass._before_dispatch_callbacks.delete before_cb
        klass._after_dispatch_callbacks.delete after_cb
      end
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.