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 250 250: def default_env 251: { "rack.test" => true, "REMOTE_ADDR" => "127.0.0.1" }.merge(headers_for_env) 252: end
# File lib/rack/test.rb, line 246 246: def digest_auth_configured? 247: @digest_username 248: end
# File lib/rack/test.rb, line 223 223: def digest_auth_header 224: challenge = last_response["WWW-Authenticate"].split(" ", 2).last 225: params = Rack::Auth::Digest::Params.parse(challenge) 226: 227: params.merge!({ 228: "username" => @digest_username, 229: "nc" => "00000001", 230: "cnonce" => "nonsensenonce", 231: "uri" => last_request.path_info, 232: "method" => last_request.env["REQUEST_METHOD"], 233: }) 234: 235: params["response"] = MockDigestRequest.new(params).response(@digest_password) 236: 237: "Digest #{params}" 238: 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 = default_env.merge(env) 165: 166: env.update("HTTPS" => "on") if URI::HTTPS === uri 167: env["HTTP_X_REQUESTED_WITH"] = "XMLHttpRequest" if env[:xhr] 168: 169: # TODO: Remove this after Rack 1.1 has been released. 170: # Stringifying and upcasing methods has be commit upstream 171: env["REQUEST_METHOD"] ||= env[:method] ? env[:method].to_s.upcase : "GET" 172: 173: if env["REQUEST_METHOD"] == "GET" 174: params = env[:params] || {} 175: params = parse_nested_query(params) if params.is_a?(String) 176: params.update(parse_query(uri.query)) 177: uri.query = build_nested_query(params) 178: elsif !env.has_key?(:input) 179: env["CONTENT_TYPE"] ||= "application/x-www-form-urlencoded" 180: 181: if env[:params].is_a?(Hash) 182: if data = build_multipart(env[:params]) 183: env[:input] = data 184: env["CONTENT_LENGTH"] ||= data.length.to_s 185: env["CONTENT_TYPE"] = "multipart/form-data; boundary=#{MULTIPART_BOUNDARY}" 186: else 187: env[:input] = params_to_string(env[:params]) 188: end 189: else 190: env[:input] = env[:params] 191: end 192: end 193: 194: env.delete(:params) 195: 196: if env.has_key?(:cookie) 197: set_cookie(env.delete(:cookie), uri) 198: end 199: 200: Rack::MockRequest.env_for(uri.to_s, env) 201: end
# File lib/rack/test.rb, line 254 254: def headers_for_env 255: converted_headers = {} 256: 257: @headers.each do |name, value| 258: env_key = name.upcase.gsub("-", "_") 259: env_key = "HTTP_" + env_key unless "CONTENT_TYPE" == env_key 260: converted_headers[env_key] = value 261: end 262: 263: converted_headers 264: end
# File lib/rack/test.rb, line 266 266: def params_to_string(params) 267: case params 268: when Hash then build_nested_query(params) 269: when nil then "" 270: else params 271: end 272: end
# File lib/rack/test.rb, line 203 203: def process_request(uri, env) 204: uri = URI.parse(uri) 205: uri.host ||= @default_host 206: 207: @rack_mock_session.request(uri, env) 208: 209: if retry_with_digest_auth?(env) 210: auth_env = env.merge({ 211: "HTTP_AUTHORIZATION" => digest_auth_header, 212: "rack-test.digest_auth_retry" => true 213: }) 214: auth_env.delete('rack.request') 215: process_request(uri.path, auth_env) 216: else 217: yield last_response if block_given? 218: 219: last_response 220: end 221: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.