forked from rails/activerecord-session_store
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper.rb
79 lines (62 loc) · 2.15 KB
/
helper.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
require 'bundler/setup'
require 'active_record'
require 'action_controller'
require 'action_dispatch'
require 'minitest/autorun'
require 'active_record/session_store'
ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')
SharedTestRoutes = ActionDispatch::Routing::RouteSet.new
module ActionDispatch
module SharedRoutes
def before_setup
@routes = SharedTestRoutes
super
end
end
end
class ActionDispatch::IntegrationTest < ActiveSupport::TestCase
include ActionDispatch::SharedRoutes
def self.build_app(routes = nil)
RoutedRackApp.new(routes || ActionDispatch::Routing::RouteSet.new) do |middleware|
middleware.use ActionDispatch::DebugExceptions
middleware.use ActionDispatch::Callbacks
middleware.use ActionDispatch::Cookies
middleware.use ActionDispatch::Flash
middleware.use Rack::Head
yield(middleware) if block_given?
end
end
private
def with_test_route_set(options = {})
controller_namespace = self.class.to_s.underscore
actions = %w[set_session_value get_session_value call_reset_session renew get_session_id]
with_routing do |set|
set.draw do
actions.each { |action| get action, controller: "#{controller_namespace}/test" }
end
@app = self.class.build_app(set) do |middleware|
middleware.use ActionDispatch::Session::ActiveRecordStore, options.reverse_merge(:key => '_session_id')
middleware.delete ActionDispatch::ShowExceptions
end
yield
end
end
def with_store(class_name)
session_class, ActionDispatch::Session::ActiveRecordStore.session_class =
ActionDispatch::Session::ActiveRecordStore.session_class, "ActiveRecord::SessionStore::#{class_name.camelize}".constantize
yield
ensure
ActionDispatch::Session::ActiveRecordStore.session_class = session_class
end
end
class RoutedRackApp
attr_reader :routes
def initialize(routes, &blk)
@routes = routes
@stack = ActionDispatch::MiddlewareStack.new(&blk).build(@routes)
end
def call(env)
@stack.call(env)
end
end
ActiveSupport::TestCase.test_order = :random