Class: Rack::Test::Session

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

Instance Method Summary collapse

Methods included from Utils

build_multipart, build_nested_query

Constructor Details

#initialize(mock_session) ⇒ Session

Initialize a new session for the given Rack app



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/rack/test.rb', line 27

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"


117
118
119
120
# File 'lib/rack/test.rb', line 117

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

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

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

Example:

delete "/"


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

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



124
125
126
127
# File 'lib/rack/test.rb', line 124

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 in the last response. If the last response was not a redirect, an error will be raised.



132
133
134
135
136
137
138
# File 'lib/rack/test.rb', line 132

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

  get(last_response["Location"])
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 "/"


46
47
48
49
# File 'lib/rack/test.rb', line 46

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 "/"


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

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.

Example:

header "User-Agent", "Firefox"


104
105
106
107
108
109
110
# File 'lib/rack/test.rb', line 104

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

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

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

Example:

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


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

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 "/"


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

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 "/"


94
95
96
97
# File 'lib/rack/test.rb', line 94

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