Class: Rack::MockSession
- Inherits:
-
Object
- Object
- Rack::MockSession
- Defined in:
- lib/rack/mock_session.rb
Overview
:nodoc:
Instance Attribute Summary collapse
- #cookie_jar ⇒ Object
-
#default_host ⇒ Object
readonly
Returns the value of attribute default_host.
Instance Method Summary collapse
- #after_request(&block) ⇒ Object
- #clear_cookies ⇒ Object
-
#initialize(app, default_host = Rack::Test::DEFAULT_HOST) ⇒ MockSession
constructor
A new instance of MockSession.
-
#last_request ⇒ Object
Return the last request issued in the session.
-
#last_response ⇒ Object
Return the last response received in the session.
- #request(uri, env) ⇒ Object
- #set_cookie(cookie, uri = nil) ⇒ Object
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
#cookie_jar ⇒ Object
60 61 62 |
# File 'lib/rack/mock_session.rb', line 60 def @cookie_jar ||= Rack::Test::CookieJar.new([], @default_host) end |
#default_host ⇒ Object (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_cookies ⇒ Object
19 20 21 |
# File 'lib/rack/mock_session.rb', line 19 def @cookie_jar = Rack::Test::CookieJar.new([], @default_host) end |
#last_request ⇒ Object
Return the last request issued in the session. Raises an error if no requests have been sent yet.
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_response ⇒ Object
Return the last response received in the session. Raises an error if no requests have been sent yet.
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"] ||= .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) .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 |
#set_cookie(cookie, uri = nil) ⇒ Object
23 24 25 |
# File 'lib/rack/mock_session.rb', line 23 def (, uri = nil) .merge(, uri) end |