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. |
Return the Rack environment used for a request to uri.
# File lib/rack/mock.rb, line 79 79: def self.env_for(uri="", opts={}) 80: uri = URI(uri) 81: uri.path = "/#{uri.path}" unless uri.path[0] == // 82: 83: env = DEFAULT_ENV.dup 84: 85: env["REQUEST_METHOD"] = opts[:method] ? opts[:method].to_s.upcase : "GET" 86: env["SERVER_NAME"] = uri.host || "example.org" 87: env["SERVER_PORT"] = uri.port ? uri.port.to_s : "80" 88: env["QUERY_STRING"] = uri.query.to_s 89: env["PATH_INFO"] = (!uri.path || uri.path.empty?) ? "/" : uri.path 90: env["rack.url_scheme"] = uri.scheme || "http" 91: env["HTTPS"] = env["rack.url_scheme"] == "https" ? "on" : "off" 92: 93: env["SCRIPT_NAME"] = opts[:script_name] || "" 94: 95: if opts[:fatal] 96: env["rack.errors"] = FatalWarner.new 97: else 98: env["rack.errors"] = StringIO.new 99: end 100: 101: if params = opts[:params] 102: if env["REQUEST_METHOD"] == "GET" 103: params = Utils.parse_nested_query(params) if params.is_a?(String) 104: params.update(Utils.parse_nested_query(env["QUERY_STRING"])) 105: env["QUERY_STRING"] = Utils.build_nested_query(params) 106: elsif !opts.has_key?(:input) 107: opts["CONTENT_TYPE"] = "application/x-www-form-urlencoded" 108: if params.is_a?(Hash) 109: if data = Utils::Multipart.build_multipart(params) 110: opts[:input] = data 111: opts["CONTENT_LENGTH"] ||= data.length.to_s 112: opts["CONTENT_TYPE"] = "multipart/form-data; boundary=#{Utils::Multipart::MULTIPART_BOUNDARY}" 113: else 114: opts[:input] = Utils.build_nested_query(params) 115: end 116: else 117: opts[:input] = params 118: end 119: end 120: end 121: 122: empty_str = "" 123: empty_str.force_encoding("ASCII-8BIT") if empty_str.respond_to? :force_encoding 124: opts[:input] ||= empty_str 125: if String === opts[:input] 126: rack_input = StringIO.new(opts[:input]) 127: else 128: rack_input = opts[:input] 129: end 130: 131: rack_input.set_encoding(Encoding::BINARY) if rack_input.respond_to?(:set_encoding) 132: env['rack.input'] = rack_input 133: 134: env["CONTENT_LENGTH"] ||= env["rack.input"].length.to_s 135: 136: opts.each { |field, value| 137: env[field] = value if String === field 138: } 139: 140: env 141: end
# File lib/rack/mock.rb, line 59 59: def delete(uri, opts={}) request("DELETE", uri, opts) end
# File lib/rack/mock.rb, line 56 56: def get(uri, opts={}) request("GET", uri, opts) end
# File lib/rack/mock.rb, line 60 60: def head(uri, opts={}) request("HEAD", uri, opts) end
# File lib/rack/mock.rb, line 57 57: def post(uri, opts={}) request("POST", uri, opts) end
# File lib/rack/mock.rb, line 58 58: def put(uri, opts={}) request("PUT", uri, opts) end
# File lib/rack/mock.rb, line 62 62: def request(method="GET", uri="", opts={}) 63: env = self.class.env_for(uri, opts.merge(:method => method)) 64: 65: if opts[:lint] 66: app = Rack::Lint.new(@app) 67: else 68: app = @app 69: end 70: 71: errors = env["rack.errors"] 72: status, headers, body = app.call(env) 73: MockResponse.new(status, headers, body, errors) 74: ensure 75: body.close if body.respond_to?(:close) 76: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.