This repository has been archived by the owner on Jan 7, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathrequest_rewriting.js
984 lines (852 loc) · 29.6 KB
/
request_rewriting.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
'use strict';
require('../test_helper');
var _ = require('lodash'),
Curler = require('curler').Curler,
Factory = require('factory-lady'),
mongoose = require('mongoose'),
request = require('request');
describe('request rewriting', function() {
describe('api key stripping', function() {
shared.runServer();
it('strips the api key from the header', function(done) {
var options = {
headers: {
'X-Api-Key': this.apiKey,
}
};
request.get('http://localhost:9333/info/', options, function(error, response, body) {
response.statusCode.should.eql(200);
var data = JSON.parse(body);
should.not.exist(data.headers['x-api-key']);
done();
}.bind(this));
});
it('strips the api key from the query string', function(done) {
request.get('http://localhost:9333/info/?test=test&api_key=' + this.apiKey, function(error, response, body) {
response.statusCode.should.eql(200);
var data = JSON.parse(body);
data.url.query.should.eql({ 'test': 'test' });
done();
}.bind(this));
});
it('strips basic auth if api key was passed in as username', function(done) {
request.get('http://' + this.apiKey + ':@localhost:9333/info/', function(error, response, body) {
response.statusCode.should.eql(200);
var data = JSON.parse(body);
should.not.exist(data.headers['authorization']);
done();
}.bind(this));
});
it('does not strip basic auth if the api key is passed via other means', function(done) {
request.get('http://foo:@localhost:9333/info/?api_key=' + this.apiKey, function(error, response, body) {
response.statusCode.should.eql(200);
var data = JSON.parse(body);
should.exist(data.headers['authorization']);
done();
}.bind(this));
});
it('strips the api key from the query string even if the query string contains invalid encoded params', function(done) {
request.get('http://localhost:9333/info/?test=foo%26%20bar&url=%ED%A1%BC&api_key=' + this.apiKey, function(error, response, body) {
response.statusCode.should.eql(200);
var data = JSON.parse(body);
Object.keys(data.url.query).sort().should.eql([
'test',
'url',
]);
data.url.query.test.should.eql('foo& bar');
(new Buffer(data.url.query.url)).toString('base64').should.eql('77+9');
done();
}.bind(this));
});
});
describe('api key stripping when api keys are not required', function() {
shared.runServer({
apis: [
{
frontend_host: 'localhost',
backend_host: 'example.com',
url_matches: [
{
frontend_prefix: '/',
backend_prefix: '/',
}
],
settings: {
disable_api_key: true,
},
},
],
});
it('strips the api key from the header', function(done) {
var options = {
headers: {
'X-Api-Key': this.apiKey,
}
};
request.get('http://localhost:9333/info/', options, function(error, response, body) {
response.statusCode.should.eql(200);
var data = JSON.parse(body);
should.not.exist(data.headers['x-api-key']);
done();
}.bind(this));
});
it('strips the api key from the query string', function(done) {
request.get('http://localhost:9333/info/?test=test&api_key=' + this.apiKey, function(error, response, body) {
response.statusCode.should.eql(200);
var data = JSON.parse(body);
data.url.query.should.eql({ 'test': 'test' });
done();
}.bind(this));
});
it('strips basic auth if api key was passed in as username despite not being required', function(done) {
request.get('http://' + this.apiKey + ':@localhost:9333/info/', function(error, response, body) {
response.statusCode.should.eql(200);
var data = JSON.parse(body);
should.not.exist(data.headers['authorization']);
done();
}.bind(this));
});
// FIXME: This situation of a key being passed along in http basic auth
// even when not required currently triggers a 403 (since the gatekeeper
// assumes the username is a key which then isn't valid). I don't think
// this is the behavior we want, so need to figure out how to address this.
xit('does not strip basic auth if it contains non-api key auth', function(done) {
request.get('http://foo:@localhost:9333/info/', function(error, response, body) {
response.statusCode.should.eql(200);
var data = JSON.parse(body);
console.info(data);
should.exist(data.headers['authorization']);
done();
}.bind(this));
});
});
describe('passing api key via header', function() {
shared.runServer({
apis: [
{
frontend_host: 'localhost',
backend_host: 'example.com',
url_matches: [
{
frontend_prefix: '/',
backend_prefix: '/',
}
],
settings: {
pass_api_key_header: true,
},
},
],
});
it('keeps the api key in the header', function(done) {
var options = {
headers: {
'X-Api-Key': this.apiKey,
}
};
request.get('http://localhost:9333/info/', options, function(error, response, body) {
response.statusCode.should.eql(200);
var data = JSON.parse(body);
data.headers['x-api-key'].should.eql(this.apiKey);
done();
}.bind(this));
});
it('passes the api key in the header even if passed in via other means', function(done) {
request.get('http://localhost:9333/info/?test=test&api_key=' + this.apiKey, function(error, response, body) {
response.statusCode.should.eql(200);
var data = JSON.parse(body);
data.headers['x-api-key'].should.eql(this.apiKey);
done();
}.bind(this));
});
it('strips the api key from the query string', function(done) {
request.get('http://localhost:9333/info/?test=test&api_key=' + this.apiKey, function(error, response, body) {
response.statusCode.should.eql(200);
var data = JSON.parse(body);
data.url.query.should.eql({ 'test': 'test' });
done();
}.bind(this));
});
});
describe('passing api key via get query param', function() {
shared.runServer({
apis: [
{
frontend_host: 'localhost',
backend_host: 'example.com',
url_matches: [
{
frontend_prefix: '/',
backend_prefix: '/',
}
],
settings: {
pass_api_key_query_param: true,
},
},
],
});
it('keeps the api key in the query string', function(done) {
request.get('http://localhost:9333/info/?api_key=' + this.apiKey, function(error, response, body) {
var data = JSON.parse(body);
data.url.query.api_key.should.eql(this.apiKey);
done();
}.bind(this));
});
it('passes the api key in the query string even if passed in via other means', function(done) {
var options = {
headers: {
'X-Api-Key': this.apiKey,
}
};
request.get('http://localhost:9333/info/', options, function(error, response, body) {
var data = JSON.parse(body);
data.url.query.api_key.should.eql(this.apiKey);
done();
}.bind(this));
});
});
describe('user id header', function() {
shared.runServer();
it('passes a header containing the user id to the backend', function(done) {
request.get('http://localhost:9333/info/?api_key=' + this.apiKey, function(error, response, body) {
var data = JSON.parse(body);
data.headers['x-api-user-id'].should.eql(this.user._id);
data.headers['x-api-user-id'].length.should.eql(36);
done();
}.bind(this));
});
it('passes user\'s with object ids as hex strings', function(done) {
Factory.create('api_user', { _id: mongoose.Types.ObjectId() }, function(user) {
user._id.should.be.instanceOf(mongoose.Types.ObjectId);
request.get('http://localhost:9333/info/?api_key=' + user.api_key, function(error, response, body) {
var data = JSON.parse(body);
data.headers['x-api-user-id'].should.eql(user._id.toHexString());
data.headers['x-api-user-id'].length.should.eql(24);
done();
});
});
});
it('strips forged role headers on the incoming request', function(done) {
var options = {
headers: {
'X-Api-Key': this.apiKey,
'X-Api-User-Id': 'bogus',
}
};
request.get('http://localhost:9333/info/', options, function(error, response, body) {
var data = JSON.parse(body);
data.headers['x-api-user-id'].should.not.eql('bogus');
data.headers['x-api-user-id'].length.should.eql(36);
done();
});
});
it('strips forged role headers, case insensitvely', function(done) {
var options = {
headers: {
'X-Api-Key': this.apiKey,
'X-API-USER-ID': 'bogus',
}
};
request.get('http://localhost:9333/info/', options, function(error, response, body) {
var data = JSON.parse(body);
data.headers['x-api-user-id'].should.not.eql('bogus');
data.headers['x-api-user-id'].length.should.eql(36);
should.not.exist(data.headers['X-Api-User-Id']);
should.not.exist(data.headers['X-API-USER-ID']);
done();
});
});
});
describe('roles header', function() {
shared.runServer();
it('passes a header defining the user\'s roles to the backend', function(done) {
Factory.create('api_user', { roles: ['private'] }, function(user) {
request.get('http://localhost:9333/info/?api_key=' + user.api_key, function(error, response, body) {
var data = JSON.parse(body);
data.headers['x-api-roles'].should.eql('private');
done();
});
});
});
it('delimits multiple roles with commas', function(done) {
Factory.create('api_user', { roles: ['private', 'foo', 'bar'] }, function(user) {
request.get('http://localhost:9333/info/?api_key=' + user.api_key, function(error, response, body) {
var data = JSON.parse(body);
data.headers['x-api-roles'].should.eql('private,foo,bar');
done();
});
});
});
it('strips forged role headers on the incoming request', function(done) {
Factory.create('api_user', { roles: null }, function(user) {
var options = {
headers: {
'X-Api-Key': user.api_key,
'X-Api-Roles': 'bogus',
}
};
request.get('http://localhost:9333/info/', options, function(error, response, body) {
var data = JSON.parse(body);
should.not.exist(data.headers['x-api-roles']);
should.not.exist(data.headers['X-Api-Roles']);
done();
});
});
});
it('strips forged role headers, case insensitvely', function(done) {
Factory.create('api_user', { roles: null }, function(user) {
var options = {
headers: {
'X-Api-Key': user.api_key,
'X-API-ROLES': 'bogus',
}
};
request.get('http://localhost:9333/info/', options, function(error, response, body) {
var data = JSON.parse(body);
should.not.exist(data.headers['x-api-roles']);
should.not.exist(data.headers['X-Api-Roles']);
should.not.exist(data.headers['X-API-ROLES']);
done();
});
});
});
});
describe('host header', function() {
shared.runServer({
apis: [
{
frontend_host: 'localhost',
backend_host: null,
url_matches: [
{
frontend_prefix: '/info/none',
backend_prefix: '/info/none',
}
],
},
{
frontend_host: 'localhost',
backend_host: 'example.com:8080',
url_matches: [
{
frontend_prefix: '/info/port',
backend_prefix: '/info/port',
}
],
},
{
frontend_host: 'localhost',
backend_host: 'example.com',
url_matches: [
{
frontend_prefix: '/',
backend_prefix: '/',
}
],
},
],
});
it('sets the host header', function(done) {
request.get('http://localhost:9333/info/?api_key=' + this.apiKey, function(error, response, body) {
var data = JSON.parse(body);
data.headers.host.should.eql('example.com');
done();
}.bind(this));
});
it('includes the port number when given', function(done) {
request.get('http://localhost:9333/info/port?api_key=' + this.apiKey, function(error, response, body) {
var data = JSON.parse(body);
data.headers.host.should.eql('example.com:8080');
done();
}.bind(this));
});
it('leaves the host header untouched when a backend replacement is not present', function(done) {
request.get('http://localhost:9333/info/none?api_key=' + this.apiKey, function(error, response, body) {
var data = JSON.parse(body);
data.headers.host.should.eql('localhost:9333');
done();
}.bind(this));
});
});
describe('appending query strings', function() {
shared.runServer({
apis: [
{
frontend_host: 'localhost',
backend_host: 'example.com',
url_matches: [
{
frontend_prefix: '/',
backend_prefix: '/',
}
],
settings: {
append_query_string: 'add_param1=test1&add_param2=test2',
},
sub_settings: [
{
http_method: 'any',
regex: '^/info/sub',
settings: {
append_query_string: 'add_param2=overridden&add_param3=new',
},
},
],
},
],
});
describe('default', function() {
it('appends the query string', function(done) {
var options = {
headers: {
'X-Api-Key': this.apiKey,
},
};
request.get('http://localhost:9333/info/?test=test', options, function(error, response, body) {
var data = JSON.parse(body);
data.url.query.should.eql({
'test': 'test',
'add_param1': 'test1',
'add_param2': 'test2',
});
done();
});
});
it('overrides existing query parameters', function(done) {
var options = {
headers: {
'X-Api-Key': this.apiKey,
},
};
request.get('http://localhost:9333/info/?test=test&add_param1=original', options, function(error, response, body) {
var data = JSON.parse(body);
data.url.query.should.eql({
'test': 'test',
'add_param1': 'test1',
'add_param2': 'test2',
});
done();
});
});
});
describe('sub-url match', function() {
it('overrides the default query string settings', function(done) {
var options = {
headers: {
'X-Api-Key': this.apiKey,
},
};
request.get('http://localhost:9333/info/sub/', options, function(error, response, body) {
var data = JSON.parse(body);
data.url.query.should.eql({
'add_param2': 'overridden',
'add_param3': 'new',
});
done();
});
});
});
});
describe('setting headers', function() {
shared.runServer({
apis: [
{
frontend_host: 'localhost',
backend_host: 'example.com',
url_matches: [
{
frontend_prefix: '/',
backend_prefix: '/',
}
],
settings: {
headers: [
{ key: 'X-Add1', value: 'test1' },
{ key: 'X-Add2', value: 'test2' },
],
},
sub_settings: [
{
http_method: 'any',
regex: '^/info/sub',
settings: {
headers: [
{ key: 'X-Add2', value: 'overridden' },
],
},
},
],
},
],
});
function stripStandardHeaders(headers) {
return _.omit(headers, 'host', 'connection', 'x-api-umbrella-backend-scheme', 'x-api-umbrella-backend-id', 'x-api-key', 'x-api-user-id');
}
describe('default', function() {
it('sets header values', function(done) {
request.get('http://localhost:9333/info/?api_key=' + this.apiKey, function(error, response, body) {
var data = JSON.parse(body);
stripStandardHeaders(data.headers).should.eql({
'x-add1': 'test1',
'x-add2': 'test2',
});
done();
});
});
it('overrides existing headers (case insensitive)', function(done) {
var options = {
headers: {
'X-ADD1': 'original',
},
};
request.get('http://localhost:9333/info/?api_key=' + this.apiKey, options, function(error, response, body) {
var data = JSON.parse(body);
stripStandardHeaders(data.headers).should.eql({
'x-add1': 'test1',
'x-add2': 'test2',
});
done();
});
});
});
describe('sub-url match', function() {
it('overrides the default header settings', function(done) {
request.get('http://localhost:9333/info/sub/?api_key=' + this.apiKey, function(error, response, body) {
var data = JSON.parse(body);
stripStandardHeaders(data.headers).should.eql({
'x-add2': 'overridden',
});
done();
});
});
});
});
describe('http basic auth', function() {
shared.runServer({
apis: [
{
frontend_host: 'localhost',
backend_host: 'example.com',
url_matches: [
{
frontend_prefix: '/auth/',
backend_prefix: '/auth/',
}
],
settings: {
http_basic_auth: 'somebody:secret',
},
sub_settings: [
{
http_method: 'any',
regex: '^/auth/sub',
settings: {
http_basic_auth: 'anotheruser:anothersecret',
},
},
{
http_method: 'any',
regex: '^/auth/invalid',
settings: {
http_basic_auth: 'anotheruser:invalid',
},
},
],
},
],
});
describe('default', function() {
it('sets the http basic authentication', function(done) {
request.get('http://localhost:9333/auth/?api_key=' + this.apiKey, function(error, response, body) {
response.statusCode.should.eql(200);
body.should.eql('somebody');
done();
});
});
it('overrides existing http basic auth', function(done) {
var options = {
auth: {
user: 'testuser',
pass: 'testpass',
},
};
request.get('http://localhost:9333/auth/?api_key=' + this.apiKey, options, function(error, response, body) {
response.statusCode.should.eql(200);
body.should.eql('somebody');
done();
});
});
});
describe('sub-url match', function() {
it('overrides the default basic auth settings', function(done) {
request.get('http://localhost:9333/auth/sub/?api_key=' + this.apiKey, function(error, response, body) {
response.statusCode.should.eql(200);
body.should.eql('anotheruser');
done();
});
});
it('passes an unthorized error up if the user/pass in the settings are wrong', function(done) {
request.get('http://localhost:9333/auth/invalid/?api_key=' + this.apiKey, function(error, response, body) {
response.statusCode.should.eql(401);
body.should.eql('Unauthorized');
done();
});
});
});
});
describe('url rewriting', function() {
shared.runServer({
apis: [
{
frontend_host: 'localhost',
backend_host: 'example.com',
url_matches: [
{
frontend_prefix: '/info/prefix/',
backend_prefix: '/info/replacement/',
},
{
frontend_prefix: '/',
backend_prefix: '/',
}
],
rewrites: [
{
matcher_type: 'route',
http_method: 'any',
frontend_matcher: '/info/:category/:id.:ext?foo=:foo&bar=:bar',
backend_replacement: '/info/?category={{category}}&id={{id}}&format={{ext}}&foo={{foo}}&bar={{bar}}',
},
{
matcher_type: 'route',
http_method: 'any',
frontend_matcher: '/info/wildcard/*after',
backend_replacement: '/info/after-wildcard/{{after}}',
},
{
matcher_type: 'route',
http_method: 'any',
frontend_matcher: '/info/replacement/',
backend_replacement: '/info/second-replacement/',
},
{
matcher_type: 'regex',
http_method: 'any',
frontend_matcher: '^/info/\\?foo=bar$',
backend_replacement: '/info/?foo=moo',
},
{
matcher_type: 'regex',
http_method: 'any',
frontend_matcher: 'state=([A-Z]+)',
backend_replacement: 'region=US-$1',
},
{
matcher_type: 'route',
http_method: 'any',
frontend_matcher: '/info/route/*after?region=:region',
backend_replacement: '/info/after-route/{{after}}?region={{region}}',
},
{
matcher_type: 'regex',
http_method: 'POST',
frontend_matcher: 'post_only=before',
backend_replacement: 'post_only=after',
},
],
},
],
});
describe('route patterns', function() {
it('matches a route pattern, without regard to query string ordering', function(done) {
var options = {
headers: {
'X-Api-Key': this.apiKey,
},
};
request.get('http://localhost:9333/info/cat/10.json?bar=hello&foo=goodbye', options, function(error, response, body) {
var data = JSON.parse(body);
data.url.query.should.eql({
'category': 'cat',
'id': '10',
'format': 'json',
'foo': 'goodbye',
'bar': 'hello',
});
done();
});
});
});
describe('regular expressions', function() {
it('replaces only the matched part of the regex', function(done) {
request.get('http://localhost:9333/info/?state=CO&api_key=' + this.apiKey, function(error, response, body) {
var data = JSON.parse(body);
data.url.query.should.eql({
region: 'US-CO',
});
done();
});
});
it('replaces all instances of the regex', function(done) {
request.get('http://localhost:9333/info/state=CO/?state=CO&api_key=' + this.apiKey, function(error, response, body) {
var data = JSON.parse(body);
data.url.pathname.should.eql('/info/region=US-CO/');
data.url.query.should.eql({
region: 'US-CO',
});
done();
});
});
it('matches the regex case insensitively', function(done) {
request.get('http://localhost:9333/info/?STATE=CO&api_key=' + this.apiKey, function(error, response, body) {
var data = JSON.parse(body);
data.url.query.should.eql({
region: 'US-CO',
});
done();
});
});
});
describe('ordering', function() {
it('matches after the api key has been removed from the query string', function(done) {
request.get('http://localhost:9333/info/?api_key=' + this.apiKey + '&foo=bar', function(error, response, body) {
var data = JSON.parse(body);
data.url.path.should.eql('/info/?foo=moo');
done();
});
});
it('matches after url prefixes have been replaced', function(done) {
request.get('http://localhost:9333/info/prefix/?api_key=' + this.apiKey, function(error, response, body) {
var data = JSON.parse(body);
data.url.pathname.should.eql('/info/second-replacement/');
done();
});
});
it('chains multiple replacements in given order', function(done) {
request.get('http://localhost:9333/info/route/hello?state=CO&api_key=' + this.apiKey, function(error, response, body) {
var data = JSON.parse(body);
data.url.path.should.eql('/info/after-route/hello?region=US-CO');
done();
});
});
});
it('matches based on the http method', function(done) {
var url = 'http://localhost:9333/info/?post_only=before&api_key=' + this.apiKey;
request.get(url, function(error, response, body) {
var data = JSON.parse(body);
data.url.query.post_only.should.eql('before');
request.post(url, function(error, response, body) {
data = JSON.parse(body);
data.url.query.post_only.should.eql('after');
done();
});
});
});
});
describe('cookie stripping', function() {
shared.runServer();
it('removes the cookie completely when only a single analytics cookie is present', function(done) {
var options = {
headers: {
'Cookie': '__utma=foo'
}
};
request.get('http://localhost:9333/info/?api_key=' + this.apiKey, options, function(error, response, body) {
var data = JSON.parse(body);
should.not.exist(data.headers['cookie']);
done();
}.bind(this));
});
it('removes the cookie completely when only multiple analytics cookies are present', function(done) {
var options = {
headers: {
'Cookie': '__utma=foo; __utmz=bar; _ga=foo'
}
};
request.get('http://localhost:9333/info/?api_key=' + this.apiKey, options, function(error, response, body) {
var data = JSON.parse(body);
should.not.exist(data.headers['cookie']);
done();
}.bind(this));
});
it('removes only the analytics cookies when other cookies are present', function(done) {
var options = {
headers: {
'Cookie': '__utma=foo; moo=boo; __utmz=bar; foo=bar; _ga=foo'
}
};
request.get('http://localhost:9333/info/?api_key=' + this.apiKey, options, function(error, response, body) {
var data = JSON.parse(body);
data.headers['cookie'].should.eql('moo=boo; foo=bar');
done();
}.bind(this));
});
it('parses cookies with variable whitespace between entries', function(done) {
var options = {
headers: {
'Cookie': '__utma=foo;moo=boo; __utmz=bar; foo=bar;_ga=foo'
}
};
request.get('http://localhost:9333/info/?api_key=' + this.apiKey, options, function(error, response, body) {
var data = JSON.parse(body);
data.headers['cookie'].should.eql('moo=boo; foo=bar');
done();
}.bind(this));
});
it('leaves the cookie alone when no analytics cookies are present', function(done) {
var options = {
headers: {
'Cookie': 'foo=bar; moo=boo'
}
};
request.get('http://localhost:9333/info/?api_key=' + this.apiKey, options, function(error, response, body) {
var data = JSON.parse(body);
data.headers['cookie'].should.eql('foo=bar; moo=boo');
done();
}.bind(this));
});
});
// These tests probably belong in the router project, once we get the full
// stack more testable there (so we can actually verify Varnish works):
// https://github.com/NREL/api-umbrella/issues/28
//
// Also, this is a little tricky to test in node.js, since all OPTIONS
// requests originating from node's http library currently add the chunked
// headers: https://github.com/joyent/node/pull/7725 So we'll drop to a curl
// library to make these test requests.
describe('OPTIONS fixes', function() {
shared.runServer();
it('does not add chunked headers for requests without a body', function(done) {
var curl = new Curler();
curl.request({
method: 'OPTIONS',
url: 'http://localhost:9333/info/?test=test&api_key=' + this.apiKey,
}, function(error, response, body) {
should.not.exist(error);
response.statusCode.should.eql(200);
var data = JSON.parse(body);
should.not.exist(data.headers['transfer-encoding']);
done();
});
});
it('passes chunked headers for requests with a body', function(done) {
var curl = new Curler();
curl.request({
method: 'OPTIONS',
url: 'http://localhost:9333/info/?test=test&api_key=' + this.apiKey,
headers: {
'Transfer-Encoding': 'chunked',
'Content-Length': '4',
},
data: 'test',
}, function(error, response, body) {
should.not.exist(error);
response.statusCode.should.eql(200);
var data = JSON.parse(body);
data.headers['transfer-encoding'].should.eql('chunked');
done();
});
});
});
});