Class Rack::MockRequest
In: lib/rack/mock.rb
Parent: Object

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

After performing a request on a URL with get/post/put/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/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.

Methods

delete   env_for   get   new   post   put   request  

Classes and Modules

Class Rack::MockRequest::FatalWarner
Class Rack::MockRequest::FatalWarning

Constants

DEFAULT_ENV = { "rack.version" => [0,1], "rack.input" => StringIO.new, "rack.errors" => StringIO.new, "rack.multithread" => true, "rack.multiprocess" => true, "rack.run_once" => false, }

Public Class methods

Return the Rack environment used for a request to uri.

[Source]

     # File lib/rack/mock.rb, line 74
 74:     def self.env_for(uri="", opts={})
 75:       uri = URI(uri)
 76:       env = DEFAULT_ENV.dup
 77: 
 78:       env["REQUEST_METHOD"] = opts[:method] || "GET"
 79:       env["SERVER_NAME"] = uri.host || "example.org"
 80:       env["SERVER_PORT"] = uri.port ? uri.port.to_s : "80"
 81:       env["QUERY_STRING"] = uri.query.to_s
 82:       env["PATH_INFO"] = (!uri.path || uri.path.empty?) ? "/" : uri.path
 83:       env["rack.url_scheme"] = uri.scheme || "http"
 84: 
 85:       env["SCRIPT_NAME"] = opts[:script_name] || ""
 86: 
 87:       if opts[:fatal]
 88:         env["rack.errors"] = FatalWarner.new
 89:       else
 90:         env["rack.errors"] = StringIO.new
 91:       end
 92: 
 93:       opts[:input] ||= ""
 94:       if String === opts[:input]
 95:         env["rack.input"] = StringIO.new(opts[:input])
 96:       else
 97:         env["rack.input"] = opts[:input]
 98:       end
 99: 
100:       opts.each { |field, value|
101:         env[field] = value  if String === field
102:       }
103: 
104:       env
105:     end

[Source]

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

Public Instance methods

[Source]

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

[Source]

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

[Source]

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

[Source]

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

[Source]

    # File lib/rack/mock.rb, line 60
60:     def request(method="GET", uri="", opts={})
61:       env = self.class.env_for(uri, opts.merge(:method => method))
62: 
63:       if opts[:lint]
64:         app = Rack::Lint.new(@app)
65:       else
66:         app = @app
67:       end
68: 
69:       errors = env["rack.errors"]
70:       MockResponse.new(*(app.call(env) + [errors]))
71:     end

[Validate]