-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathutil.spec.js
1810 lines (1743 loc) · 57.7 KB
/
util.spec.js
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
// Generated by CoffeeScript 1.12.3
(function() {
var AWS, Buffer, helpers;
helpers = require('./helpers');
AWS = helpers.AWS;
Buffer = AWS.util.Buffer;
describe('uriEscape', function() {
var e;
e = AWS.util.uriEscape;
it('escapes spaces as %20', function() {
return expect(e('a b')).to.equal('a%20b');
});
it('escapes + as %2B', function() {
return expect(e('a+b')).to.equal('a%2Bb');
});
it('escapes / as %2F', function() {
return expect(e('a/b')).to.equal('a%2Fb');
});
it('escapes \' as %27', function() {
return expect(e('a\'b')).to.equal('a%27b');
});
it('escapes * as %2A', function() {
return expect(e('a*b')).to.equal('a%2Ab');
});
it('does not escape ~', function() {
return expect(e('a~b')).to.equal('a~b');
});
return it('encodes utf8 characters', function() {
return expect(e('ёŝ')).to.equal('%D1%91%C5%9D');
});
});
describe('readFileSync', function() {
var err, errorFound, readFileSync;
readFileSync = AWS.util.readFileSync;
if (AWS.util.isBrowser()) {
return it('will always return null in the browser', function() {
return expect(readFileSync('fake/path')).to.eql(null);
});
} else {
errorFound = false;
try {
readFileSync('fake/path');
} catch (error) {
err = error;
errorFound = true;
}
return expect(errorFound).to.equal(true);
}
});
describe('uriEscapePath', function() {
var e;
e = AWS.util.uriEscapePath;
it('does not escape forward slashes', function() {
var s;
s = 'a&b/x=y/1+2/m?n';
return expect(e(s)).to.equal('a%26b/x%3Dy/1%2B2/m%3Fn');
});
return it('leaves leading and trailing forward slashes in place', function() {
var s;
s = '/ab cd/';
return expect(e(s)).to.equal('/ab%20cd/');
});
});
describe('AWS.util.queryParamsToString', function() {
var qpts;
qpts = AWS.util.queryParamsToString;
it('sorts query parameters before stringifying', function() {
return expect(qpts({
c: '1',
b: '2',
a: '3'
})).to.equal('a=3&b=2&c=1');
});
it('handles empty values', function() {
return expect(qpts({
a: '',
b: '2'
})).to.equal('a=&b=2');
});
it('handles null/undefined values', function() {
return expect(qpts({
a: void 0,
b: null
})).to.equal('a=&b=');
});
it('calls uriEscape on each name and value', function() {
var spy;
spy = helpers.spyOn(AWS.util, 'uriEscape').andCallThrough();
qpts({
c: '1',
b: '2',
a: '3'
});
return expect(spy.calls.length).to.equal(6);
});
it('handles values as lists', function() {
return expect(qpts({
a: ['c', 'b', 'a'],
b: '4'
})).to.equal('a=a&a=b&a=c&b=4');
});
it('escapes list values', function() {
return expect(qpts({
a: ['+', '&', '*'],
b: '4'
})).to.equal('a=%26&a=%2A&a=%2B&b=4');
});
return it('does not provide value if value is null', function() {
return expect(qpts({
a: null,
b: null
})).to.equal('a=&b=');
});
});
describe('AWS.util.date', function() {
var util;
util = AWS.util.date;
describe('getDate', function() {
it('should return current date by default', function() {
var now, obj;
now = new Date(0);
obj = AWS.util.isNode() ? global : window;
helpers.spyOn(obj, 'Date').andCallFake(function() {
return now;
});
return expect(util.getDate()).to.equal(now);
});
return describe('systemClockOffset', function() {
var config, date, mocked, ref;
ref = [], date = ref[0], mocked = ref[1], config = ref[2];
beforeEach(function() {
var obj, ref1;
ref1 = [Date, false, AWS.config], date = ref1[0], mocked = ref1[1], config = ref1[2];
obj = AWS.util.isNode() ? global : window;
return helpers.spyOn(obj, 'Date').andCallFake(function(t) {
if (mocked) {
return new date(t);
} else {
mocked = true;
return new date(0);
}
});
});
afterEach(function() {
AWS.config = config;
return AWS.config.systemClockOffset = 0;
});
it('returns a date with a millisecond offset if provided', function() {
AWS.config.systemClockOffset = 10000;
return expect(util.getDate().getTime()).to.equal(10000);
});
it('returns a date with a millisecond offset from a non-Config object', function() {
AWS.config = {
systemClockOffset: 10000
};
expect(util.getDate().getTime()).to.equal(10000);
return AWS.config = config;
});
return it('returns a date with no offset if non-Config object has no systemClockOffset property', function() {
AWS.config = {};
return expect(util.getDate().getTime()).to.equal(0);
});
});
});
describe('applyClockOffset', function() {
it('should apply new clock offset to AWS.config given new service time', function() {
var now = new Date().getTime();
AWS.config.systemClockOffset = 0;
AWS.util.applyClockOffset(now + 30000);
var updatedOffset = AWS.config.systemClockOffset;
expect(29900 < updatedOffset && 30100 > updatedOffset).to.equal(true);
});
});
describe('isClockSkewed', function() {
it('should apply new clock offset to AWS.config given new service time', function() {
var util = AWS.util;
var now = new Date();
obj = AWS.util.isNode() ? global : window;
helpers.spyOn(obj, 'Date').andCallFake(function() {
return now;
});
expect(util.isClockSkewed(now.getTime() + 300100)).to.equal(true);
expect(util.isClockSkewed(now.getTime() + 299900)).to.equal(false);
});
});
describe('iso8601', function() {
it('should return date formatted as YYYYMMDDTHHmmssZ', function() {
var date;
date = new Date(600000);
date.setMilliseconds(0);
helpers.spyOn(util, 'getDate').andCallFake(function() {
return date;
});
return expect(util.iso8601()).to.equal('1970-01-01T00:10:00Z');
});
return it('should allow date parameter', function() {
var date;
date = new Date(660000);
date.setMilliseconds(0);
return expect(util.iso8601(date)).to.equal('1970-01-01T00:11:00Z');
});
});
describe('rfc822', function() {
it('should return date formatted as YYYYMMDDTHHmmssZ', function() {
var date;
date = new Date(600000);
date.setMilliseconds(0);
helpers.spyOn(util, 'getDate').andCallFake(function() {
return date;
});
return expect(util.rfc822()).to.match(/^Thu, 0?1 Jan 1970 00:10:00 (GMT|UTC)$/);
});
return it('should allow date parameter', function() {
var date;
date = new Date(660000);
date.setMilliseconds(0);
return expect(util.rfc822(date)).to.match(/^Thu, 0?1 Jan 1970 00:11:00 (GMT|UTC)$/);
});
});
return describe('unixTimestamp', function() {
it('should return date formatted as unix timestamp', function() {
var date;
date = new Date(600000);
date.setMilliseconds(0);
helpers.spyOn(util, 'getDate').andCallFake(function() {
return date;
});
return expect(util.unixTimestamp()).to.equal(600);
});
it('should allow date parameter', function() {
var date;
date = new Date(660000);
date.setMilliseconds(0);
return expect(util.unixTimestamp(date)).to.equal(660);
});
return it('should return date formatted as unix timestamp with milliseconds', function() {
helpers.spyOn(util, 'getDate').andCallFake(function() {
return new Date(600123);
});
return expect(util.unixTimestamp()).to.equal(600.123);
});
});
});
describe('AWS.util.string', function() {
var len;
len = AWS.util.string.byteLength;
return describe('byteLength', function() {
it('handles null/undefined objects', function() {
expect(len(void 0)).to.equal(0);
return expect(len(null)).to.equal(0);
});
it('handles buffer input', function() {
return expect(len(new AWS.util.buffer.toBuffer('∂ƒ©∆'))).to.equal(10);
});
it('handles string input', function() {
expect(len('')).to.equal(0);
return expect(len('∂ƒ©∆')).to.equal(10);
});
if (AWS.util.isNode()) {
it('handles file object input (path property)', function() {
var file, fileLen, fs;
fs = require('fs');
file = fs.createReadStream(__filename);
fileLen = fs.lstatSync(file.path).size;
expect(len(file)).to.equal(fileLen);
return expect(len({
path: __filename
})).to.equal(fileLen);
});
}
it('fails if input is not a string, buffer, or file', function() {
var e, err;
err = null;
try {
len(3.14);
} catch (error) {
e = error;
err = e;
}
expect(err.message).to.equal('Cannot determine length of 3.14');
return expect(err.object).to.equal(3.14);
});
return it('ignores path property unless it is a string', function() {
var e, err, object;
object = {};
err = null;
try {
len(object);
} catch (error) {
e = error;
err = e;
}
expect(err.message).to.match(/Cannot determine length of /);
return expect(err.object).to.equal(object);
});
});
});
describe('AWS.util.ini', function() {
describe('parse', function() {
it('parses an ini file', function() {
var ini, map;
ini = '; comment at the beginning of the line\n[section1] ; comment at end of line\ninvalidline\nkey1=value1 ; another comment\n key2 = value2;value3\n key3 = value4 # yet another comment\n[emptysection]\n#key1=value1';
map = AWS.util.ini.parse(ini);
expect(map.section1.key1).to.equal('value1');
expect(map.section1.key2).to.equal('value2;value3');
expect(map.section1.key3).to.equal('value4');
expect(map.emptysection).to.equal(void 0);
});
it('ignores leading and trailing white space', function() {
var ini, map;
ini = '[section1] ; comment at end of line\n\r\tkey1=\t\rvalue1\t\r\n\v\f\tkey2=value2\f\v\n\u00a0key3 = \u00a0value3\u3000\n[emptysection]';
map = AWS.util.ini.parse(ini);
expect(map.section1.key1).to.equal('value1');
expect(map.section1.key2).to.equal('value2');
expect(map.section1.key3).to.equal('value3');
});
it('throws if the profile name is __proto__', function(done) {
var ini, map;
ini = '[__proto__]\nkey1=value1';
try {
AWS.util.ini.parse(ini);
} catch (err) {
expect(err.message).to.equal('Cannot load profile name \'__proto__\' from shared ini file.');
done();
}
});
it('throws if the profile name is "profile __proto__"', function(done) {
var ini, map;
ini = '[profile __proto__]\nkey2=value2';
try {
AWS.util.ini.parse(ini);
} catch (err) {
expect(err.message).to.equal('Cannot load profile name \'profile __proto__\' from shared ini file.');
done();
}
});
});
});
describe('AWS.util.buffer', function() {
describe ('alloc', function () {
it('should throw if first parameter is not number', function(done) {
try {
AWS.util.buffer.alloc('foo');
} catch (e) {
expect(e.message).to.eql('size passed to alloc must be a number.');
done();
}
});
});
return describe('concat', function() {
return it('concatenates a list of buffers', function() {
var buffer1, buffer2, buffer3;
buffer1 = AWS.util.buffer.toBuffer('abcdefg');
buffer2 = AWS.util.buffer.toBuffer('hijklmn');
buffer3 = AWS.util.buffer.concat([buffer1, buffer2]);
expect(buffer3.length).to.equal(14);
return expect(buffer3.toString()).to.equal('abcdefghijklmn');
});
});
});
describe('AWS.util.crypto', function() {
var util;
util = AWS.util.crypto;
describe('crc32', function() {
it('returns the correct CRC32 value for binary data', function() {
var buffer, i, j, ref;
buffer = AWS.util.buffer.alloc(4433);
for (i = j = 0, ref = buffer.length; 0 <= ref ? j < ref : j > ref; i = 0 <= ref ? ++j : --j) {
buffer.writeUInt8(i % 256, i);
}
return expect(util.crc32(buffer)).to.equal(899332870);
});
return it('handles String values', function() {
var string;
string = '{"ConsumedCapacityUnits":1.0}';
return expect(util.crc32(string)).to.equal(2614884069);
});
});
describe('toHex', function() {
return it('should convert binary data to hex string', function() {
return expect(util.toHex('ABC')).to.equal('414243');
});
});
describe('hmac', function() {
var input, key, result;
input = 'foo';
key = 'KEY';
result = '116a3725a3540067a09e4dba64bb6b3fb27b4d98a1a2e2dbcb8b4cffa73585d5';
it('should return a keyed hash as a binary digest', function() {
var expected;
expected = util.hmac(key, input);
return expect(util.toHex(expected)).to.equal(result);
});
it('should return a keyed hash as hex digest', function() {
var expected;
expected = util.hmac(key, input, 'hex');
return expect(expected).to.equal(result);
});
it('accepts the crypto function as an argument', function() {
var r;
r = util.hmac('secret', 'the quick brown fox', 'base64', 'sha1');
return expect(r).to.equal('z1BzGT+uG/2qGzE1UHb5m/skn1E=');
});
return it('accepts UTF-8 input for string', function() {
var r;
r = util.hmac('foo', 'å∆ç∂', 'hex');
return expect(r).to.equal('bc11556145cbe4935ba187b9f8ba0c12bae2c866e5795013dfe2d08cabc33e13');
});
});
describe('sha256', function() {
var input, result;
input = 'foo';
result = '2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae';
it('should return binary data hashed with sha256', function() {
var expected;
expected = util.sha256(input);
return expect(util.toHex(expected)).to.equal(result);
});
it('should return hex data hashed with sha256', function() {
var expected;
expected = util.sha256(input, 'hex');
return expect(expected).to.equal(result);
});
it('accepts UTF-8 input', function() {
var r;
r = util.sha256('ß∂ƒ©', 'hex');
return expect(r).to.equal('3c01ddd413d2cacac59a255e4aade0d9058a8a9ea8b2dfe5bb2dc4ed132b4139');
});
it('handles async interface', function() {
return util.sha256(input, 'hex', function(e, d) {
return expect(d).to.equal(result);
});
});
if (AWS.util.isNode()) {
it('handles streams in async interface', function(done) {
var Transform, tr;
Transform = AWS.util.stream.Transform;
tr = new Transform;
tr._transform = function(data, encoding, callback) {
return callback(null, data);
};
tr.push(AWS.util.buffer.toBuffer(input));
tr.end();
return util.sha256(tr, 'hex', function(e, d) {
expect(d).to.equal(result);
return done();
});
});
}
if (AWS.util.isBrowser()) {
it('handles Blobs (no phantomjs)', function(done) {
result = 'a665a45920422f9d417e4867efdc4fb8a04a1f3fff1fa07e998e86f7f7a27ae3';
return util.sha256(new Blob([1, 2, 3]), 'hex', function(e, d) {
expect(e).to.eql(null);
expect(d).to.equal(result);
return done();
});
});
return it('handles Uint8Array objects directly', function(done) {
result = '039058c6f2c0cb492c533b0a4d14ef77cc0f78abccced5287d84a1a2011cfb81';
return util.sha256(new Uint8Array([1, 2, 3]), 'hex', function(e, d) {
expect(e).to.eql(null);
expect(d).to.equal(result);
return done();
});
});
}
});
return describe('md5', function() {
var input, result;
input = 'foo';
result = 'acbd18db4cc2f85cedef654fccc4a4d8';
it('should return binary data hashed with md5', function() {
var expected;
expected = util.md5(input);
return expect(util.toHex(expected)).to.equal(result);
});
it('should return hex data hashed with md5', function() {
var expected;
expected = util.md5(input, 'hex');
return expect(expected).to.equal(result);
});
it('accepts UTF-8 input', function() {
var r;
r = util.md5('ᅢ', 'hex');
return expect(r).to.equal('b497dbbe19fb58cddaeef11f9d40804c');
});
it('handles async interface', function() {
return util.md5(input, 'hex', function(e, d) {
return expect(d).to.equal(result);
});
});
if (AWS.util.isNode()) {
return it('handles streams in async interface', function(done) {
var Transform, tr;
Transform = AWS.util.stream.Transform;
tr = new Transform;
tr._transform = function(data, enc, callback) {
return callback(null, data);
};
tr.push(AWS.util.buffer.toBuffer(input));
tr.end();
return util.md5(tr, 'hex', function(e, d) {
expect(d).to.equal(result);
return done();
});
});
}
});
});
describe('AWS.util.each', function() {
it('should iterate over a hash', function() {
var parts;
parts = [];
AWS.util.each({
a: 1,
b: 2,
c: 3
}, function(key, item) {
return parts.push([key + '_', item + 1]);
});
return expect(parts).to.eql([['a_', 2], ['b_', 3], ['c_', 4]]);
});
it('should iterate over an array', function() {
var total;
total = 0;
AWS.util.each([1, 2, 3], function(idx, item) {
return total += item;
});
return expect(total).to.equal(6);
});
it('should ignore inherited properties', function() {
var obj, objCtor, parts;
objCtor = function() {
this.a = 1;
this.b = 2;
return this.c = 3;
};
objCtor.prototype = {
d: 4,
e: 5,
f: 6
};
obj = new objCtor();
parts = [];
AWS.util.each(obj, function(key, item) {
return parts.push([key + '_', item + 1]);
});
return expect(parts).to.eql([['a_', 2], ['b_', 3], ['c_', 4]]);
});
it('callback should not change "this" scope', function() {
return new function() {
var self;
this['class'] = 'MyClass';
self = this;
return AWS.util.each.apply(this, [
[1, 2, 3], function() {
return expect(this).to.equal(self);
}
]);
};
});
return it('can abort out of loop', function() {
var string;
string = '';
AWS.util.each({
a: 1,
b: 2,
c: 3
}, function(key, item) {
if (item === 2) {
return AWS.util.abort;
}
return string += key;
});
return expect(string).to.equal('a');
});
});
describe('AWS.util.arrayEach', function() {
it('should iterate over arrays', function() {
var total;
total = 0;
AWS.util.arrayEach([1, 2, 3], function(item) {
return total += item;
});
return expect(total).to.equal(6);
});
it('should pass index as second parameter', function() {
var lastIndex;
lastIndex = null;
return AWS.util.arrayEach([1, 2, 3], function(item, idx) {
expect(typeof idx).to.equal('number');
if (lastIndex !== null) {
expect(lastIndex).to.equal(idx - 1);
}
return lastIndex = idx;
});
});
return it('can abort out of loop', function() {
var total;
total = 0;
AWS.util.arrayEach([1, 2, 3], function(item, idx) {
if (idx === 1) {
return AWS.util.abort;
}
return total += item;
});
return expect(total).to.equal(1);
});
});
describe('AWS.util.copy', function() {
it('does not copy null or undefined', function() {
expect(AWS.util.copy(null)).to.equal(null);
return expect(AWS.util.copy(void 0)).to.equal(void 0);
});
it('should perform a shallow copy of an object', function() {
var copied, obj;
obj = {
a: 1,
b: 2,
c: 3
};
copied = AWS.util.copy(obj);
expect(copied).not.to.equal(obj);
return expect(copied).to.eql({
a: 1,
b: 2,
c: 3
});
});
return it('should copy inherited properties', function() {
var copied, obj, objCtor;
objCtor = function() {
this.a = 1;
this.b = 2;
return this.c = 3;
};
objCtor.prototype = {
d: 4
};
obj = new objCtor();
copied = AWS.util.copy(obj);
expect(copied).not.to.equal(obj);
return expect(copied).to.eql({
a: 1,
b: 2,
c: 3,
d: 4
});
});
});
describe('AWS.util.merge', function() {
it('should merge an object into another and return new object', function() {
var newObj, obj;
obj = {
a: 1,
b: 2,
c: 3
};
newObj = AWS.util.merge(obj, {
d: 4,
e: 5,
a: 6
});
expect(newObj).to.eql({
a: 6,
b: 2,
c: 3,
d: 4,
e: 5
});
return expect(obj).to.eql({
a: 1,
b: 2,
c: 3
});
});
});
describe('AWS.util.update', function() {
it('should merge an object into another', function() {
var obj;
obj = {
a: 1,
b: 2,
c: 3
};
AWS.util.update(obj, {
d: 4,
e: 5,
a: 6
});
return expect(obj).to.eql({
a: 6,
b: 2,
c: 3,
d: 4,
e: 5
});
});
it('should return the merged object', function() {
var obj;
obj = {
a: 1,
b: 2
};
return expect(AWS.util.update(obj, {
c: 3
})).to.eql({a: 1, b: 2, c: 3});
});
});
describe('AWS.util.inherit', function() {
it('should inherit an object and append features', function() {
var Base, Derived, derived;
Base = function(value) {
return this.defaultValue = value;
};
Base.prototype = {
main: function() {
return 'main';
},
other: 'other'
};
Derived = AWS.util.inherit(Base, {
constructor: function(value) {
return Base.apply(this, [value + 5]);
},
main: function() {
return 'notMain';
},
foo: function() {
return 'bar';
}
});
derived = new Derived(5);
expect(derived).to.be.instanceOf(Base);
expect(derived.constructor).to.equal(Derived);
expect(derived.main()).to.equal('notMain');
expect(derived.other).to.equal('other');
expect(derived.defaultValue).to.equal(10);
return expect(derived.foo()).to.equal('bar');
});
return it('should create pass-through constructor if not defined', function() {
var Base, Derived, derived;
Base = AWS.util.inherit({
constructor: helpers.createSpy()
});
Derived = AWS.util.inherit(Base, {
other: true
});
derived = new Derived(1, 2, 'three');
expect(derived.other).to.equal(true);
return expect(Base.prototype.constructor.calls[0]['arguments']).to.eql([1, 2, 'three']);
});
});
describe('AWS.util.mixin', function() {
it('copies properties to other object prototype', function() {
var obj;
obj = {
prototype: {
a: 1,
b: 2
}
};
AWS.util.mixin(obj, {
prototype: {
b: 3,
c: 4
}
});
return expect(obj.prototype).to.eql({
a: 1,
b: 3,
c: 4
});
});
it('resets prototype constructor', function() {
var obj;
obj = {
prototype: {
constructor: 'PASS'
}
};
AWS.util.mixin(obj, {
prototype: {
constructor: 'FAIL'
}
});
return expect(obj.prototype).to.eql({
constructor: 'PASS'
});
});
return it('returns original klass', function() {
var obj, out;
obj = {
prototype: {
foo: 1
}
};
out = AWS.util.mixin(obj, {
prototype: {
bar: 2
}
});
return expect(out).to.equal(obj);
});
});
describe('AWS.util.isType', function() {
it('accepts function for type', function() {
return expect(AWS.util.isType([], Array)).to.equal(true);
});
return it('accepts string for type', function() {
return expect(AWS.util.isType([], 'Array')).to.equal(true);
});
});
describe('AWS.util.isEmpty', function() {
it('returns true when passed an empty object literal', function() {
return expect(AWS.util.isEmpty({})).to.equal(true);
});
return it('returns true when passed a non-empty object literal', function() {
return expect(AWS.util.isEmpty({
a: 1
})).to.equal(false);
});
});
describe('AWS.util.error', function() {
it('returns the created error object with extra options', function() {
var err, origError;
origError = new Error();
err = AWS.util.error(origError, {
message: 'msg',
code: 'code'
});
expect(err).to.equal(origError);
expect(err.message).to.equal('msg');
return expect(err.code).to.equal('code');
});
it('accepts missing options', function() {
var err, origError;
origError = new Error('ERROR');
err = AWS.util.error(origError);
expect(err).to.equal(origError);
return expect(err.message).to.equal('ERROR');
});
it('maintains the original error message property', function() {
var err, origError;
origError = new Error('ERROR');
err = AWS.util.error(origError, {
code: 'code'
});
expect(err).to.equal(origError);
expect(err.message).to.equal('ERROR');
return expect(err.code).to.equal('code');
});
return it('keeps track of the old error', function() {
var err, origError;
origError = new Error('ERROR');
origError.value = 1;
err = AWS.util.error(origError, {
code: 'code',
message: 'FOO'
});
expect(err.originalError.message).to.equal('ERROR');
expect(err.originalError.code).to.equal(void 0);
return expect(err.originalError.value).to.equal(1);
});
});
describe('AWS.util.base64', function() {
var base64;
base64 = AWS.util.base64;
describe('encode', function() {
it('encodes the given string', function() {
expect(base64.encode('foo')).to.equal('Zm9v');
return expect(base64.encode('ёŝ')).to.equal('0ZHFnQ==');
});
it('encodes the given buffer', function() {
expect(base64.encode(AWS.util.buffer.toBuffer('foo'))).to.equal('Zm9v');
return expect(base64.encode(AWS.util.buffer.toBuffer('ёŝ'))).to.equal('0ZHFnQ==');
});
it('throws if a number is supplied', function() {
var e, err;
err = null;
try {
base64.encode(3.14);
} catch (error) {
e = error;
err = e;
}
return expect(err.message).to.equal('Cannot base64 encode number 3.14');
});
it('does not encode null', function() {
return expect(base64.encode(null)).to.eql(null);
});
return it('does not encode undefined', function() {
return expect(base64.encode(void 0)).to.eql(void 0);
});
});
return describe('decode', function() {
it('decodes the given string', function() {
expect(base64.decode('Zm9v').toString()).to.equal('foo');
return expect(base64.decode('0ZHFnQ==').toString()).to.equal('ёŝ');
});
it('decodes the given buffer', function() {
expect(base64.decode(AWS.util.buffer.toBuffer('Zm9v', 'base64')).toString()).to.equal('foo');
return expect(base64.decode(AWS.util.buffer.toBuffer('0ZHFnQ==', 'base64')).toString()).to.equal('ёŝ');
});
it('throws if a number is supplied', function() {
var e, err;
err = null;
try {
base64.decode(3.14);
} catch (error) {
e = error;
err = e;
}
return expect(err.message).to.equal('Cannot base64 decode number 3.14');
});
it('does not decode null', function() {
return expect(base64.decode(null)).to.eql(null);
});
return it('does not decode undefined', function() {
return expect(base64.decode(void 0)).to.eql(void 0);
});
});
});
describe('AWS.util.hoistPayloadMember', function() {
var buildService, hoist, service;
hoist = AWS.util.hoistPayloadMember;
service = null;
buildService = function(api) {
return service = new AWS.Service({
endpoint: 'http://localhost',
apiConfig: api
});
};
it('hoists structure payload members', function(done) {
var api, httpResp, req;
api = {
'metadata': {
'protocol': 'rest-xml'
},
'operations': {
'sample': {
'output': {
'shape': 'OutputShape'
}
}
},
'shapes': {
'OutputShape': {
'type': 'structure',
'payload': 'Data',
'members': {
'Data': {
'shape': 'SingleStructure'
}
}
},
'StringType': {
'type': 'string'
},
'SingleStructure': {
'type': 'structure',
'members': {
'Foo': {
'shape': 'StringType'
}
}
}
}