dassets 0.14.4 → 0.15.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -7
- data/Gemfile +5 -1
- data/README.md +15 -17
- data/dassets.gemspec +14 -9
- data/lib/dassets.rb +28 -22
- data/lib/dassets/asset_file.rb +27 -24
- data/lib/dassets/cache.rb +27 -33
- data/lib/dassets/config.rb +55 -50
- data/lib/dassets/engine.rb +11 -23
- data/lib/dassets/file_store.rb +27 -28
- data/lib/dassets/server.rb +27 -27
- data/lib/dassets/server/request.rb +44 -41
- data/lib/dassets/server/response.rb +101 -80
- data/lib/dassets/source.rb +24 -9
- data/lib/dassets/source_file.rb +110 -81
- data/lib/dassets/source_proxy.rb +36 -20
- data/lib/dassets/version.rb +3 -1
- data/test/helper.rb +31 -25
- data/test/support/app.rb +5 -5
- data/test/support/empty/{.gitkeep → .keep} +0 -0
- data/test/support/factory.rb +3 -2
- data/test/support/linked_source_files/linked_file.txt +0 -0
- data/test/support/source_files/linked +1 -0
- data/test/support/source_files/linked_file2.txt +1 -0
- data/test/system/rack_tests.rb +65 -61
- data/test/unit/asset_file_tests.rb +68 -60
- data/test/unit/cache_tests.rb +15 -34
- data/test/unit/config_tests.rb +58 -51
- data/test/unit/dassets_tests.rb +27 -26
- data/test/unit/engine_tests.rb +9 -43
- data/test/unit/file_store_tests.rb +34 -24
- data/test/unit/server/request_tests.rb +49 -52
- data/test/unit/server/response_tests.rb +84 -81
- data/test/unit/server_tests.rb +5 -9
- data/test/unit/source_file_tests.rb +86 -74
- data/test/unit/source_proxy_tests.rb +84 -90
- data/test/unit/source_tests.rb +89 -50
- data/tmp/.gitkeep +0 -0
- metadata +92 -64
- data/.gitignore +0 -19
@@ -1,107 +1,104 @@
|
|
1
|
-
|
2
|
-
require 'dassets/server/request'
|
1
|
+
# frozen_string_literal: true
|
3
2
|
|
4
|
-
require
|
5
|
-
require
|
3
|
+
require "assert"
|
4
|
+
require "dassets/server/request"
|
6
5
|
|
7
|
-
|
6
|
+
require "dassets/asset_file"
|
8
7
|
|
8
|
+
class Dassets::Server::Request
|
9
9
|
class UnitTests < Assert::Context
|
10
10
|
desc "Dassets::Server::Request"
|
11
|
+
subject{ @req }
|
12
|
+
|
11
13
|
setup do
|
12
|
-
@path =
|
13
|
-
@req = file_request(
|
14
|
+
@path = "/file1-daa05c683a4913b268653f7a7e36a5b4.txt"
|
15
|
+
@req = file_request("GET", @path)
|
14
16
|
end
|
15
|
-
subject{ @req }
|
16
17
|
|
17
18
|
should have_imeths :dassets_base_url
|
18
19
|
should have_imeths :for_asset_file?, :asset_path, :asset_file
|
19
20
|
|
20
|
-
should "know its
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
assert_equal @path, subject.path_info
|
26
|
-
end
|
27
|
-
|
28
|
-
should "know its asset_path" do
|
29
|
-
assert_equal 'file1.txt', subject.asset_path
|
30
|
-
end
|
31
|
-
|
32
|
-
should "know its asset_file" do
|
33
|
-
assert_equal Dassets['file1.txt'], subject.asset_file
|
21
|
+
should "know its attributes" do
|
22
|
+
assert_that(subject.dassets_base_url).equals(Dassets.config.base_url.to_s)
|
23
|
+
assert_that(subject.path_info).equals(@path)
|
24
|
+
assert_that(subject.asset_path).equals("file1.txt")
|
25
|
+
assert_that(subject.asset_file).equals(Dassets["file1.txt"])
|
34
26
|
end
|
35
27
|
|
36
28
|
should "know if it is for an asset file" do
|
37
29
|
# find nested path with matching fingerprint
|
38
|
-
req =
|
39
|
-
|
30
|
+
req =
|
31
|
+
file_request(
|
32
|
+
"GET",
|
33
|
+
"/nested/file3-d41d8cd98f00b204e9800998ecf8427e.txt",
|
34
|
+
)
|
35
|
+
assert_that(req.for_asset_file?).is_true
|
40
36
|
|
41
37
|
# find not nested path with matching fingerprint
|
42
|
-
req = file_request(
|
43
|
-
|
38
|
+
req = file_request("HEAD", "/file1-daa05c683a4913b268653f7a7e36a5b4.txt")
|
39
|
+
assert_that(req.for_asset_file?).is_true
|
44
40
|
|
45
|
-
# find even if fingerprint is *not* matching - just need to have any
|
46
|
-
|
47
|
-
|
41
|
+
# find even if fingerprint is *not* matching - just need to have any
|
42
|
+
# fingerprint
|
43
|
+
req = file_request("GET", "/file1-d41d8cd98f00b204e9800998ecf8427e.txt")
|
44
|
+
assert_that(req.for_asset_file?).is_true
|
48
45
|
|
49
46
|
# not find an invalid fingerprint
|
50
|
-
req = file_request(
|
51
|
-
|
47
|
+
req = file_request("GET", "/file1-abc123.txt")
|
48
|
+
assert_that(req.for_asset_file?).is_false
|
52
49
|
|
53
50
|
# not find a missing fingerprint
|
54
|
-
req = file_request(
|
55
|
-
|
51
|
+
req = file_request("HEAD", "/file1.txt")
|
52
|
+
assert_that(req.for_asset_file?).is_false
|
56
53
|
|
57
54
|
# not find an unknown file with a missing fingerprint
|
58
|
-
req = file_request(
|
59
|
-
|
55
|
+
req = file_request("GET", "/some-file.txt")
|
56
|
+
assert_that(req.for_asset_file?).is_false
|
60
57
|
|
61
|
-
#
|
62
|
-
req =
|
63
|
-
|
58
|
+
# not find an unknown file with a valid fingerprint
|
59
|
+
req =
|
60
|
+
file_request("GET", "/some-file-daa05c683a4913b268653f7a7e36a5b4.txt")
|
61
|
+
assert_that(req.for_asset_file?).is_false
|
64
62
|
end
|
65
63
|
|
66
|
-
should "return an
|
67
|
-
req = file_request(
|
64
|
+
should "return an empty path and file if request not for an asset file" do
|
65
|
+
req = file_request("GET", "/some-file.txt")
|
68
66
|
|
69
|
-
|
70
|
-
|
67
|
+
assert_that(req.asset_path).equals("")
|
68
|
+
assert_that(req.asset_file).equals(Dassets::AssetFile.new(""))
|
71
69
|
end
|
72
70
|
|
73
|
-
|
71
|
+
private
|
74
72
|
|
75
73
|
def file_request(method, path_info)
|
76
|
-
require 'dassets/server/request'
|
77
74
|
Dassets::Server::Request.new({
|
78
|
-
|
79
|
-
|
75
|
+
"REQUEST_METHOD" => method,
|
76
|
+
"PATH_INFO" => path_info,
|
80
77
|
})
|
81
78
|
end
|
82
|
-
|
83
79
|
end
|
84
80
|
|
85
81
|
class BaseUrlTests < UnitTests
|
86
82
|
desc "when a base url is configured"
|
83
|
+
|
87
84
|
setup do
|
88
85
|
@orig_base_url = Dassets.config.base_url
|
89
86
|
@new_base_url = Factory.url
|
90
87
|
Dassets.config.base_url(@new_base_url)
|
91
88
|
end
|
89
|
+
|
92
90
|
teardown do
|
93
91
|
Dassets.config.set_base_url(@orig_base_url)
|
94
92
|
end
|
95
93
|
|
96
94
|
should "have the same base url as is configured" do
|
97
|
-
|
95
|
+
assert_that(subject.dassets_base_url).equals(@new_base_url.to_s)
|
98
96
|
end
|
99
97
|
|
100
98
|
should "remove the configured base url from the path info" do
|
101
|
-
|
102
|
-
|
99
|
+
assert_that(file_request("GET", @path).path_info).equals(@path)
|
100
|
+
assert_that(file_request("GET", "#{@new_base_url}#{@path}").path_info)
|
101
|
+
.equals(@path)
|
103
102
|
end
|
104
|
-
|
105
103
|
end
|
106
|
-
|
107
104
|
end
|
@@ -1,74 +1,80 @@
|
|
1
|
-
|
2
|
-
require 'dassets/server/response'
|
1
|
+
# frozen_string_literal: true
|
3
2
|
|
4
|
-
require
|
5
|
-
require
|
6
|
-
require 'dassets/asset_file'
|
3
|
+
require "assert"
|
4
|
+
require "dassets/server/response"
|
7
5
|
|
8
|
-
|
6
|
+
require "rack/utils"
|
7
|
+
require "dassets/asset_file"
|
9
8
|
|
9
|
+
class Dassets::Server::Response
|
10
10
|
class UnitTests < Assert::Context
|
11
11
|
desc "Dassets::Server::Response"
|
12
|
+
subject{ @response }
|
13
|
+
|
12
14
|
setup do
|
13
|
-
@env
|
14
|
-
@asset_file = Dassets[
|
15
|
+
@env = {}
|
16
|
+
@asset_file = Dassets["file1.txt"]
|
15
17
|
|
16
18
|
@response = Dassets::Server::Response.new(@env, @asset_file)
|
17
19
|
end
|
18
|
-
subject{ @response }
|
19
20
|
|
20
21
|
should have_readers :asset_file, :status, :headers, :body
|
21
22
|
should have_imeths :to_rack
|
22
23
|
|
23
24
|
should "handle not modified files" do
|
24
|
-
env = {
|
25
|
+
env = { "HTTP_IF_MODIFIED_SINCE" => @asset_file.mtime }
|
25
26
|
resp = Dassets::Server::Response.new(env, @asset_file)
|
26
27
|
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
exp_headers = Rack::Utils::HeaderHash.new('Last-Modified' => @asset_file.mtime.to_s)
|
31
|
-
assert_equal exp_headers, resp.headers
|
28
|
+
assert_that(resp.status).equals(304)
|
29
|
+
assert_that(resp.body).equals([])
|
32
30
|
|
33
|
-
|
31
|
+
exp_headers =
|
32
|
+
Rack::Utils::HeaderHash.new("Last-Modified" => @asset_file.mtime.to_s)
|
33
|
+
assert_that(resp.headers).equals(exp_headers)
|
34
|
+
assert_that(resp.to_rack).equals([304, exp_headers.to_hash, []])
|
34
35
|
end
|
35
36
|
|
36
37
|
should "handle found files" do
|
37
38
|
resp = Dassets::Server::Response.new(@env, @asset_file)
|
38
39
|
|
39
|
-
|
40
|
+
assert_that(resp.status).equals(200)
|
40
41
|
|
41
42
|
exp_body = Body.new(@env, @asset_file)
|
42
|
-
|
43
|
+
assert_that(resp.body).equals(exp_body)
|
43
44
|
|
44
|
-
exp_headers =
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
45
|
+
exp_headers =
|
46
|
+
@asset_file.response_headers.merge({
|
47
|
+
"Content-Type" => "text/plain",
|
48
|
+
"Content-Length" => @asset_file.size.to_s,
|
49
|
+
"Last-Modified" => @asset_file.mtime.to_s,
|
50
|
+
})
|
51
|
+
assert_that(resp.headers).equals(exp_headers)
|
50
52
|
|
51
|
-
|
53
|
+
assert_that(resp.to_rack).equals([200, exp_headers, exp_body])
|
52
54
|
end
|
53
55
|
|
54
56
|
should "have an empty body for found files with a HEAD request" do
|
55
|
-
env = {
|
57
|
+
env = { "REQUEST_METHOD" => "HEAD" }
|
56
58
|
resp = Dassets::Server::Response.new(env, @asset_file)
|
57
59
|
|
58
|
-
|
59
|
-
|
60
|
+
assert_that(resp.status).equals(200)
|
61
|
+
assert_that(resp.body).equals([])
|
60
62
|
end
|
61
63
|
|
62
64
|
should "handle not found files" do
|
63
|
-
|
64
|
-
|
65
|
-
end
|
66
|
-
end
|
65
|
+
af = Dassets.asset_file("not-found-file.txt")
|
66
|
+
resp = Dassets::Server::Response.new(@env, af)
|
67
67
|
|
68
|
+
assert_that(resp.status).equals(404)
|
69
|
+
assert_that(resp.body).equals(["Not Found"])
|
70
|
+
assert_that(resp.headers).equals(Rack::Utils::HeaderHash.new)
|
71
|
+
assert_that(resp.to_rack).equals([404, {}, ["Not Found"]])
|
72
|
+
end
|
68
73
|
end
|
69
74
|
|
70
75
|
class PartialContentTests < UnitTests
|
71
76
|
desc "for a partial content request"
|
77
|
+
|
72
78
|
setup do
|
73
79
|
@body = Body.new(@env, @asset_file)
|
74
80
|
Assert.stub(Body, :new).with(@env, @asset_file){ @body }
|
@@ -81,55 +87,54 @@ class Dassets::Server::Response
|
|
81
87
|
end
|
82
88
|
|
83
89
|
should "be a partial content response" do
|
84
|
-
|
90
|
+
assert_that(subject.status).equals(206)
|
85
91
|
|
86
|
-
|
87
|
-
|
92
|
+
assert_that(subject.headers).includes("Content-Range")
|
93
|
+
assert_that(subject.headers["Content-Range"]).equals(@body.content_range)
|
88
94
|
end
|
89
|
-
|
90
95
|
end
|
91
96
|
|
92
97
|
class BodyTests < UnitTests
|
93
98
|
desc "Body"
|
99
|
+
subject{ @body }
|
100
|
+
|
94
101
|
setup do
|
95
102
|
@body = Body.new(@env, @asset_file)
|
96
103
|
end
|
97
|
-
subject{ @body }
|
98
104
|
|
99
105
|
should have_readers :asset_file, :size, :content_range
|
100
106
|
should have_imeths :partial?, :range_begin, :range_end
|
101
107
|
should have_imeths :each
|
102
108
|
|
103
109
|
should "know its chunk size" do
|
104
|
-
|
110
|
+
assert_that(Body::CHUNK_SIZE).equals(8192)
|
105
111
|
end
|
106
112
|
|
107
113
|
should "know its asset file" do
|
108
|
-
|
114
|
+
assert_that(subject.asset_file).equals(@asset_file)
|
109
115
|
end
|
110
116
|
|
111
117
|
should "know if it is equal to another body" do
|
112
118
|
same_af_same_range = Body.new(@env, @asset_file)
|
113
119
|
Assert.stub(same_af_same_range, :range_begin){ subject.range_begin }
|
114
120
|
Assert.stub(same_af_same_range, :range_end){ subject.range_end }
|
115
|
-
|
121
|
+
assert_that(subject).equals(same_af_same_range)
|
116
122
|
|
117
|
-
other_af_same_range = Body.new(@env, Dassets[
|
123
|
+
other_af_same_range = Body.new(@env, Dassets["file2.txt"])
|
118
124
|
Assert.stub(other_af_same_range, :range_begin){ subject.range_begin }
|
119
125
|
Assert.stub(other_af_same_range, :range_end){ subject.range_end }
|
120
|
-
|
126
|
+
assert_that(subject).does_not_equal(other_af_same_range)
|
121
127
|
|
122
128
|
same_af_other_range = Body.new(@env, @asset_file)
|
123
129
|
|
124
130
|
Assert.stub(same_af_other_range, :range_begin){ Factory.integer }
|
125
131
|
Assert.stub(same_af_other_range, :range_end){ subject.range_end }
|
126
|
-
|
132
|
+
assert_that(subject).does_not_equal(same_af_other_range)
|
127
133
|
|
128
134
|
Assert.stub(same_af_other_range, :range_begin){ subject.range_begin }
|
129
135
|
Assert.stub(same_af_other_range, :range_end){ Factory.integer }
|
130
|
-
|
136
|
+
assert_that(subject).does_not_equal(same_af_other_range)
|
131
137
|
end
|
132
|
-
|
133
138
|
end
|
134
139
|
|
135
140
|
class BodyIOTests < BodyTests
|
@@ -137,132 +142,130 @@ class Dassets::Server::Response
|
|
137
142
|
@min_num_chunks = 3
|
138
143
|
@num_chunks = @min_num_chunks + Factory.integer(3)
|
139
144
|
|
140
|
-
content =
|
145
|
+
content = "a" * (@num_chunks * Body::CHUNK_SIZE)
|
141
146
|
Assert.stub(@asset_file, :content){ content }
|
142
147
|
end
|
143
|
-
|
144
148
|
end
|
145
149
|
|
146
150
|
class NonPartialBodyTests < BodyIOTests
|
147
151
|
desc "for non/multi/invalid partial content requests"
|
152
|
+
|
148
153
|
setup do
|
149
|
-
range = [nil,
|
150
|
-
env = range.nil? ? {} : {
|
154
|
+
range = [nil, "bytes=", "bytes=0-1,2-3", "bytes=3-2", "bytes=abc"].sample
|
155
|
+
env = range.nil? ? {} : { "HTTP_RANGE" => range }
|
151
156
|
@body = Body.new(env, @asset_file)
|
152
157
|
end
|
153
158
|
|
154
159
|
should "not be partial" do
|
155
|
-
|
160
|
+
assert_that(subject.partial?).is_false
|
156
161
|
end
|
157
162
|
|
158
163
|
should "be the full content size" do
|
159
|
-
|
164
|
+
assert_that(subject.size).equals(@asset_file.size)
|
160
165
|
end
|
161
166
|
|
162
167
|
should "have no content range" do
|
163
|
-
|
168
|
+
assert_that(subject.content_range).is_nil
|
164
169
|
end
|
165
170
|
|
166
171
|
should "have the full content size as its range" do
|
167
|
-
|
168
|
-
|
172
|
+
assert_that(subject.range_begin).equals(0)
|
173
|
+
assert_that(subject.range_end).equals(subject.size - 1)
|
169
174
|
end
|
170
175
|
|
171
176
|
should "chunk the full content when iterated" do
|
172
177
|
chunks = []
|
173
178
|
subject.each{ |chunk| chunks << chunk }
|
174
179
|
|
175
|
-
|
176
|
-
|
177
|
-
|
180
|
+
assert_that(chunks.size).equals(@num_chunks)
|
181
|
+
assert_that(chunks.first.size).equals(subject.class::CHUNK_SIZE)
|
182
|
+
assert_that(chunks.join("")).equals(@asset_file.content)
|
178
183
|
end
|
179
|
-
|
180
184
|
end
|
181
185
|
|
182
186
|
class PartialBodySetupTests < BodyIOTests
|
183
187
|
desc "for a partial content request"
|
188
|
+
|
184
189
|
setup do
|
185
190
|
@start_chunk = Factory.boolean ? 0 : 1
|
186
191
|
@partial_begin = @start_chunk * Body::CHUNK_SIZE
|
187
192
|
@partial_chunks = @num_chunks - Factory.integer(@min_num_chunks)
|
188
193
|
@partial_size = @partial_chunks * Body::CHUNK_SIZE
|
189
|
-
@partial_end = @partial_begin + (@partial_size-1)
|
194
|
+
@partial_end = @partial_begin + (@partial_size - 1)
|
190
195
|
|
191
|
-
@env = {
|
196
|
+
@env = { "HTTP_RANGE" => "bytes=#{@partial_begin}-#{@partial_end}" }
|
192
197
|
end
|
193
|
-
|
194
198
|
end
|
195
199
|
|
196
200
|
class PartialBodyTests < PartialBodySetupTests
|
201
|
+
subject{ @body }
|
202
|
+
|
197
203
|
setup do
|
198
204
|
@body = Body.new(@env, @asset_file)
|
199
205
|
end
|
200
|
-
subject{ @body }
|
201
206
|
|
202
207
|
should "be partial" do
|
203
|
-
|
208
|
+
assert_that(subject.partial?).is_true
|
204
209
|
end
|
205
210
|
|
206
211
|
should "be the specified partial size" do
|
207
|
-
|
212
|
+
assert_that(subject.size).equals(@partial_size)
|
208
213
|
end
|
209
214
|
|
210
215
|
should "know its content range" do
|
211
216
|
exp = "bytes #{@partial_begin}-#{@partial_end}/#{@asset_file.size}"
|
212
|
-
|
217
|
+
assert_that(subject.content_range).equals(exp)
|
213
218
|
end
|
214
219
|
|
215
220
|
should "have the know its range" do
|
216
|
-
|
217
|
-
|
221
|
+
assert_that(subject.range_begin).equals(@partial_begin)
|
222
|
+
assert_that(subject.range_end).equals(@partial_end)
|
218
223
|
end
|
219
224
|
|
220
225
|
should "chunk the range when iterated" do
|
221
226
|
chunks = []
|
222
227
|
subject.each{ |chunk| chunks << chunk }
|
223
228
|
|
224
|
-
|
225
|
-
|
229
|
+
assert_that(chunks.size).equals(@partial_chunks)
|
230
|
+
assert_that(chunks.first.size).equals(subject.class::CHUNK_SIZE)
|
226
231
|
|
227
232
|
exp = @asset_file.content[@partial_begin..@partial_end]
|
228
|
-
|
233
|
+
assert_that(chunks.join("")).equals(exp)
|
229
234
|
end
|
230
|
-
|
231
235
|
end
|
232
236
|
|
233
237
|
class LegacyRackTests < PartialBodySetupTests
|
234
238
|
desc "when using a legacy version of rack that can't interpret byte ranges"
|
239
|
+
|
235
240
|
setup do
|
236
241
|
Assert.stub(Rack::Utils, :respond_to?).with(:byte_ranges){ false }
|
237
242
|
@body = Body.new(@env, @asset_file)
|
238
243
|
end
|
239
244
|
|
240
245
|
should "not be partial" do
|
241
|
-
|
246
|
+
assert_that(subject.partial?).is_false
|
242
247
|
end
|
243
248
|
|
244
249
|
should "be the full content size" do
|
245
|
-
|
250
|
+
assert_that(subject.size).equals(@asset_file.size)
|
246
251
|
end
|
247
252
|
|
248
253
|
should "have no content range" do
|
249
|
-
|
254
|
+
assert_that(subject.content_range).is_nil
|
250
255
|
end
|
251
256
|
|
252
257
|
should "have the full content size as its range" do
|
253
|
-
|
254
|
-
|
258
|
+
assert_that(subject.range_begin).equals(0)
|
259
|
+
assert_that(subject.range_end).equals(subject.size - 1)
|
255
260
|
end
|
256
261
|
|
257
262
|
should "chunk the full content when iterated" do
|
258
263
|
chunks = []
|
259
264
|
subject.each{ |chunk| chunks << chunk }
|
260
265
|
|
261
|
-
|
262
|
-
|
263
|
-
|
266
|
+
assert_that(chunks.size).equals(@num_chunks)
|
267
|
+
assert_that(chunks.first.size).equals(subject.class::CHUNK_SIZE)
|
268
|
+
assert_that(chunks.join("")).equals(@asset_file.content)
|
264
269
|
end
|
265
|
-
|
266
270
|
end
|
267
|
-
|
268
271
|
end
|