Class: Rack::MockSession

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/mock_session.rb

Overview

:nodoc:

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, default_host = Rack::Test::DEFAULT_HOST) ⇒ MockSession

Returns a new instance of MockSession.



7
8
9
10
11
12
13
# File 'lib/rack/mock_session.rb', line 7

def initialize(app, default_host = Rack::Test::DEFAULT_HOST)
  @app = app
  @after_request = []
  @default_host = default_host
  @last_request = nil
  @last_response = nil
end

Instance Attribute Details



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

def cookie_jar
  @cookie_jar ||= Rack::Test::CookieJar.new([], @default_host)
end

#default_hostObject (readonly)

Returns the value of attribute default_host.



5
6
7
# File 'lib/rack/mock_session.rb', line 5

def default_host
  @default_host
end

Instance Method Details

#after_request(&block) ⇒ Object



15
16
17
# File 'lib/rack/mock_session.rb', line 15

def after_request(&block)
  @after_request << block
end

#clear_cookiesObject



19
20
21
# File 'lib/rack/mock_session.rb', line 19

def clear_cookies
  @cookie_jar = Rack::Test::CookieJar.new([], @default_host)
end

#last_requestObject

Return the last request issued in the session. Raises an error if no requests have been sent yet.

Raises:



43
44
45
46
# File 'lib/rack/mock_session.rb', line 43

def last_request
  raise Rack::Test::Error.new("No request yet. Request a page first.") unless @last_request
  @last_request
end

#last_responseObject

Return the last response received in the session. Raises an error if no requests have been sent yet.

Raises:



50
51
52
53
# File 'lib/rack/mock_session.rb', line 50

def last_response
  raise Rack::Test::Error.new("No response yet. Request a page first.") unless @last_response
  @last_response
end

#request(uri, env) ⇒ Object



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

def request(uri, env)
  env["HTTP_COOKIE"] ||= cookie_jar.for(uri)
  @last_request = Rack::Request.new(env)
  status, headers, body = @app.call(@last_request.env)

  @last_response = MockResponse.new(status, headers, body, env["rack.errors"].flush)
  body.close if body.respond_to?(:close)

  cookie_jar.merge(last_response.headers["Set-Cookie"], uri)

  @after_request.each { |hook| hook.call }
  @last_response
end


23
24
25
# File 'lib/rack/mock_session.rb', line 23

def set_cookie(cookie, uri = nil)
  cookie_jar.merge(cookie, uri)
end