forked from basecamp/aws-s3
/
object_test.rb
408 lines (297 loc) · 11.2 KB
/
object_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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
require File.dirname(__FILE__) + '/test_helper'
class RemoteS3ObjectTest < Test::Unit::TestCase
def setup
establish_real_connection
end
def teardown
disconnect!
end
def test_object
key = 'testing_s3objects'
value = 'testing'
content_type = 'text/plain'
unauthenticated_url = ['http:/', Base.connection.http.address, TEST_BUCKET, key].join('/')
# Create an object
response = nil
assert_nothing_raised do
response = S3Object.create(key, value, TEST_BUCKET, :access => :public_read, :content_type => content_type)
end
# Check response
assert response.success?
# Extract the object's etag
etag = nil
assert_nothing_raised do
etag = response.etag
end
assert etag
# Confirm we can't create an object unless the bucket is set
assert_raises(NoBucketSpecified) do
object = S3Object.new
object.key = 'hello'
object.store
end
# Fetch newly created object to show it was actually created
object = nil
assert_nothing_raised do
object = S3Object.find(key, TEST_BUCKET)
end
assert object
# Confirm it has the right etag
assert_equal etag, object.etag
# Check if its owner is properly set
assert_nothing_raised do
object.owner.display_name
end
# Confirm we can get the object's key
assert_equal key, object.key
# Confirm its value was properly set
assert_equal value, object.value
assert_equal value, S3Object.value(key, TEST_BUCKET)
streamed_value = ''
assert_nothing_raised do
S3Object.stream(key, TEST_BUCKET) do |segment|
streamed_value << segment
end
end
assert_equal value, streamed_value
# Change its value
new_value = "<script>alert('foo');</script>"
assert_nothing_raised do
object.value = new_value
end
assert_equal new_value, object.value
# Confirm content type was properly set
assert_equal content_type, object.content_type
# Change its content type
new_content_type = 'text/javascript'
assert_nothing_raised do
object.content_type = new_content_type
end
assert_equal new_content_type, object.content_type
# Test that it is publicly readable
response = fetch_object_at(unauthenticated_url)
assert (200..299).include?(response.code.to_i)
# Confirm that it has no meta data
assert object.metadata.empty?
# Set some meta data
metadata_key = :secret_sauce
metadata_value = "it's a secret"
object.metadata[metadata_key] = metadata_value
# Persist all changes
assert_nothing_raised do
object.store
end
# Refetch the object
key = object.key
object = nil
assert_nothing_raised do
object = S3Object.find(key, TEST_BUCKET)
end
# Confirm all changes were persisted
assert object
assert_equal key, object.key
assert_equal new_content_type, object.content_type
assert_equal new_value, object.value
assert_equal new_value, object.value(:reload)
assert !object.metadata.empty?
assert_equal metadata_value, object.metadata[metadata_key]
# Change acl
assert_nothing_raised do
S3Object.create(object.key, object.value, TEST_BUCKET, :access => :private, :content_type => object.content_type)
end
# Confirm object is no longer publicly readable
response = fetch_object_at(unauthenticated_url)
assert (400..499).include?(response.code.to_i)
# Confirm object is accessible from its authenticated url
response = fetch_object_at(object.url)
assert (200..299).include?(response.code.to_i)
# Copy the object
assert_nothing_raised do
object.copy('testing_s3objects-copy')
end
# Confirm the object is identical
copy = nil
assert_nothing_raised do
copy = S3Object.find('testing_s3objects-copy', TEST_BUCKET)
end
assert copy
assert_equal object.value, copy.value
assert_equal object.content_type, copy.content_type
# Test copy to an filename with an accent
copy_to_accent = nil
assert_nothing_raised do
object.copy('testing_s3objects-copy-to-accent-é')
copy_to_accent = S3Object.find('testing_s3objects-copy-to-accent-é', TEST_BUCKET)
assert copy_to_accent
assert_equal copy_to_accent.value, object.value
assert_equal copy_to_accent.content_type, object.content_type
end
# Test copy from an filename with an accent
assert_nothing_raised do
object_with_accent = S3Object.find('testing_s3objects-copy-to-accent-é')
object_with_accent.copy('testing_s3objects-copy-from-accent')
copy_from_accent = S3Object.find('testing_s3objects-copy-from-accent', TEST_BUCKET)
assert copy_from_accent
assert_equal copy_from_accent.value, object_with_accent.value
assert_equal copy_from_accent.content_type, object_with_accent.content_type
end
# Delete object
assert_nothing_raised do
object.delete
end
# Confirm we can rename objects
renamed_to = copy.key + '-renamed'
renamed_value = copy.value
assert_nothing_raised do
S3Object.rename(copy.key, renamed_to, TEST_BUCKET)
end
# Confirm renamed copy exists
renamed = nil
assert_nothing_raised do
renamed = S3Object.find(renamed_to, TEST_BUCKET)
end
assert renamed
assert_equal renamed_value, renamed.value
# Confirm copy is deleted
assert_raises(NoSuchKey) do
S3Object.find(copy.key, TEST_BUCKET)
end
# Confirm that you can not store an object once it is deleted
assert_raises(DeletedObject) do
object.store
end
assert_raises(NoSuchKey) do
S3Object.find(key, TEST_BUCKET)
end
# Confirm we can pass in an IO stream and have the uploading sent in chunks
response = nil
test_file_key = File.basename(TEST_FILE)
assert_nothing_raised do
response = S3Object.store(test_file_key, open(TEST_FILE), TEST_BUCKET)
end
assert response.success?
assert_equal File.size(TEST_FILE), Integer(S3Object.about(test_file_key, TEST_BUCKET)['content-length'])
result = nil
assert_nothing_raised do
result = S3Object.delete(test_file_key, TEST_BUCKET)
end
assert result
end
def test_content_type_inference
# Confirm appropriate content type is inferred when not specified
content_type_objects = {'foo.jpg' => 'image/jpeg', 'no-extension-specified' => 'binary/octet-stream', 'foo.txt' => 'text/plain'}
content_type_objects.each_key do |key|
S3Object.store(key, 'fake data', TEST_BUCKET) # No content type explicitly set
end
content_type_objects.each do |key, content_type|
assert_equal content_type, S3Object.about(key, TEST_BUCKET)['content-type']
end
# Confirm we can update the content type
assert_nothing_raised do
object = S3Object.find('no-extension-specified', TEST_BUCKET)
object.content_type = 'application/pdf'
object.store
end
assert_equal 'application/pdf', S3Object.about('no-extension-specified', TEST_BUCKET)['content-type']
ensure
# Get rid of objects we just created
content_type_objects.each_key {|key| S3Object.delete(key, TEST_BUCKET) }
end
def test_body_can_be_more_than_just_string_or_io
require 'stringio'
key = 'testing-body-as-string-io'
io = StringIO.new('hello there')
S3Object.store(key, io, TEST_BUCKET)
assert_equal 'hello there', S3Object.value(key, TEST_BUCKET)
ensure
S3Object.delete(key, TEST_BUCKET)
end
def test_fetching_information_about_an_object_that_does_not_exist_raises_no_such_key
assert_raises(NoSuchKey) do
S3Object.about('asdfasdfasdfas-this-does-not-exist', TEST_BUCKET)
end
end
# Regression test for http://developer.amazonwebservices.com/connect/thread.jspa?messageID=49152&tstart=0#49152
def test_finding_an_object_with_slashes_in_its_name_does_not_escape_the_slash
S3Object.store('rails/1', 'value does not matter', TEST_BUCKET)
S3Object.store('rails/1.html', 'value does not matter', TEST_BUCKET)
object = nil
assert_nothing_raised do
object = S3Object.find('rails/1.html', TEST_BUCKET)
end
assert_equal 'rails/1.html', object.key
ensure
%w(rails/1 rails/1.html).each {|key| S3Object.delete(key, TEST_BUCKET)}
end
def test_finding_an_object_with_spaces_in_its_name
assert_nothing_raised do
S3Object.store('name with spaces', 'value does not matter', TEST_BUCKET)
end
object = nil
assert_nothing_raised do
object = S3Object.find('name with spaces', TEST_BUCKET)
end
assert object
assert_equal 'name with spaces', object.key
# Confirm authenticated url is generated correctly despite space in file name
response = fetch_object_at(object.url)
assert (200..299).include?(response.code.to_i)
ensure
S3Object.delete('name with spaces', TEST_BUCKET)
end
def test_copying_an_object_should_copy_over_its_acl_also_if_requested
key = 'copied-objects-inherit-acl'
copy_key = key + '2'
S3Object.store(key, 'value does not matter', TEST_BUCKET)
original_object = S3Object.find(key, TEST_BUCKET)
original_object.acl.grants << ACL::Grant.grant(:public_read)
original_object.acl.grants << ACL::Grant.grant(:public_read_acp)
S3Object.acl(key, TEST_BUCKET, original_object.acl)
acl = S3Object.acl(key, TEST_BUCKET)
assert_equal 3, acl.grants.size
S3Object.copy(key, copy_key, TEST_BUCKET, :copy_acl => true)
copied_object = S3Object.find(copy_key, TEST_BUCKET)
assert_equal acl.grants, copied_object.acl.grants
ensure
S3Object.delete(key, TEST_BUCKET)
S3Object.delete(copy_key, TEST_BUCKET)
end
def test_handling_a_path_that_is_not_valid_utf8
key = "318597/620065/GTL_75\24300_A600_A610.zip"
assert_nothing_raised do
S3Object.store(key, 'value does not matter', TEST_BUCKET)
end
object = nil
assert_nothing_raised do
object = S3Object.find(key, TEST_BUCKET)
end
assert object
url = nil
assert_nothing_raised do
url = S3Object.url_for(key, TEST_BUCKET)
end
assert url
assert_equal object.value, fetch_object_at(url).body
ensure
assert_nothing_raised do
S3Object.delete(key, TEST_BUCKET)
end
end
def test_updating_an_object_should_replace_its_metadata
key = 'updated-object'
S3Object.store(key, 'value does not matter', TEST_BUCKET)
object = S3Object.find(key, TEST_BUCKET)
object.content_type = 'foo/bar'
object.metadata[:foo] = 'bar'
object.update
reloaded_object = S3Object.find(key, TEST_BUCKET)
assert_equal 'foo/bar', reloaded_object.content_type
assert_equal 'bar', reloaded_object.metadata[:foo]
ensure
S3Object.delete(key, TEST_BUCKET)
end
private
def fetch_object_at(url)
Net::HTTP.get_response(URI.parse(url))
end
end