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.


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

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


59
60
61
# File 'lib/rack/mock_session.rb', line 59

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

#default_hostObject (readonly)

Returns the value of attribute default_host


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

def default_host
  @default_host
end

Instance Method Details

#after_request(&block) ⇒ Object


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

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

#clear_cookiesObject


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

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:


47
48
49
50
# File 'lib/rack/mock_session.rb', line 47

def last_request
  raise Rack::Test::Error, '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:


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

def last_response
  raise Rack::Test::Error, 'No response yet. Request a page first.' unless @last_response
  @last_response
end

#request(uri, env) ⇒ Object


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

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(&:call)

  if @last_response.respond_to?(:finish)
    @last_response.finish
  else
    @last_response
  end
end

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

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