/
methods.rb
94 lines (84 loc) · 2.88 KB
/
methods.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# frozen_string_literal: true
require 'forwardable'
module Rack
module Test
# This module serves as the primary integration point for using Rack::Test
# in a testing environment. It depends on an app method being defined in the
# same context, and provides the Rack::Test API methods (see Rack::Test::Session
# for their documentation). It defines the following methods that are delegated
# to the current session: :request, :get, :post, :put, :patch, :delete, :options,
# :head, :custom_request, :follow_redirect!, :header, :env, :set_cookie,
# :clear_cookies, :authorize, :basic_authorize, :last_response, and :last_request.
#
# Example:
#
# class HomepageTest < Test::Unit::TestCase
# include Rack::Test::Methods
#
# def app
# MyApp
# end
# end
module Methods
extend Forwardable
# Return the existing session with the given name, or a new
# rack session. Always use a new session if name is nil.
def rack_test_session(name = :default) # :nodoc:
return build_rack_test_session(name) unless name
@_rack_test_sessions ||= {}
@_rack_test_sessions[name] ||= build_rack_test_session(name)
end
# For backwards compatibility with older rack-test versions.
alias rack_mock_session rack_test_session # :nodoc:
# Create a new Rack::Test::Session for #app.
def build_rack_test_session(_name) # :nodoc:
if respond_to?(:build_rack_mock_session, true)
# Backwards compatibility for capybara
build_rack_mock_session
else
if respond_to?(:default_host)
Session.new(app, default_host)
else
Session.new(app)
end
end
end
# Return the currently actively session. This is the session to
# which the delegated methods are sent.
def current_session
@_rack_test_current_session ||= rack_test_session
end
# Create a new session (or reuse an existing session with the given name),
# and make it the current session for the given block.
def with_session(name)
session = _rack_test_current_session
yield(@_rack_test_current_session = rack_test_session(name))
ensure
@_rack_test_current_session = session
end
def_delegators(:current_session,
:request,
:get,
:post,
:put,
:patch,
:delete,
:options,
:head,
:custom_request,
:follow_redirect!,
:header,
:env,
:set_cookie,
:clear_cookies,
:authorize,
:basic_authorize,
:last_response,
:last_request,
)
# Private accessor to avoid uninitialized instance variable warning in Ruby 2.*
attr_accessor :_rack_test_current_session
private :_rack_test_current_session
end
end
end