-
Notifications
You must be signed in to change notification settings - Fork 42
/
TileRequestHandler.js
executable file
·929 lines (892 loc) · 30.7 KB
/
TileRequestHandler.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
var async = require('async');
var TileServer = require('../lib/TileServer.js');
var TileRequest = require('../lib/TileRequest.js');
var TileRequestHandler = require('../lib/TileRequestHandler.js');
var assert = require('chai').assert;
describe('TileRequestHandler', function() {
describe('constructor', function() {
it('should detect invalid cacheFetchMode values', function() {
assert.throws(function() {
new TileRequestHandler({cacheFetchMode: 'awawf'});
}, /Invalid cache fetch mode/);
});
it('should default cacheFetchMode to "sequential"', function() {
var handler = new TileRequestHandler();
assert.equal(handler.cacheFetchMode, 'sequential');
});
it('should set "transforms" property', function() {
var handler = new TileRequestHandler();
assert.deepEqual(handler.transforms, []);
});
it('should set "caches" property', function() {
var handler = new TileRequestHandler();
assert.deepEqual(handler.caches, []);
});
it('should set "provider" property', function() {
var handler = new TileRequestHandler();
assert.deepEqual(handler.provider, null);
});
});
describe('use()', function() {
it('should ignore falsy values', function() {
var handler = new TileRequestHandler();
assert.equal(handler.use(null), handler);
assert.equal(handler.use(false), handler);
assert.equal(handler.use(), handler);
});
it('should allow chaining', function() {
var handler = new TileRequestHandler();
var _plugin = {serve: function() {}};
var returned = handler.use(_plugin);
assert.equal(returned, handler);
});
it('should recognize providers', function() {
var handler = new TileRequestHandler();
var _plugin = {serve: function() {}};
handler.use(_plugin);
assert.equal(handler.provider.plugin, _plugin);
});
it('should recognize caches', function() {
var handler = new TileRequestHandler();
var _plugin = {get: function() {}, set: function() {}};
handler.use(_plugin);
assert.equal(handler.caches[0].plugin, _plugin);
});
it('should recognize transforms', function() {
var handler = new TileRequestHandler();
var _plugin = {transform: function() {}};
handler.use(_plugin);
assert.equal(handler.transforms[0].plugin, _plugin);
});
it('should recognize request hooks', function() {
var handler = new TileRequestHandler();
var _plugin = {reqhook: function() {}};
handler.use(_plugin);
assert.equal(handler.requestHooks[0].plugin, _plugin);
});
it('should recognize response hooks', function() {
var handler = new TileRequestHandler();
var _plugin = {reshook: function() {}};
handler.use(_plugin);
assert.equal(handler.responseHooks[0].plugin, _plugin);
});
it('should accept arrays', function() {
var handler = new TileRequestHandler();
var _transform = {transform: function() {}};
var _provider = {serve: function() {}};
var _cache = {get: function() {}, set: function() {}};
var _reqhook = {reqhook: function() {}};
var _reshook = {reshook: function() {}};
handler.use([
[[_transform]],
_provider,
_cache,
_reqhook,
_reshook
]);
assert.equal(handler.provider.plugin, _provider);
assert.equal(handler.caches[0].plugin, _cache);
assert.equal(handler.transforms[0].plugin, _transform);
assert.equal(handler.requestHooks[0].plugin, _reqhook);
assert.equal(handler.responseHooks[0].plugin, _reshook);
});
it('should throw if unable to recognize plugin', function() {
var handler = new TileRequestHandler();
assert.throws(function() {
handler.use({});
}, /Invalid plugin/);
});
});
describe('_registerProvider()', function() {
it('should throw if passed invalid value', function() {
assert.throws(function() {
var handler = new TileRequestHandler();
handler._registerProvider(null);
}, /Falsy value passed/);
assert.throws(function() {
var handler = new TileRequestHandler();
handler._registerProvider({});
}, /Attempted to register a provider with no serve/);
});
it('should throw if provider already set', function() {
assert.throws(function() {
var handler = new TileRequestHandler();
handler._registerProvider({serve: function() {}});
handler._registerProvider({serve: function() {}});
}, /provider already registered/);
});
it('should operate normally', function() {
var handler = new TileRequestHandler();
var _provider = {serve: function() {}};
handler._registerProvider(_provider);
assert.deepEqual(handler.provider, {id: 'provider#0', plugin: _provider});
});
});
describe('_registerTransform()', function() {
it('should throw if passed invalid value', function() {
assert.throws(function() {
var handler = new TileRequestHandler();
handler._registerTransform(null);
}, /Falsy value passed/);
assert.throws(function() {
var handler = new TileRequestHandler();
handler._registerTransform({});
}, /Attempted to register a transform with no transform/);
});
it('should operate normally', function() {
var handler = new TileRequestHandler();
var _transform1 = {transform: function() {}};
var _transform2 = {transform: function() {}};
handler._registerTransform(_transform1);
handler._registerTransform(_transform2);
assert.deepEqual(handler.transforms, [
{id: 'transform#0', plugin: _transform1},
{id: 'transform#1', plugin: _transform2}
]);
});
});
describe('_registerCache()', function() {
it('should throw if passed invalid value', function() {
assert.throws(function() {
var handler = new TileRequestHandler();
handler._registerCache(null);
}, /Falsy value passed/);
assert.throws(function() {
var handler = new TileRequestHandler();
handler._registerCache({set: function() {}});
}, /Attempted to register a cache with no get/);
assert.throws(function() {
var handler = new TileRequestHandler();
handler._registerCache({get: function() {}});
}, /Attempted to register a cache with no set/);
});
it('should operate normally', function() {
var handler = new TileRequestHandler();
var _cache1 = {get: function() {}, set: function() {}};
var _cache2 = {get: function() {}, set: function() {}};
handler._registerCache(_cache1);
handler._registerCache(_cache2);
assert.deepEqual(handler.caches, [
{id: 'cache#0', plugin: _cache1},
{id: 'cache#1', plugin: _cache2}
]);
});
});
describe('_registerRequestHook()', function() {
it('should throw if passed invalid value', function() {
assert.throws(function() {
var handler = new TileRequestHandler();
handler._registerRequestHook(null);
}, /Falsy value passed/);
assert.throws(function() {
var handler = new TileRequestHandler();
handler._registerRequestHook({set: function() {}});
}, /Attempted to register a request hook with no reqhook/);
assert.throws(function() {
var handler = new TileRequestHandler();
handler._registerRequestHook({get: function() {}});
}, /Attempted to register a request hook with no reqhook/);
});
it('should operate normally', function() {
var handler = new TileRequestHandler();
var _hook1 = {reqhook: function() {}};
var _hook2 = {reqhook: function() {}};
handler._registerRequestHook(_hook1);
handler._registerRequestHook(_hook2);
assert.deepEqual(handler.requestHooks, [
{id: 'reqhook#0', plugin: _hook1},
{id: 'reqhook#1', plugin: _hook2}
]);
});
});
describe('_registerResponseHook()', function() {
it('should throw if passed invalid value', function() {
assert.throws(function() {
var handler = new TileRequestHandler();
handler._registerResponseHook(null);
}, /Falsy value passed/);
assert.throws(function() {
var handler = new TileRequestHandler();
handler._registerResponseHook({set: function() {}});
}, /Attempted to register a response hook with no reshook/);
assert.throws(function() {
var handler = new TileRequestHandler();
handler._registerResponseHook({get: function() {}});
}, /Attempted to register a response hook with no reshook/);
});
it('should operate normally', function() {
var handler = new TileRequestHandler();
var _hook1 = {reshook: function() {}};
var _hook2 = {reshook: function() {}};
handler._registerResponseHook(_hook1);
handler._registerResponseHook(_hook2);
assert.deepEqual(handler.responseHooks, [
{id: 'reshook#0', plugin: _hook1},
{id: 'reshook#1', plugin: _hook2}
]);
});
});
describe('_initialize()', function() {
it('should handle no provider / no caches gracefully', function(done) {
var server = new TileServer();
var handler = new TileRequestHandler();
handler._initialize(server, function(err) {
assert.isNull(err);
done();
});
});
it('should call init() on provider, caches, transforms, and hooks', function(done) {
var server = new TileServer();
var handler = new TileRequestHandler();
var _cache1_called = false;
var _cache2_called = false;
var _transform1_called = false;
var _transform2_called = false;
var _reqhook1_called = false;
var _reqhook2_called = false;
var _reshook1_called = false;
var _reshook2_called = false;
var _provider_called = false;
handler.use({
init: function(_server, callback) {
_cache1_called = true;
assert.equal(_server, server);
callback();
},
get: function() {},
set: function() {}
});
handler.use({
init: function(_server, callback) {
_cache2_called = true;
assert.equal(_server, server);
callback();
},
get: function() {},
set: function() {}
});
handler.use({
init: function(_server, callback) {
assert.equal(_server, server);
_provider_called = true;
callback();
},
serve: function() {}
});
handler.use({
init: function(_server, callback) {
assert.equal(_server, server);
_transform1_called = true;
callback();
},
transform: function() {}
});
handler.use({
init: function(_server, callback) {
assert.equal(_server, server);
_transform2_called = true;
callback();
},
transform: function() {}
});
handler.use({
init: function(_server, callback) {
assert.equal(_server, server);
_reqhook1_called = true;
callback();
},
reqhook: function() {}
});
handler.use({
init: function(_server, callback) {
assert.equal(_server, server);
_reqhook2_called = true;
callback();
},
reqhook: function() {}
});
handler.use({
init: function(_server, callback) {
assert.equal(_server, server);
_reshook1_called = true;
callback();
},
reshook: function() {}
});
handler.use({
init: function(_server, callback) {
assert.equal(_server, server);
_reshook2_called = true;
callback();
},
reshook: function() {}
});
handler._initialize(server, function(err) {
assert.isNull(err);
assert.isTrue(_cache1_called, 'Cache 1 initialized');
assert.isTrue(_cache2_called, 'Cache 2 initialized');
assert.isTrue(_transform1_called, 'Transform 1 initialized');
assert.isTrue(_transform2_called, 'Transform 1 initialized');
assert.isTrue(_reqhook1_called, 'Request hook 1 initialized');
assert.isTrue(_reqhook2_called, 'Request hook 1 initialized');
assert.isTrue(_reshook1_called, 'Response hook 1 initialized');
assert.isTrue(_reshook2_called, 'Response hook 1 initialized');
assert.isTrue(_provider_called, 'Provider initialized');
done();
});
});
it('should fail if the provider init fails', function(done) {
var _err = new Error('It failed');
var server = new TileServer();
var handler = new TileRequestHandler();
handler.use({init: function(server, callback) { callback(_err); }, serve: function() {}});
handler._initialize(server, function(err) {
assert.equal(err, _err);
done();
});
});
it('should fail if any of the cache inits fail', function(done) {
var _err = new Error('It failed');
var server = new TileServer();
var handler = new TileRequestHandler();
handler.use({init: function(server, callback) { callback(_err); }, get: function() {}, set: function() {}});
handler._initialize(server, function(err) {
assert.equal(err, _err);
done();
});
});
});
describe('GET()', function() {
it('it should hit caches first', function(done) {
var mockServer = new TileServer();
var mockRequest = TileRequest.parse('/layer/1/2/3/tile.png');
var handler = new TileRequestHandler();
var _cache1_called = false;
var _cache2_called = false;
handler.use({
get: function(server, req, callback) {
_cache1_called = true;
assert.equal(server, mockServer);
assert.equal(req, mockRequest);
assert.isFalse(_cache2_called, 'Cache 2 shouldn\'t have been called yet');
callback();
},
set: function() {}
});
handler.use({
get: function(server, req, callback) {
_cache2_called = true;
assert.equal(server, mockServer);
assert.isTrue(_cache1_called, 'Cache 1 should have been called first');
callback(null, Buffer.from('success', 'utf8'), {'X-Test-Status': 'success'});
},
set: function() {}
});
handler.use({
serve: function(server, req, callback) {
throw new Error('Shouldn\'t have been called');
}
});
handler.GET(mockServer, mockRequest, function(status, buffer, headers) {
assert.isTrue(_cache1_called, 'Cache 1 should have been called');
assert.isTrue(_cache2_called, 'Cache 2 should have been called');
assert.equal(status, 200);
assert.equal(buffer.toString('utf8'), 'success');
assert.deepEqual(headers, {'X-Test-Status': 'success', 'X-TileStrata-CacheHit': '1'});
done();
});
});
it('should ignore any cache errors and fallback to provider', function(done) {
var mockServer = new TileServer();
var mockRequest = TileRequest.parse('/layer/1/2/3/tile.png');
var handler = new TileRequestHandler();
var _cache_called = false;
handler.use({
get: function(server, req, callback) {
_cache_called = true;
callback(new Error('Cache failure'));
},
set: function() {}
});
handler.use({
serve: function(server, req, callback) {
callback(null, Buffer.from('success', 'utf8'), {'X-Test-Status': 'success'});
}
});
handler.GET(mockServer, mockRequest, function(status, buffer, headers) {
assert.isTrue(_cache_called);
assert.equal(status, 200);
assert.equal(buffer.toString('utf8'), 'success');
assert.deepEqual(headers, {'X-Test-Status': 'success'});
done();
});
});
it('should return statusCode attached to provider error if available', function(done) {
var mockServer = new TileServer();
var mockRequest = TileRequest.parse('/layer/1/2/3/tile.png');
var handler = new TileRequestHandler();
handler.use({
serve: function(server, req, callback) {
var err = new Error('My error');
err.statusCode = 999;
callback(err);
}
});
handler.GET(mockServer, mockRequest, function(status, buffer, headers) {
assert.equal(status, 999);
done();
});
});
it('should skip transforms if provider fails', function(done) {
var mockServer = new TileServer();
var mockRequest = TileRequest.parse('/layer/1/2/3/tile.png');
var handler = new TileRequestHandler();
handler.use({
serve: function(server, req, callback) {
callback(new Error('Provider failed'));
}
});
handler.use({
transform: function(server, req, buffer, headers, callback) {
throw new Error('Should not be called');
}
});
handler.use({
transform: function(server, req, buffer, headers, callback) {
throw new Error('Should not be called');
}
});
handler.GET(mockServer, mockRequest, function(status, buffer, headers) {
assert.equal(status, 500);
assert.equal(buffer.toString('utf8'), 'Provider failed');
assert.deepEqual(headers, {});
setImmediate(done);
});
});
it('should return 500 status if transform fails (and skip cache set)', function(done) {
var mockServer = new TileServer();
var mockRequest = TileRequest.parse('/layer/1/2/3/tile.png');
var handler = new TileRequestHandler();
handler.use({
get: function(server, req, callback) {
callback();
},
set: function() {
throw new Error('Should not be called');
}
});
handler.use({
serve: function(server, req, callback) {
callback(null, Buffer.from('success', 'utf8'), {'X-Test-Status': 'success'});
}
});
handler.use({
transform: function(server, req, buffer, headers, callback) {
callback(new Error('Something went wrong w/transform'))
}
});
handler.use({
transform: function(server, req, buffer, headers, callback) {
throw new Error('Should not be called');
}
});
handler.GET(mockServer, mockRequest, function(status, buffer, headers) {
assert.equal(status, 500);
assert.equal(buffer.toString('utf8'), 'Something went wrong w/transform');
assert.deepEqual(headers, {});
setImmediate(done);
});
});
it('should execute transforms after provider, and then cache transform result', function(done) {
var mockServer = new TileServer();
var mockRequest = TileRequest.parse('/layer/1/2/3/tile.png');
var handler = new TileRequestHandler();
var _cache_called = false;
handler.use({
get: function(server, req, callback) {
_cache_called = true;
callback(new Error('Cache failure'));
},
set: function(server, req, buffer, headers, callback) {
assert.equal(buffer.toString('utf8'), 'transform2');
assert.deepEqual(headers, {'X-Transform': '2'});
done();
}
});
handler.use({
serve: function(server, req, callback) {
callback(null, Buffer.from('success', 'utf8'), {'X-Test-Status': 'success'});
}
});
handler.use({
transform: function(server, req, buffer, headers, callback) {
assert.equal(server, mockServer);
assert.equal(req, mockRequest);
assert.instanceOf(buffer, Buffer);
assert.equal(buffer.toString('utf8'), 'success');
assert.deepEqual(headers, {'X-Test-Status': 'success'});
callback(null, Buffer.from('transform1', 'utf8'), {'X-Transform': '1'});
}
});
handler.use({
transform: function(server, req, buffer, headers, callback) {
assert.equal(server, mockServer);
assert.equal(req, mockRequest);
assert.instanceOf(buffer, Buffer);
assert.equal(buffer.toString('utf8'), 'transform1');
assert.deepEqual(headers, {'X-Transform': '1'});
callback(null, Buffer.from('transform2', 'utf8'), {'X-Transform': '2'});
}
});
handler.GET(mockServer, mockRequest, function(status, buffer, headers) {
assert.isTrue(_cache_called);
assert.equal(status, 200);
assert.equal(buffer.toString('utf8'), 'transform2');
assert.deepEqual(headers, {'X-Transform': '2'});
});
});
it('should skip caching and return 500 status when provider fails', function(done) {
var mockServer = new TileServer();
var mockRequest = TileRequest.parse('/layer/1/2/3/tile.png');
var handler = new TileRequestHandler();
handler.use({
get: function(server, req, callback) { callback(); },
set: function() { throw new Error('Should not have been called') }
});
handler.use({
serve: function(server, req, callback) {
callback(new Error('Something went wrong'))
}
});
handler.GET(mockServer, mockRequest, function(status, buffer, headers) {
assert.equal(status, 500);
assert.equal(buffer.toString('utf8'), 'Something went wrong');
assert.deepEqual(headers, {});
setImmediate(done);
});
});
it('should should acknowledge "race" cacheFetchMode', function(done) {
var mockServer = new TileServer();
var mockRequest = TileRequest.parse('/layer/1/2/3/tile.png');
var handler = new TileRequestHandler({cacheFetchMode: 'race'});
var _cache1_called = false;
var _cache1_finished = false;
var _cache2_finished = false;
handler.use({
get: function(server, req, callback) {
_cache1_called = true;
// never call back
},
set: function() {}
});
handler.use({
get: function(server, req, callback) {
assert.isTrue(_cache1_called);
callback(null, Buffer.from('success', 'utf8'), {'X-Test-Status': 'success'});
},
set: function() {}
});
handler.use({
serve: function(server, req, callback) {
throw new Error('Shouldn\'t have been called');
}
});
handler.GET(mockServer, mockRequest, function(status, buffer, headers) {
assert.isTrue(_cache1_called, 'Cache 1 should have been called');
assert.equal(status, 200);
assert.equal(buffer.toString('utf8'), 'success');
assert.deepEqual(headers, {'X-Test-Status': 'success', 'X-TileStrata-CacheHit': '1'});
done();
});
});
it('should refresh tile in background if any of the caches return true for "refresh" arg on callback', function(done) {
var _responded = false;
var _cache_set_calls = 0;
var mockServer = new TileServer();
var mockRequest = TileRequest.parse('/layer/1/2/3/tile.png');
var handler = new TileRequestHandler();
var assertCacheSet = function(server, req, buffer, headers, callback) {
_cache_set_calls++;
assert.isTrue(_responded, 'Set should happen after response sent');
assert.equal(buffer.toString('utf8'), 'thedata');
assert.deepEqual(headers, {'X-Step': 'provider'});
callback();
if (_cache_set_calls === 2) {
done();
}
};
handler.use({
get: function(server, req, callback) { callback(); },
set: assertCacheSet
});
handler.use({
get: function(server, req, callback) {
var cacheData = Buffer.from('cachedata', 'utf8');
var cacheHeaders = {'X-Step': 'cache'};
callback(null, cacheData, cacheHeaders, true);
},
set: assertCacheSet
});
handler.use({
serve: function(server, req, callback) {
var _buffer = Buffer.from('thedata', 'utf8');
callback(null, _buffer, {'X-Step': 'provider'});
}
});
handler.GET(mockServer, mockRequest, function(status, buffer, headers) {
_responded = true;
assert.equal(status, 200);
assert.equal(buffer.toString('utf8'), 'cachedata');
assert.deepEqual(headers, {'X-Step': 'cache', 'X-TileStrata-CacheHit': '1'});
});
});
it('should attempt to cache after successful provider result', function(done) {
var mockServer = new TileServer();
var mockRequest = TileRequest.parse('/layer/1/2/3/tile.png');
var handler = new TileRequestHandler();
var _cache_set_calls = 0;
var cacheSet = function(server, req, buffer, headers, callback) {
_cache_set_calls++;
assert.equal(server, mockServer);
assert.equal(req, mockRequest);
assert.equal(buffer.toString('utf8'), 'success');
assert.deepEqual(headers, {'X-Test-Status': 'success'});
callback();
if (_cache_set_calls === 2) {
setImmediate(done);
}
};
handler.use({
get: function(server, req, callback) { callback(); },
set: cacheSet
});
handler.use({
get: function(server, req, callback) { callback(); },
set: cacheSet
});
handler.use({
serve: function(server, req, callback) {
var _buffer = Buffer.from('success', 'utf8');
var _headers = {'X-Test-Status': 'success'};
callback(null, _buffer, _headers);
}
});
handler.GET(mockServer, mockRequest, function(status, buffer, headers) {
assert.equal(_cache_set_calls, 0);
assert.equal(status, 200);
assert.equal(buffer.toString('utf8'), 'success');
assert.deepEqual(headers, {'X-Test-Status': 'success'});
});
});
it('should handle no caches fine', function(done) {
var mockServer = new TileServer();
var mockRequest = TileRequest.parse('/layer/1/2/3/tile.png');
var handler = new TileRequestHandler();
handler.use({
serve: function(server, req, callback) {
assert.equal(server, mockServer);
assert.equal(req, mockRequest);
var _buffer = Buffer.from('success', 'utf8');
var _headers = {'X-Test-Status': 'success'};
callback(null, _buffer, _headers);
}
});
handler.GET(mockServer, mockRequest, function(status, buffer, headers) {
assert.equal(status, 200);
assert.equal(buffer.toString('utf8'), 'success');
assert.deepEqual(headers, {'X-Test-Status': 'success'});
setImmediate(done);
});
});
it('should return 404 status when no provider configured', function(done) {
var mockServer = new TileServer();
var mockRequest = TileRequest.parse('/layer/1/2/3/tile.png');
var handler = new TileRequestHandler();
handler.GET(mockServer, mockRequest, function(status, buffer, headers) {
assert.equal(status, 404);
assert.equal(buffer.toString('utf8'), 'No provider configured for layer');
assert.deepEqual(headers, {});
setImmediate(done);
});
});
it('should batch identical requests while busy fetching', function(done) {
var mockServer = new TileServer();
var req1 = TileRequest.parse('/layer/1/2/3/tile.png');
var req2 = TileRequest.parse('/layer/1/2/3/tile.png');
var _calls_provider = 0;
var _responses = 0;
var handler = new TileRequestHandler();
handler.use({
serve: function(server, req, callback) {
_calls_provider++;
setImmediate(function() {
var _buffer = Buffer.from('success', 'utf8');
var _headers = {'X-Test-Status': 'success'};
callback(null, _buffer, _headers);
});
}
});
function handleResponse(status, buffer, headers) {
_responses++;
assert.equal(status, 200);
assert.equal(buffer.toString('utf8'), 'success');
assert.deepEqual(headers, {'X-Test-Status': 'success'});
if (_responses === 2) {
assert.equal(_calls_provider, 1)
setImmediate(done);
}
}
handler.GET(mockServer, req1, handleResponse);
handler.GET(mockServer, req2, handleResponse);
});
it('should should wait for cache to be set before returning if "X-TileStrata-CacheWait" header present', function(done) {
var mockServer = new TileServer();
var mockRequest = TileRequest.parse('/layer/1/2/3/tile.png', {'x-tilestrata-cachewait':'1'});
var handler = new TileRequestHandler();
var cache_set = false;
handler.use({
get: function(server, req, callback) {
callback();
},
set: function(server, req, buffer, headers, callback) {
setTimeout(function() {
cache_set = true;
callback();
}, 200);
}
});
handler.use({
serve: function(server, req, callback) {
var _buffer = Buffer.from('success', 'utf8');
var _headers = {'X-Test-Status': 'success'};
callback(null, _buffer, _headers);
}
});
handler.GET(mockServer, mockRequest, function(status, buffer, headers) {
assert.equal(status, 200);
assert.equal(buffer.toString('utf8'), 'success');
assert.deepEqual(headers, {'X-Test-Status': 'success'});
assert.isTrue(cache_set);
done();
});
});
it('should skip cache get if "X-TileStrata-SkipCache" header present and matches (wildcard)', function(done) {
var mockServer = new TileServer();
var mockRequest = TileRequest.parse('/layer/1/2/3/tile.png', {'x-tilestrata-skipcache': '*'});
var handler = new TileRequestHandler();
handler.use({
get: function(server, req, callback) {
throw new Error('Cache get should not be executed');
},
set: function(server, req, buffer, headers, callback) {
done();
}
});
handler.use({
serve: function(server, req, callback) {
var _buffer = Buffer.from('success', 'utf8');
var _headers = {'X-Test-Status': 'success'};
callback(null, _buffer, _headers);
}
});
handler.GET(mockServer, mockRequest, function(status, buffer, headers) {
assert.equal(status, 200);
assert.equal(buffer.toString('utf8'), 'success');
assert.deepEqual(headers, {'X-Test-Status': 'success'});
});
});
it('should skip cache get if "X-TileStrata-SkipCache" header present and matches', function(done) {
var mockServer = new TileServer();
var mockRequest = TileRequest.parse('/layer/1/2/3/tile.png', {'x-tilestrata-skipcache': 'a/tile.png,layer/tile.png', 'x-tilestrata-cachewait': true});
var handler = new TileRequestHandler();
var cacheSet = false;
handler.use({
get: function(server, req, callback) {
throw new Error('Cache get should not be executed');
},
set: function(server, req, buffer, headers, callback) {
cacheSet = true;
setTimeout(callback, 20);
}
});
handler.use({
serve: function(server, req, callback) {
var _buffer = Buffer.from('success', 'utf8');
var _headers = {'X-Test-Status': 'success'};
callback(null, _buffer, _headers);
}
});
handler.GET(mockServer, mockRequest, function(status, buffer, headers) {
assert.equal(status, 200);
assert.equal(buffer.toString('utf8'), 'success');
assert.deepEqual(headers, {'X-Test-Status': 'success'});
if (!cacheSet) throw new Error('Cache set should have been called!');
done();
});
});
it('should not batch requests with different "X-TileStrata-SkipCache" headers', function(done) {
var mockServer = new TileServer();
var handler = new TileRequestHandler();
var _calls = 0;
handler.use({
serve: function(server, req, callback) {
++_calls;
setTimeout(function() {
var _buffer = Buffer.from('success', 'utf8');
callback(null, _buffer, {});
}, 10);
}
});
async.parallel([
function a(callback) {
var mockRequestA = TileRequest.parse('/layer/1/2/3/tile.png', {'x-tilestrata-skipcache': '*'});
handler.GET(mockServer, mockRequestA, function(status, buffer) {
assert.equal(buffer.toString('utf8'), 'success');
callback();
});
},
function b(callback) {
var mockRequestB = TileRequest.parse('/layer/1/2/3/tile.png', {});
handler.GET(mockServer, mockRequestB, function(status,buffer) {
assert.equal(buffer.toString('utf8'), 'success');
callback();
});
}
], function() {
assert.equal(_calls, 2, 'calls to provider');
done();
});
});
it('should not skip cache get if "X-TileStrata-SkipCache" header present and does not match', function(done) {
var mockServer = new TileServer();
var mockRequest = TileRequest.parse('/layer/1/2/3/tile.png', {'x-tilestrata-skipcache': 'layer/tile.pnggg'});
var handler = new TileRequestHandler();
var _cache_get_calls = 0;
var _cache_set_calls = 0;
handler.use({
get: function(server, req, callback) {
_cache_get_calls++;
callback();
},
set: function(server, req, buffer, headers, callback) {
_cache_set_calls++;
callback();
setImmediate(done);
}
});
handler.use({
serve: function(server, req, callback) {
var _buffer = Buffer.from('success', 'utf8');
var _headers = {'X-Test-Status': 'success'};
callback(null, _buffer, _headers);
}
});
handler.GET(mockServer, mockRequest, function(status, buffer, headers) {
assert.equal(_cache_get_calls, 1);
assert.equal(status, 200);
assert.equal(buffer.toString('utf8'), 'success');
assert.deepEqual(headers, {'X-Test-Status': 'success'});
});
});
});
});