Class: Rack::MockRequest
- Inherits:
-
Object
show all
- Defined in:
- rack/rack/mock.rb
Overview
Rack::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 Rack::Lint.
Defined Under Namespace
Classes: FatalWarner, FatalWarning
Constant Summary
- DEFAULT_ENV =
{
"rack.version" => Rack::VERSION,
"rack.input" => StringIO.new,
"rack.errors" => StringIO.new,
"rack.multithread" => true,
"rack.multiprocess" => true,
"rack.run_once" => false,
}
Class Method Summary
(collapse)
Instance Method Summary
(collapse)
Constructor Details
Returns a new instance of MockRequest
52
53
54
|
# File 'rack/rack/mock.rb', line 52
def initialize(app)
@app = app
end
|
Class Method Details
+ (void) env_for(uri = "", opts = {})
Return the Rack environment used for a request to uri
.
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
|
# File 'rack/rack/mock.rb', line 80
def self.env_for(uri="", opts={})
uri = URI(uri)
uri.path = "/#{uri.path}" unless uri.path[0] == ?/
env = DEFAULT_ENV.dup
env["REQUEST_METHOD"] = opts[:method] ? opts[:method].to_s.upcase : "GET"
env["SERVER_NAME"] = uri.host || "example.org"
env["SERVER_PORT"] = uri.port ? uri.port.to_s : "80"
env["QUERY_STRING"] = uri.query.to_s
env["PATH_INFO"] = (!uri.path || uri.path.empty?) ? "/" : uri.path
env["rack.url_scheme"] = uri.scheme || "http"
env["HTTPS"] = env["rack.url_scheme"] == "https" ? "on" : "off"
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 = Utils::Multipart.build_multipart(params)
opts[:input] = data
opts["CONTENT_LENGTH"] ||= data.length.to_s
opts["CONTENT_TYPE"] = "multipart/form-data; boundary=#{Utils::Multipart::MULTIPART_BOUNDARY}"
else
opts[:input] = Utils.build_nested_query(params)
end
else
opts[:input] = params
end
end
end
empty_str = ""
empty_str.force_encoding("ASCII-8BIT") if empty_str.respond_to? :force_encoding
opts[:input] ||= empty_str
if String === opts[:input]
rack_input = StringIO.new(opts[:input])
else
rack_input = opts[:input]
end
rack_input.set_encoding(Encoding::BINARY) if rack_input.respond_to?(:set_encoding)
env['rack.input'] = rack_input
env["CONTENT_LENGTH"] ||= env["rack.input"].length.to_s
opts.each { |field, value|
env[field] = value if String === field
}
env
end
|
Instance Method Details
- (void) delete(uri, opts = {})
60
|
# File 'rack/rack/mock.rb', line 60
def delete(uri, opts={}) request("DELETE", uri, opts) end
|
- (void) get(uri, opts = {})
56
|
# File 'rack/rack/mock.rb', line 56
def get(uri, opts={}) request("GET", uri, opts) end
|
- (void) head(uri, opts = {})
61
|
# File 'rack/rack/mock.rb', line 61
def head(uri, opts={}) request("HEAD", uri, opts) end
|
- (void) patch(uri, opts = {})
59
|
# File 'rack/rack/mock.rb', line 59
def patch(uri, opts={}) request("PATCH", uri, opts) end
|
- (void) post(uri, opts = {})
57
|
# File 'rack/rack/mock.rb', line 57
def post(uri, opts={}) request("POST", uri, opts) end
|
- (void) put(uri, opts = {})
58
|
# File 'rack/rack/mock.rb', line 58
def put(uri, opts={}) request("PUT", uri, opts) end
|
- (void) request(method = "GET", uri = "", opts = {})
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
|
# File 'rack/rack/mock.rb', line 63
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, , body = app.call(env)
MockResponse.new(status, , body, errors)
ensure
body.close if body.respond_to?(:close)
end
|