Class: Rack::Test::Session
- Inherits:
-
Object
- Object
- Rack::Test::Session
- 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
-
#basic_authorize(username, password) ⇒ Object
(also: #authorize)
Set the username and password for HTTP Basic authorization, to be included in subsequent requests in the HTTP_AUTHORIZATION header.
-
#custom_request(verb, uri, params = {}, env = {}, &block) ⇒ Object
Issue a request using the given verb for the given URI.
-
#delete(uri, params = {}, env = {}, &block) ⇒ Object
Issue a DELETE request for the given URI.
-
#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.
-
#env(name, value) ⇒ Object
Set an env var to be included on all subsequent requests through the session.
-
#follow_redirect! ⇒ Object
Rack::Test will not follow any redirects automatically.
-
#get(uri, params = {}, env = {}, &block) ⇒ Object
Issue a GET request for the given URI with the given params and Rack environment.
-
#head(uri, params = {}, env = {}, &block) ⇒ Object
Issue a HEAD request for the given URI.
-
#header(name, value) ⇒ Object
Set a header to be included on all subsequent requests through the session.
-
#initialize(mock_session) ⇒ Session
constructor
Creates a Rack::Test::Session for a given Rack app or Rack::MockSession.
-
#options(uri, params = {}, env = {}, &block) ⇒ Object
Issue an OPTIONS request for the given URI.
-
#patch(uri, params = {}, env = {}, &block) ⇒ Object
Issue a PATCH request for the given URI.
-
#post(uri, params = {}, env = {}, &block) ⇒ Object
Issue a POST request for the given URI.
-
#put(uri, params = {}, env = {}, &block) ⇒ Object
Issue a PUT request for the given URI.
-
#request(uri, env = {}, &block) ⇒ Object
Issue a request to the Rack app for the given URI and optional Rack environment.
Methods included from Utils
build_file_part, build_multipart, build_nested_query, build_parts, build_primitive_part, get_parts
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)
51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/rack/test.rb', line 51 def initialize(mock_session) @headers = {} @env = {} @digest_username = nil @digest_password = nil @rack_mock_session = if mock_session.is_a?(MockSession) mock_session else MockSession.new(mock_session) end @default_host = @rack_mock_session.default_host end |
Instance Method Details
#basic_authorize(username, password) ⇒ Object Also known as:
Set the username and password for HTTP Basic authorization, to be included in subsequent requests in the HTTP_AUTHORIZATION header.
Example:
"bryan", "secret"
182 183 184 185 |
# File 'lib/rack/test.rb', line 182 def (username, password) encoded_login = ["#{username}:#{password}"].pack('m0') header('Authorization', "Basic #{encoded_login}") end |
#custom_request(verb, uri, params = {}, env = {}, &block) ⇒ Object
Issue a request using the given verb for the given URI. See #get
Example:
custom_request "LINK", "/"
142 143 144 145 146 |
# File 'lib/rack/test.rb', line 142 def custom_request(verb, uri, params = {}, env = {}, &block) uri = parse_uri(uri, env) env = env_for(uri, env.merge(method: verb.to_s.upcase, params: params)) process_request(uri, env, &block) end |
#delete(uri, params = {}, env = {}, &block) ⇒ Object
Issue a DELETE request for the given URI. See #get
Example:
delete "/"
105 106 107 |
# File 'lib/rack/test.rb', line 105 def delete(uri, params = {}, env = {}, &block) custom_request('DELETE', uri, params, 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:
"bryan", "secret"
194 195 196 197 |
# File 'lib/rack/test.rb', line 194 def (username, password) @digest_username = username @digest_password = password end |
#env(name, value) ⇒ Object
Set an env var to be included on all subsequent requests through the session. Use a value of nil to remove a previously configured env.
Example:
env "rack.session", {:csrf => 'token'}
169 170 171 172 173 174 175 |
# File 'lib/rack/test.rb', line 169 def env(name, value) if value.nil? @env.delete(name) else @env[name] = value end 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.
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 |
# File 'lib/rack/test.rb', line 203 def follow_redirect! unless last_response.redirect? raise Error, 'Last response was not a redirect. Cannot follow_redirect!' end request_method, params = if last_response.status == 307 [last_request.request_method.downcase.to_sym, last_request.params] else [:get, {}] end # Compute the next location by appending the location header with the # last request, as per https://tools.ietf.org/html/rfc7231#section-7.1.2 # Adding two absolute locations returns the right-hand location next_location = URI.parse(last_request.url) + URI.parse(last_response['Location']) send( request_method, next_location.to_s, params, 'HTTP_REFERER' => last_request.url, 'rack.session' => last_request.session, 'rack.session.options' => last_request. ) 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 "/"
73 74 75 |
# File 'lib/rack/test.rb', line 73 def get(uri, params = {}, env = {}, &block) custom_request('GET', uri, params, env, &block) end |
#head(uri, params = {}, env = {}, &block) ⇒ Object
Issue a HEAD request for the given URI. See #get
Example:
head "/"
121 122 123 |
# File 'lib/rack/test.rb', line 121 def head(uri, params = {}, env = {}, &block) custom_request('HEAD', uri, params, 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"
156 157 158 159 160 161 162 |
# File 'lib/rack/test.rb', line 156 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:
"/"
113 114 115 |
# File 'lib/rack/test.rb', line 113 def (uri, params = {}, env = {}, &block) custom_request('OPTIONS', uri, params, env, &block) end |
#patch(uri, params = {}, env = {}, &block) ⇒ Object
Issue a PATCH request for the given URI. See #get
Example:
patch "/"
97 98 99 |
# File 'lib/rack/test.rb', line 97 def patch(uri, params = {}, env = {}, &block) custom_request('PATCH', uri, params, env, &block) end |
#post(uri, params = {}, env = {}, &block) ⇒ Object
Issue a POST request for the given URI. See #get
Example:
post "/signup", "name" => "Bryan"
81 82 83 |
# File 'lib/rack/test.rb', line 81 def post(uri, params = {}, env = {}, &block) custom_request('POST', uri, params, env, &block) end |
#put(uri, params = {}, env = {}, &block) ⇒ Object
Issue a PUT request for the given URI. See #get
Example:
put "/"
89 90 91 |
# File 'lib/rack/test.rb', line 89 def put(uri, params = {}, env = {}, &block) custom_request('PUT', uri, params, 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 "/"
132 133 134 135 136 |
# File 'lib/rack/test.rb', line 132 def request(uri, env = {}, &block) uri = parse_uri(uri, env) env = env_for(uri, env) process_request(uri, env, &block) end |