forked from jruby/jruby-rack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathintegration_spec.rb
More file actions
479 lines (357 loc) · 16.8 KB
/
integration_spec.rb
File metadata and controls
479 lines (357 loc) · 16.8 KB
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
require File.expand_path('spec_helper', File.dirname(__FILE__) + '/../..')
java_import org.jruby.rack.RackContext
java_import org.jruby.rack.servlet.ServletRackContext
java_import org.jruby.rack.RackApplication
java_import org.jruby.rack.DefaultRackApplication
java_import org.jruby.rack.RackApplicationFactory
java_import org.jruby.rack.DefaultRackApplicationFactory
java_import org.jruby.rack.SharedRackApplicationFactory
java_import org.jruby.rack.PoolingRackApplicationFactory
java_import org.jruby.rack.rails.RailsRackApplicationFactory
describe "integration" do
before(:all) { require 'fileutils' }
#after(:all) { JRuby::Rack.context = nil }
describe 'rack (lambda)' do
before do
@servlet_context = org.jruby.rack.mock.MockServletContext.new "file://#{STUB_DIR}/rack"
@servlet_context.logger = raise_logger
# make sure we always boot runtimes in the same mode as specs :
set_compat_version @servlet_context
end
it "initializes" do
@servlet_context.addInitParameter('rackup',
"run lambda { |env| [ 200, {'Content-Type' => 'text/plain'}, 'OK' ] }"
)
listener = org.jruby.rack.RackServletContextListener.new
listener.contextInitialized javax.servlet.ServletContextEvent.new(@servlet_context)
rack_factory = @servlet_context.getAttribute("rack.factory")
rack_factory.should be_a(RackApplicationFactory)
rack_factory.should be_a(SharedRackApplicationFactory)
rack_factory.realFactory.should be_a(DefaultRackApplicationFactory)
@servlet_context.getAttribute("rack.context").should be_a(RackContext)
@servlet_context.getAttribute("rack.context").should be_a(ServletRackContext)
end
context "initialized" do
before :each do
@servlet_context.addInitParameter('rackup',
"run lambda { |env| [ 200, {'Via' => 'JRuby-Rack', 'Content-Type' => 'text/plain'}, 'OK' ] }"
)
listener = org.jruby.rack.RackServletContextListener.new
listener.contextInitialized javax.servlet.ServletContextEvent.new(@servlet_context)
@rack_context = @servlet_context.getAttribute("rack.context")
@rack_factory = @servlet_context.getAttribute("rack.factory")
end
it "inits servlet" do
servlet_config = org.jruby.rack.mock.MockServletConfig.new @servlet_context
servlet = org.jruby.rack.RackServlet.new
servlet.init(servlet_config)
expect( servlet.getContext ).to_not be nil
expect( servlet.getDispatcher ).to_not be nil
end
it "serves (servlet)" do
dispatcher = org.jruby.rack.DefaultRackDispatcher.new(@rack_context)
servlet = org.jruby.rack.RackServlet.new(dispatcher, @rack_context)
request = org.jruby.rack.mock.MockHttpServletRequest.new(@servlet_context)
request.setMethod("GET")
request.setRequestURI("/")
request.setContentType("text/html")
request.setContent("".to_java.bytes)
response = org.jruby.rack.mock.MockHttpServletResponse.new
servlet.service(request, response)
expect( response.getStatus ).to eql 200
expect( response.getContentType ).to eql 'text/plain'
expect( response.getContentAsString ).to eql 'OK'
expect( response.getHeader("Via") ).to eql 'JRuby-Rack'
end
end
end
shared_examples_for 'a rails app', :shared => true do
let(:servlet_context) { new_servlet_context(base_path) }
it "initializes (pooling by default)" do
listener = org.jruby.rack.rails.RailsServletContextListener.new
listener.contextInitialized javax.servlet.ServletContextEvent.new(servlet_context)
rack_factory = servlet_context.getAttribute("rack.factory")
rack_factory.should be_a(RackApplicationFactory)
rack_factory.should be_a(PoolingRackApplicationFactory)
rack_factory.should respond_to(:realFactory)
rack_factory.realFactory.should be_a(RailsRackApplicationFactory)
servlet_context.getAttribute("rack.context").should be_a(RackContext)
servlet_context.getAttribute("rack.context").should be_a(ServletRackContext)
rack_factory.getApplication.should be_a(DefaultRackApplication)
end
it "initializes threadsafe!" do
servlet_context.addInitParameter('jruby.max.runtimes', '1')
listener = org.jruby.rack.rails.RailsServletContextListener.new
listener.contextInitialized javax.servlet.ServletContextEvent.new(servlet_context)
rack_factory = servlet_context.getAttribute("rack.factory")
rack_factory.should be_a(RackApplicationFactory)
rack_factory.should be_a(SharedRackApplicationFactory)
rack_factory.realFactory.should be_a(RailsRackApplicationFactory)
rack_factory.getApplication.should be_a(DefaultRackApplication)
end
end
describe 'rails 3.0', :lib => :rails30 do
before(:all) { copy_gemfile("rails30") }
def base_path; "file://#{STUB_DIR}/rails30" end
it_should_behave_like 'a rails app'
context "initialized" do
before :all do
initialize_rails nil, base_path
end
after(:all) { restore_rails }
it "loaded rack ~> 1.2" do
@runtime = @rack_factory.getApplication.getRuntime
should_eval_as_not_nil "defined?(Rack.release)"
should_eval_as_eql_to "Rack.release.to_s[0, 3]", '1.2'
end
it "disables rack's chunked support (by default)" do
@runtime = @rack_factory.getApplication.getRuntime
expect_to_have_monkey_patched_chunked
end
end
context "initialized (custom)" do
before :all do
initialize_rails 'custom', base_path
end
it "booted a custom env with a custom logger" do
@runtime = @rack_factory.getApplication.getRuntime
should_eval_as_not_nil "defined?(Rails)"
should_eval_as_eql_to "Rails.env", 'custom'
should_eval_as_not_nil "Rails.logger"
should_eval_as_eql_to "Rails.logger.class.name", 'CustomLogger'
end
end
end
describe 'rails 3.1', :lib => :rails31 do
before(:all) { copy_gemfile("rails31") }
def base_path; "file://#{STUB_DIR}/rails31" end
it_should_behave_like 'a rails app'
context "initialized" do
before :all do
initialize_rails 'production', base_path
end
after(:all) { restore_rails }
it "loaded META-INF/init.rb" do
@runtime = @rack_factory.getApplication.getRuntime
should_eval_as_not_nil "defined?(WARBLER_CONFIG)"
end
it "loaded rack ~> 1.3" do
@runtime = @rack_factory.getApplication.getRuntime
should_eval_as_not_nil "defined?(Rack.release)"
should_eval_as_eql_to "Rack.release.to_s[0, 3]", '1.3'
end
it "booted with a servlet logger" do
@runtime = @rack_factory.getApplication.getRuntime
should_eval_as_not_nil "defined?(Rails)"
should_eval_as_not_nil "Rails.logger"
should_eval_as_not_nil "Rails.logger.instance_variable_get(:'@logdev')" # Logger::LogDevice
should_eval_as_eql_to "Rails.logger.instance_variable_get(:'@logdev').dev.class.name", 'JRuby::Rack::ServletLog'
should_eval_as_eql_to "Rails.logger.level", Logger::DEBUG
end
it "disables rack's chunked support (by default)" do
@runtime = @rack_factory.getApplication.getRuntime
expect_to_have_monkey_patched_chunked
end
end
end
describe 'rails 3.2', :lib => :rails32 do
before(:all) { copy_gemfile("rails32") }
def base_path; "file://#{STUB_DIR}/rails32" end
it_should_behave_like 'a rails app'
context "initialized" do
before(:all) { initialize_rails 'production', base_path }
after(:all) { restore_rails }
it "loaded rack ~> 1.4" do
@runtime = @rack_factory.getApplication.getRuntime
should_eval_as_not_nil "defined?(Rack.release)"
should_eval_as_eql_to "Rack.release.to_s[0, 3]", '1.4'
end
it "booted with a servlet logger" do
@runtime = @rack_factory.getApplication.getRuntime
should_eval_as_not_nil "defined?(Rails)"
should_eval_as_not_nil "Rails.logger"
should_eval_as_eql_to "Rails.logger.class.name", 'ActiveSupport::TaggedLogging'
should_eval_as_not_nil "Rails.logger.instance_variable_get(:'@logger')"
should_eval_as_eql_to "logger = Rails.logger.instance_variable_get(:'@logger'); " +
"logger.instance_variable_get(:'@logdev').dev.class.name", 'JRuby::Rack::ServletLog'
should_eval_as_eql_to "Rails.logger.level", Logger::INFO
end
it "sets up public_path (as for a war)" do
@runtime = @rack_factory.getApplication.getRuntime
should_eval_as_eql_to "Rails.public_path", "#{STUB_DIR}/rails32"
# make sure it was set early on (before initializers run) :
should_eval_as_not_nil "defined? Rails32::Application::PUBLIC_PATH"
should_eval_as_eql_to "Rails32::Application::PUBLIC_PATH", "#{STUB_DIR}/rails32"
# check if image_tag resolves path to images correctly :
should_eval_as_eql_to %q{
config = ActionController::Base.config;
asset_paths = ActionView::Helpers::AssetTagHelper::AssetPaths.new(config);
image_path = asset_paths.compute_public_path('image.jpg', 'images');
image_path[0, 18]
}, '/images/image.jpg?'
end
it "disables rack's chunked support (by default)" do
@runtime = @rack_factory.getApplication.getRuntime
expect_to_have_monkey_patched_chunked
end
end
end
describe 'rails 4.0', :lib => :rails40 do
before(:all) { copy_gemfile("rails40") }
def base_path; "file://#{STUB_DIR}/rails40" end
it_should_behave_like 'a rails app'
context "initialized" do
before(:all) { initialize_rails 'production', base_path }
after(:all) { restore_rails }
it "loaded rack ~> 1.5" do
@runtime = @rack_factory.getApplication.getRuntime
should_eval_as_not_nil "defined?(Rack.release)"
should_eval_as_eql_to "Rack.release.to_s[0, 3]", '1.5'
end
it "booted with a servlet logger" do
@runtime = @rack_factory.getApplication.getRuntime
should_eval_as_not_nil "defined?(Rails)"
should_eval_as_not_nil "Rails.logger"
# NOTE: TaggedLogging is a module that extends the instance now :
should_eval_as_eql_to "Rails.logger.is_a? ActiveSupport::TaggedLogging", true
should_eval_as_eql_to "Rails.logger.instance_variable_get(:'@logdev').dev.class.name",
'JRuby::Rack::ServletLog'
should_eval_as_eql_to "Rails.logger.level", Logger::INFO
end
it "sets up public_path (as for a war)" do
@runtime = @rack_factory.getApplication.getRuntime
should_eval_as_eql_to "Rails.public_path.to_s", "#{STUB_DIR}/rails40" # Pathname
# due config.assets.digest = true and since we're asset pre-compiled :
#should_eval_as_eql_to %q{
# ActionController::Base.helpers.image_path('image.jpg')[0, 14];
#}, '/assets/image-'
end
end
end
describe 'rails 4.1', :lib => :rails41 do
before(:all) do name = :rails41 # copy_gemfile :
FileUtils.cp File.join(GEMFILES_DIR, "#{name}.gemfile"), File.join(STUB_DIR, "#{name}/Gemfile")
FileUtils.cp File.join(GEMFILES_DIR, "#{name}.gemfile.lock"), File.join(STUB_DIR, "#{name}/Gemfile.lock")
Dir.chdir File.join(STUB_DIR, name.to_s)
end
def prepare_servlet_context(servlet_context)
servlet_context.addInitParameter('rails.root', "#{STUB_DIR}/rails41")
servlet_context.addInitParameter('jruby.rack.layout_class', 'FileSystemLayout')
end
def base_path; "file://#{STUB_DIR}/rails41" end
# let(:base_path) { "file://#{STUB_DIR}/rails41" }
it_should_behave_like 'a rails app'
context "initialized" do
before(:all) { initialize_rails 'production', base_path }
after(:all) { restore_rails }
it "loaded rack ~> 1.5" do
@runtime = @rack_factory.getApplication.getRuntime
should_eval_as_not_nil "defined?(Rack.release)"
should_eval_as_eql_to "Rack.release.to_s[0, 3]", '1.5'
end
it "booted with a servlet logger" do
@runtime = @rack_factory.getApplication.getRuntime
should_eval_as_not_nil "defined?(Rails)"
should_eval_as_not_nil "Rails.logger"
# NOTE: TaggedLogging is a module that extends the instance now :
should_eval_as_eql_to "Rails.logger.is_a? ActiveSupport::TaggedLogging", true
should_eval_as_eql_to "Rails.logger.instance_variable_get(:'@logdev').dev.class.name",
'JRuby::Rack::ServletLog'
should_eval_as_eql_to "Rails.logger.level", Logger::INFO
end
it "sets up public_path (as for a war)" do
@runtime = @rack_factory.getApplication.getRuntime
should_eval_as_eql_to "Rails.public_path.to_s", "#{STUB_DIR}/rails41/public"
end
end
end
describe 'rails 2.3', :lib => :rails23 do
before(:all) do
copy_gemfile('rails23')
path = File.join(STUB_DIR, 'rails23/WEB-INF/init.rb') # hard-coded RAILS_GEM_VERSION
File.open(path, 'w') { |f| f << "RAILS_GEM_VERSION = '#{Rails::VERSION::STRING}'\n" }
end
let(:base_path) { "file://#{STUB_DIR}/rails23" }
it_should_behave_like 'a rails app'
context "initialized" do
before(:all) { initialize_rails 'production', base_path }
after(:all) { restore_rails }
it "loaded rack ~> 1.1" do
@runtime = @rack_factory.getApplication.getRuntime
should_eval_as_not_nil "defined?(Rack.release)"
should_eval_as_eql_to "Rack.release.to_s[0, 3]", '1.1'
end
it "booted with a servlet logger" do
@runtime = @rack_factory.getApplication.getRuntime
should_eval_as_not_nil "defined?(Rails)"
should_eval_as_not_nil "defined?(Rails.logger)"
should_eval_as_not_nil "defined?(ActiveSupport::BufferedLogger) && Rails.logger.is_a?(ActiveSupport::BufferedLogger)"
should_eval_as_not_nil "Rails.logger.send(:instance_variable_get, '@log')"
should_eval_as_eql_to "log = Rails.logger.send(:instance_variable_get, '@log');" +
"log.class.name", 'JRuby::Rack::ServletLog'
should_eval_as_eql_to "Rails.logger.level", Logger::INFO
@runtime.evalScriptlet "Rails.logger.debug 'logging works'"
end
end
end
def expect_to_have_monkey_patched_chunked
@runtime.evalScriptlet "require 'rack/chunked'"
script = %{
headers = { 'Transfer-Encoding' => 'chunked' }
body = [ \"1\".freeze, \"\", \"\nsecond\" ]
if defined? Rack::Chunked::Body # Rails 3.x
body = Rack::Chunked::Body.new body
else # Rack 1.1 / 1.2
chunked = Rack::Chunked.new(nil)
chunked.chunk(200, headers, body)
body = chunked
end
parts = []; body.each { |part| parts << part }
parts.join
}
should_eval_as_eql_to script, "1\nsecond"
end
ENV_COPY = ENV.to_h
def initialize_rails(env = nil, servlet_context = @servlet_context)
if ! servlet_context || servlet_context.is_a?(String)
base = servlet_context.is_a?(String) ? servlet_context : nil
servlet_context = new_servlet_context(base)
end
listener = org.jruby.rack.rails.RailsServletContextListener.new
# Travis-CI might have RAILS_ENV=test set, which is not desired for us :
#if ENV['RAILS_ENV'] || ENV['RACK_ENV']
#ENV['RAILS_ENV'] = env; ENV.delete('RACK_ENV')
#end
the_env = "GEM_HOME=#{ENV['GEM_HOME']},GEM_PATH=#{ENV['GEM_PATH']}"
the_env << "\nRAILS_ENV=#{env}" if env
servlet_context.addInitParameter("jruby.runtime.env", the_env)
yield(servlet_context, listener) if block_given?
listener.contextInitialized javax.servlet.ServletContextEvent.new(servlet_context)
@rack_context = servlet_context.getAttribute("rack.context")
@rack_factory = servlet_context.getAttribute("rack.factory")
@servlet_context ||= servlet_context
end
def restore_rails
#ENV['RACK_ENV'] = ENV_COPY['RACK_ENV'] if ENV.key?('RACK_ENV')
#ENV['RAILS_ENV'] = ENV_COPY['RAILS_ENV'] if ENV.key?('RAILS_ENV')
end
def new_servlet_context(base_path = nil)
servlet_context = org.jruby.rack.mock.MockServletContext.new base_path
servlet_context.logger = raise_logger
prepare_servlet_context servlet_context
servlet_context
end
def prepare_servlet_context(servlet_context)
set_compat_version servlet_context
end
def set_compat_version(servlet_context = @servlet_context); require 'jruby'
compat_version = JRuby.runtime.getInstanceConfig.getCompatVersion # RUBY1_9
servlet_context.addInitParameter("jruby.compat.version", compat_version.to_s)
end
private
GEMFILES_DIR = File.expand_path('../../../gemfiles', STUB_DIR)
def copy_gemfile(name) # e.g. 'rails30'
FileUtils.cp File.join(GEMFILES_DIR, "#{name}.gemfile"), File.join(STUB_DIR, "#{name}/WEB-INF/Gemfile")
FileUtils.cp File.join(GEMFILES_DIR, "#{name}.gemfile.lock"), File.join(STUB_DIR, "#{name}/WEB-INF/Gemfile.lock")
end
end