Class: Rack::MockRequest
Relationships & Source Files | |
Namespace Children | |
Classes:
| |
Exceptions:
| |
Inherits: | Object |
Defined in: | lib/rack/mock_request.rb |
Overview
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
Lint
.
Class Method Summary
-
.env_for(uri = "", opts = {})
Return the
::Rack
environment used for a request touri
. - .new(app) ⇒ MockRequest constructor
-
.parse_uri_rfc2396(uri)
For historical reasons, we’re pinning to RFC 2396.
Instance Method Summary
-
#delete(uri, opts = {})
Make a DELETE request and return a
MockResponse
. -
#get(uri, opts = {})
Make a GET request and return a
MockResponse
. -
#head(uri, opts = {})
Make a HEAD request and return a
MockResponse
. -
#options(uri, opts = {})
Make an OPTIONS request and return a
MockResponse
. -
#patch(uri, opts = {})
Make a PATCH request and return a
MockResponse
. -
#post(uri, opts = {})
Make a POST request and return a
MockResponse
. -
#put(uri, opts = {})
Make a PUT request and return a
MockResponse
. -
#request(method = GET, uri = "", opts = {})
Make a request using the given request method for the given uri to the rack application and return a
MockResponse
.
Constructor Details
.new(app) ⇒ MockRequest
# File 'lib/rack/mock_request.rb', line 44
def initialize(app) @app = app end
Class Method Details
.env_for(uri = "", opts = {})
Return the ::Rack
environment used for a request to uri
. All options that are strings are added to the returned environment. Options:
:fatal
-
Whether to raise an exception if request outputs to rack.errors
:input
-
The rack.input to set
:http_version
-
The SERVER_PROTOCOL to set
:method
-
The HTTP request method to use
:params
-
The params to use
:script_name
-
The SCRIPT_NAME to set
# File 'lib/rack/mock_request.rb', line 98
def self.env_for(uri = "", opts = {}) uri = parse_uri_rfc2396(uri) uri.path = "/#{uri.path}" unless uri.path[0] == ?/ env = {} env[REQUEST_METHOD] = (opts[:method] ? opts[:method].to_s.upcase : GET).b env[SERVER_NAME] = (uri.host || "example.org").b env[SERVER_PORT] = (uri.port ? uri.port.to_s : "80").b env[SERVER_PROTOCOL] = opts[:http_version] || 'HTTP/1.1' env[QUERY_STRING] = (uri.query.to_s).b env[PATH_INFO] = (uri.path).b env[RACK_URL_SCHEME] = (uri.scheme || "http").b env[HTTPS] = (env[RACK_URL_SCHEME] == "https" ? "on" : "off").b 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 = Rack::Multipart.build_multipart(params) opts[:input] = data opts["CONTENT_LENGTH"] ||= data.length.to_s opts["CONTENT_TYPE"] = "multipart/form-data; boundary=#{Rack::Multipart::MULTIPART_BOUNDARY}" else opts[:input] = Utils.build_nested_query(params) end else opts[:input] = params end end end rack_input = opts[:input] if String === rack_input rack_input = StringIO.new(rack_input) end if rack_input rack_input.set_encoding(Encoding::BINARY) if rack_input.respond_to?(:set_encoding) env[RACK_INPUT] = rack_input env["CONTENT_LENGTH"] ||= env[RACK_INPUT].size.to_s if env[RACK_INPUT].respond_to?(:size) end opts.each { |field, value| env[field] = value if String === field } env end
.parse_uri_rfc2396(uri)
For historical reasons, we’re pinning to RFC 2396. URI::Parser
= URI::RFC2396_Parser
# File 'lib/rack/mock_request.rb', line 84
def self.parse_uri_rfc2396(uri) @parser ||= URI::Parser.new @parser.parse(uri) end
Instance Method Details
#delete(uri, opts = {})
Make a DELETE request and return a MockResponse
. See #request.
#get(uri, opts = {})
Make a GET request and return a MockResponse
. See #request.
#head(uri, opts = {})
Make a HEAD request and return a MockResponse
. See #request.
#options(uri, opts = {})
Make an OPTIONS request and return a MockResponse
. See #request.
#patch(uri, opts = {})
Make a PATCH request and return a MockResponse
. See #request.
#post(uri, opts = {})
Make a POST request and return a MockResponse
. See #request.
#put(uri, opts = {})
Make a PUT request and return a MockResponse
. See #request.
#request(method = GET, uri = "", opts = {})
Make a request using the given request method for the given uri to the rack application and return a MockResponse
. Options given are passed to .env_for.
# File 'lib/rack/mock_request.rb', line 66
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