### Home Classes Methods

### In Files

- lib/rack/mock.rb

### Parent

Object

### Namespace

- CLASS Rack::MockRequest::FatalWarner
- CLASS Rack::MockRequest::FatalWarning

### Methods

- ::env_for
- ::new
- ::parse_uri_rfc2396
- #delete
- #get
- #head
- #options
- #patch
- #post
- #put
- #request

### Files

- KNOWN-ISSUES

### Class/Module Index

Quicksearch

- Rack
- Rack::Auth
- Rack::Auth::AbstractHandler
- Rack::Auth::AbstractRequest
- Rack::Auth::Basic
- Rack::Auth::Basic::Request
- Rack::Auth::Digest
- Rack::Auth::Digest::MD5
- Rack::Auth::Digest::Nonce
- Rack::Auth::Digest::Params
- Rack::Auth::Digest::Request
- Rack::BodyProxy
- Rack::Builder
- Rack::Cascade
- Rack::Chunked
- Rack::Chunked::Body
- Rack::CommonLogger
- Rack::ConditionalGet
- Rack::Config
- Rack::ContentLength
- Rack::ContentType
- Rack::Deflater
- Rack::Deflater::DeflateStream
- Rack::Deflater::GzipStream
- Rack::Directory
- Rack::ETag
- Rack::File
- Rack::ForwardRequest
- Rack::Handler
- Rack::Handler::CGI
- Rack::Handler::EventedMongrel
- Rack::Handler::FastCGI
- Rack::Handler::LSWS
- Rack::Handler::Mongrel
- Rack::Handler::SCGI
- Rack::Handler::SwiftipliedMongrel
- Rack::Handler::Thin
- Rack::Handler::WEBrick
- Rack::Head
- Rack::Lint
- Rack::Lobster
- Rack::Lock
- Rack::Logger
- Rack::MethodOverride
- Rack::Mime
- Rack::MockRequest
- Rack::MockRequest::FatalWarner
- Rack::MockRequest::FatalWarning
- Rack::MockResponse
- Rack::Multipart
- Rack::Multipart::Generator
- Rack::Multipart::MultipartPartLimitError
- Rack::Multipart::Parser
- Rack::Multipart::UploadedFile
- Rack::NullLogger
- Rack::Recursive
- Rack::Reloader
- Rack::Reloader::Stat
- Rack::Request
- Rack::Response
- Rack::Response::Helpers
- Rack::RewindableInput
- Rack::RewindableInput::Tempfile
- Rack::Runtime
- Rack::Sendfile
- Rack::Server
- Rack::Server::Options
- Rack::Session
- Rack::Session::Abstract
- Rack::Session::Abstract::ID
- Rack::Session::Abstract::SessionHash
- Rack::Session::Cookie
- Rack::Session::Cookie::Base64
- Rack::Session::Cookie::Base64::JSON
- Rack::Session::Cookie::Base64::Marshal
- Rack::Session::Cookie::Base64::ZipJSON
- Rack::Session::Cookie::Identity
- Rack::Session::Memcache
- Rack::Session::Pool
- Rack::ShowExceptions
- Rack::ShowStatus
- Rack::Static
- Rack::TempfileReaper
- Rack::URLMap
- Rack::Utils
- Rack::Utils::Context
- Rack::Utils::HeaderHash
- Rack::Utils::InvalidParameterError
- Rack::Utils::KeySpaceConstrainedParams
- Rack::Utils::Multipart
- Rack::Utils::OkJson
- Rack::Utils::OkJson::Error
- Rack::Utils::OkJson::Utf8Error
- Rack::Utils::ParameterTypeError
- FCGI
- FCGI::Stream
- WEBrick
- WEBrick::HTTPResponse
- Object

No matching classes.

# Rack::MockRequest

Rack::MockRequest helps testing your Rack application without actually using HTTP.

After performing a request on a URL with get/post/put/patch/delete, it returns a MockResponse with useful helper methods for effective testing.

You can pass a hash with additional configuration to the get/post/put/patch/delete.

`:input` | A String or IO-like to be used as rack.input.
`:fatal` | Raise a FatalWarning if the app writes to rack.errors.
`:lint` | If true, wrap the application in a Rack::Lint.

### Constants

DEFAULT_ENV

### Public Class Methods

env_for(uri="", opts={}) click to toggle source

