-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathdiscover_endpoint.spec.js
943 lines (892 loc) · 37.3 KB
/
discover_endpoint.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
var helpers = require('./helpers');
var http = require('http');
var url = require('url');
var AWS = helpers.AWS;
var endpoint_discovery_module = require('../lib/discover_endpoint');
var iniLoader = AWS.util.iniLoader;
var getCacheKey = endpoint_discovery_module.getCacheKey;
var marshallCustomIdentifiers = endpoint_discovery_module.marshallCustomIdentifiers;
var api = {
metadata: {
apiVersion: '2018-09-19',
endpointPrefix: 'mockservice',
serviceId: 'MockService',
serviceAbbreviation: 'MockService',
protocol: 'json'
},
operations: {
DescribeEndpoints: {
name: 'DescribeEndpoints',
http: {},
endpointoperation: true,
input: {
type: 'structure',
required: ['Operation'],
members: {
Operation: {},
Identifiers: {
type: 'map',
key: {},
value: {},
}
}
},
output: {
type: 'structure',
required: ['Endpoints'],
members: {
Endpoints: {
type: 'list',
member: {
type: 'structure',
members: {
Address: {},
CachePeriodInMinutes: {type: 'integer'}
}
}
}
}
}
},
OptionalEDOperation: {
name: 'OptionalEDOperation',
http: {},
endpointdiscovery: {},
input: {
type: 'structure',
members: {
Query: {}
}
},
output: {
type: 'structure',
members: {
Item: {}
}
}
},
RequiredEDOperation: {
name: 'RequiredEDOperation',
http: {},
endpointdiscovery: {
required: true
},
input: {
type: 'structure',
required: ['Query'],
members: {
Query: {
type: 'string',
endpointdiscoveryid: true
},
Record: {
type: 'string',
}
}
},
output: {
type: 'structure',
members: {
Item: {}
}
}
},
ComplexEDOperation: {
name: 'ComplexEDOperation',
http: {},
endpointdiscovery: {
required: false
},
input: {
type: 'structure',
required: ['Query'],
members: {
Query: {
type: 'structure',
required: ['Record'],
members: {
Record: {
type: 'string',
locationName: 'RecordItem',
endpointdiscoveryid: true
}
}
}
}
}
}
}
};
// API with only optional endpoint discovery operations
var optionalEDOnlyApi = {
metadata: api.metadata,
operations: {
DescribeEndpoints: api.operations.DescribeEndpoints,
ComplexEDOperation: api.operations.ComplexEDOperation,
OptionalEDOperation: api.operations.OptionalEDOperation
}
};
// API with only required endpoint discovery operations
var requiredEDOnlyApi = {
metadata: api.metadata,
operations: {
DescribeEndpoints: api.operations.DescribeEndpoints,
RequiredEDOperation: api.operations.RequiredEDOperation
}
};
describe('endpoint discovery', function() {
afterEach(function() {
AWS.config.endpoint = undefined;
if (iniLoader) iniLoader.clearCachedFiles(); //iniLoader not available in browsers
});
describe('getCacheKey', function() {
it ('generate correct keys', function() {
var client = new AWS.Service({
region: 'fake-region-1',
apiConfig: new AWS.Model.Api(api)
});
var request = client.makeRequest('optionalEDOperation', {});
var cacheKey = getCacheKey(request);
expect(cacheKey).to.eql({
region: 'fake-region-1',
serviceId: 'MockService',
accessKeyId: 'akid'
});
});
it('generate correct keys with endpointdiscoveryid traits', function() {
var spy = helpers.spyOn(AWS.endpointCache, 'get').andReturn([{
Address: 'https://fakeService.amaoznaws.com'
}]);
var client = new AWS.Service({
endpointDiscoveryEnabled: true,
region: 'fake-region-1',
apiConfig: new AWS.Model.Api(api)
});
helpers.mockHttpResponse(200, {}, '{}');
var request = client.makeRequest('requiredEDOperation', {Query: 'query', Record: 'record'});
request.send();
expect(spy.calls.length).to.eql(1);
expect(spy.calls[0].arguments[0]).to.eql(AWS.EndpointCache.getKeyString({
region: 'fake-region-1',
operation: 'RequiredEDOperation',
serviceId: 'MockService',
accessKeyId: 'akid',
Query: 'query'
}));
});
});
describe('marshallCustomIdentifiers', function() {
it('can marshall params according to endpointdiscoveryid trait', function() {
var MockService = helpers.MockServiceFromApi(api);
var client = new MockService();
var ids = marshallCustomIdentifiers({
params: {Query: 'query', Record: 'record'}
}, client.api.operations.requiredEDOperation.input);
expect(ids).to.eql({Query: 'query'});
ids = marshallCustomIdentifiers({
params: {Query: {Record: 'record'}}
}, client.api.operations.complexEDOperation.input);
expect(ids).to.eql({RecordItem: 'record'});
});
});
describe('optionalEndpointDiscovery', function() {
beforeEach(function() {
AWS.endpointCache.empty();
});
it('gets first corresponding endpoint from endpoint cache', function() {
var spy = helpers.spyOn(AWS.endpointCache, 'get').andReturn([{
Address: 'https://fakeService.amazonaws.com:22/path'
}, {
Address: 'https://fakeService.amaoznaws.com'
}]);
var client = new AWS.Service({
endpointDiscoveryEnabled: true,
region: 'fake-region-1',
apiConfig: new AWS.Model.Api(optionalEDOnlyApi)
});
helpers.mockHttpResponse(200, {}, '{}');
var request = client.makeRequest('optionalEDOperation', {Query: 'query'});
request.send();
expect(spy.calls.length).to.eql(1);
expect(request.httpRequest.endpoint).to.include({
hostname: 'fakeservice.amazonaws.com',
port: 22,
protocol: 'https:',
path: '/path'
});
});
it('use regional endpoint of new endpoint cannot be discovered', function() {
var client = new AWS.Service({
endpointDiscoveryEnabled: true,
region: 'fake-region-1',
apiConfig: new AWS.Model.Api(optionalEDOnlyApi)
});
var request = client.makeRequest('optionalEDOperation', {Query: 'query'});
expect(request.httpRequest.endpoint.hostname).to.eql('mockservice.fake-region-1.amazonaws.com');
});
it('put in endpoint cache is endpoint operation succeeds', function() {
var client = new AWS.Service({
endpointDiscoveryEnabled: true,
region: 'fake-region-1',
maxRetries: 0,
apiConfig: new AWS.Model.Api(optionalEDOnlyApi)
});
var spy = helpers.spyOn(AWS.endpointCache, 'put').andCallThrough();
var request = client.makeRequest('optionalEDOperation', {Query: 'query'});
expect(request.httpRequest.endpoint.hostname).to.eql('mockservice.fake-region-1.amazonaws.com');
helpers.mockHttpResponse(200, {}, '{"Endpoints": [{"Address": "https://cell1.fakeservice.amazonaws.com/fakeregion", "CachePeriodInMinutes": 1}]}');
request.send();
expect(spy.calls.length).to.eql(2);
expect(spy.calls[0].arguments[0]).to.eql({
accessKeyId: 'akid',
region: 'fake-region-1',
serviceId: 'MockService'
});
expect(spy.calls[0].arguments[1]).to.eql([{Address: '', CachePeriodInMinutes: 1}]);
expect(spy.calls[1].arguments[0]).to.eql({
accessKeyId: 'akid',
region: 'fake-region-1',
serviceId: 'MockService'
});
expect(spy.calls[1].arguments[1]).to.eql([{Address: 'https://cell1.fakeservice.amazonaws.com/fakeregion', CachePeriodInMinutes: 1}]);
var makeRequestSpy = helpers.spyOn(client, 'makeRequest').andCallThrough();
var replicatedRequest = client.makeRequest('optionalEDOperation', {Query: 'query'});
replicatedRequest.send(function() {
expect(this.request.httpRequest.endpoint).to.include({
hostname: 'cell1.fakeservice.amazonaws.com',
protocol: 'https:',
path: '/fakeregion'
});
//do not request endpoint again
expect(makeRequestSpy.calls.length).to.eql(1);
expect(makeRequestSpy.calls[0].arguments[0]).to.eql('optionalEDOperation');
});
});
it('make one endpoint operation for same cache keys before endpoint operation response returning', function() {
var spy = helpers.spyOn(AWS.HttpClient, 'getInstance').andCallThrough();
var client = new AWS.Service({
endpointDiscoveryEnabled: true,
region: 'fake-region-1',
maxRetries: 0,
apiConfig: new AWS.Model.Api(optionalEDOnlyApi)
});
client.makeRequest('optionalEDOperation', {Query: 'query'}).send();
client.makeRequest('optionalEDOperation', {Query: 'query'}).send();
//should not make 4 calls which request endpoint once for every api call
expect(spy.calls.length).to.eql(3);
});
it('should put in an undefined cache record valid for 1 minute when failed', function() {
var client = new AWS.Service({
endpointDiscoveryEnabled: true,
maxRetries: 0,
apiConfig: new AWS.Model.Api(optionalEDOnlyApi)
});
helpers.mockHttpResponse(400, {}, '');
var spy = helpers.spyOn(AWS.endpointCache, 'put').andCallThrough();
client.makeRequest('optionalEDOperation', {Query: 'query'}).send();
expect(spy.calls.length).to.eql(2);
expect(spy.calls[1].arguments[0]).to.eql({
accessKeyId: 'akid',
region: 'mock-region',
serviceId: 'MockService'
});
expect(spy.calls[1].arguments[1]).to.eql([{
Address: '',
CachePeriodInMinutes: 1
}]);
});
it('evict invalid endpoint records from cache if InvalidEndpointException encountered', function() {
var client = new AWS.Service({
region: 'fake-region-1',
endpointDiscoveryEnabled: true,
apiConfig: new AWS.Model.Api(optionalEDOnlyApi)
});
helpers.mockHttpResponse(400, {}, '{"__type":"InvalidEndpointException", "Message":"Error Message" }');
var cacheKey = {
accessKeyId: 'akid',
region: 'fake-region-1',
serviceId: 'MockService'
};
AWS.endpointCache.put(cacheKey, [{Address: 'https://cell1.fakeservice.amazonaws.com/fakeregion'}]);
var cacheRemoveSpy = helpers.spyOn(AWS.endpointCache, 'remove').andCallThrough();
var request = client.makeRequest('optionalEDOperation', {Query: 'query'});
request.send();
expect(request.httpRequest.endpoint.href).to.eql('https://cell1.fakeservice.amazonaws.com/fakeregion');
expect(cacheRemoveSpy.calls.length).to.eql(1);
expect(cacheRemoveSpy.calls[0].arguments[0]).to.eql(cacheKey);
expect(AWS.endpointCache.size).to.eql(0);
});
it('add "x-amz-api-version" header to discovery endpoint operation in optional endpoint discovery', function() {
var client = new AWS.Service({
endpointDiscoveryEnabled: true,
apiConfig: new AWS.Model.Api(optionalEDOnlyApi),
});
var https = require('https');
helpers.spyOn(https, 'request').andCallThrough();
var request = client.makeRequest('optionalEDOperation', {Query: 'query'});
helpers.mockHttpResponse(200, {}, '{"Endpoints": [{"Address": "https://cell1.fakeservice.amazonaws.com/fakeregion", "CachePeriodInMinutes": 1}]}');
var spy = helpers.createSpy('send inject');
AWS.events.on('sign', function(req) {
if (req.operation === 'describeEndpoints') {
spy(req);
}
});
request.send();
expect(spy.calls.length).to.eql(1);
expect(spy.calls[0].arguments[0].httpRequest.headers['x-amz-api-version']).to.eql('2018-09-19');
});
});
describe('requiredEndpointDiscovery', function() {
beforeEach(function() {
AWS.endpointCache.empty();
});
it('fail when endpoint discovery fails', function() {
var client = new AWS.Service({
maxRetries: 0,
apiConfig: new AWS.Model.Api(requiredEDOnlyApi)
});
var body = '{"message": "failed to discover endpoint", "__type": "EndpointNotReady"}';
helpers.mockHttpResponse(400, {}, body);
var request = client.makeRequest('requiredEDOperation', {Query: 'query', Record: 'record'});
request.send(function(err, data) {
expect(err).not.to.eql(null);
expect(data).to.eql(null);
expect(err.code).to.eql('EndpointNotReady');
expect(err.message).to.eql('failed to discover endpoint');
});
});
it('endpoint operation will be called at the same rate of api requests', function() {
var client = new AWS.Service({
apiConfig: new AWS.Model.Api(requiredEDOnlyApi)
});
helpers.mockHttpResponse(400, {}, '');
var makeRequestSpy = helpers.spyOn(client, 'makeRequest').andCallThrough();
for (var apiRequestCount = 0; apiRequestCount < 10; apiRequestCount++) {
var request = client.makeRequest('requiredEDOperation', {Query: 'query', Record: 'record'});
request.send();
}
expect(makeRequestSpy.calls.length).to.eql(20);
var apiRequestCount = 0;
var endpointOperationRequestCount = 0;
for (var i = 0; i < makeRequestSpy.calls.length; i++) {
var operationName = makeRequestSpy.calls[i].arguments[0];
if (operationName === 'requiredEDOperation') apiRequestCount++;
else if (operationName === 'describeEndpoints') endpointOperationRequestCount++;
}
expect(apiRequestCount).to.eql(10);
expect(endpointOperationRequestCount).to.eql(10);
});
it('put in endpoint cache and use returned endpoint when endpoint discovery succeeds', function() {
var client = new AWS.Service({
apiConfig: new AWS.Model.Api(requiredEDOnlyApi)
});
var request = client.makeRequest('requiredEDOperation', {Query: 'query', Record: 'record'});
expect(request.httpRequest.endpoint.hostname).to.eql('mockservice.mock-region.amazonaws.com');
helpers.mockHttpResponse(200, {}, '{"Endpoints": [{"Address": "https://cell2.fakeservice.amazonaws.com/fakeregion", "CachePeriodInMinutes": 1}]}');
var putCacheCount = 0;
var putInCacheSpy = helpers.spyOn(AWS.endpointCache, 'put').andCallFake(function() {
//when discover endpoint optionally, \'put\' method is called twice each time
if (arguments[1].length > 0 && arguments[1][0].Address !== '') putCacheCount++;
//call through origin put method
putInCacheSpy.origMethod.apply(putInCacheSpy.object, arguments);
});
request.send();
expect(putInCacheSpy.calls.length).to.eql(2);
expect(putCacheCount).to.eql(1);
expect(putInCacheSpy.calls[1].arguments[1]).to.eql([{Address: 'https://cell2.fakeservice.amazonaws.com/fakeregion', CachePeriodInMinutes: 1}]);
var makeRequestSpy = helpers.spyOn(client, 'makeRequest').andCallThrough();
var replicatedRequest = client.makeRequest('requiredEDOperation', {Query: 'query', Record: 'record'});
replicatedRequest.send(function() {
expect(this.request.httpRequest.endpoint).to.include({
hostname: 'cell2.fakeservice.amazonaws.com',
protocol: 'https:',
path: '/fakeregion'
});
//do not request endpoint again
expect(makeRequestSpy.calls.length).to.eql(1);
expect(makeRequestSpy.calls[0].arguments[0]).to.eql('requiredEDOperation');
});
});
it('evict invalid endpoint records from cache if InvalidEndpointException encountered', function() {
var client = new AWS.Service({
apiConfig: new AWS.Model.Api(requiredEDOnlyApi)
});
helpers.mockHttpResponse(400, {}, '{"__type":"InvalidEndpointException", "Message":"Error Message" }');
var cacheKey = {
accessKeyId: 'akid',
operation: 'RequiredEDOperation',
region: 'mock-region',
serviceId: 'MockService',
Query: 'query'
};
AWS.endpointCache.put(cacheKey, [{Address: 'https://cell1.fakeservice.amazonaws.com/fakeregion', CachePeriodInMinutes: 1}]);
var cacheRemoveSpy = helpers.spyOn(AWS.endpointCache, 'remove').andCallThrough();
var request = client.makeRequest('requiredEDOperation', {Query: 'query', Record: 'record'});
request.send();
expect(request.httpRequest.endpoint.href).to.eql('https://cell1.fakeservice.amazonaws.com/fakeregion');
expect(cacheRemoveSpy.calls.length).to.eql(1);
expect(cacheRemoveSpy.calls[0].arguments[0]).to.eql(cacheKey);
expect(AWS.endpointCache.size).to.eql(0);
});
it('add "x-amz-api-version" header to discovery endpoint operation in required endpoint discovery', function() {
var client = new AWS.Service({
apiConfig: new AWS.Model.Api(requiredEDOnlyApi),
});
var https = require('https');
helpers.spyOn(https, 'request').andCallThrough();
var request = client.makeRequest('requiredEDOperation', {Query: 'query', Record: 'record'});
helpers.mockHttpResponse(200, {}, '{"Endpoints": [{"Address": "https://cell2.fakeservice.amazonaws.com/fakeregion", "CachePeriodInMinutes": 1}]}');
var spy = helpers.createSpy('send inject');
AWS.events.on('sign', function(req) {
if (req.operation === 'describeEndpoints') {
spy(req);
}
});
request.send();
expect(spy.calls.length).to.eql(1);
expect(spy.calls[0].arguments[0].httpRequest.headers['x-amz-api-version']).to.eql('2018-09-19');
});
it('update "Host" header with newly discovered endpoint', function() {
var client = new AWS.Service({
apiConfig: new AWS.Model.Api(requiredEDOnlyApi),
});
var https = require('https');
helpers.spyOn(https, 'request').andCallThrough();
var request = client.makeRequest('requiredEDOperation', {Query: 'query', Record: 'record'});
helpers.mockHttpResponse(200, {}, '{"Endpoints": [{"Address": "https://cell1.fakeservice.amazonaws.com/fakeregion", "CachePeriodInMinutes": 1}]}');
request.send();
expect(request.httpRequest.headers.Host).to.eql('cell1.fakeservice.amazonaws.com');
});
if (AWS.util.isNode()) {
describe('not make more endpoint operation requests if there is an in-flight request', function() {
var port;
var client;
var app;
function extractOperation(request) {
if (request.headers['x-amz-target']) {
var target = request.headers['x-amz-target'];
var targetArr = target.split('.');
return targetArr[targetArr.length - 1];
}
}
var server = http.createServer(function(req, res) {
app(req, res);
});
beforeEach(function() {
port = 1024 + parseInt(Math.random() * 1024);
server.listen(port);
AWS.events.on('build', function(req) {
req.httpRequest.updateEndpoint('http://127.0.0.1:' + port);
});
client = new AWS.Service({
apiConfig: new AWS.Model.Api(requiredEDOnlyApi),
});
});
afterEach(function() {
AWS.events.removeAllListeners();
try {server.close();} catch (e) {}//fix service node running error in node 0.10
});
it('endpoint operation succeed', function(done) {
var concurrency = 10;
var requestsCounter = 0;
var endpointOperationCount = 0;
var apiOperationCount = 0;
app = function(req, res) {
var timer;
var operation = extractOperation(req);
if (operation) {
res.writeHead(200, {
'Content-Type': 'text/plain'
});
if (operation === 'DescribeEndpoints') {
endpointOperationCount++;
timer = setTimeout(function() {
res.write('{"Endpoints": [{"Address": "http://127.0.0.1:' + port + '/discovered", "CachePeriodInMinutes": 10}]}');
res.end();
}, 50);
} else {
apiOperationCount++;
expect(url.parse(req.url).path).to.eql('/discovered');
res.end('{"Operation": "' + operation + '"}');
}
} else {
res.writeHead(404, {});
res.end();
}
if (timer) {
clearTimeout(timer);
timer = null;
}
};
for (var i = 0; i < concurrency; i++) {
var request = client.makeRequest('requiredEDOperation', {Query: 'query', Record: 'record'});
request.send(function(err, data) {
requestsCounter ++;
if (requestsCounter === concurrency) {
AWS.events.removeAllListeners();
server.close();
expect(endpointOperationCount).to.eql(1);
expect(apiOperationCount).to.eql(concurrency);
done();
}
});
}
});
it('endpoint operation fail', function(done) {
var concurrency = 10;
var requestsCounter = 0;
var endpointOperationCount = 0;
var apiOperationCount = 0;
app = function(req, res) {
var timer;
var operation = extractOperation(req);
if (operation) {
if (operation === 'DescribeEndpoints') {
endpointOperationCount++;
timer = setTimeout(function() {
res.statusCode = 400;
res.write('{"__type": "EndpointUnavailable", "message": "endpoint is currently unavailable"}');
res.end();
}, 50);
} else {
apiOperationCount++;
res.statusCode = 200;
res.end('{"Operation": "' + operation + '"}');
}
} else {
res.writeHead(404, {});
res.end();
}
if (timer) {
clearTimeout(timer);
timer = null;
}
};
var failedRequestCounter = 0;
for (var i = 0; i < concurrency; i++) {
var request = client.makeRequest('requiredEDOperation', {Query: 'query', Record: 'record'});
request.send(function(err, data) {
requestsCounter ++;
if (err) failedRequestCounter ++;
expect(err.code).to.eql('EndpointUnavailable');
expect(err.message).to.eql('endpoint is currently unavailable');
if (requestsCounter === concurrency) {
AWS.events.removeAllListeners();
server.close();
expect(endpointOperationCount).to.eql(1);
expect(apiOperationCount).to.eql(0);
expect(requestsCounter).to.eql(concurrency);
expect(failedRequestCounter).to.eql(concurrency);
done();
}
});
}
});
});
}
});
describe('discoverEndpoints', function() {
beforeEach(function() {
AWS.endpointCache.empty();
});
it('accesses a single global endpoint cache for different services', function() {
var clientOptions = {
endpointDiscoveryEnabled: true,
apiConfig: new AWS.Model.Api(api)
};
var client1 = new AWS.Service(clientOptions);
var client2 = new AWS.Service(clientOptions);
var putCacheCount = 0;
var putInCacheSpy = helpers.spyOn(AWS.endpointCache, 'put').andCallFake(function() {
//when discover endpoint optionally, \'put\' method is called twice each time
if (arguments[1].length > 0 && arguments[1][0].Address !== '') putCacheCount++;
//call through origin put method
putInCacheSpy.origMethod.apply(putInCacheSpy.object, arguments);
});
helpers.mockHttpResponse(200, {}, '{"Endpoints": [{"Address": "https://cell1.fakeservice.amazonaws.com/fakeregion", "CachePeriodInMinutes": 1}]}');
client1.makeRequest('optionalEDOperation', {}).send();
expect(putCacheCount).to.eql(1);
expect(putInCacheSpy.calls.length).to.eql(2);
client1.makeRequest('requiredEDOperation', {}).send();
expect(putCacheCount).to.eql(2);
//client2 will use the same cache so it won't request new endpoints
client2.makeRequest('optionalEDOperation', {}).send();
expect(putCacheCount).to.eql(2);
client2.makeRequest('requiredEDOperation', {}).send();
expect(putCacheCount).to.eql(2);
});
it('default not run endpoint operation for optional endpoint discovery', function() {
var client = new AWS.Service({
apiConfig: new AWS.Model.Api(optionalEDOnlyApi)
});
var putInCacheSpy = helpers.spyOn(AWS.endpointCache, 'put').andCallThrough();
client.makeRequest('optionalEDOperation', {Query: 'query'}).send();
expect(putInCacheSpy.calls.length).to.eql(0);
client = new AWS.Service({
endpointDiscoveryEnabled: true,
apiConfig: new AWS.Model.Api(optionalEDOnlyApi)
});
client.makeRequest('optionalEDOperation', {Query: 'query'}).send();
expect(putInCacheSpy.calls.length).to.eql(1);
});
it('if endpoint specified for client, custom endpoint should be preferred', function() {
var client = new AWS.Service({
apiConfig: new AWS.Model.Api(api),
endpointDiscoveryEnabled: true,
endpoint: 'custom-endpoint.amazonaws.com/fack-region',
});
var error;
try {
client.makeRequest('optionalEDOperation', {Query: 'query'}).build();
} catch (e) {
error = e;
}
expect(error).not.to.eql(null);
expect(error.code).to.eql('ConfigurationException');
expect(error.message).to.eql('Custom endpoint is supplied; endpointDiscoveryEnabled must not be true.');
client = new AWS.Service({
apiConfig: new AWS.Model.Api(api),
endpoint: 'custom-endpoint.amazonaws.com/fake-region',
});
var getFromCacheSpy = helpers.spyOn(AWS.endpointCache, 'get').andCallThrough();
client.makeRequest('requiredEDOperation', {Query: 'query', Record: 'record'}).send();
expect(getFromCacheSpy.calls.length).to.eql(0);
});
it('append "endpoint-discovery" to user-agent on all requests', function() {
var client = new AWS.Service({
endpointDiscoveryEnabled: true,
apiConfig: new AWS.Model.Api(api),
});
helpers.mockHttpResponse(200, {}, '{"Endpoints": [{"Address": "https://cell1.fakeservice.amazonaws.com/fakeregion", "CachePeriodInMinutes": 1}]}');
var request = client.makeRequest('requiredEDOperation', {Query: 'query', Record: 'record'});
request.send();
if (AWS.util.isNode()) {
expect(request.httpRequest.headers['User-Agent']).include('endpoint-discovery');
} else {
expect(request.httpRequest.headers['X-Amz-User-Agent']).include('endpoint-discovery');
}
request = client.makeRequest('optionalEDOperation', {Query: 'query'});
request.send();
if (AWS.util.isNode()) {
expect(request.httpRequest.headers['User-Agent']).include('endpoint-discovery');
} else {
expect(request.httpRequest.headers['X-Amz-User-Agent']).include('endpoint-discovery');
}
});
});
describe('configurations', function() {
var env;
beforeEach(function() {
env = process.env;
process.env = {};
if (AWS.util.isBrowser()) process.env = null;
AWS.endpointCache.empty();
delete AWS.config.endpointDiscoveryEnabled;
});
afterEach(function() {
process.env = env;
});
it('will enable endpoint discovery if at least one operation requires endpoint discovery', function() {
var client = new AWS.Service({
maxRetries: 0,
apiConfig: new AWS.Model.Api(api)
});
var spy = helpers.createSpy('send inject');
AWS.events.on('sign', function(req) {
if (req.operation === 'describeEndpoints') {
spy(req);
}
});
var body = '{"Endpoints": [{"Address": "https://cell1.fakeservice.amazonaws.com/fakeregion", "CachePeriodInMinutes": 1}]}';
helpers.mockHttpResponse(200, {}, body);
client.makeRequest('requiredEDOperation', {Query: 'query', Record: 'record'}).send();
expect(spy.calls.length).to.eql(1);
client.makeRequest('optionalEDOperation', {Query: 'query', Record: 'record'}).send();
// endpoint discovery is enabled even including operations where endpoint discovery is optional.
expect(spy.calls.length).to.eql(2);
});
it('throws when endpoint discovery is required for operation but explicitly disabled from client', function() {
var client = new AWS.Service({
endpointDiscoveryEnabled: false,
maxRetries: 0,
apiConfig: new AWS.Model.Api(requiredEDOnlyApi)
});
var body = '{"Endpoints": [{"Address": "https://cell1.fakeservice.amazonaws.com/fakeregion", "CachePeriodInMinutes": 1}]}';
helpers.mockHttpResponse(200, {}, body);
var request = client.makeRequest('requiredEDOperation', {Query: 'query', Record: 'record'});
request.send(function(err, data) {
expect(err).not.to.eql(null);
expect(data).to.eql(null);
expect(err.code).to.eql('ConfigurationException');
expect(err.message).to.eql(
'Endpoint Discovery is disabled but MockService.requiredEDOperation() requires it. Please check your configurations.'
);
});
});
it('throws when endpoint discovery is required for operation but explicitly disabled from global config', function() {
AWS.config.endpointDiscoveryEnabled = false;
var client = new AWS.Service({
maxRetries: 0,
apiConfig: new AWS.Model.Api(requiredEDOnlyApi)
});
var body = '{"Endpoints": [{"Address": "https://cell1.fakeservice.amazonaws.com/fakeregion", "CachePeriodInMinutes": 1}]}';
helpers.mockHttpResponse(200, {}, body);
var request = client.makeRequest('requiredEDOperation', {Query: 'query', Record: 'record'});
request.send(function(err, data) {
expect(err).not.to.eql(null);
// expect(data).to.eql(null);
expect(err.code).to.eql('ConfigurationException');
expect(err.message).to.eql(
'Endpoint Discovery is disabled but MockService.requiredEDOperation() requires it. Please check your configurations.'
);
});
});
it('default to turnoff endpoint discovery behavior for operational endpoint discovery', function() {
client = new AWS.Service({
apiConfig: new AWS.Model.Api(optionalEDOnlyApi),
});
var spy = helpers.createSpy('send inject');
AWS.events.on('sign', function(req) {
if (req.operation === 'describeEndpoints') {
spy(req);
}
});
helpers.mockHttpResponse(200, {}, '{"Endpoints": [{"Address": "https://cell1.fakeservice.amazonaws.com/fakeregion", "CachePeriodInMinutes": 1}]}');
client.makeRequest('optionalEDOperation', {Query: 'query'}).send();
expect(spy.calls.length).to.eql(0);
});
it('turn on endpoint discovery in client config', function() {
client = new AWS.Service({
endpointDiscoveryEnabled: true,
apiConfig: new AWS.Model.Api(api),
});
var spy = helpers.createSpy('send inject');
AWS.events.on('sign', function(req) {
if (req.operation === 'describeEndpoints') {
spy(req);
}
});
helpers.mockHttpResponse(200, {}, '{"Endpoints": [{"Address": "https://cell1.fakeservice.amazonaws.com/fakeregion", "CachePeriodInMinutes": 1}]}');
client.makeRequest('optionalEDOperation', {Query: 'query'}).send();
expect(spy.calls.length).to.eql(1);
client.makeRequest('requiredEDOperation', {Query: 'query', Record: 'record'}).send();
expect(spy.calls.length).to.eql(2);
});
it('turn on endpoint discovery in global config', function() {
AWS.config.endpointDiscoveryEnabled = true;
var client = new AWS.Service({
apiConfig: new AWS.Model.Api(api),
});
var spy = helpers.createSpy('send inject');
AWS.events.on('sign', function(req) {
if (req.operation === 'describeEndpoints') {
spy(req);
}
});
helpers.mockHttpResponse(200, {}, '{"Endpoints": [{"Address": "https://cell1.fakeservice.amazonaws.com/fakeregion", "CachePeriodInMinutes": 1}]}');
client.makeRequest('optionalEDOperation', {Query: 'query'}).send();
expect(spy.calls.length).to.eql(1);
client.makeRequest('requiredEDOperation', {Query: 'query', Record: 'record'}).send();
expect(spy.calls.length).to.eql(2);
});
if (AWS.util.isNode()) {
it('turn on endpoint discovery from environmental variable AWS_ENABLE_ENDPOINT_DISCOVERY', function() {
var client = new AWS.Service({
apiConfig: new AWS.Model.Api(optionalEDOnlyApi),
});
var spy = helpers.createSpy('send inject');
AWS.events.on('sign', function(req) {
if (req.operation === 'describeEndpoints') {
spy(req);
}
});
process.env.AWS_ENABLE_ENDPOINT_DISCOVERY = '1';
helpers.mockHttpResponse(200, {}, '{"Endpoints": [{"Address": "https://cell1.fakeservice.amazonaws.com/fakeregion", "CachePeriodInMinutes": 1}]}');
client.makeRequest('optionalEDOperation', {Query: 'query'}).send();
expect(spy.calls.length).to.eql(1);
process.env.AWS_ENABLE_ENDPOINT_DISCOVERY = undefined;
request = client.makeRequest('optionalEDOperation', {Query: 'query'});
var error;
try {
request.send();
} catch (e) {
error = e;
}
expect(error).not.to.eql(undefined);
expect(error.code).to.eql('ConfigurationException');
expect(error.message).to.eql('environmental variable AWS_ENABLE_ENDPOINT_DISCOVERY cannot be set to nothing');
});
it('throws when endpoint discovery is required for operation but explicitly disabled from environmental variable', function() {
var client = new AWS.Service({
maxRetries: 0,
apiConfig: new AWS.Model.Api(requiredEDOnlyApi)
});
var body = '{"Endpoints": [{"Address": "https://cell1.fakeservice.amazonaws.com/fakeregion", "CachePeriodInMinutes": 1}]}';
helpers.mockHttpResponse(200, {}, body);
process.env.AWS_ENABLE_ENDPOINT_DISCOVERY = '0';
var request = client.makeRequest('requiredEDOperation', {Query: 'query', Record: 'record'});
request.send(function(err, data) {
expect(err).not.to.eql(null);
// expect(data).to.eql(null);
expect(err.code).to.eql('ConfigurationException');
expect(err.message).to.eql(
'Endpoint Discovery is disabled but MockService.requiredEDOperation() requires it. Please check your configurations.'
);
});
});
it('turn on endpoint discovery from environmental variable AWS_ENDPOINT_DISCOVERY_ENABLED', function() {
var client = new AWS.Service({
apiConfig: new AWS.Model.Api(optionalEDOnlyApi),
});
var spy = helpers.createSpy('send inject');
AWS.events.on('sign', function(req) {
if (req.operation === 'describeEndpoints') {
spy(req);
}
});
process.env.AWS_ENDPOINT_DISCOVERY_ENABLED = '1';
helpers.mockHttpResponse(200, {}, '{"Endpoints": [{"Address": "https://cell1.fakeservice.amazonaws.com/fakeregion", "CachePeriodInMinutes": 1}]}');
client.makeRequest('optionalEDOperation', {Query: 'query'}).send();
expect(spy.calls.length).to.eql(1);
process.env.AWS_ENDPOINT_DISCOVERY_ENABLED = undefined;
request = client.makeRequest('optionalEDOperation', {Query: 'query'});
var error;
try {
request.send();
} catch (e) {
error = e;
}
expect(error).not.to.eql(undefined);
expect(error.code).to.eql('ConfigurationException');
expect(error.message).to.eql('environmental variable AWS_ENDPOINT_DISCOVERY_ENABLED cannot be set to nothing');
});
it('turn on endpoint discovery from config file', function() {
var client = new AWS.Service({
apiConfig: new AWS.Model.Api(optionalEDOnlyApi),
});
helpers.spyOn(iniLoader, 'loadFrom').andReturn({'dummyRole': {endpoint_discovery_enabled: true}});
process.env.AWS_PROFILE = 'dummyRole';
var spy = helpers.createSpy('send inject');
AWS.events.on('sign', function(req) {
if (req.operation === 'describeEndpoints') {
spy(req);
}
});
helpers.mockHttpResponse(200, {}, '{"Endpoints": [{"Address": "https://cell1.fakeservice.amazonaws.com/fakeregion", "CachePeriodInMinutes": 1}]}');
client.makeRequest('optionalEDOperation', {Query: 'query'}).send();
expect(spy.calls.length).to.eql(1);
helpers.spyOn(iniLoader, 'loadFrom').andReturn({'dummyRole': {endpoint_discovery_enabled: undefined}});
var request = client.makeRequest('optionalEDOperation', {Query: 'query', Record: 'record'});
var error;
try {
request.send();
} catch (e) {
error = e;
}
expect(error).not.to.eql(undefined);
expect(error.code).to.eql('ConfigurationException');
expect(error.message).to.eql('config file entry \'endpoint_discovery_enabled\' cannot be set to nothing');
});
}
});
});