forked from rstacruz/sinatra-assetpack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathasset_host_test.rb
72 lines (59 loc) · 2.31 KB
/
asset_host_test.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
require File.expand_path('../test_helper', __FILE__)
class AssetHostTest < UnitTest
class App < Main
assets {
serve '/css', :from => 'app/css'
serve '/js', :from => 'app/js'
serve '/images', :from => 'app/images'
asset_hosts [
'//cdn-0.example.org',
'//cdn-1.example.org'
]
css :a, ["/css/style.css"]
js :b, ["/js/hello.js"]
}
get('/helper/img') { img '/images/background.jpg' }
end
def app
App
end
test "hosts sets option" do
assert app.assets.asset_hosts.include? '//cdn-0.example.org'
assert app.assets.asset_hosts.include? '//cdn-1.example.org'
end
test "host gets added to css source path" do
app.stubs(:development?).returns(false)
assert App.assets.packages['a.css'].to_production_html('/') =~ %r{href='//cdn-[0|1].example.org/assets/a.[a-f0-9]{32}.css'}
end
test "host gets added to js source path" do
app.stubs(:development?).returns(false)
assert App.assets.packages['b.js'].to_production_html('/') =~ %r{src='//cdn-[0|1].example.org/assets/b.[a-f0-9]{32}.js'}
end
test "host gets added to image helper path in production" do
app.stubs(:development?).returns(false)
get '/helper/img'
assert body =~ /<img src='\/\/cdn-[0|1].example.org\/images\/background.[a-f0-9]{32}.jpg' \/>/
end
test "host doesn't get added to image helper path in development" do
app.stubs(:development?).returns(true)
get '/helper/img'
assert body =~ /<img src='\/images\/background.jpg' \/>/
end
test "host gets added to css image path in production" do
app.stubs(:development?).returns(false)
get '/css/style.css'
assert body =~ /background: url\(\/\/cdn-[0|1].example.org\/images\/background.[a-f0-9]{32}.jpg\)/
# Does not alter not served assets
assert body.include?('background: url(/images/404.png)')
end
test "do not add asset host to filename in dev mode" do
app.stubs(:development?).returns(true)
file = '/js/hello.js'
assert !(Sinatra::AssetPack::HtmlHelpers.get_file_uri(file, App.assets) =~ /cdn-[0|1].example.org/)
end
test "add asset host to filename in production/qa mode" do
app.stubs(:development?).returns(false)
file = '/js/hello.js'
assert Sinatra::AssetPack::HtmlHelpers.get_file_uri(file, App.assets) =~ /cdn-[0|1].example.org/
end
end