In Files

Parent

Included Modules

Rack::Test::Session

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.

Public Class Methods

new(mock_session) click to toggle source

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

Public Instance Methods

authorize(username, password) click to toggle source
Alias for: basic_authorize
basic_authorize(username, password) click to toggle source

Set the username and password for HTTP Basic authorization, to be included in subsequent requests in the HTTP_AUTHORIZATION header.

Example:

  basic_authorize "bryan", "secret"
     # File lib/rack/test.rb, line 129
129:       def basic_authorize(username, password)
130:         encoded_login = ["#{username}:#{password}"].pack("m*")
131:         header('Authorization', "Basic #{encoded_login}")
132:       end
Also aliased as: authorize
delete(uri, params = {}, env = {}, &block) click to toggle source

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
digest_authorize(username, password) click to toggle source

Set the username and password for HTTP Digest authorization, to be included in subsequent requests in the HTTP_AUTHORIZATION header.

Example:

  digest_authorize "bryan", "secret"
     # File lib/rack/test.rb, line 141
141:       def digest_authorize(username, password)
142:         @digest_username = username
143:         @digest_password = password
144:       end
follow_redirect!() click to toggle source

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
get(uri, params = {}, env = {}, &block) click to toggle source

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
head(uri, params = {}, env = {}, &block) click to toggle source

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
header(name, value) click to toggle source

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
post(uri, params = {}, env = {}, &block) click to toggle source

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
put(uri, params = {}, env = {}, &block) click to toggle source

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
request(uri, env = {}, &block) click to toggle source

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

Private Instance Methods

default_env() click to toggle source
     # 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
digest_auth_configured?() click to toggle source
     # File lib/rack/test.rb, line 248
248:       def digest_auth_configured?
249:         @digest_username
250:       end
digest_auth_header() click to toggle source
     # 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
env_for(path, env) click to toggle source
     # 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
headers_for_env() click to toggle source
     # 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
params_to_string(params) click to toggle source
     # 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
process_request(uri, env) click to toggle source
     # 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
retry_with_digest_auth?(env) click to toggle source
     # File lib/rack/test.rb, line 242
242:       def retry_with_digest_auth?(env)
243:         last_response.status == 401 &&
244:         digest_auth_configured? &&
245:         !env["rack-test.digest_auth_retry"]
246:       end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.