Return the Rack environment used for a request to `uri`.

    # File lib/rack/mock.rb, line 88
    def self.env_for(uri="", opts={})
      uri = parse_uri_rfc2396(uri)
      uri.path = "/#{uri.path}" unless uri.path[0] == //

      env = DEFAULT_ENV.dup

      env[REQUEST_METHOD] = opts[:method] ? opts[:method].to_s.upcase : "GET"
      env["SERVER_NAME"] = uri.host || "example.org"
      env["SERVER_PORT"] = uri.port ? uri.port.to_s : "80"
      env[QUERY_STRING] = uri.query.to_s
      env[PATH_INFO] = (!uri.path || uri.path.empty?) ? "/" : uri.path
      env["rack.url_scheme"] = uri.scheme || "http"
      env["HTTPS"] = env["rack.url_scheme"] == "https" ? "on" : "off"

      env[SCRIPT_NAME] = opts[:script_name] || ""

      if opts[:fatal]
        env["rack.errors"] = FatalWarner.new
      else
        env["rack.errors"] = StringIO.new
      end

      if params = opts[:params]
        if env[REQUEST_METHOD] == "GET"
          params = Utils.parse_nested_query(params) if params.is_a?(String)
          params.update(Utils.parse_nested_query(env[QUERY_STRING]))
          env[QUERY_STRING] = Utils.build_nested_query(params)
        elsif !opts.has_key?(:input)
          opts["CONTENT_TYPE"] = "application/x-www-form-urlencoded"
          if params.is_a?(Hash)
            if data = Utils::Multipart.build_multipart(params)
              opts[:input] = data
              opts["CONTENT_LENGTH"] ||= data.length.to_s
              opts["CONTENT_TYPE"] = "multipart/form-data; boundary=#{Utils::Multipart::MULTIPART_BOUNDARY}"
            else
              opts[:input] = Utils.build_nested_query(params)
            end
          else
            opts[:input] = params
          end
        end
      end

      empty_str = ""
      empty_str.force_encoding("ASCII-8BIT") if empty_str.respond_to? :force_encoding
      opts[:input] ||= empty_str
      if String === opts[:input]
        rack_input = StringIO.new(opts[:input])
      else
        rack_input = opts[:input]
      end

      rack_input.set_encoding(Encoding::BINARY) if rack_input.respond_to?(:set_encoding)
      env['rack.input'] = rack_input

      env["CONTENT_LENGTH"] ||= env["rack.input"].length.to_s

      opts.each { |field, value|
        env[field] = value  if String === field
      }

      env
    end

new(app) click to toggle source

    # File lib/rack/mock.rb, line 52
    def initialize(app)
      @app = app
    end

parse_uri_rfc2396(uri) click to toggle source

For historical reasons, we're pinning to RFC 2396. It's easier for users and we get support from ruby 1.8 to 2.2 using this method.

    # File lib/rack/mock.rb, line 82
    def self.parse_uri_rfc2396(uri)
      @parser ||= defined?(URI::RFC2396_Parser) ? URI::RFC2396_Parser.new : URI
      @parser.parse(uri)
    end

### Public Instance Methods

delete(uri, opts={}) click to toggle source

    # File lib/rack/mock.rb, line 60
    def delete(uri, opts={})  request("DELETE", uri, opts)  end

get(uri, opts={}) click to toggle source

    # File lib/rack/mock.rb, line 56
    def get(uri, opts={})     request("GET", uri, opts)     end

head(uri, opts={}) click to toggle source

    # File lib/rack/mock.rb, line 61
    def head(uri, opts={})    request("HEAD", uri, opts)    end

options(uri, opts={}) click to toggle source

    # File lib/rack/mock.rb, line 62
    def options(uri, opts={}) request("OPTIONS", uri, opts) end

patch(uri, opts={}) click to toggle source

    # File lib/rack/mock.rb, line 59
    def patch(uri, opts={})   request("PATCH", uri, opts)   end

post(uri, opts={}) click to toggle source

    # File lib/rack/mock.rb, line 57
    def post(uri, opts={})    request("POST", uri, opts)    end

put(uri, opts={}) click to toggle source

    # File lib/rack/mock.rb, line 58
    def put(uri, opts={})     request("PUT", uri, opts)     end

request(method="GET", uri="", opts={}) click to toggle source

    # File lib/rack/mock.rb, line 64
    def request(method="GET", uri="", opts={})
      env = self.class.env_for(uri, opts.merge(:method => method))

      if opts[:lint]
        app = Rack::Lint.new(@app)
      else
        app = @app
      end

      errors = env["rack.errors"]
      status, headers, body  = app.call(env)
      MockResponse.new(status, headers, body, errors)
    ensure
      body.close if body.respond_to?(:close)
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.