Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 346 lines (279 sloc) 13.34 kB
2b6d8dc @guilleiguaran Fix helper tests
guilleiguaran authored
1 require File.expand_path(File.dirname(__FILE__) + "/test_helper")
c2ac118 @guilleiguaran Importing tests from Rails
guilleiguaran authored
2
2b6d8dc @guilleiguaran Fix helper tests
guilleiguaran authored
3 class SprocketsHelperTest < ActiveSupport::TestCase
4 include Sprockets::Rails::Helpers::RailsHelper
c2ac118 @guilleiguaran Importing tests from Rails
guilleiguaran authored
5
2b6d8dc @guilleiguaran Fix helper tests
guilleiguaran authored
6 attr_accessor :assets, :controller, :params
c2ac118 @guilleiguaran Importing tests from Rails
guilleiguaran authored
7
8 class MockRequest
9 def protocol() 'http://' end
10 def ssl?() false end
11 def host_with_port() 'localhost' end
12 def script_name() nil end
13 end
14
15 def setup
16 @controller = BasicController.new
17 @controller.request = MockRequest.new
18
19 @assets = Sprockets::Environment.new
2b6d8dc @guilleiguaran Fix helper tests
guilleiguaran authored
20 @assets.append_path(FIXTURES.join("app/javascripts"))
21 @assets.append_path(FIXTURES.join("app/stylesheets"))
22 @assets.append_path(FIXTURES.join("app/images"))
23 @assets.append_path(FIXTURES.join("app/fonts"))
c2ac118 @guilleiguaran Importing tests from Rails
guilleiguaran authored
24
25 application = Struct.new(:config, :assets).new(config, @assets)
2b6d8dc @guilleiguaran Fix helper tests
guilleiguaran authored
26 ::Rails.stubs(:application).returns(application)
c2ac118 @guilleiguaran Importing tests from Rails
guilleiguaran authored
27 @config = config
28 @config.perform_caching = true
29 @config.assets.digest = true
30 @config.assets.compile = true
2b6d8dc @guilleiguaran Fix helper tests
guilleiguaran authored
31 @params = {}
c2ac118 @guilleiguaran Importing tests from Rails
guilleiguaran authored
32 end
33
34 def url_for(*args)
35 "http://www.example.com"
36 end
37
38 def config
39 @controller ? @controller.config : @config
40 end
41
42 def compute_host(source, request, options = {})
43 raise "Should never get here"
44 end
45
46 test "asset_path" do
47 assert_match %r{/assets/logo-[0-9a-f]+.png},
48 asset_path("logo.png")
49 assert_match %r{/assets/logo-[0-9a-f]+.png},
50 asset_path("logo.png", :digest => true)
51 assert_match %r{/assets/logo.png},
52 asset_path("logo.png", :digest => false)
53 end
54
55 test "custom_asset_path" do
56 @config.assets.prefix = '/s'
57 assert_match %r{/s/logo-[0-9a-f]+.png},
58 asset_path("logo.png")
59 assert_match %r{/s/logo-[0-9a-f]+.png},
60 asset_path("logo.png", :digest => true)
61 assert_match %r{/s/logo.png},
62 asset_path("logo.png", :digest => false)
63 end
64
65 test "asset_path with root relative assets" do
66 assert_equal "/images/logo",
67 asset_path("/images/logo")
68 assert_equal "/images/logo.gif",
69 asset_path("/images/logo.gif")
70
71 assert_equal "/dir/audio",
72 asset_path("/dir/audio")
73 end
74
75 test "asset_path with absolute urls" do
76 assert_equal "http://www.example.com/video/play",
77 asset_path("http://www.example.com/video/play")
78 assert_equal "http://www.example.com/video/play.mp4",
79 asset_path("http://www.example.com/video/play.mp4")
80 end
81
82 test "with a simple asset host the url should default to protocol relative" do
83 @controller.config.default_asset_host_protocol = :relative
84 @controller.config.asset_host = "assets-%d.example.com"
85 assert_match %r{^//assets-\d.example.com/assets/logo-[0-9a-f]+.png},
86 asset_path("logo.png")
87 end
88
89 test "with a simple asset host the url can be changed to use the request protocol" do
90 @controller.config.asset_host = "assets-%d.example.com"
91 @controller.config.default_asset_host_protocol = :request
92 assert_match %r{http://assets-\d.example.com/assets/logo-[0-9a-f]+.png},
93 asset_path("logo.png")
94 end
95
96 test "With a proc asset host that returns no protocol the url should be protocol relative" do
97 @controller.config.default_asset_host_protocol = :relative
98 @controller.config.asset_host = Proc.new do |asset|
99 "assets-999.example.com"
100 end
101 assert_match %r{^//assets-999.example.com/assets/logo-[0-9a-f]+.png},
102 asset_path("logo.png")
103 end
104
105 test "with a proc asset host that returns a protocol the url use it" do
106 @controller.config.asset_host = Proc.new do |asset|
107 "http://assets-999.example.com"
108 end
109 assert_match %r{http://assets-999.example.com/assets/logo-[0-9a-f]+.png},
110 asset_path("logo.png")
111 end
112
113 test "stylesheets served with a controller in scope can access the request" do
114 config.asset_host = Proc.new do |asset, request|
115 assert_not_nil request
116 "http://assets-666.example.com"
117 end
118 assert_match %r{http://assets-666.example.com/assets/logo-[0-9a-f]+.png},
119 asset_path("logo.png")
120 end
121
122 test "stylesheets served without a controller in scope cannot access the request" do
123 @controller = nil
124 @config.asset_host = Proc.new do |asset, request|
125 fail "This should not have been called."
126 end
127 assert_raises ActionController::RoutingError do
128 asset_path("logo.png")
129 end
130 @config.asset_host = method :compute_host
131 assert_raises ActionController::RoutingError do
132 asset_path("logo.png")
133 end
134 end
135
136 test "image_tag" do
137 assert_dom_equal '<img alt="Xml" src="/assets/xml.png" />', image_tag("xml.png")
138 end
139
140 test "image_path" do
141 assert_match %r{/assets/logo-[0-9a-f]+.png},
142 image_path("logo.png")
143
144 assert_match %r{/assets/logo-[0-9a-f]+.png},
145 path_to_image("logo.png")
146 end
147
148 test "font_path" do
149 assert_match %r{/assets/font-[0-9a-f]+.ttf},
150 font_path("font.ttf")
151
152 assert_match %r{/assets/font-[0-9a-f]+.ttf},
153 path_to_font("font.ttf")
154 end
155
156 test "javascript_path" do
157 assert_match %r{/assets/application-[0-9a-f]+.js},
158 javascript_path("application")
159
160 assert_match %r{/assets/application-[0-9a-f]+.js},
161 javascript_path("application.js")
162
163 assert_match %r{/assets/application-[0-9a-f]+.js},
164 path_to_javascript("application.js")
165 end
166
167 test "stylesheet_path" do
168 assert_match %r{/assets/application-[0-9a-f]+.css},
169 stylesheet_path("application")
170
171 assert_match %r{/assets/application-[0-9a-f]+.css},
172 stylesheet_path("application.css")
173
174 assert_match %r{/assets/application-[0-9a-f]+.css},
175 path_to_stylesheet("application.css")
176 end
177
178 test "stylesheets served without a controller in do not use asset hosts when the default protocol is :request" do
179 @controller = nil
180 @config.asset_host = "assets-%d.example.com"
181 @config.default_asset_host_protocol = :request
182 @config.perform_caching = true
183
184 assert_match %r{/assets/logo-[0-9a-f]+.png},
185 asset_path("logo.png")
186 end
187
188 test "asset path with relative url root" do
189 @controller.config.relative_url_root = "/collaboration/hieraki"
190 assert_equal "/collaboration/hieraki/images/logo.gif",
191 asset_path("/images/logo.gif")
192 end
193
194 test "asset path with relative url root when controller isn't present but relative_url_root is" do
195 @controller = nil
196 @config.relative_url_root = "/collaboration/hieraki"
197 assert_equal "/collaboration/hieraki/images/logo.gif",
198 asset_path("/images/logo.gif")
199 end
200
201 test "font path through asset_path" do
202 assert_match %r{/assets/font-[0-9a-f]+.ttf},
203 asset_path('font.ttf')
204
205 assert_match %r{/assets/dir/font-[0-9a-f]+.ttf},
206 asset_path("dir/font.ttf")
207
208 assert_equal "http://www.example.com/fonts/font.ttf",
209 asset_path("http://www.example.com/fonts/font.ttf")
210 end
211
212 test "javascript path through asset_path" do
213 assert_match %r{/assets/application-[0-9a-f]+.js},
214 asset_path(:application, :ext => "js")
215
216 assert_match %r{/assets/xmlhr-[0-9a-f]+.js},
217 asset_path("xmlhr", :ext => "js")
218 assert_match %r{/assets/dir/xmlhr-[0-9a-f]+.js},
219 asset_path("dir/xmlhr.js", :ext => "js")
220
221 assert_equal "/dir/xmlhr.js",
222 asset_path("/dir/xmlhr", :ext => "js")
223
224 assert_equal "http://www.example.com/js/xmlhr",
225 asset_path("http://www.example.com/js/xmlhr", :ext => "js")
226 assert_equal "http://www.example.com/js/xmlhr.js",
227 asset_path("http://www.example.com/js/xmlhr.js", :ext => "js")
228 end
229
230 test "javascript include tag" do
42af481 @guilleiguaran type isn't required in <scrypt> and <link> in HTML5, fix broken tests
guilleiguaran authored
231 assert_match %r{<script src="/assets/application-[0-9a-f]+.js"></script>},
c2ac118 @guilleiguaran Importing tests from Rails
guilleiguaran authored
232 javascript_include_tag(:application)
42af481 @guilleiguaran type isn't required in <scrypt> and <link> in HTML5, fix broken tests
guilleiguaran authored
233 assert_match %r{<script src="/assets/application-[0-9a-f]+.js"></script>},
c2ac118 @guilleiguaran Importing tests from Rails
guilleiguaran authored
234 javascript_include_tag(:application, :digest => true)
42af481 @guilleiguaran type isn't required in <scrypt> and <link> in HTML5, fix broken tests
guilleiguaran authored
235 assert_match %r{<script src="/assets/application.js"></script>},
c2ac118 @guilleiguaran Importing tests from Rails
guilleiguaran authored
236 javascript_include_tag(:application, :digest => false)
237
42af481 @guilleiguaran type isn't required in <scrypt> and <link> in HTML5, fix broken tests
guilleiguaran authored
238 assert_match %r{<script src="/assets/xmlhr-[0-9a-f]+.js"></script>},
c2ac118 @guilleiguaran Importing tests from Rails
guilleiguaran authored
239 javascript_include_tag("xmlhr")
42af481 @guilleiguaran type isn't required in <scrypt> and <link> in HTML5, fix broken tests
guilleiguaran authored
240 assert_match %r{<script src="/assets/xmlhr-[0-9a-f]+.js"></script>},
c2ac118 @guilleiguaran Importing tests from Rails
guilleiguaran authored
241 javascript_include_tag("xmlhr.js")
42af481 @guilleiguaran type isn't required in <scrypt> and <link> in HTML5, fix broken tests
guilleiguaran authored
242 assert_equal '<script src="http://www.example.com/xmlhr"></script>',
c2ac118 @guilleiguaran Importing tests from Rails
guilleiguaran authored
243 javascript_include_tag("http://www.example.com/xmlhr")
244
42af481 @guilleiguaran type isn't required in <scrypt> and <link> in HTML5, fix broken tests
guilleiguaran authored
245 assert_match %r{<script src=\"/assets/xmlhr-[0-9a-f]+.js"></script>\n<script src=\"/assets/extra-[0-9a-f]+.js"></script>},
c2ac118 @guilleiguaran Importing tests from Rails
guilleiguaran authored
246 javascript_include_tag("xmlhr", "extra")
247
42af481 @guilleiguaran type isn't required in <scrypt> and <link> in HTML5, fix broken tests
guilleiguaran authored
248 assert_match %r{<script src="/assets/xmlhr-[0-9a-f]+.js\?body=1"></script>\n<script src="/assets/application-[0-9a-f]+.js\?body=1"></script>},
c2ac118 @guilleiguaran Importing tests from Rails
guilleiguaran authored
249 javascript_include_tag(:application, :debug => true)
250
42af481 @guilleiguaran type isn't required in <scrypt> and <link> in HTML5, fix broken tests
guilleiguaran authored
251 assert_match %r{<script src="/assets/jquery.plugin.js"></script>},
c2ac118 @guilleiguaran Importing tests from Rails
guilleiguaran authored
252 javascript_include_tag('jquery.plugin', :digest => false)
253
254 @config.assets.compile = true
255 @config.assets.debug = true
42af481 @guilleiguaran type isn't required in <scrypt> and <link> in HTML5, fix broken tests
guilleiguaran authored
256 assert_match %r{<script src="/javascripts/application.js"></script>},
c2ac118 @guilleiguaran Importing tests from Rails
guilleiguaran authored
257 javascript_include_tag('/javascripts/application')
42af481 @guilleiguaran type isn't required in <scrypt> and <link> in HTML5, fix broken tests
guilleiguaran authored
258 assert_match %r{<script src="/assets/xmlhr-[0-9a-f]+.js\?body=1"></script>\n<script src="/assets/application-[0-9a-f]+.js\?body=1"></script>},
c2ac118 @guilleiguaran Importing tests from Rails
guilleiguaran authored
259 javascript_include_tag(:application)
260 end
261
262 test "stylesheet path through asset_path" do
263 assert_match %r{/assets/application-[0-9a-f]+.css}, asset_path(:application, :ext => "css")
264
265 assert_match %r{/assets/style-[0-9a-f]+.css}, asset_path("style", :ext => "css")
266 assert_match %r{/assets/dir/style-[0-9a-f]+.css}, asset_path("dir/style.css", :ext => "css")
267 assert_equal "/dir/style.css", asset_path("/dir/style.css", :ext => "css")
268
269 assert_equal "http://www.example.com/css/style",
270 asset_path("http://www.example.com/css/style", :ext => "css")
271 assert_equal "http://www.example.com/css/style.css",
272 asset_path("http://www.example.com/css/style.css", :ext => "css")
273 end
274
275 test "stylesheet link tag" do
42af481 @guilleiguaran type isn't required in <scrypt> and <link> in HTML5, fix broken tests
guilleiguaran authored
276 assert_match %r{<link href="/assets/application-[0-9a-f]+.css" media="screen" rel="stylesheet" />},
c2ac118 @guilleiguaran Importing tests from Rails
guilleiguaran authored
277 stylesheet_link_tag(:application)
42af481 @guilleiguaran type isn't required in <scrypt> and <link> in HTML5, fix broken tests
guilleiguaran authored
278 assert_match %r{<link href="/assets/application-[0-9a-f]+.css" media="screen" rel="stylesheet" />},
c2ac118 @guilleiguaran Importing tests from Rails
guilleiguaran authored
279 stylesheet_link_tag(:application, :digest => true)
42af481 @guilleiguaran type isn't required in <scrypt> and <link> in HTML5, fix broken tests
guilleiguaran authored
280 assert_match %r{<link href="/assets/application.css" media="screen" rel="stylesheet" />},
c2ac118 @guilleiguaran Importing tests from Rails
guilleiguaran authored
281 stylesheet_link_tag(:application, :digest => false)
282
42af481 @guilleiguaran type isn't required in <scrypt> and <link> in HTML5, fix broken tests
guilleiguaran authored
283 assert_match %r{<link href="/assets/style-[0-9a-f]+.css" media="screen" rel="stylesheet" />},
c2ac118 @guilleiguaran Importing tests from Rails
guilleiguaran authored
284 stylesheet_link_tag("style")
42af481 @guilleiguaran type isn't required in <scrypt> and <link> in HTML5, fix broken tests
guilleiguaran authored
285 assert_match %r{<link href="/assets/style-[0-9a-f]+.css" media="screen" rel="stylesheet" />},
c2ac118 @guilleiguaran Importing tests from Rails
guilleiguaran authored
286 stylesheet_link_tag("style.css")
287
42af481 @guilleiguaran type isn't required in <scrypt> and <link> in HTML5, fix broken tests
guilleiguaran authored
288 assert_equal '<link href="http://www.example.com/style.css" media="screen" rel="stylesheet" />',
c2ac118 @guilleiguaran Importing tests from Rails
guilleiguaran authored
289 stylesheet_link_tag("http://www.example.com/style.css")
42af481 @guilleiguaran type isn't required in <scrypt> and <link> in HTML5, fix broken tests
guilleiguaran authored
290 assert_match %r{<link href="/assets/style-[0-9a-f]+.css" media="all" rel="stylesheet" />},
c2ac118 @guilleiguaran Importing tests from Rails
guilleiguaran authored
291 stylesheet_link_tag("style", :media => "all")
42af481 @guilleiguaran type isn't required in <scrypt> and <link> in HTML5, fix broken tests
guilleiguaran authored
292 assert_match %r{<link href="/assets/style-[0-9a-f]+.css" media="print" rel="stylesheet" />},
c2ac118 @guilleiguaran Importing tests from Rails
guilleiguaran authored
293 stylesheet_link_tag("style", :media => "print")
294
42af481 @guilleiguaran type isn't required in <scrypt> and <link> in HTML5, fix broken tests
guilleiguaran authored
295 assert_match %r{<link href="/assets/style-[0-9a-f]+.css" media="screen" rel="stylesheet" />\n<link href="/assets/extra-[0-9a-f]+.css" media="screen" rel="stylesheet" />},
c2ac118 @guilleiguaran Importing tests from Rails
guilleiguaran authored
296 stylesheet_link_tag("style", "extra")
297
42af481 @guilleiguaran type isn't required in <scrypt> and <link> in HTML5, fix broken tests
guilleiguaran authored
298 assert_match %r{<link href="/assets/style-[0-9a-f]+.css\?body=1" media="screen" rel="stylesheet" />\n<link href="/assets/application-[0-9a-f]+.css\?body=1" media="screen" rel="stylesheet" />},
c2ac118 @guilleiguaran Importing tests from Rails
guilleiguaran authored
299 stylesheet_link_tag(:application, :debug => true)
300
301 @config.assets.compile = true
302 @config.assets.debug = true
42af481 @guilleiguaran type isn't required in <scrypt> and <link> in HTML5, fix broken tests
guilleiguaran authored
303 assert_match %r{<link href="/stylesheets/application.css" media="screen" rel="stylesheet" />},
c2ac118 @guilleiguaran Importing tests from Rails
guilleiguaran authored
304 stylesheet_link_tag('/stylesheets/application')
305
42af481 @guilleiguaran type isn't required in <scrypt> and <link> in HTML5, fix broken tests
guilleiguaran authored
306 assert_match %r{<link href="/assets/style-[0-9a-f]+.css\?body=1" media="screen" rel="stylesheet" />\n<link href="/assets/application-[0-9a-f]+.css\?body=1" media="screen" rel="stylesheet" />},
c2ac118 @guilleiguaran Importing tests from Rails
guilleiguaran authored
307 stylesheet_link_tag(:application)
308
42af481 @guilleiguaran type isn't required in <scrypt> and <link> in HTML5, fix broken tests
guilleiguaran authored
309 assert_match %r{<link href="/assets/style-[0-9a-f]+.css\?body=1" media="print" rel="stylesheet" />\n<link href="/assets/application-[0-9a-f]+.css\?body=1" media="print" rel="stylesheet" />},
c2ac118 @guilleiguaran Importing tests from Rails
guilleiguaran authored
310 stylesheet_link_tag(:application, :media => "print")
311 end
312
313 test "alternate asset prefix" do
314 stubs(:asset_prefix).returns("/themes/test")
315 assert_match %r{/themes/test/style-[0-9a-f]+.css}, asset_path("style", :ext => "css")
316 end
317
318 test "alternate asset environment" do
319 assets = Sprockets::Environment.new
2b6d8dc @guilleiguaran Fix helper tests
guilleiguaran authored
320 assets.append_path(FIXTURES.join("alternate/stylesheets"))
c2ac118 @guilleiguaran Importing tests from Rails
guilleiguaran authored
321 stubs(:asset_environment).returns(assets)
322 assert_match %r{/assets/style-[0-9a-f]+.css}, asset_path("style", :ext => "css")
323 end
324
325 test "alternate hash based on environment" do
326 assets = Sprockets::Environment.new
327 assets.version = 'development'
2b6d8dc @guilleiguaran Fix helper tests
guilleiguaran authored
328 assets.append_path(FIXTURES.join("alternate/stylesheets"))
c2ac118 @guilleiguaran Importing tests from Rails
guilleiguaran authored
329 stubs(:asset_environment).returns(assets)
330 dev_path = asset_path("style", :ext => "css")
331
332 assets.version = 'production'
333 prod_path = asset_path("style", :ext => "css")
334
335 assert_not_equal prod_path, dev_path
336 end
337
338 test "precedence of `config.digest = false` over manifest.yml asset digests" do
339 Rails.application.config.assets.digests = {'logo.png' => 'logo-d1g3st.png'}
340 @config.assets.digest = false
341
342 assert_equal '/assets/logo.png',
343 asset_path("logo.png")
344 end
345 end
Something went wrong with that request. Please try again.