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 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
Issue a DELETE request for the given URI. See get
Example:
delete "/"
# File lib/rack/test.rb, line 91 def delete(uri, params = {}, env = {}, &block) env = env_for(uri, env.merge(:method => "DELETE", :params => params)) process_request(uri, env, &block) end
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.
# File lib/rack/test.rb, line 168 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
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 "/"
# 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
Issue a HEAD request for the given URI. See get
Example:
head "/"
# File lib/rack/test.rb, line 109 def head(uri, params = {}, env = {}, &block) env = env_for(uri, env.merge(:method => "HEAD", :params => params)) process_request(uri, env, &block) 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 134 def header(name, value) if value.nil? @headers.delete(name) else @headers[name] = value end end
Issue an OPTIONS request for the given URI. See get
Example:
options "/"
# File lib/rack/test.rb, line 100 def options(uri, params = {}, env = {}, &block) env = env_for(uri, env.merge(:method => "OPTIONS", :params => params)) process_request(uri, env, &block) end
Issue a PATCH request for the given URI. See get
Example:
patch "/"
# File lib/rack/test.rb, line 82 def patch(uri, params = {}, env = {}, &block) env = env_for(uri, env.merge(:method => "PATCH", :params => params)) process_request(uri, env, &block) end
Issue a POST request for the given URI. See get
Example:
post "/signup", "name" => "Bryan"
# 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
Issue a PUT request for the given URI. See get
Example:
put "/"
# 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
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 "/"
# File lib/rack/test.rb, line 121 def request(uri, env = {}, &block) env = env_for(uri, env) process_request(uri, env, &block) end
Generated with the Darkfish Rdoc Generator 2.