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



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

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:



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

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:



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

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
40
41
42
43
44
# 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 }

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


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

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