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. |
DEFAULT_ENV | = | { "rack.version" => [0,1], "rack.input" => StringIO.new, "rack.errors" => StringIO.new, "rack.multithread" => true, "rack.multiprocess" => true, "rack.run_once" => false, } |
Return the Rack environment used for a request to uri.
# File lib/rack/mock.rb, line 69 69: def self.env_for(uri="", opts={}) 70: uri = URI(uri) 71: env = DEFAULT_ENV.dup 72: 73: env["REQUEST_METHOD"] = opts[:method] || "GET" 74: env["SERVER_NAME"] = uri.host || "example.org" 75: env["SERVER_PORT"] = uri.port ? uri.port.to_s : "80" 76: env["QUERY_STRING"] = uri.query.to_s 77: env["PATH_INFO"] = (!uri.path || uri.path.empty?) ? "/" : uri.path 78: env["rack.url_scheme"] = uri.scheme || "http" 79: 80: env["SCRIPT_NAME"] = opts[:script_name] || "" 81: 82: if opts[:fatal] 83: env["rack.errors"] = FatalWarner.new 84: else 85: env["rack.errors"] = StringIO.new 86: end 87: 88: opts[:input] ||= "" 89: if String === opts[:input] 90: env["rack.input"] = StringIO.new(opts[:input]) 91: else 92: env["rack.input"] = opts[:input] 93: end 94: 95: opts.each { |field, value| 96: env[field] = value if String === field 97: } 98: 99: env 100: end
# File lib/rack/mock.rb, line 55 55: def request(method="GET", uri="", opts={}) 56: env = self.class.env_for(uri, opts.merge(:method => method)) 57: 58: if opts[:lint] 59: app = Rack::Lint.new(@app) 60: else 61: app = @app 62: end 63: 64: errors = env["rack.errors"] 65: MockResponse.new(*(app.call(env) + [errors])) 66: end