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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
|
require "isolation/abstract_unit"
module ApplicationTests
class ConfigurationTest < Test::Unit::TestCase
include ActiveSupport::Testing::Isolation
def new_app
File.expand_path("#{app_path}/../new_app")
end
def copy_app
FileUtils.cp_r(app_path, new_app)
end
def app
@app ||= Rails.application
end
def setup
build_app
boot_rails
FileUtils.rm_rf("#{app_path}/config/environments")
end
def teardown
FileUtils.rm_rf(new_app) if File.directory?(new_app)
end
test "Rails::Application.instance is nil until app is initialized" do
require 'rails'
assert_nil Rails::Application.instance
require "#{app_path}/config/environment"
assert_equal AppTemplate::Application.instance, Rails::Application.instance
end
test "Rails::Application responds to all instance methods" do
require "#{app_path}/config/environment"
assert_respond_to Rails::Application, :routes_reloader
assert_equal Rails::Application.routes_reloader, Rails.application.routes_reloader
assert_equal Rails::Application.routes_reloader, AppTemplate::Application.routes_reloader
end
test "Rails::Application responds to paths" do
require "#{app_path}/config/environment"
assert_respond_to AppTemplate::Application, :paths
assert_equal AppTemplate::Application.paths.app.views.to_a, ["#{app_path}/app/views"]
end
test "the application root is set correctly" do
require "#{app_path}/config/environment"
assert_equal Pathname.new(app_path), Rails.application.root
end
test "the application root can be seen from the application singleton" do
require "#{app_path}/config/environment"
assert_equal Pathname.new(app_path), AppTemplate::Application.root
end
test "the application root can be set" do
copy_app
add_to_config <<-RUBY
config.root = '#{new_app}'
RUBY
use_frameworks []
require "#{app_path}/config/environment"
assert_equal Pathname.new(new_app), Rails.application.root
end
test "the application root is Dir.pwd if there is no config.ru" do
File.delete("#{app_path}/config.ru")
use_frameworks []
Dir.chdir("#{app_path}") do
require "#{app_path}/config/environment"
assert_equal Pathname.new("#{app_path}"), Rails.application.root
end
end
test "Rails.root should be a Pathname" do
add_to_config <<-RUBY
config.root = "#{app_path}"
RUBY
require "#{app_path}/config/environment"
assert_instance_of Pathname, Rails.root
end
test "marking the application as threadsafe sets the correct config variables" do
add_to_config <<-RUBY
config.threadsafe!
RUBY
require "#{app_path}/config/application"
assert AppTemplate::Application.config.allow_concurrency
end
test "the application can be marked as threadsafe when there are no frameworks" do
FileUtils.rm_rf("#{app_path}/config/environments")
add_to_config <<-RUBY
config.threadsafe!
RUBY
use_frameworks []
assert_nothing_raised do
require "#{app_path}/config/application"
end
end
test "Frameworks are not preloaded by default" do
require "#{app_path}/config/environment"
assert ActionController.autoload?(:RecordIdentifier)
end
test "frameworks are preloaded with config.preload_frameworks is set" do
add_to_config <<-RUBY
config.preload_frameworks = true
RUBY
require "#{app_path}/config/environment"
assert !ActionController.autoload?(:RecordIdentifier)
end
test "runtime error is raised if config.frameworks= is used" do
add_to_config "config.frameworks = []"
assert_raises RuntimeError do
require "#{app_path}/config/environment"
end
end
test "runtime error is raised if config.frameworks is used" do
add_to_config "config.frameworks -= []"
assert_raises RuntimeError do
require "#{app_path}/config/environment"
end
end
test "filter_parameters should be able to set via config.filter_parameters" do
add_to_config <<-RUBY
config.filter_parameters += [ :foo, 'bar', lambda { |key, value|
value = value.reverse if key =~ /baz/
}]
RUBY
assert_nothing_raised do
require "#{app_path}/config/application"
end
end
test "config.to_prepare is forwarded to ActionDispatch" do
$prepared = false
add_to_config <<-RUBY
config.to_prepare do
$prepared = true
end
RUBY
assert !$prepared
require "#{app_path}/config/environment"
require 'rack/test'
extend Rack::Test::Methods
get "/"
assert $prepared
end
test "config.action_dispatch.x_sendfile_header defaults to X-Sendfile" do
require "rails"
require "action_controller/railtie"
class MyApp < Rails::Application
config.cookie_secret = "3b7cd727ee24e8444053437c36cc66c4"
config.session_store :cookie_store, :key => "_myapp_session"
end
MyApp.initialize!
class ::OmgController < ActionController::Base
def index
send_file __FILE__
end
end
MyApp.routes.draw do
match "/" => "omg#index"
end
require 'rack/test'
extend Rack::Test::Methods
get "/"
assert_equal File.expand_path(__FILE__), last_response.headers["X-Sendfile"]
end
test "config.action_dispatch.x_sendfile_header is sent to Rack::Sendfile" do
require "rails"
require "action_controller/railtie"
class MyApp < Rails::Application
config.cookie_secret = "3b7cd727ee24e8444053437c36cc66c4"
config.session_store :cookie_store, :key => "_myapp_session"
config.action_dispatch.x_sendfile_header = 'X-Lighttpd-Send-File'
end
MyApp.initialize!
class ::OmgController < ActionController::Base
def index
send_file __FILE__
end
end
MyApp.routes.draw do
match "/" => "omg#index"
end
require 'rack/test'
extend Rack::Test::Methods
get "/"
assert_equal File.expand_path(__FILE__), last_response.headers["X-Lighttpd-Send-File"]
end
test "protect from forgery is the default in a new app" do
require "rails"
require "action_controller/railtie"
class MyApp < Rails::Application
config.session_store :disabled
routes.draw do
match "/" => "omg#index"
end
class ::OmgController < ActionController::Base
protect_from_forgery
def index
render :inline => "<%= csrf_meta_tag %>"
end
end
end
require 'rack/test'
extend Rack::Test::Methods
get "/"
assert last_response.body =~ /csrf\-param/
end
end
end
|