In Files

Parent

Class Index [+]

Quicksearch

Rack::MockRequest

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.

Constants

DEFAULT_ENV
(Not documented)

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 74
 74:     def self.env_for(uri="", opts={})
 75:       uri = URI(uri)
 76:       uri.path = "/#{uri.path}" unless uri.path[0] == ?/
 77: 
 78:       env = DEFAULT_ENV.dup
 79: 
 80:       env["REQUEST_METHOD"] = opts[:method] ? opts[:method].to_s.upcase : "GET"
 81:       env["SERVER_NAME"] = uri.host || "example.org"
 82:       env["SERVER_PORT"] = uri.port ? uri.port.to_s : "80"
 83:       env["QUERY_STRING"] = uri.query.to_s
 84:       env["PATH_INFO"] = (!uri.path || uri.path.empty?) ? "/" : uri.path
 85:       env["rack.url_scheme"] = uri.scheme || "http"
 86:       env["HTTPS"] = env["rack.url_scheme"] == "https" ? "on" : "off"
 87: 
 88:       env["SCRIPT_NAME"] = opts[:script_name] || ""
 89: 
 90:       if opts[:fatal]
 91:         env["rack.errors"] = FatalWarner.new
 92:       else
 93:         env["rack.errors"] = StringIO.new
 94:       end
 95: 
 96:       if params = opts[:params]
 97:         if env["REQUEST_METHOD"] == "GET"
 98:           params = Utils.parse_nested_query(params) if params.is_a?(String)
 99:           params.update(Utils.parse_nested_query(env["QUERY_STRING"]))
100:           env["QUERY_STRING"] = Utils.build_nested_query(params)
101:         elsif !opts.has_key?(:input)
102:           opts["CONTENT_TYPE"] = "application/x-www-form-urlencoded"
103:           if params.is_a?(Hash)
104:             if data = Utils::Multipart.build_multipart(params)
105:               opts[:input] = data
106:               opts["CONTENT_LENGTH"] ||= data.length.to_s
107:               opts["CONTENT_TYPE"] = "multipart/form-data; boundary=#{Utils::Multipart::MULTIPART_BOUNDARY}"
108:             else
109:               opts[:input] = Utils.build_nested_query(params)
110:             end
111:           else
112:             opts[:input] = params
113:           end
114:         end
115:       end
116: 
117:       empty_str = ""
118:       empty_str.force_encoding("ASCII-8BIT") if empty_str.respond_to? :force_encoding
119:       opts[:input] ||= empty_str
120:       if String === opts[:input]
121:         rack_input = StringIO.new(opts[:input])
122:       else
123:         rack_input = opts[:input]
124:       end
125: 
126:       rack_input.set_encoding(Encoding::BINARY) if rack_input.respond_to?(:set_encoding)
127:       env['rack.input'] = rack_input
128: 
129:       env["CONTENT_LENGTH"] ||= env["rack.input"].length.to_s
130: 
131:       opts.each { |field, value|
132:         env[field] = value  if String === field
133:       }
134: 
135:       env
136:     end
new(app) click to toggle source

(Not documented)

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

Public Instance Methods

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

(Not documented)

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

(Not documented)

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

(Not documented)

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

(Not documented)

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

(Not documented)

    # 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

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.