/
s3.spec.coffee
1622 lines (1387 loc) · 72.7 KB
/
s3.spec.coffee
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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
helpers = require('../helpers')
AWS = helpers.AWS
Stream = AWS.util.stream
Buffer = AWS.util.Buffer
describe 'AWS.S3', ->
s3 = null
request = (operation, params) -> s3.makeRequest(operation, params)
beforeEach (done) ->
s3 = new AWS.S3(region: undefined)
s3.clearBucketRegionCache()
done()
describe 'dnsCompatibleBucketName', ->
it 'must be at least 3 characters', ->
expect(s3.dnsCompatibleBucketName('aa')).to.equal(false)
it 'must not be longer than 63 characters', ->
b = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'
expect(s3.dnsCompatibleBucketName(b)).to.equal(false)
it 'must start with a lower-cased letter or number', ->
expect(s3.dnsCompatibleBucketName('Abc')).to.equal(false)
expect(s3.dnsCompatibleBucketName('-bc')).to.equal(false)
expect(s3.dnsCompatibleBucketName('abc')).to.equal(true)
it 'must end with a lower-cased letter or number', ->
expect(s3.dnsCompatibleBucketName('abC')).to.equal(false)
expect(s3.dnsCompatibleBucketName('ab-')).to.equal(false)
expect(s3.dnsCompatibleBucketName('abc')).to.equal(true)
it 'may not contain multiple contiguous dots', ->
expect(s3.dnsCompatibleBucketName('abc.123')).to.equal(true)
expect(s3.dnsCompatibleBucketName('abc..123')).to.equal(false)
it 'may only contain letters numbers and dots', ->
expect(s3.dnsCompatibleBucketName('abc123')).to.equal(true)
expect(s3.dnsCompatibleBucketName('abc_123')).to.equal(false)
it 'must not look like an ip address', ->
expect(s3.dnsCompatibleBucketName('1.2.3.4')).to.equal(false)
expect(s3.dnsCompatibleBucketName('a.b.c.d')).to.equal(true)
describe 'constructor', ->
it 'requires endpoint if s3BucketEndpoint is passed', ->
expect(-> new AWS.S3(s3BucketEndpoint: true)).to.throw(
/An endpoint must be provided/)
describe 'endpoint', ->
it 'sets hostname to s3.amazonaws.com when region is un-specified', ->
s3 = new AWS.S3(region: undefined)
expect(s3.endpoint.hostname).to.equal('s3.amazonaws.com')
it 'sets hostname to s3.amazonaws.com when region is us-east-1', ->
s3 = new AWS.S3(region: 'us-east-1')
expect(s3.endpoint.hostname).to.equal('s3.amazonaws.com')
it 'sets region to us-east-1 when unspecified', ->
s3 = new AWS.S3(region: 'us-east-1')
expect(s3.config.region).to.equal('us-east-1')
it 'combines the region with s3 in the endpoint using a - instead of .', ->
s3 = new AWS.S3(region: 'us-west-1')
expect(s3.endpoint.hostname).to.equal('s3-us-west-1.amazonaws.com')
it 'sets a region-specific dualstack endpoint when dualstack enabled', ->
s3 = new AWS.S3(region: 'us-west-1', useDualstack: true)
expect(s3.endpoint.hostname).to.equal('s3.dualstack.us-west-1.amazonaws.com')
s3 = new AWS.S3(region: 'us-east-1', useDualstack: true)
expect(s3.endpoint.hostname).to.equal('s3.dualstack.us-east-1.amazonaws.com')
describe 'clearing bucket region cache', ->
beforeEach ->
s3.bucketRegionCache = a: 'rg-fake-1', b: 'rg-fake-2', c: 'rg-fake-3'
it 'clears one bucket name', ->
s3.clearBucketRegionCache 'b'
expect(s3.bucketRegionCache).to.eql(a: 'rg-fake-1', c: 'rg-fake-3')
it 'clears a list of bucket names', ->
s3.clearBucketRegionCache ['a', 'c']
expect(s3.bucketRegionCache).to.eql(b: 'rg-fake-2')
it 'clears entire cache', ->
s3.clearBucketRegionCache()
expect(s3.bucketRegionCache).to.eql({})
describe 'getSignerClass', ->
getVersion = (signer) ->
if (signer == AWS.Signers.S3)
return 's3'
else if (signer == AWS.Signers.V4)
return 'v4'
else if (signer == AWS.Signers.V2)
return 'v2'
describe 'when using presigned requests', ->
req = null
beforeEach (done) ->
req = request('mock')
helpers.spyOn(req, 'isPresigned').andReturn(true)
done()
describe 'will return an s3 (v2) signer when', ->
it 'user does not specify a signatureVersion for a region that supports v2', (done) ->
s3 = new AWS.S3(region: 'us-east-1')
expect(getVersion(s3.getSignerClass(req))).to.equal('s3')
done()
it 'user specifies a signatureVersion of s3', (done) ->
s3 = new AWS.S3(signatureVersion: 's3')
expect(getVersion(s3.getSignerClass(req))).to.equal('s3')
done()
it 'user specifies a signatureVersion of v2', (done) ->
s3 = new AWS.S3(signatureVersion: 'v2')
expect(getVersion(s3.getSignerClass(req))).to.equal('s3')
done()
describe 'will return a v4 signer when', ->
it 'user does not specify a signatureVersion for a region that supports only v4', (done) ->
s3 = new AWS.S3(region: 'eu-central-1')
expect(getVersion(s3.getSignerClass(req))).to.equal('v4')
done()
it 'user specifies a signatureVersion of v4', (done) ->
s3 = new AWS.S3(signatureVersion: 'v4')
expect(getVersion(s3.getSignerClass(req))).to.equal('v4')
done()
describe 'when not using presigned requests', ->
describe 'will return an s3 (v2) signer when', ->
it 'user specifies a signatureVersion of s3', (done) ->
s3 = new AWS.S3(signatureVersion: 's3')
expect(getVersion(s3.getSignerClass())).to.equal('s3')
done()
it 'user specifies a signatureVersion of v2', (done) ->
s3 = new AWS.S3(signatureVersion: 'v2')
expect(getVersion(s3.getSignerClass())).to.equal('s3')
done()
it 'user does not specify a signatureVersion and region supports v2', (done) ->
s3 = new AWS.S3({region: 'us-east-1'})
expect(getVersion(s3.getSignerClass())).to.equal('s3')
done()
describe 'will return a v4 signer when', ->
it 'user does not specify a signatureVersion and region only supports v4', (done) ->
s3 = new AWS.S3({region: 'eu-central-1'})
expect(getVersion(s3.getSignerClass())).to.equal('v4')
done()
it 'user specifies a signatureVersion of v4', (done) ->
s3 = new AWS.S3(signatureVersion: 'v4')
expect(getVersion(s3.getSignerClass())).to.equal('v4')
done()
describe 'building a request', ->
build = (operation, params) ->
request(operation, params).build().httpRequest
it 'obeys the configuration for s3ForcePathStyle', ->
config = new AWS.Config(s3ForcePathStyle: true, accessKeyId: 'AKID', secretAccessKey: 'SECRET')
s3 = new AWS.S3(config)
expect(s3.config.s3ForcePathStyle).to.equal(true)
req = build('headObject', {Bucket:'bucket', Key:'key'})
expect(req.endpoint.hostname).to.equal('s3.amazonaws.com')
expect(req.path).to.equal('/bucket/key')
it 'does not enable path style if endpoint is a bucket', ->
s3 = new AWS.S3(endpoint: 'foo.bar', s3BucketEndpoint: true)
req = build('listObjects', Bucket: 'bucket')
expect(req.endpoint.hostname).to.equal('foo.bar')
expect(req.path).to.equal('/')
expect(req.virtualHostedBucket).to.equal('bucket')
it 'allows user override if an endpoint is specified', ->
s3 = new AWS.S3(endpoint: 'foo.bar', s3ForcePathStyle: true)
req = build('listObjects', Bucket: 'bucket')
expect(req.endpoint.hostname).to.equal('foo.bar')
expect(req.path).to.equal('/bucket')
it 'does not allow non-bucket operations with s3BucketEndpoint set', ->
s3 = new AWS.S3(endpoint: 'foo.bar', s3BucketEndpoint: true, paramValidation: true)
req = s3.listBuckets().build()
expect(req.response.error.code).to.equal('ConfigError')
it 'corrects virtual-hosted bucket region on request if bucket region stored in cache', ->
s3 = new AWS.S3(region: 'us-east-1')
s3.bucketRegionCache.name = 'us-west-2'
param = Bucket: 'name'
req = s3.headBucket(param).build()
httpRequest = req.httpRequest
expect(httpRequest.region).to.equal('us-west-2')
expect(httpRequest.endpoint.hostname).to.equal('name.s3-us-west-2.amazonaws.com')
expect(httpRequest.headers.Host).to.equal('name.s3-us-west-2.amazonaws.com')
expect(httpRequest.path).to.equal('/')
it 'corrects path-style bucket region on request if bucket region stored in cache', ->
s3 = new AWS.S3(region: 'us-east-1', s3ForcePathStyle: true)
s3.bucketRegionCache.name = 'us-west-2'
param = Bucket: 'name'
req = s3.headBucket(param).build()
httpRequest = req.httpRequest
expect(httpRequest.region).to.equal('us-west-2')
expect(httpRequest.endpoint.hostname).to.equal('s3-us-west-2.amazonaws.com')
expect(httpRequest.headers.Host).to.equal('s3-us-west-2.amazonaws.com')
expect(httpRequest.path).to.equal('/name')
describe 'with useAccelerateEndpoint set to true', ->
beforeEach ->
s3 = new AWS.S3(useAccelerateEndpoint: true)
it 'changes the hostname to use s3-accelerate for dns-comaptible buckets', ->
req = build('getObject', {Bucket: 'foo', Key: 'bar'})
expect(req.endpoint.hostname).to.equal('foo.s3-accelerate.amazonaws.com')
it 'overrides s3BucketEndpoint configuration when s3BucketEndpoint is set', ->
s3 = new AWS.S3(useAccelerateEndpoint: true, s3BucketEndpoint: true, endpoint: 'foo.region.amazonaws.com')
req = build('getObject', {Bucket: 'foo', Key: 'baz'})
expect(req.endpoint.hostname).to.equal('foo.s3-accelerate.amazonaws.com')
describe 'does not use s3-accelerate.dualstack or s3-accelerate', ->
it 'on dns-incompatible buckets', ->
req = build('getObject', {Bucket: 'foo.baz', Key: 'bar'})
expect(req.endpoint.hostname).to.not.contain('s3-accelerate')
it 'on excluded operations', ->
req = build('listBuckets')
expect(req.endpoint.hostname).to.not.contain('s3-accelerate')
req = build('createBucket', {Bucket: 'foo'})
expect(req.endpoint.hostname).to.not.contain('s3-accelerate')
req = build('deleteBucket', {Bucket: 'foo'})
expect(req.endpoint.hostname).to.not.contain('s3-accelerate')
describe 'with useAccelerateEndpoint and dualstack set to true', ->
beforeEach ->
s3 = new AWS.S3(useAccelerateEndpoint: true, useDualstack: true)
it 'changes the hostname to use s3-accelerate for dns-comaptible buckets', ->
req = build('getObject', {Bucket: 'foo', Key: 'bar'})
expect(req.endpoint.hostname).to.equal('foo.s3-accelerate.dualstack.amazonaws.com')
it 'overrides s3BucketEndpoint configuration when s3BucketEndpoint is set', ->
s3 = new AWS.S3(useAccelerateEndpoint: true, useDualstack: true, s3BucketEndpoint: true, endpoint: 'foo.region.amazonaws.com')
req = build('getObject', {Bucket: 'foo', Key: 'baz'})
expect(req.endpoint.hostname).to.equal('foo.s3-accelerate.dualstack.amazonaws.com')
describe 'does not use s3-accelerate.dualstack or s3-accelerate', ->
it 'on dns-incompatible buckets', ->
req = build('getObject', {Bucket: 'foo.baz', Key: 'bar'})
expect(req.endpoint.hostname).to.not.contain('s3-accelerate')
it 'on excluded operations', ->
req = build('listBuckets')
expect(req.endpoint.hostname).to.not.contain('s3-accelerate')
req = build('createBucket', {Bucket: 'foo'})
expect(req.endpoint.hostname).to.not.contain('s3-accelerate')
req = build('deleteBucket', {Bucket: 'foo'})
expect(req.endpoint.hostname).to.not.contain('s3-accelerate')
describe 'uri escaped params', ->
it 'uri-escapes path and querystring params', ->
# bucket param ends up as part of the hostname
params = { Bucket: 'bucket', Key: 'a b c', VersionId: 'a&b' }
req = build('headObject', params)
expect(req.path).to.equal('/a%20b%20c?versionId=a%26b')
it 'does not uri-escape forward slashes in the path', ->
params = { Bucket: 'bucket', Key: 'k e/y' }
req = build('headObject', params)
expect(req.path).to.equal('/k%20e/y')
it 'ensures a single forward slash exists', ->
req = build('listObjects', { Bucket: 'bucket' })
expect(req.path).to.equal('/')
req = build('listObjects', { Bucket: 'bucket', MaxKeys:123 })
expect(req.path).to.equal('/?max-keys=123')
describe 'adding Expect: 100-continue', ->
if AWS.util.isNode()
it 'does not add expect header to payloads less than 1MB', ->
req = build('putObject', Bucket: 'bucket', Key: 'key', Body: new Buffer(1024 * 1024 - 1))
expect(req.headers['Expect']).not.to.exist
it 'adds expect header to payloads greater than 1MB', ->
req = build('putObject', Bucket: 'bucket', Key: 'key', Body: new Buffer(1024 * 1024 + 1))
expect(req.headers['Expect']).to.equal('100-continue')
if AWS.util.isBrowser()
beforeEach -> helpers.spyOn(AWS.util, 'isBrowser').andReturn(true)
it 'does not add expect header in the browser', ->
req = build('putObject', Bucket: 'bucket', Key: 'key', Body: new Buffer(1024 * 1024 + 1))
expect(req.headers['Expect']).not.to.exist
describe 'with s3DisableBodySigning set to true', ->
it 'will disable body signing when using signature version 4 and the endpoint uses https', ->
s3 = new AWS.S3(s3DisableBodySigning: true, signatureVersion: 'v4')
req = build('putObject', Bucket: 'bucket', Key: 'key', Body: new Buffer(1024*1024*5))
expect(req.headers['X-Amz-Content-Sha256']).to.equal('UNSIGNED-PAYLOAD')
it 'will compute contentMD5', ->
s3 = new AWS.S3(s3DisableBodySigning: true, signatureVersion: 'v4')
buf = new Buffer(1024*1024*5)
buf.fill(0)
req = build('putObject', Bucket: 'bucket', Key: 'key', Body: buf)
expect(req.headers['Content-MD5']).to.equal('XzY+DlipXwbL6bvGYsXftg==')
it 'will not disable body signing when the endpoint is not https', ->
s3 = new AWS.S3(s3DisableBodySigning: true, signatureVersion: 'v4', sslEnabled: false)
req = build('putObject', Bucket: 'bucket', Key: 'key', Body: new Buffer(1024*1024*5))
expect(req.headers['X-Amz-Content-Sha256']).to.exist
expect(req.headers['X-Amz-Content-Sha256']).to.not.equal('UNSIGNED-PAYLOAD')
it 'will have no effect when sigv2 signing is used', ->
s3 = new AWS.S3(s3DisableBodySigning: true, signatureVersion: 's3', sslEnabled: true)
req = build('putObject', Bucket: 'bucket', Key: 'key', Body: new Buffer(1024*1024*5))
expect(req.headers['X-Amz-Content-Sha256']).to.not.exist
describe 'with s3DisableBodySigning set to false', ->
it 'will sign the body when sigv4 is used', ->
s3 = new AWS.S3(s3DisableBodySigning: false, signatureVersion: 'v4')
req = build('putObject', Bucket: 'bucket', Key: 'key', Body: new Buffer(1024*1024*5))
expect(req.headers['X-Amz-Content-Sha256']).to.exist
expect(req.headers['X-Amz-Cotnent-Sha256']).to.not.equal('UNSIGNED-PAYLOAD')
it 'will have no effect when sigv2 signing is used', ->
s3 = new AWS.S3(s3DisableBodySigning: false, signatureVersion: 's3', sslEnabled: true)
req = build('putObject', Bucket: 'bucket', Key: 'key', Body: new Buffer(1024*1024*5))
expect(req.headers['X-Amz-Content-Sha256']).to.not.exist
describe 'adding Content-Type', ->
beforeEach -> helpers.spyOn(AWS.util, 'isBrowser').andReturn(true)
it 'adds default content-type when not supplied', ->
req = build('putObject', Bucket: 'bucket', Key: 'key', Body: 'body')
expect(req.headers['Content-Type']).to.equal('application/octet-stream; charset=UTF-8')
it 'does not add content-type for GET/HEAD requests', ->
req = build('getObject', Bucket: 'bucket', Key: 'key')
expect(req.headers['Content-Type']).not.to.exist
req = build('headObject', Bucket: 'bucket', Key: 'key')
expect(req.headers['Content-Type']).not.to.exist
it 'adds charset to existing content-type if not supplied', ->
req = build('putObject', Bucket: 'bucket', Key: 'key', Body: 'body', ContentType: 'text/html')
expect(req.headers['Content-Type']).to.equal('text/html; charset=UTF-8')
it 'normalized charset to uppercase', ->
req = build('putObject', Bucket: 'bucket', Key: 'key', Body: 'body', ContentType: 'text/html; charset=utf-8')
expect(req.headers['Content-Type']).to.equal('text/html; charset=UTF-8')
it 'does not add charset to non-string data', ->
req = build('putObject', Bucket: 'bucket', Key: 'key', Body: new Buffer('body'), ContentType: 'image/png')
expect(req.headers['Content-Type']).to.equal('image/png')
describe 'virtual-hosted vs path-style bucket requests', ->
describe 'HTTPS', ->
beforeEach ->
s3 = new AWS.S3(sslEnabled: true, region: undefined)
it 'puts dns-compat bucket names in the hostname', ->
req = build('headObject', {Bucket:'bucket-name',Key:'abc'})
expect(req.method).to.equal('HEAD')
expect(req.endpoint.hostname).to.equal('bucket-name.s3.amazonaws.com')
expect(req.path).to.equal('/abc')
it 'ensures the path contains / at a minimum when moving bucket', ->
req = build('listObjects', {Bucket:'bucket-name'})
expect(req.endpoint.hostname).to.equal('bucket-name.s3.amazonaws.com')
expect(req.path).to.equal('/')
it 'puts dns-compat bucket names in path if they contain a dot', ->
req = build('listObjects', {Bucket:'bucket.name'})
expect(req.endpoint.hostname).to.equal('s3.amazonaws.com')
expect(req.path).to.equal('/bucket.name')
it 'puts dns-compat bucket names in path if configured to do so', ->
s3 = new AWS.S3(sslEnabled: true, s3ForcePathStyle: true, region: undefined)
req = build('listObjects', {Bucket:'bucket-name'})
expect(req.endpoint.hostname).to.equal('s3.amazonaws.com')
expect(req.path).to.equal('/bucket-name')
it 'puts dns-incompat bucket names in path', ->
req = build('listObjects', {Bucket:'bucket_name'})
expect(req.endpoint.hostname).to.equal('s3.amazonaws.com')
expect(req.path).to.equal('/bucket_name')
describe 'HTTP', ->
beforeEach ->
s3 = new AWS.S3(sslEnabled: false, region: undefined)
it 'puts dns-compat bucket names in the hostname', ->
req = build('listObjects', {Bucket:'bucket-name'})
expect(req.endpoint.hostname).to.equal('bucket-name.s3.amazonaws.com')
expect(req.path).to.equal('/')
it 'puts dns-compat bucket names in the hostname if they contain a dot', ->
req = build('listObjects', {Bucket:'bucket.name'})
expect(req.endpoint.hostname).to.equal('bucket.name.s3.amazonaws.com')
expect(req.path).to.equal('/')
it 'puts dns-incompat bucket names in path', ->
req = build('listObjects', {Bucket:'bucket_name'})
expect(req.endpoint.hostname).to.equal('s3.amazonaws.com')
expect(req.path).to.equal('/bucket_name')
describe 'HTTPS dualstack', ->
beforeEach ->
s3 = new AWS.S3(sslEnabled: true, region: undefined, useDualstack: true)
it 'puts dns-compat bucket names in the hostname', ->
req = build('headObject', {Bucket:'bucket-name',Key:'abc'})
expect(req.method).to.equal('HEAD')
expect(req.endpoint.hostname).to.equal('bucket-name.s3.dualstack.us-east-1.amazonaws.com')
expect(req.path).to.equal('/abc')
it 'ensures the path contains / at a minimum when moving bucket', ->
req = build('listObjects', {Bucket:'bucket-name'})
expect(req.endpoint.hostname).to.equal('bucket-name.s3.dualstack.us-east-1.amazonaws.com')
expect(req.path).to.equal('/')
it 'puts dns-compat bucket names in path if they contain a dot', ->
req = build('listObjects', {Bucket:'bucket.name'})
expect(req.endpoint.hostname).to.equal('s3.dualstack.us-east-1.amazonaws.com')
expect(req.path).to.equal('/bucket.name')
it 'puts dns-compat bucket names in path if configured to do so', ->
s3 = new AWS.S3(sslEnabled: true, s3ForcePathStyle: true, region: undefined, useDualstack: true)
req = build('listObjects', {Bucket:'bucket-name'})
expect(req.endpoint.hostname).to.equal('s3.dualstack.us-east-1.amazonaws.com')
expect(req.path).to.equal('/bucket-name')
it 'puts dns-incompat bucket names in path', ->
req = build('listObjects', {Bucket:'bucket_name'})
expect(req.endpoint.hostname).to.equal('s3.dualstack.us-east-1.amazonaws.com')
expect(req.path).to.equal('/bucket_name')
describe 'HTTP dualstack', ->
beforeEach ->
s3 = new AWS.S3(sslEnabled: false, region: undefined, useDualstack: true)
it 'puts dns-compat bucket names in the hostname', ->
req = build('listObjects', {Bucket:'bucket-name'})
expect(req.endpoint.hostname).to.equal('bucket-name.s3.dualstack.us-east-1.amazonaws.com')
expect(req.path).to.equal('/')
it 'puts dns-compat bucket names in the hostname if they contain a dot', ->
req = build('listObjects', {Bucket:'bucket.name'})
expect(req.endpoint.hostname).to.equal('bucket.name.s3.dualstack.us-east-1.amazonaws.com')
expect(req.path).to.equal('/')
it 'puts dns-incompat bucket names in path', ->
req = build('listObjects', {Bucket:'bucket_name'})
expect(req.endpoint.hostname).to.equal('s3.dualstack.us-east-1.amazonaws.com')
expect(req.path).to.equal('/bucket_name')
describe 'SSE support', ->
beforeEach -> s3 = new AWS.S3
it 'fails if the scheme is not HTTPS: when SSECustomerKey is provided', ->
req = s3.putObject
Bucket: 'bucket', Key: 'key', Body: 'object'
SSECustomerKey: 'sse-key', SSECustomerAlgorithm: 'AES256'
req.httpRequest.endpoint.protocol = 'http:'
req.build()
expect(req.response.error.code).to.equal('ConfigError')
it 'fails if the scheme is not HTTPS: when CopySourceSSECustomerKey is provided', ->
req = s3.putObject
Bucket: 'bucket', Key: 'key', Body: 'object'
CopySourceSSECustomerKey: 'sse-key', CopySourceSSECustomerAlgorithm: 'AES256'
req.httpRequest.endpoint.protocol = 'http:'
req.build()
expect(req.response.error.code).to.equal('ConfigError')
describe 'SSECustomerKey', ->
it 'encodes strings keys and fills in MD5', ->
req = s3.putObject
Bucket: 'bucket', Key: 'key', Body: 'data'
SSECustomerKey: 'KEY', SSECustomerAlgorithm: 'AES256'
req.build()
expect(req.httpRequest.headers['x-amz-server-side-encryption-customer-key']).
to.equal('S0VZ')
expect(req.httpRequest.headers['x-amz-server-side-encryption-customer-key-MD5']).
to.equal('O1lJ4MJrh3Z6R1Kidt6VcA==')
it 'encodes blob keys and fills in MD5', ->
req = s3.putObject
Bucket: 'bucket', Key: 'key', Body: 'data'
SSECustomerKey: new AWS.util.Buffer('098f6bcd4621d373cade4e832627b4f6', 'hex')
SSECustomerAlgorithm: 'AES256'
req.build()
expect(req.httpRequest.headers['x-amz-server-side-encryption-customer-key']).
to.equal('CY9rzUYh03PK3k6DJie09g==')
expect(req.httpRequest.headers['x-amz-server-side-encryption-customer-key-MD5']).
to.equal('YM1UqSjLvLtue1WVurRqng==')
describe 'CopySourceSSECustomerKey', ->
it 'encodes string keys and fills in MD5', ->
req = s3.copyObject
Bucket: 'bucket', Key: 'key', CopySource: 'bucket/oldkey', Body: 'data'
CopySourceSSECustomerKey: 'KEY', CopySourceSSECustomerAlgorithm: 'AES256'
req.build()
expect(req.httpRequest.headers['x-amz-copy-source-server-side-encryption-customer-key']).
to.equal('S0VZ')
expect(req.httpRequest.headers['x-amz-copy-source-server-side-encryption-customer-key-MD5']).
to.equal('O1lJ4MJrh3Z6R1Kidt6VcA==')
it 'encodes blob keys and fills in MD5', ->
req = s3.copyObject
Bucket: 'bucket', Key: 'key', CopySource: 'bucket/oldkey', Body: 'data'
CopySourceSSECustomerKey: new AWS.util.Buffer('098f6bcd4621d373cade4e832627b4f6', 'hex')
CopySourceSSECustomerAlgorithm: 'AES256'
req.build()
expect(req.httpRequest.headers['x-amz-copy-source-server-side-encryption-customer-key']).
to.equal('CY9rzUYh03PK3k6DJie09g==')
expect(req.httpRequest.headers['x-amz-copy-source-server-side-encryption-customer-key-MD5']).
to.equal('YM1UqSjLvLtue1WVurRqng==')
describe 'retry behavior', ->
it 'retries RequestTimeout errors', ->
s3.config.maxRetries = 3
helpers.mockHttpResponse 400, {},
'<xml><Code>RequestTimeout</Code><Message>message</Message></xml>'
s3.putObject (err, data) ->
expect(@retryCount).to.equal(s3.config.maxRetries)
# Managed Upload integration point
describe 'upload', ->
it 'accepts parameters in upload() call', ->
helpers.mockResponses [ { data: { ETag: 'ETAG' } } ]
done = false
s3.upload({Bucket: 'bucket', Key: 'key', Body: 'body'}, -> done = true)
expect(done).to.equal(true)
it 'accepts options as a second parameter', ->
helpers.mockResponses [ { data: { ETag: 'ETAG' } } ]
upload = s3.upload({Bucket: 'bucket', Key: 'key', Body: 'body'}, {queueSize: 2}, ->)
expect(upload.queueSize).to.equal(2)
it 'does not send if no callback is supplied', ->
s3.upload(Bucket: 'bucket', Key: 'key', Body: 'body')
describe 'extractData', ->
it 'caches bucket region if found in header', ->
req = request('operation', {Bucket: 'name'})
resp = new AWS.Response(req)
resp.httpResponse.headers = 'x-amz-bucket-region': 'rg-fake-1'
req.emit('extractData', [resp])
expect(s3.bucketRegionCache.name).to.equal('rg-fake-1')
# S3 returns a handful of errors without xml bodies (to match the
# http spec) these tests ensure we give meaningful codes/messages for these.
describe 'errors with no XML body', ->
regionReqOperation = if AWS.util.isNode() then 'headBucket' else 'listObjects'
maxKeysParam = if regionReqOperation == 'listObjects' then 0 else undefined
extractError = (statusCode, body, addHeaders, req) ->
if !req
req = request('operation')
resp = new AWS.Response(req)
resp.httpResponse.body = new Buffer(body || '')
resp.httpResponse.statusCode = statusCode
resp.httpResponse.headers = {'x-amz-request-id': 'RequestId', 'x-amz-id-2': 'ExtendedRequestId'}
for header, value of addHeaders
resp.httpResponse.headers[header] = value
req.emit('extractError', [resp])
resp.error
it 'handles 304 errors', ->
error = extractError(304)
expect(error.code).to.equal('NotModified')
expect(error.message).to.equal(null)
it 'handles 400 errors', ->
error = extractError(400)
expect(error.code).to.equal('BadRequest')
expect(error.message).to.equal(null)
it 'handles 403 errors', ->
error = extractError(403)
expect(error.code).to.equal('Forbidden')
expect(error.message).to.equal(null)
it 'handles 404 errors', ->
error = extractError(404)
expect(error.code).to.equal('NotFound')
expect(error.message).to.equal(null)
it 'extracts the region from body and takes precedence over cache', ->
s3.bucketRegionCache.name = 'us-west-2'
req = request('operation', {Bucket: 'name'})
body = """
<Error>
<Code>InvalidArgument</Code>
<Message>Provided param is bad</Message>
<Region>eu-west-1</Region>
</Error>
"""
error = extractError(400, body, {}, req)
expect(error.region).to.equal('eu-west-1')
expect(s3.bucketRegionCache.name).to.equal('eu-west-1')
it 'extracts the region from header and takes precedence over body and cache', ->
s3.bucketRegionCache.name = 'us-west-2'
req = request('operation', {Bucket: 'name'})
body = """
<Error>
<Code>InvalidArgument</Code>
<Message>Provided param is bad</Message>
<Region>eu-west-1</Region>
</Error>
"""
headers = 'x-amz-bucket-region': 'us-east-1'
error = extractError(400, body, headers, req)
expect(error.region).to.equal('us-east-1')
expect(s3.bucketRegionCache.name).to.equal('us-east-1')
it 'uses cache if region not extracted from body or header', ->
s3.bucketRegionCache.name = 'us-west-2'
req = request('operation', {Bucket: 'name'})
body = """
<Error>
<Code>InvalidArgument</Code>
<Message>Provided param is bad</Message>
</Error>
"""
error = extractError(400, body, {}, req)
expect(error.region).to.equal('us-west-2')
expect(s3.bucketRegionCache.name).to.equal('us-west-2')
it 'does not use cache if not different from current region', ->
s3.bucketRegionCache.name = 'us-west-2'
req = request('operation', {Bucket: 'name'})
req.httpRequest.region = 'us-west-2'
body = """
<Error>
<Code>InvalidArgument</Code>
<Message>Provided param is bad</Message>
</Error>
"""
error = extractError(400, body)
expect(error.region).to.not.exist
expect(s3.bucketRegionCache.name).to.equal('us-west-2')
it 'does not make async request for bucket region if error.region is set', ->
regionReq = send: (fn) ->
fn()
spy = helpers.spyOn(s3, regionReqOperation).andReturn(regionReq)
req = request('operation', {Bucket: 'name'})
body = """
<Error>
<Code>PermanentRedirect</Code>
<Message>Message</Message>
</Error>
"""
headers = 'x-amz-bucket-region': 'us-east-1'
error = extractError(301, body, headers, req)
expect(error.region).to.exist
expect(spy.calls.length).to.equal(0)
expect(regionReq._requestRegionForBucket).to.not.exist
it 'makes async request for bucket region if error.region not set for a region redirect error code', ->
regionReq = send: (fn) ->
fn()
spy = helpers.spyOn(s3, regionReqOperation).andReturn(regionReq)
params = Bucket: 'name'
req = request('operation', params)
body = """
<Error>
<Code>PermanentRedirect</Code>
<Message>Message</Message>
</Error>
"""
error = extractError(301, body, {}, req)
expect(error.region).to.not.exist
expect(spy.calls.length).to.equal(1)
expect(spy.calls[0].arguments[0].Bucket).to.equal('name')
expect(spy.calls[0].arguments[0].MaxKeys).to.equal(maxKeysParam)
expect(regionReq._requestRegionForBucket).to.exist
it 'does not make request for bucket region if error code is not a region redirect code', ->
regionReq = send: (fn) ->
fn()
spy = helpers.spyOn(s3, regionReqOperation).andReturn(regionReq)
req = request('operation', {Bucket: 'name'})
body = """
<Error>
<Code>InvalidCode</Code>
<Message>Message</Message>
</Error>
"""
error = extractError(301, body, {}, req)
expect(error.region).to.not.exist
expect(spy.calls.length).to.equal(0)
expect(regionReq._requestRegionForBucket).to.not.exist
it 'updates error.region if async request adds region to cache', ->
regionReq = send: (fn) ->
s3.bucketRegionCache.name = 'us-west-2'
fn()
spy = helpers.spyOn(s3, regionReqOperation).andReturn(regionReq)
req = request('operation', {Bucket: 'name'})
body = """
<Error>
<Code>PermanentRedirect</Code>
<Message>Message</Message>
</Error>
"""
error = extractError(301, body, {}, req)
expect(spy.calls.length).to.equal(1)
expect(spy.calls[0].arguments[0].Bucket).to.equal('name')
expect(spy.calls[0].arguments[0].MaxKeys).to.equal(maxKeysParam)
expect(error.region).to.equal('us-west-2')
it 'extracts the request ids', ->
error = extractError(400)
expect(error.requestId).to.equal('RequestId')
expect(error.extendedRequestId).to.equal('ExtendedRequestId')
it 'misc errors not known to return an empty body', ->
error = extractError(412) # made up
expect(error.code).to.equal(412)
expect(error.message).to.equal(null)
it 'uses canned errors only when the body is empty', ->
body = """
<xml>
<Code>ErrorCode</Code>
<Message>ErrorMessage</Message>
</xml>
"""
error = extractError(403, body)
expect(error.code).to.equal('ErrorCode')
expect(error.message).to.equal('ErrorMessage')
describe 'retryableError', ->
it 'should retry on authorization header with updated region', ->
err = {code: 'AuthorizationHeaderMalformed', statusCode:400, region: 'eu-west-1'}
req = request('operation', {Bucket: 'name'})
req.build()
retryable = s3.retryableError(err, req)
expect(retryable).to.equal(true)
expect(req.httpRequest.region).to.equal('eu-west-1')
expect(req.httpRequest.endpoint.hostname).to.equal('name.s3.amazonaws.com')
it 'should retry on bad request with updated region', ->
err = {code: 'BadRequest', statusCode:400, region: 'eu-west-1'}
req = request('operation', {Bucket: 'name'})
req.build()
retryable = s3.retryableError(err, req)
expect(retryable).to.equal(true)
expect(req.httpRequest.region).to.equal('eu-west-1')
expect(req.httpRequest.endpoint.hostname).to.equal('name.s3.amazonaws.com')
it 'should retry on permanent redirect with updated region and endpoint', ->
err = {code: 'PermanentRedirect', statusCode:301, region: 'eu-west-1'}
req = request('operation', {Bucket: 'name'})
req.build()
retryable = s3.retryableError(err, req)
expect(retryable).to.equal(true)
expect(req.httpRequest.region).to.equal('eu-west-1')
expect(req.httpRequest.endpoint.hostname).to.equal('name.s3-eu-west-1.amazonaws.com')
it 'should retry on error code 301 with updated region and endpoint', ->
err = {code: 301, statusCode:301, region: 'eu-west-1'}
req = request('operation', {Bucket: 'name'})
req.build()
retryable = s3.retryableError(err, req)
expect(retryable).to.equal(true)
expect(req.httpRequest.region).to.equal('eu-west-1')
expect(req.httpRequest.endpoint.hostname).to.equal('name.s3-eu-west-1.amazonaws.com')
it 'should retry with updated region but not endpoint if non-S3 url endpoint is specified', ->
err = {code: 'PermanentRedirect', statusCode:301, region: 'eu-west-1'}
s3 = new AWS.S3(endpoint: 'https://fake-custom-url.com', s3BucketEndpoint: true)
req = request('operation', {Bucket: 'name'})
req.build()
retryable = s3.retryableError(err, req)
expect(retryable).to.equal(true)
expect(req.httpRequest.region).to.equal('eu-west-1')
expect(req.httpRequest.endpoint.hostname).to.equal('fake-custom-url.com')
it 'should retry with updated endpoint if S3 url endpoint is specified', ->
err = {code: 'PermanentRedirect', statusCode:301, region: 'eu-west-1'}
s3 = new AWS.S3(endpoint: 'https://name.s3-us-west-2.amazonaws.com', s3BucketEndpoint: true)
req = request('operation', {Bucket: 'name'})
req.build()
retryable = s3.retryableError(err, req)
expect(retryable).to.equal(true)
expect(req.httpRequest.region).to.equal('eu-west-1')
expect(req.httpRequest.endpoint.hostname).to.equal('name.s3-eu-west-1.amazonaws.com')
it 'should retry with updated region but not endpoint if accelerate endpoint is used', ->
err = {code: 'PermanentRedirect', statusCode:301, region: 'eu-west-1'}
s3 = new AWS.S3(useAccelerateEndpoint: true)
req = request('operation', {Bucket: 'name'})
req.build()
retryable = s3.retryableError(err, req)
expect(retryable).to.equal(true)
expect(req.httpRequest.region).to.equal('eu-west-1')
expect(req.httpRequest.endpoint.hostname).to.equal('name.s3-accelerate.amazonaws.com')
it 'should retry with updated endpoint if dualstack endpoint is used', ->
err = {code: 'PermanentRedirect', statusCode:301, region: 'eu-west-1'}
s3 = new AWS.S3(useDualstack: true)
req = request('operation', {Bucket: 'name'})
req.build()
retryable = s3.retryableError(err, req)
expect(retryable).to.equal(true)
expect(req.httpRequest.region).to.equal('eu-west-1')
expect(req.httpRequest.endpoint.hostname).to.equal('name.s3.dualstack.eu-west-1.amazonaws.com')
it 'should not retry on requests for bucket region once region is obtained', ->
err = {code: 'PermanentRedirect', statusCode:301, region: 'eu-west-1'}
req = request('operation', {Bucket: 'name'})
req._requestRegionForBucket = 'name'
retryable = []
retryable.push s3.retryableError(err, req)
s3.bucketRegionCache.name = 'eu-west-1'
retryable.push s3.retryableError(err, req)
expect(retryable).to.eql([true, false])
describe 'browser NetworkingError due to wrong region', ->
done = ->
spy = null
regionReq = null
callNetworkingErrorListener = (req) ->
if !req
req = request('operation', {Bucket: 'name'})
if req._asm.currentState == 'validate'
req.build()
resp = new AWS.Response(req)
resp.error = code: 'NetworkingError'
s3.reqRegionForNetworkingError(resp, done)
req
beforeEach ->
s3 = new AWS.S3(region: 'us-west-2')
regionReq = request('operation', {Bucket: 'name'})
regionReq.send = (fn) ->
fn()
helpers.spyOn(AWS.util, 'isBrowser').andReturn(true)
spy = helpers.spyOn(s3, 'listObjects').andReturn(regionReq)
it 'updates region to us-east-1 if bucket name not DNS compatible', ->
req = request('operation', {Bucket: 'name!'})
callNetworkingErrorListener(req)
expect(req.httpRequest.region).to.equal('us-east-1')
expect(req.httpRequest.endpoint.hostname).to.equal('s3.amazonaws.com')
expect(s3.bucketRegionCache['name!']).to.equal('us-east-1')
expect(spy.calls.length).to.equal(0)
it 'updates region if cached and not current region', ->
req = request('operation', {Bucket: 'name'})
req.build()
s3.bucketRegionCache.name = 'eu-west-1'
callNetworkingErrorListener(req)
expect(req.httpRequest.region).to.equal('eu-west-1')
expect(req.httpRequest.endpoint.hostname).to.equal('name.s3-eu-west-1.amazonaws.com')
expect(spy.calls.length).to.equal(0)
it 'makes async request in us-east-1 if not in cache', ->
regionReq.send = (fn) ->
s3.bucketRegionCache.name = 'eu-west-1'
fn()
req = callNetworkingErrorListener()
expect(spy.calls.length).to.equal(1)
expect(regionReq.httpRequest.region).to.equal('us-east-1')
expect(regionReq.httpRequest.endpoint.hostname).to.equal('name.s3.amazonaws.com')
expect(req.httpRequest.region).to.equal('eu-west-1')
expect(req.httpRequest.endpoint.hostname).to.equal('name.s3-eu-west-1.amazonaws.com')
it 'makes async request in us-east-1 if cached region matches current region', ->
s3.bucketRegionCache.name = 'us-west-2'
regionReq.send = (fn) ->
s3.bucketRegionCache.name = 'eu-west-1'
fn()
req = callNetworkingErrorListener()
expect(spy.calls.length).to.equal(1)
expect(regionReq.httpRequest.region).to.equal('us-east-1')
expect(regionReq.httpRequest.endpoint.hostname).to.equal('name.s3.amazonaws.com')
expect(req.httpRequest.region).to.equal('eu-west-1')
expect(req.httpRequest.endpoint.hostname).to.equal('name.s3-eu-west-1.amazonaws.com')
it 'does not update region if path-style bucket is dns-compliant and not in cache', ->
s3.config.s3ForcePathStyle = true
regionReq.send = (fn) ->
s3.bucketRegionCache.name = 'eu-west-1'
fn()
req = callNetworkingErrorListener()
expect(spy.calls.length).to.equal(0)
expect(req.httpRequest.region).to.equal('us-west-2')
expect(req.httpRequest.endpoint.hostname).to.equal('s3-us-west-2.amazonaws.com')
# tests from this point on are "special cases" for specific aws operations
describe 'getBucketAcl', ->
it 'correctly parses the ACL XML document', ->
headers = { 'x-amz-request-id' : 'request-id' }
body =
"""
<AccessControlPolicy xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
<AccessControlList>
<Grant>
<Grantee xsi:type="CanonicalUser" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<DisplayName>aws-sdk</DisplayName>
<ID>id</ID>
</Grantee>
<Permission>FULL_CONTROL</Permission>
</Grant>
<Grant>
<Grantee xsi:type="Group" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<URI>uri</URI>
</Grantee>
<Permission>READ</Permission>
</Grant>
</AccessControlList>
<Owner>
<DisplayName>aws-sdk</DisplayName>
<ID>id</ID>
</Owner>
</AccessControlPolicy>
"""
helpers.mockHttpResponse 200, headers, body
s3.getBucketAcl (error, data) ->
expect(error).to.equal(null)
expect(data).to.eql({
Owner:
DisplayName: 'aws-sdk',
ID: 'id'
Grants: [
{
Permission: 'FULL_CONTROL'
Grantee:
Type: 'CanonicalUser'
DisplayName: 'aws-sdk'
ID: 'id'
},
{
Permission : 'READ'
Grantee:
Type: 'Group'
URI: 'uri'
}
]
})
describe 'putBucketAcl', ->
it 'correctly builds the ACL XML document', ->
xml =
"""
<AccessControlPolicy xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
<AccessControlList>
<Grant>
<Grantee xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="CanonicalUser">
<DisplayName>aws-sdk</DisplayName>
<ID>id</ID>
</Grantee>
<Permission>FULL_CONTROL</Permission>
</Grant>
<Grant>
<Grantee xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="Group">
<URI>uri</URI>
</Grantee>
<Permission>READ</Permission>
</Grant>
</AccessControlList>
<Owner>
<DisplayName>aws-sdk</DisplayName>
<ID>id</ID>
</Owner>
</AccessControlPolicy>
"""
helpers.mockHttpResponse 200, {}, ''