/
spec_rack_try_static.rb
59 lines (51 loc) · 1.61 KB
/
spec_rack_try_static.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
# frozen_string_literal: true
require 'minitest/autorun'
require 'rack'
require 'rack/contrib/try_static'
require 'rack/mock'
def build_options(opts)
{
:urls => %w[/],
:root => ::File.expand_path(::File.dirname(__FILE__)),
}.merge(opts)
end
def request(options = {})
@request =
Rack::MockRequest.new(
Rack::Lint.new(
Rack::TryStatic.new(
lambda { |_| [200, {}, ["Hello World"]]},
options)))
end
describe "Rack::TryStatic" do
describe 'when file cannot be found' do
it 'should call call app' do
res = request(build_options(:try => ['html'])).get('/statics')
_(res.ok?).must_equal(true)
_(res.body).must_equal "Hello World"
end
end
describe 'when file can be found' do
it 'should serve first found' do
res = request(build_options(:try => ['.html', '/index.html', '/index.htm'])).get('/statics')
_(res.ok?).must_equal(true)
_(res.body.strip).must_equal "index.html"
end
end
describe 'when path_info maps directly to file' do
it 'should serve existing' do
res = request(build_options(:try => ['/index.html'])).get('/statics/existing.html')
_(res.ok?).must_equal(true)
_(res.body.strip).must_equal "existing.html"
end
end
describe 'when sharing options' do
it 'should not mutate given options' do
org_options = build_options :try => ['/index.html']
given_options = org_options.dup
_(request(given_options).get('/statics').ok?).must_equal(true)
_(request(given_options).get('/statics').ok?).must_equal(true)
_(given_options).must_equal org_options
end
end
end