forked from ampproject/amphtml
/
test-url-replacements.js
1881 lines (1696 loc) · 67.1 KB
/
test-url-replacements.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Copyright 2015 The AMP HTML Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS-IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import * as trackPromise from '../../src/impression';
import {Observable} from '../../src/observable';
import {Services} from '../../src/services';
import {cidServiceForDocForTesting} from
'../../src/service/cid-impl';
import {createIframePromise} from '../../testing/iframe';
import {
extractClientIdFromGaCookie,
installUrlReplacementsServiceForDoc,
} from '../../src/service/url-replacements-impl';
import {getMode} from '../../src/mode';
import {
installActivityServiceForTesting,
} from '../../extensions/amp-analytics/0.1/activity-impl';
import {installCryptoService} from '../../src/service/crypto-impl';
import {installDocService} from '../../src/service/ampdoc-impl';
import {installDocumentInfoServiceForDoc} from
'../../src/service/document-info-impl';
import {
markElementScheduledForTesting,
resetScheduledElementForTesting,
} from '../../src/service/custom-element-registry';
import {
mockWindowInterface,
stubServiceForDoc,
} from '../../testing/test-helper';
import {parseUrlDeprecated} from '../../src/url';
import {registerServiceBuilder} from '../../src/service';
import {setCookie} from '../../src/cookies';
import {user} from '../../src/log';
describes.sandboxed('UrlReplacements', {}, () => {
let canonical;
let loadObservable;
let replacements;
let viewerService;
let userErrorStub;
let ampdoc;
beforeEach(() => {
canonical = 'https://canonical.com/doc1';
userErrorStub = sandbox.stub(user(), 'error');
});
function getReplacements(opt_options) {
return createIframePromise().then(iframe => {
ampdoc = iframe.ampdoc;
iframe.doc.title = 'Pixel Test';
const link = iframe.doc.createElement('link');
link.setAttribute('href', 'https://pinterest.com:8080/pin1');
link.setAttribute('rel', 'canonical');
iframe.doc.head.appendChild(link);
installDocumentInfoServiceForDoc(iframe.ampdoc);
resetScheduledElementForTesting(iframe.win, 'amp-analytics');
resetScheduledElementForTesting(iframe.win, 'amp-experiment');
resetScheduledElementForTesting(iframe.win, 'amp-share-tracking');
if (opt_options) {
if (opt_options.withCid) {
markElementScheduledForTesting(iframe.win, 'amp-analytics');
cidServiceForDocForTesting(iframe.ampdoc);
installCryptoService(iframe.win);
}
if (opt_options.withActivity) {
markElementScheduledForTesting(iframe.win, 'amp-analytics');
installActivityServiceForTesting(iframe.ampdoc);
}
if (opt_options.withVariant) {
markElementScheduledForTesting(iframe.win, 'amp-experiment');
registerServiceBuilder(iframe.win, 'variant', function() {
return Promise.resolve({
'x1': 'v1',
'x2': null,
});
});
}
if (opt_options.withShareTracking) {
markElementScheduledForTesting(iframe.win, 'amp-share-tracking');
registerServiceBuilder(iframe.win, 'share-tracking', function() {
return Promise.resolve({
incomingFragment: '12345',
outgoingFragment: '54321',
});
});
}
if (opt_options.withStoryVariableService) {
markElementScheduledForTesting(iframe.win, 'amp-story');
registerServiceBuilder(iframe.win, 'story-variable', function() {
return Promise.resolve({
pageIndex: 546,
pageId: 'id-123',
});
});
}
if (opt_options.withViewerIntegrationVariableService) {
markElementScheduledForTesting(iframe.win, 'amp-viewer-integration');
registerServiceBuilder(iframe.win, 'viewer-integration-variable',
function() {
return Promise.resolve(
opt_options.withViewerIntegrationVariableService);
});
}
if (opt_options.withOriginalTitle) {
iframe.doc.originalTitle = 'Original Pixel Test';
}
}
viewerService = Services.viewerForDoc(iframe.ampdoc);
replacements = Services.urlReplacementsForDoc(iframe.ampdoc);
return replacements;
});
}
function expandUrlAsync(url, opt_bindings, opt_options) {
return getReplacements(opt_options).then(
replacements => replacements.expandUrlAsync(url, opt_bindings)
);
}
function getFakeWindow() {
loadObservable = new Observable();
const win = {
addEventListener(type, callback) {
loadObservable.add(callback);
},
Object,
performance: {
timing: {
navigationStart: 100,
loadEventStart: 0,
},
},
removeEventListener(type, callback) {
loadObservable.remove(callback);
},
document: {
nodeType: /* document */ 9,
querySelector: selector => {
if (selector.startsWith('meta')) {
return {
getAttribute: () => {return 'https://whitelisted.com https://greylisted.com http://example.com';},
hasAttribute: () => {return true;},
};
} else {
return {href: canonical};
}
},
getElementById: () => {},
cookie: '',
},
Math: {
random: () => 0.1234,
},
services: {
'viewport': {obj: {}},
'cid': {
promise: Promise.resolve({
get: config => Promise.resolve('test-cid(' + config.scope + ')'),
}),
},
},
};
win.document.defaultView = win;
installDocService(win, /* isSingleDoc */ true);
const ampdoc = Services.ampdocServiceFor(win).getAmpDoc();
installDocumentInfoServiceForDoc(ampdoc);
win.ampdoc = ampdoc;
installUrlReplacementsServiceForDoc(ampdoc);
return win;
}
it('limit replacement params size', () => {
return getReplacements().then(replacements => {
replacements.getVariableSource().initialize();
const variables =
Object.keys(replacements.getVariableSource().replacements_);
// Restrict the number of replacement params to globalVaraibleSource
// Please consider adding the logic to amp-analytics instead.
// Please contact @lannka or @zhouyx if the test fail.
expect(variables.length).to.equal(69);
});
});
it('should replace RANDOM', () => {
return expandUrlAsync('ord=RANDOM?').then(res => {
expect(res).to.match(/ord=(\d+(\.\d+)?)\?$/);
});
});
it('should replace COUNTER', () => {
return expandUrlAsync(
'COUNTER(foo),COUNTER(bar),COUNTER(foo),COUNTER(bar),COUNTER(bar)')
.then(res => {
expect(res).to.equal('1,1,2,2,3');
});
});
it('should replace CANONICAL_URL', () => {
return expandUrlAsync('?href=CANONICAL_URL').then(res => {
expect(res).to.equal('?href=https%3A%2F%2Fpinterest.com%3A8080%2Fpin1');
});
});
it('should replace CANONICAL_HOST', () => {
return expandUrlAsync('?host=CANONICAL_HOST').then(res => {
expect(res).to.equal('?host=pinterest.com%3A8080');
});
});
it('should replace CANONICAL_HOSTNAME', () => {
return expandUrlAsync('?host=CANONICAL_HOSTNAME').then(res => {
expect(res).to.equal('?host=pinterest.com');
});
});
it('should replace CANONICAL_PATH', () => {
return expandUrlAsync('?path=CANONICAL_PATH').then(res => {
expect(res).to.equal('?path=%2Fpin1');
});
});
it('should replace DOCUMENT_REFERRER', () => {
return expandUrlAsync('?ref=DOCUMENT_REFERRER').then(res => {
expect(res).to.equal('?ref=http%3A%2F%2Flocalhost%3A9876%2Fcontext.html');
});
});
it('should replace EXTERNAL_REFERRER', () => {
const windowInterface = mockWindowInterface(sandbox);
windowInterface.getHostname.returns('different.org');
return getReplacements().then(replacements => {
stubServiceForDoc(sandbox, ampdoc, 'viewer', 'getReferrerUrl')
.returns(Promise.resolve('http://example.org/page.html'));
return replacements.expandUrlAsync('?ref=EXTERNAL_REFERRER');
}).then(res => {
expect(res).to.equal('?ref=http%3A%2F%2Fexample.org%2Fpage.html');
});
});
it('should replace EXTERNAL_REFERRER to empty string ' +
'if referrer is of same domain', () => {
const windowInterface = mockWindowInterface(sandbox);
windowInterface.getHostname.returns('example.org');
return getReplacements().then(replacements => {
stubServiceForDoc(sandbox, ampdoc, 'viewer', 'getReferrerUrl')
.returns(Promise.resolve('http://example.org/page.html'));
return replacements.expandUrlAsync('?ref=EXTERNAL_REFERRER');
}).then(res => {
expect(res).to.equal('?ref=');
});
});
it('should replace EXTERNAL_REFERRER to empty string ' +
'if referrer is CDN proxy of same domain', () => {
const windowInterface = mockWindowInterface(sandbox);
windowInterface.getHostname.returns('example.org');
return getReplacements().then(replacements => {
stubServiceForDoc(sandbox, ampdoc, 'viewer', 'getReferrerUrl')
.returns(Promise.resolve(
'https://example-org.cdn.ampproject.org/v/example.org/page.html'));
return replacements.expandUrlAsync('?ref=EXTERNAL_REFERRER');
}).then(res => {
expect(res).to.equal('?ref=');
});
});
it('should replace EXTERNAL_REFERRER to empty string ' +
'if referrer is CDN proxy of same domain (before CURLS)', () => {
const windowInterface = mockWindowInterface(sandbox);
windowInterface.getHostname.returns('example.org');
return getReplacements().then(replacements => {
stubServiceForDoc(sandbox, ampdoc, 'viewer', 'getReferrerUrl')
.returns(Promise.resolve(
'https://cdn.ampproject.org/v/example.org/page.html'));
return replacements.expandUrlAsync('?ref=EXTERNAL_REFERRER');
}).then(res => {
expect(res).to.equal('?ref=');
});
});
it('should replace TITLE', () => {
return expandUrlAsync('?title=TITLE').then(res => {
expect(res).to.equal('?title=Pixel%20Test');
});
});
it('should prefer original title for TITLE', () => {
return expandUrlAsync('?title=TITLE',
/*opt_bindings*/undefined, {withOriginalTitle: true}).then(res => {
expect(res).to.equal('?title=Original%20Pixel%20Test');
});
});
describe('AMPDOC_URL', () => {
it('should replace AMPDOC_URL', () => {
return expandUrlAsync('?ref=AMPDOC_URL').then(res => {
expect(res).to.not.match(/AMPDOC_URL/);
});
});
it('should add extra params to AMPDOC_URL', () => {
const win = getFakeWindow();
win.location = parseUrlDeprecated(
'https://cdn.ampproject.org/a/o.com/foo/?amp_r=hello%3Dworld');
return Services.urlReplacementsForDoc(win.ampdoc)
.expandUrlAsync('?url=AMPDOC_URL')
.then(res => {
expect(res).to.contain(
encodeURIComponent(
'https://cdn.ampproject.org/a/o.com/foo/?hello=world'));
});
});
it('should merge extra params in AMPDOC_URL', () => {
const win = getFakeWindow();
win.location = parseUrlDeprecated(
'https://cdn.ampproject.org/a/o.com/foo/?test=case&_r=hello%3Dworld');
return Services.urlReplacementsForDoc(win.ampdoc)
.expandUrlAsync('?url=AMPDOC_URL')
.then(res => {
expect(res).to.contain(
encodeURIComponent(
'https://cdn.ampproject.org/a/o.com/foo/?test=case&hello=world'));
});
});
it('should allow an embedded amp_r parameter', () => {
const win = getFakeWindow();
win.location = parseUrlDeprecated(
'https://cdn.ampproject.org/a/o.com/foo/?amp_r=amp_r%3Dweird');
return Services.urlReplacementsForDoc(win.ampdoc)
.expandUrlAsync('?url=AMPDOC_URL')
.then(res => {
expect(res).to.contain(
encodeURIComponent(
'https://cdn.ampproject.org/a/o.com/foo/?amp_r=weird'));
});
});
it('should prefer original params in AMPDOC_URL', () => {
const win = getFakeWindow();
win.location = parseUrlDeprecated(
'https://cdn.ampproject.org/a/o.com/foo/?test=case&_r=test%3Devil');
return Services.urlReplacementsForDoc(win.ampdoc)
.expandUrlAsync('?url=AMPDOC_URL')
.then(res => {
expect(res).to.contain(
encodeURIComponent(
'https://cdn.ampproject.org/a/o.com/foo/?test=case'));
});
});
it('should merge multiple extra params safely in AMPDOC_URL', () => {
const win = getFakeWindow();
win.location = parseUrlDeprecated(
'https://cdn.ampproject.org/a/o.com/foo/?test=case&a&hello=you&_r=hello%3Dworld%26goodnight%3Dmoon');
return Services.urlReplacementsForDoc(win.ampdoc)
.expandUrlAsync('?url=AMPDOC_URL')
.then(res => {
expect(res).to.contain(
encodeURIComponent(
'https://cdn.ampproject.org/a/o.com/foo/?test=case&a&hello=you&goodnight=moon'));
});
});
});
it('should replace AMPDOC_HOST', () => {
return expandUrlAsync('?ref=AMPDOC_HOST').then(res => {
expect(res).to.not.match(/AMPDOC_HOST/);
});
});
it('should replace AMPDOC_HOSTNAME', () => {
return expandUrlAsync('?ref=AMPDOC_HOSTNAME').then(res => {
expect(res).to.not.match(/AMPDOC_HOSTNAME/);
});
});
describe('SOURCE_URL', () => {
it('should replace SOURCE_URL and SOURCE_HOST', () => {
const win = getFakeWindow();
win.location = parseUrlDeprecated('https://wrong.com');
sandbox.stub(trackPromise, 'getTrackImpressionPromise').callsFake(() => {
return new Promise(resolve => {
win.location = parseUrlDeprecated('https://example.com/test');
resolve();
});
});
return Services.urlReplacementsForDoc(win.ampdoc)
.expandUrlAsync('?url=SOURCE_URL&host=SOURCE_HOST')
.then(res => {
expect(res).to.equal(
'?url=https%3A%2F%2Fexample.com%2Ftest&host=example.com');
});
});
it('should replace SOURCE_URL and SOURCE_HOSTNAME', () => {
const win = getFakeWindow();
win.location = parseUrlDeprecated('https://wrong.com');
sandbox.stub(trackPromise, 'getTrackImpressionPromise').callsFake(() => {
return new Promise(resolve => {
win.location = parseUrlDeprecated('https://example.com/test');
resolve();
});
});
return Services.urlReplacementsForDoc(win.ampdoc)
.expandUrlAsync('?url=SOURCE_URL&hostname=SOURCE_HOSTNAME')
.then(res => {
expect(res).to.equal(
'?url=https%3A%2F%2Fexample.com%2Ftest&hostname=example.com');
});
});
it('should update SOURCE_URL after track impression', () => {
const win = getFakeWindow();
win.location = parseUrlDeprecated('https://wrong.com');
sandbox.stub(trackPromise, 'getTrackImpressionPromise').callsFake(() => {
return new Promise(resolve => {
win.location = parseUrlDeprecated('https://example.com?gclid=123456');
resolve();
});
});
return Services.urlReplacementsForDoc(win.ampdoc)
.expandUrlAsync('?url=SOURCE_URL')
.then(res => {
expect(res).to.contain('example.com');
});
});
it('should add extra params to SOURCE_URL', () => {
const win = getFakeWindow();
win.location = parseUrlDeprecated(
'https://cdn.ampproject.org/a/o.com/foo/?a&_r=hello%3Dworld');
sandbox.stub(trackPromise, 'getTrackImpressionPromise').callsFake(() => {
return Promise.resolve();
});
return Services.urlReplacementsForDoc(win.ampdoc)
.expandUrlAsync('?url=SOURCE_URL')
.then(res => {
expect(res).to.equal(
'?url=' + encodeURIComponent('http://o.com/foo/?a&hello=world'));
});
});
it('should ignore extra params that already exists in SOURCE_URL', () => {
const win = getFakeWindow();
win.location = parseUrlDeprecated(
'https://cdn.ampproject.org/a/o.com/foo/?a=1&safe=1&_r=hello%3Dworld%26safe=evil');
sandbox.stub(trackPromise, 'getTrackImpressionPromise').callsFake(() => {
return Promise.resolve();
});
return Services.urlReplacementsForDoc(win.ampdoc)
.expandUrlAsync('?url=SOURCE_URL')
.then(res => {
expect(res).to.equal(
'?url=' + encodeURIComponent('http://o.com/foo/?a=1&safe=1&hello=world'));
});
});
it('should not change SOURCE_URL if is not ad landing page', () => {
const win = getFakeWindow();
win.location = parseUrlDeprecated(
'https://cdn.ampproject.org/v/o.com/foo/?a&_r=hello%3Dworld');
sandbox.stub(trackPromise, 'getTrackImpressionPromise').callsFake(() => {
return Promise.resolve();
});
return Services.urlReplacementsForDoc(win.ampdoc)
.expandUrlAsync('?url=SOURCE_URL')
.then(res => {
expect(res).to.equal(
'?url=' + encodeURIComponent('http://o.com/foo/?a'));
});
});
});
it('should replace SOURCE_PATH', () => {
return expandUrlAsync('?path=SOURCE_PATH').then(res => {
expect(res).to.not.match(/SOURCE_PATH/);
});
});
it('should replace PAGE_VIEW_ID', () => {
return expandUrlAsync('?pid=PAGE_VIEW_ID').then(res => {
expect(res).to.match(/pid=\d+/);
});
});
it('should replace CLIENT_ID', () => {
setCookie(window, 'url-abc', 'cid-for-abc');
// Make sure cookie does not exist
setCookie(window, 'url-xyz', '');
return expandUrlAsync('?a=CLIENT_ID(url-abc)&b=CLIENT_ID(url-xyz)',
/*opt_bindings*/undefined, {withCid: true}).then(res => {
expect(res).to.match(/^\?a=cid-for-abc\&b=amp-([a-zA-Z0-9_-]+){10,}/);
});
});
it('should allow empty CLIENT_ID', () => {
return getReplacements().then(replacements => {
stubServiceForDoc(sandbox, ampdoc, 'cid', 'get')
.returns(Promise.resolve());
return replacements.expandUrlAsync('?a=CLIENT_ID(_ga)');
}).then(res => {
expect(res).to.equal('?a=');
});
});
it('should replace CLIENT_ID with opt_cookieName', () => {
setCookie(window, 'url-abc', 'cid-for-abc');
// Make sure cookie does not exist
setCookie(window, 'url-xyz', '');
return expandUrlAsync(
'?a=CLIENT_ID(abc,,url-abc)&b=CLIENT_ID(xyz,,url-xyz)',
/*opt_bindings*/undefined, {withCid: true}).then(res => {
expect(res).to.match(/^\?a=cid-for-abc\&b=amp-([a-zA-Z0-9_-]+){10,}/);
});
});
it('should replace CLIENT_ID with empty string for inabox', () => {
setCookie(window, '_ga', 'GA1.2.12345.54321');
const origMode = getMode().runtime;
getMode().runtime = 'inabox';
return expandUrlAsync('?a=CLIENT_ID(url-abc)&b=CLIENT_ID(url-xyz)',
/*opt_bindings*/undefined, {withCid: true}).then(res => {
getMode().runtime = origMode;
expect(res).to.equal('?a=&b=');
});
});
it('should parse _ga cookie correctly', () => {
setCookie(window, '_ga', 'GA1.2.12345.54321');
return expandUrlAsync(
'?a=CLIENT_ID(AMP_ECID_GOOGLE,,_ga)&b=CLIENT_ID(_ga)',
/*opt_bindings*/undefined, {withCid: true}).then(res => {
expect(res).to.match(/^\?a=12345.54321&b=12345.54321/);
});
});
// TODO(alanorozco, #11827): Make this test work on Safari.
it.configure().skipSafari().run('should replace CLIENT_ID synchronously ' +
'when available', () => {
return getReplacements({withCid: true}).then(urlReplacements => {
setCookie(window, 'url-abc', 'cid-for-abc');
setCookie(window, 'url-xyz', 'cid-for-xyz');
// Only requests cid-for-xyz in async path
return urlReplacements.expandUrlAsync('b=CLIENT_ID(url-xyz)')
.then(res => {
expect(res).to.equal('b=cid-for-xyz');
}).then(() => {
const result = urlReplacements.expandUrlSync(
'?a=CLIENT_ID(url-abc)&b=CLIENT_ID(url-xyz)' +
'&c=CLIENT_ID(other)');
expect(result).to.equal('?a=&b=cid-for-xyz&c=');
});
});
});
it('should replace AMP_STATE(key)', () => {
const win = getFakeWindow();
sandbox.stub(Services, 'bindForDocOrNull').returns(Promise.resolve({
getStateValue(key) {
expect(key).to.equal('foo.bar');
return Promise.resolve('baz');
},
}));
return Services.urlReplacementsForDoc(win.ampdoc)
.expandUrlAsync('?state=AMP_STATE(foo.bar)')
.then(res => {
expect(res).to.equal('?state=baz');
});
});
// TODO(#16916): Make this test work with synchronous throws.
it.skip('should replace VARIANT', () => {
return expect(expandUrlAsync(
'?x1=VARIANT(x1)&x2=VARIANT(x2)&x3=VARIANT(x3)',
/*opt_bindings*/undefined, {withVariant: true}))
.to.eventually.equal('?x1=v1&x2=none&x3=');
});
// TODO(#16916): Make this test work with synchronous throws.
it.skip('should replace VARIANT with empty string if ' +
'amp-experiment is not configured ', () => {
return expect(expandUrlAsync(
'?x1=VARIANT(x1)&x2=VARIANT(x2)&x3=VARIANT(x3)'))
.to.eventually.equal('?x1=&x2=&x3=');
});
it('should replace VARIANTS', () => {
return expect(expandUrlAsync('?VARIANTS', /*opt_bindings*/undefined,
{withVariant: true})).to.eventually.equal('?x1.v1!x2.none');
});
// TODO(#16916): Make this test work with synchronous throws.
it.skip('should replace VARIANTS with empty string if ' +
'amp-experiment is not configured ', () => {
return expect(expandUrlAsync('?VARIANTS')).to.eventually.equal('?');
});
it('should replace SHARE_TRACKING_INCOMING and' +
' SHARE_TRACKING_OUTGOING', () => {
return expect(
expandUrlAsync(
'?in=SHARE_TRACKING_INCOMING&out=SHARE_TRACKING_OUTGOING',
/*opt_bindings*/undefined, {withShareTracking: true}))
.to.eventually.equal('?in=12345&out=54321');
});
// TODO(#16916): Make this test work with synchronous throws.
it.skip('should replace SHARE_TRACKING_INCOMING and SHARE_TRACKING_OUTGOING' +
' with empty string if amp-share-tracking is not configured', () => {
return expect(
expandUrlAsync(
'?in=SHARE_TRACKING_INCOMING&out=SHARE_TRACKING_OUTGOING'))
.to.eventually.equal('?in=&out=');
});
it('should replace STORY_PAGE_INDEX and STORY_PAGE_ID', () => {
return expect(
expandUrlAsync('?index=STORY_PAGE_INDEX&id=STORY_PAGE_ID',
/*opt_bindings*/ undefined, {withStoryVariableService: true}))
.to.eventually.equal('?index=546&id=id-123');
});
// TODO(#16916): Make this test work with synchronous throws.
it.skip('should replace STORY_PAGE_INDEX and STORY_PAGE_ID' +
' with empty string if amp-story is not configured', () => {
return expect(
expandUrlAsync('?index=STORY_PAGE_INDEX&id=STORY_PAGE_ID'))
.to.eventually.equal('?index=&id=');
});
it('should replace TIMESTAMP', () => {
return expandUrlAsync('?ts=TIMESTAMP').then(res => {
expect(res).to.match(/ts=\d+/);
});
});
it('should replace TIMESTAMP_ISO', () => {
return expandUrlAsync('?tsf=TIMESTAMP_ISO').then(res => {
expect(res).to.match(/tsf=\d+/);
});
});
it('should return correct ISO timestamp', () => {
const fakeTime = 1499979336612;
sandbox.useFakeTimers(fakeTime);
return expect(expandUrlAsync('?tsf=TIMESTAMP_ISO'))
.to.eventually.equal('?tsf=2017-07-13T20%3A55%3A36.612Z');
});
it('should replace TIMEZONE', () => {
return expandUrlAsync('?tz=TIMEZONE').then(res => {
expect(res).to.match(/tz=-?\d+/);
});
});
it('should replace TIMEZONE_CODE', () => {
return expandUrlAsync('?tz_code=TIMEZONE_CODE').then(res => {
expect(res).to.match(/tz_code=\w+|^$/);
});
});
it('should replace SCROLL_TOP', () => {
return expandUrlAsync('?scrollTop=SCROLL_TOP').then(res => {
expect(res).to.match(/scrollTop=\d+/);
});
});
it('should replace SCROLL_LEFT', () => {
return expandUrlAsync('?scrollLeft=SCROLL_LEFT').then(res => {
expect(res).to.match(/scrollLeft=\d+/);
});
});
it('should replace SCROLL_HEIGHT', () => {
return expandUrlAsync('?scrollHeight=SCROLL_HEIGHT').then(res => {
expect(res).to.match(/scrollHeight=\d+/);
});
});
it('should replace SCREEN_WIDTH', () => {
return expandUrlAsync('?sw=SCREEN_WIDTH').then(res => {
expect(res).to.match(/sw=\d+/);
});
});
it('should replace SCREEN_HEIGHT', () => {
return expandUrlAsync('?sh=SCREEN_HEIGHT').then(res => {
expect(res).to.match(/sh=\d+/);
});
});
it('should replace VIEWPORT_WIDTH', () => {
return expandUrlAsync('?vw=VIEWPORT_WIDTH').then(res => {
expect(res).to.match(/vw=\d+/);
});
});
it('should replace VIEWPORT_HEIGHT', () => {
return expandUrlAsync('?vh=VIEWPORT_HEIGHT').then(res => {
expect(res).to.match(/vh=\d+/);
});
});
it('should replace PAGE_LOAD_TIME', () => {
return expandUrlAsync('?sh=PAGE_LOAD_TIME').then(res => {
expect(res).to.match(/sh=\d+/);
});
});
it('should replace FIRST_CONTENTFUL_PAINT', () => {
const win = getFakeWindow();
sandbox.stub(Services, 'performanceFor')
.returns({
getFirstContentfulPaint() {
return 1;
},
});
return Services.urlReplacementsForDoc(win.ampdoc)
.expandUrlAsync('FIRST_CONTENTFUL_PAINT').then(res => {
expect(res).to.match(/^\d+$/);
});
});
it('should replace FIRST_VIEWPORT_READY', () => {
const win = getFakeWindow();
sandbox.stub(Services, 'performanceFor')
.returns({
getFirstViewportReady() {
return 1;
},
});
return Services.urlReplacementsForDoc(win.ampdoc)
.expandUrlAsync('FIRST_VIEWPORT_READY').then(res => {
expect(res).to.match(/^\d+$/);
});
});
it('should replace MAKE_BODY_VISIBLE', () => {
const win = getFakeWindow();
sandbox.stub(Services, 'performanceFor')
.returns({
getMakeBodyVisible() {
return 1;
},
});
return Services.urlReplacementsForDoc(win.ampdoc)
.expandUrlAsync('MAKE_BODY_VISIBLE').then(res => {
expect(res).to.match(/^\d+$/);
});
});
it('should reject protocol changes', () => {
const win = getFakeWindow();
const urlReplacements = Services.urlReplacementsForDoc(win.ampdoc);
return urlReplacements.expandUrlAsync(
'PROTOCOL://example.com/?r=RANDOM', {
'PROTOCOL': Promise.resolve('abc'),
}).then(expanded => {
expect(expanded).to.equal('PROTOCOL://example.com/?r=RANDOM');
});
});
it('Should replace BACKGROUND_STATE with 0', () => {
const win = getFakeWindow();
win.services.viewer = {
obj: {isVisible: () => true},
};
return Services.urlReplacementsForDoc(win.ampdoc)
.expandUrlAsync('?sh=BACKGROUND_STATE')
.then(res => {
expect(res).to.equal('?sh=0');
});
});
it('Should replace BACKGROUND_STATE with 1', () => {
const win = getFakeWindow();
win.services.viewer = {
obj: {isVisible: () => false},
};
return Services.urlReplacementsForDoc(win.ampdoc)
.expandUrlAsync('?sh=BACKGROUND_STATE')
.then(res => {
expect(res).to.equal('?sh=1');
});
});
it('Should replace VIDEO_STATE(video,parameter) with video data', () => {
const win = getFakeWindow();
sandbox.stub(Services, 'videoManagerForDoc').returns({
getAnalyticsDetails() {
return Promise.resolve({currentTime: 1.5});
},
});
sandbox.stub(win.document, 'getElementById')
.withArgs('video')
.returns(document.createElement('video'));
return Services.urlReplacementsForDoc(win.ampdoc)
.expandUrlAsync('?sh=VIDEO_STATE(video,currentTime)')
.then(res => {
expect(res).to.equal('?sh=1.5');
});
});
describe('PAGE_LOAD_TIME', () => {
let win;
let eventListeners;
beforeEach(() => {
win = getFakeWindow();
eventListeners = {};
win.document.readyState = 'loading';
win.document.addEventListener = function(eventType, handler) {
eventListeners[eventType] = handler;
};
win.document.removeEventListener = function(eventType, handler) {
if (eventListeners[eventType] == handler) {
delete eventListeners[eventType];
}
};
});
it('is replaced if timing info is not available', () => {
win.document.readyState = 'complete';
return Services.urlReplacementsForDoc(win.ampdoc)
.expandUrlAsync('?sh=PAGE_LOAD_TIME&s')
.then(res => {
expect(res).to.match(/sh=&s/);
});
});
it('is replaced if PAGE_LOAD_TIME is available within a delay', () => {
const urlReplacements = Services.urlReplacementsForDoc(win.ampdoc);
const validMetric =
urlReplacements.expandUrlAsync('?sh=PAGE_LOAD_TIME&s');
urlReplacements.ampdoc.win.performance.timing.loadEventStart = 109;
win.document.readyState = 'complete';
loadObservable.fire({type: 'load'});
return validMetric.then(res => {
expect(res).to.match(/sh=9&s/);
});
});
});
it('should replace NAV_REDIRECT_COUNT', () => {
return expandUrlAsync('?sh=NAV_REDIRECT_COUNT').then(res => {
expect(res).to.match(/sh=\d+/);
});
});
// TODO(cvializ, #12336): unskip
it.skip('should replace NAV_TIMING', () => {
return expandUrlAsync('?a=NAV_TIMING(navigationStart)' +
'&b=NAV_TIMING(navigationStart,responseStart)').then(res => {
expect(res).to.match(/a=\d+&b=\d+/);
});
});
it('should replace NAV_TIMING when attribute names are invalid', () => {
return expandUrlAsync('?a=NAV_TIMING(invalid)' +
'&b=NAV_TIMING(invalid,invalid)' +
'&c=NAV_TIMING(navigationStart,invalid)' +
'&d=NAV_TIMING(invalid,responseStart)').then(res => {
expect(res).to.match(/a=&b=&c=&d=/);
});
});
it('should replace NAV_TYPE', () => {
return expandUrlAsync('?sh=NAV_TYPE').then(res => {
expect(res).to.match(/sh=\d+/);
});
});
it('should replace DOMAIN_LOOKUP_TIME', () => {
return expandUrlAsync('?sh=DOMAIN_LOOKUP_TIME').then(res => {
expect(res).to.match(/sh=\d+/);
});
});
it('should replace TCP_CONNECT_TIME', () => {
return expandUrlAsync('?sh=TCP_CONNECT_TIME').then(res => {
expect(res).to.match(/sh=\d+/);
});
});
it('should replace SERVER_RESPONSE_TIME', () => {
return expandUrlAsync('?sh=SERVER_RESPONSE_TIME').then(res => {
expect(res).to.match(/sh=\d+/);
});
});
it('should replace PAGE_DOWNLOAD_TIME', () => {
return expandUrlAsync('?sh=PAGE_DOWNLOAD_TIME').then(res => {
expect(res).to.match(/sh=\d+/);
});
});
// TODO(cvializ, #12336): unskip
it.skip('should replace REDIRECT_TIME', () => {
return expandUrlAsync('?sh=REDIRECT_TIME').then(res => {
expect(res).to.match(/sh=\d+/);
});
});
it('should replace DOM_INTERACTIVE_TIME', () => {
return expandUrlAsync('?sh=DOM_INTERACTIVE_TIME').then(res => {
expect(res).to.match(/sh=\d+/);
});
});
it('should replace CONTENT_LOAD_TIME', () => {
return expandUrlAsync('?sh=CONTENT_LOAD_TIME').then(res => {
expect(res).to.match(/sh=\d+/);
});
});
it('should replace AVAILABLE_SCREEN_HEIGHT', () => {
return expandUrlAsync('?sh=AVAILABLE_SCREEN_HEIGHT').then(res => {
expect(res).to.match(/sh=\d+/);
});
});
it('should replace AVAILABLE_SCREEN_WIDTH', () => {
return expandUrlAsync('?sh=AVAILABLE_SCREEN_WIDTH').then(res => {
expect(res).to.match(/sh=\d+/);
});
});
it('should replace SCREEN_COLOR_DEPTH', () => {
return expandUrlAsync('?sh=SCREEN_COLOR_DEPTH').then(res => {
expect(res).to.match(/sh=\d+/);
});
});
it('should replace BROWSER_LANGUAGE', () => {
return expandUrlAsync('?sh=BROWSER_LANGUAGE').then(res => {
expect(res).to.match(/sh=\w+/);
});
});
it('should replace USER_AGENT', () => {
return expandUrlAsync('?sh=USER_AGENT').then(res => {
expect(res).to.match(/sh=\w+/);
});
});
it('should replace VIEWER with origin', () => {
return getReplacements().then(replacements => {
sandbox.stub(viewerService, 'getViewerOrigin').returns(
Promise.resolve('https://www.google.com'));
return replacements.expandUrlAsync('?sh=VIEWER').then(res => {
expect(res).to.equal('?sh=https%3A%2F%2Fwww.google.com');
});
});
});
it('should replace VIEWER with empty string', () => {
return getReplacements().then(replacements => {
sandbox.stub(viewerService, 'getViewerOrigin').returns(
Promise.resolve(''));
return replacements.expandUrlAsync('?sh=VIEWER').then(res => {
expect(res).to.equal('?sh=');
});
});
});
it('should replace TOTAL_ENGAGED_TIME', () => {
return expandUrlAsync('?sh=TOTAL_ENGAGED_TIME', /*opt_bindings*/undefined,
{withActivity: true}).then(res => {
expect(res).to.match(/sh=\d+/);
});
});
it('should replace INCREMENTAL_ENGAGED_TIME', () => {
return expandUrlAsync('?sh=' +
'INCREMENTAL_ENGAGED_TIME', /*opt_bindings*/undefined,
{withActivity: true}).then(res => {
expect(res).to.match(/sh=\d+/);
});
});