Object
This class represents a series of requests issued to a Rack app, sharing a single cookie jar
Rack::Test::Session’s methods are most often called through Rack::Test::Methods, which will automatically build a session when it’s first used.
Creates a Rack::Test::Session for a given Rack app or Rack::MockSession.
Note: Generally, you won’t need to initialize a Rack::Test::Session directly. Instead, you should include Rack::Test::Methods into your testing context. (See README.rdoc for an example)
# File lib/rack/test.rb, line 36
36: def initialize(mock_session)
37: @headers = {}
38:
39: if mock_session.is_a?(MockSession)
40: @rack_mock_session = mock_session
41: else
42: @rack_mock_session = MockSession.new(mock_session)
43: end
44:
45: @default_host = @rack_mock_session.default_host
46: end
Issue a DELETE request for the given URI. See #
Example:
delete "/"
# File lib/rack/test.rb, line 82
82: def delete(uri, params = {}, env = {}, &block)
83: env = env_for(uri, env.merge(:method => "DELETE", :params => params))
84: process_request(uri, env, &block)
85: end
Rack::Test will not follow any redirects automatically. This method will follow the redirect returned in the last response. If the last response was not a redirect, an error will be raised.
# File lib/rack/test.rb, line 149
149: def follow_redirect!
150: unless last_response.redirect?
151: raise Error.new("Last response was not a redirect. Cannot follow_redirect!")
152: end
153:
154: get(last_response["Location"])
155: end
Issue a GET request for the given URI with the given params and Rack environment. Stores the issues request object in # and the app’s response in #. Yield # to a block if given.
Example:
get "/"
# File lib/rack/test.rb, line 55
55: def get(uri, params = {}, env = {}, &block)
56: env = env_for(uri, env.merge(:method => "GET", :params => params))
57: process_request(uri, env, &block)
58: end
Issue a HEAD request for the given URI. See #
Example:
head "/"
# File lib/rack/test.rb, line 91
91: def head(uri, params = {}, env = {}, &block)
92: env = env_for(uri, env.merge(:method => "HEAD", :params => params))
93: process_request(uri, env, &block)
94: end
Set a header to be included on all subsequent requests through the session. Use a value of nil to remove a previously configured header.
In accordance with the Rack spec, headers will be included in the Rack environment hash in HTTP_USER_AGENT form.
Example:
header "User-Agent", "Firefox"
# File lib/rack/test.rb, line 116
116: def header(name, value)
117: if value.nil?
118: @headers.delete(name)
119: else
120: @headers[name] = value
121: end
122: end
Issue a POST request for the given URI. See #
Example:
post "/signup", "name" => "Bryan"
# File lib/rack/test.rb, line 64
64: def post(uri, params = {}, env = {}, &block)
65: env = env_for(uri, env.merge(:method => "POST", :params => params))
66: process_request(uri, env, &block)
67: end
Issue a PUT request for the given URI. See #
Example:
put "/"
# File lib/rack/test.rb, line 73
73: def put(uri, params = {}, env = {}, &block)
74: env = env_for(uri, env.merge(:method => "PUT", :params => params))
75: process_request(uri, env, &block)
76: end
Issue a request to the Rack app for the given URI and optional Rack environment. Stores the issues request object in # and the app’s response in #. Yield # to a block if given.
Example:
request "/"
# File lib/rack/test.rb, line 103
103: def request(uri, env = {}, &block)
104: env = env_for(uri, env)
105: process_request(uri, env, &block)
106: end
# File lib/rack/test.rb, line 252
252: def default_env
253: { "rack.test" => true, "REMOTE_ADDR" => "127.0.0.1" }.merge(headers_for_env)
254: end
# File lib/rack/test.rb, line 248
248: def digest_auth_configured?
249: @digest_username
250: end
# File lib/rack/test.rb, line 225
225: def digest_auth_header
226: challenge = last_response["WWW-Authenticate"].split(" ", 2).last
227: params = Rack::Auth::Digest::Params.parse(challenge)
228:
229: params.merge!({
230: "username" => @digest_username,
231: "nc" => "00000001",
232: "cnonce" => "nonsensenonce",
233: "uri" => last_request.path_info,
234: "method" => last_request.env["REQUEST_METHOD"],
235: })
236:
237: params["response"] = MockDigestRequest.new(params).response(@digest_password)
238:
239: "Digest #{params}"
240: end
# File lib/rack/test.rb, line 159
159: def env_for(path, env)
160: uri = URI.parse(path)
161: uri.path = "/#{uri.path}" unless uri.path[0] == //
162: uri.host ||= @default_host
163:
164: env["HTTP_HOST"] ||= [uri.host, uri.port].compact.join(":")
165:
166: env = default_env.merge(env)
167:
168: env.update("HTTPS" => "on") if URI::HTTPS === uri
169: env["HTTP_X_REQUESTED_WITH"] = "XMLHttpRequest" if env[:xhr]
170:
171: # TODO: Remove this after Rack 1.1 has been released.
172: # Stringifying and upcasing methods has be commit upstream
173: env["REQUEST_METHOD"] ||= env[:method] ? env[:method].to_s.upcase : "GET"
174:
175: if env["REQUEST_METHOD"] == "GET"
176: params = env[:params] || {}
177: params = parse_nested_query(params) if params.is_a?(String)
178: params.update(parse_nested_query(uri.query))
179: uri.query = build_nested_query(params)
180: elsif !env.has_key?(:input)
181: env["CONTENT_TYPE"] ||= "application/x-www-form-urlencoded"
182:
183: if env[:params].is_a?(Hash)
184: if data = build_multipart(env[:params])
185: env[:input] = data
186: env["CONTENT_LENGTH"] ||= data.length.to_s
187: env["CONTENT_TYPE"] = "multipart/form-data; boundary=#{MULTIPART_BOUNDARY}"
188: else
189: env[:input] = params_to_string(env[:params])
190: end
191: else
192: env[:input] = env[:params]
193: end
194: end
195:
196: env.delete(:params)
197:
198: if env.has_key?(:cookie)
199: set_cookie(env.delete(:cookie), uri)
200: end
201:
202: Rack::MockRequest.env_for(uri.to_s, env)
203: end
# File lib/rack/test.rb, line 256
256: def headers_for_env
257: converted_headers = {}
258:
259: @headers.each do |name, value|
260: env_key = name.upcase.gsub("-", "_")
261: env_key = "HTTP_" + env_key unless "CONTENT_TYPE" == env_key
262: converted_headers[env_key] = value
263: end
264:
265: converted_headers
266: end
# File lib/rack/test.rb, line 268
268: def params_to_string(params)
269: case params
270: when Hash then build_nested_query(params)
271: when nil then ""
272: else params
273: end
274: end
# File lib/rack/test.rb, line 205
205: def process_request(uri, env)
206: uri = URI.parse(uri)
207: uri.host ||= @default_host
208:
209: @rack_mock_session.request(uri, env)
210:
211: if retry_with_digest_auth?(env)
212: auth_env = env.merge({
213: "HTTP_AUTHORIZATION" => digest_auth_header,
214: "rack-test.digest_auth_retry" => true
215: })
216: auth_env.delete('rack.request')
217: process_request(uri.path, auth_env)
218: else
219: yield last_response if block_given?
220:
221: last_response
222: end
223: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.