Class: Rack::Test::Session
- Inherits:
-
Object
- Object
- Rack::Test::Session
- Extended by:
- Forwardable
- Includes:
- Utils
- Defined in:
- lib/rack/test.rb
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.
-
#delete(uri, params = {}, env = {}, &block) ⇒ Object
Issue a DELETE request for the given URI.
- #digest_authorize(username, password) ⇒ Object
-
#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
Initialize a new session for the given Rack app.
-
#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
multipart_body, multipart_requestify, requestify
Constructor Details
#initialize(mock_session) ⇒ Session
Initialize a new session for the given Rack app
31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/rack/test.rb', line 31 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:
Set the username and password for HTTP Basic authorization, to be included in subsequent requests in the HTTP_AUTHORIZATION header.
Example:
"bryan", "secret"
121 122 123 124 |
# File 'lib/rack/test.rb', line 121 def (username, password) encoded_login = ["#{username}:#{password}"].pack("m*") header('HTTP_AUTHORIZATION', "Basic #{encoded_login}") end |
#delete(uri, params = {}, env = {}, &block) ⇒ Object
Issue a DELETE request for the given URI. See #get
Example:
delete "/"
77 78 79 80 |
# File 'lib/rack/test.rb', line 77 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
128 129 130 131 |
# File 'lib/rack/test.rb', line 128 def (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.
136 137 138 139 140 141 142 |
# File 'lib/rack/test.rb', line 136 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 "/"
50 51 52 53 |
# File 'lib/rack/test.rb', line 50 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 "/"
86 87 88 89 |
# File 'lib/rack/test.rb', line 86 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"
108 109 110 111 112 113 114 |
# File 'lib/rack/test.rb', line 108 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"
59 60 61 62 |
# File 'lib/rack/test.rb', line 59 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 "/"
68 69 70 71 |
# File 'lib/rack/test.rb', line 68 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 "/"
98 99 100 101 |
# File 'lib/rack/test.rb', line 98 def request(uri, env = {}, &block) env = env_for(uri, env) process_request(uri, env, &block) end |