Class: Rack::Test::Session

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Utils
Defined in:
lib/rack/test.rb

Overview

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.

Instance Method Summary collapse

Methods included from Utils

build_multipart, build_nested_query

Constructor Details

#initialize(mock_session) ⇒ Session

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)



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/rack/test.rb', line 36

def initialize(mock_session)
  @headers = {}

  if mock_session.is_a?(MockSession)
    @rack_mock_session = mock_session
  else
    @rack_mock_session = MockSession.new(mock_session)
  end

  @default_host = @rack_mock_session.default_host
end

Instance Method Details

#basic_authorize(username, password) ⇒ Object Also known as: authorize

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"


138
139
140
141
# File 'lib/rack/test.rb', line 138

def basic_authorize(username, password)
   = ["#{username}:#{password}"].pack("m*")
  header('Authorization', "Basic #{}")
end

#delete(uri, params = {}, env = {}, &block) ⇒ Object

Issue a DELETE request for the given URI. See #get

Example:

delete "/"


82
83
84
85
# File 'lib/rack/test.rb', line 82

def delete(uri, params = {}, env = {}, &block)
  env = env_for(uri, env.merge(:method => "DELETE", :params => params))
  process_request(uri, env, &block)
end

#digest_authorize(username, password) ⇒ Object

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"


150
151
152
153
# File 'lib/rack/test.rb', line 150

def digest_authorize(username, password)
  @digest_username = username
  @digest_password = password
end

#follow_redirect!Object

Rack::Test will not follow any redirects automatically. This method will follow the redirect returned (including setting the Referer header on the new request) in the last response. If the last response was not a redirect, an error will be raised.



159
160
161
162
163
164
165
# File 'lib/rack/test.rb', line 159

def follow_redirect!
  unless last_response.redirect?
    raise Error.new("Last response was not a redirect. Cannot follow_redirect!")
  end

  get(last_response["Location"], {}, { "HTTP_REFERER" => last_request.url })
end

#get(uri, params = {}, env = {}, &block) ⇒ Object

Issue a GET request for the given URI with the given params and Rack environment. Stores the issues request object in #last_request and the app’s response in #last_response. Yield #last_response to a block if given.

Example:

get "/"


55
56
57
58
# File 'lib/rack/test.rb', line 55

def get(uri, params = {}, env = {}, &block)
  env = env_for(uri, env.merge(:method => "GET", :params => params))
  process_request(uri, env, &block)
end

#head(uri, params = {}, env = {}, &block) ⇒ Object

Issue a HEAD request for the given URI. See #get

Example:

head "/"


100
101
102
103
# File 'lib/rack/test.rb', line 100

def head(uri, params = {}, env = {}, &block)
  env = env_for(uri, env.merge(:method => "HEAD", :params => params))
  process_request(uri, env, &block)
end

#header(name, value) ⇒ Object

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"


125
126
127
128
129
130
131
# File 'lib/rack/test.rb', line 125

def header(name, value)
  if value.nil?
    @headers.delete(name)
  else
    @headers[name] = value
  end
end

#options(uri, params = {}, env = {}, &block) ⇒ Object

Issue an OPTIONS request for the given URI. See #get

Example:

options "/"


91
92
93
94
# File 'lib/rack/test.rb', line 91

def options(uri, params = {}, env = {}, &block)
  env = env_for(uri, env.merge(:method => "OPTIONS", :params => params))
  process_request(uri, env, &block)
end

#post(uri, params = {}, env = {}, &block) ⇒ Object

Issue a POST request for the given URI. See #get

Example:

post "/signup", "name" => "Bryan"


64
65
66
67
# File 'lib/rack/test.rb', line 64

def post(uri, params = {}, env = {}, &block)
  env = env_for(uri, env.merge(:method => "POST", :params => params))
  process_request(uri, env, &block)
end

#put(uri, params = {}, env = {}, &block) ⇒ Object

Issue a PUT request for the given URI. See #get

Example:

put "/"


73
74
75
76
# File 'lib/rack/test.rb', line 73

def put(uri, params = {}, env = {}, &block)
  env = env_for(uri, env.merge(:method => "PUT", :params => params))
  process_request(uri, env, &block)
end

#request(uri, env = {}, &block) ⇒ Object

Issue a request to the Rack app for the given URI and optional Rack environment. Stores the issues request object in #last_request and the app’s response in #last_response. Yield #last_response to a block if given.

Example:

request "/"


112
113
114
115
# File 'lib/rack/test.rb', line 112

def request(uri, env = {}, &block)
  env = env_for(uri, env)
  process_request(uri, env, &block)
end