5
5
class Propshaft ::ServerTest < ActiveSupport ::TestCase
6
6
include Rack ::Test ::Methods
7
7
8
+ class RackApp
9
+ attr_reader :calls
10
+
11
+ def initialize
12
+ @calls = [ ]
13
+ end
14
+
15
+ def call ( env )
16
+ @calls << env
17
+ [ 200 , { } , [ "OK" ] ]
18
+ end
19
+ end
20
+
8
21
setup do
9
22
@assembly = Propshaft ::Assembly . new ( ActiveSupport ::OrderedOptions . new . tap { |config |
10
23
config . paths = [ Pathname . new ( "#{ __dir__ } /../fixtures/assets/vendor" ) , Pathname . new ( "#{ __dir__ } /../fixtures/assets/first_path" ) ]
11
24
config . output_path = Pathname . new ( "#{ __dir__ } ../fixtures/output" )
25
+ config . prefix = "/assets"
12
26
} )
13
27
28
+ @rack_app = RackApp . new
14
29
@assembly . compilers . register "text/css" , Propshaft ::Compiler ::CssAssetUrls
15
- @server = Propshaft ::Server . new ( @assembly )
30
+ @server = Propshaft ::Server . new ( @rack_app , @assembly )
31
+ end
32
+
33
+ test "forward requests not under prefix" do
34
+ get "/test"
35
+ assert_not_empty @rack_app . calls
36
+ end
37
+
38
+ test "forward requests that aren't GET or HEAD" do
39
+ asset = @assembly . load_path . find ( "foobar/source/test.css" )
40
+ post "/assets/#{ asset . digested_path } "
41
+ assert_not_empty @rack_app . calls
16
42
end
17
43
18
44
test "serve a compiled file" do
19
45
asset = @assembly . load_path . find ( "foobar/source/test.css" )
20
- get "/#{ asset . digested_path } "
46
+ get "/assets/ #{ asset . digested_path } "
21
47
22
48
assert_equal 200 , last_response . status
23
- assert_equal "62" , last_response . headers [ 'content-length' ]
49
+ assert_equal last_response . body . bytesize . to_s , last_response . headers [ 'content-length' ]
24
50
assert_equal "text/css" , last_response . headers [ 'content-type' ]
25
51
assert_equal "Accept-Encoding" , last_response . headers [ 'vary' ]
26
52
assert_equal "\" #{ asset . digest } \" " , last_response . headers [ 'etag' ]
27
53
assert_equal "public, max-age=31536000, immutable" , last_response . headers [ 'cache-control' ]
28
- assert_equal ".hero { background: url(\" /foobar/source/file-3e6a1297.jpg\" ) }\n " ,
54
+ assert_equal ".hero { background: url(\" /assets/ foobar/source/file-3e6a1297.jpg\" ) }\n " ,
29
55
last_response . body
30
56
end
31
57
32
58
test "serve a predigested file" do
33
59
asset = @assembly . load_path . find ( "file-already-abcdefVWXYZ0123456789_-.digested.css" )
34
- get "/#{ asset . digested_path } "
60
+ get "/assets/ #{ asset . digested_path } "
35
61
assert_equal 200 , last_response . status
36
62
end
37
63
38
64
test "serve a sourcemap" do
39
65
asset = @assembly . load_path . find ( "file-is-a-sourcemap.js.map" )
40
- get "/#{ asset . digested_path } "
66
+ get "/assets/ #{ asset . digested_path } "
41
67
assert_equal 200 , last_response . status
42
68
end
43
69
44
70
test "not found" do
45
- get "/not-found.js"
71
+ get "/assets/ not-found.js"
46
72
47
73
assert_equal 404 , last_response . status
48
74
assert_equal "9" , last_response . headers [ 'content-length' ]
@@ -55,17 +81,12 @@ class Propshaft::ServerTest < ActiveSupport::TestCase
55
81
56
82
test "not found if digest does not match" do
57
83
asset = @assembly . load_path . find ( "foobar/source/test.css" )
58
- get "/#{ asset . logical_path } "
84
+ get "/assets/ #{ asset . logical_path } "
59
85
assert_equal 404 , last_response . status
60
86
end
61
87
62
88
private
63
- def default_app
64
- builder = Rack ::Builder . new
65
- builder . run @server
66
- end
67
-
68
89
def app
69
- @app ||= Rack ::Lint . new ( default_app )
90
+ @app ||= Rack ::Lint . new ( @server )
70
91
end
71
92
end
0 commit comments