forked from DevExpress/testcafe-hammerhead
-
Notifications
You must be signed in to change notification settings - Fork 1
/
url-test.js
767 lines (582 loc) · 30.4 KB
/
url-test.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
var settings = hammerhead.settings;
var urlUtils = hammerhead.utils.url;
var sharedUrlUtils = hammerhead.sharedUtils.url;
var destLocation = hammerhead.utils.destLocation;
var browserUtils = hammerhead.utils.browser;
var nativeMethods = hammerhead.nativeMethods;
var nodeSandbox = hammerhead.sandbox.node;
var Promise = hammerhead.Promise;
var PROXY_PORT = 1337;
var PROXY_HOSTNAME = '127.0.0.1';
var PROXY_HOST = PROXY_HOSTNAME + ':' + PROXY_PORT;
function getProxyUrl (url, resourceType, protocol, windowId) {
return urlUtils.getProxyUrl(url, {
proxyHostname: PROXY_HOSTNAME,
proxyPort: PROXY_PORT,
sessionId: 'sessionId',
resourceType: resourceType,
proxyProtocol: protocol || 'http:',
windowId: windowId
});
}
test('getCrossDomainIframeProxyUrl (GH-749)', function () {
var destUrl = 'test.html';
var storedCrossDomainport = settings.get().crossDomainProxyPort;
settings.get().crossDomainProxyPort = '5555';
strictEqual(urlUtils.getCrossDomainIframeProxyUrl(destUrl),
'http://' + location.hostname + ':5555' + '/sessionId!i!s*example.com/https://example.com/' + destUrl);
settings.get().crossDomainProxyPort = storedCrossDomainport;
});
test('resolveUrlAsDest', function () {
strictEqual(urlUtils.resolveUrlAsDest('/index.html#hash'), 'https://example.com/index.html#hash');
strictEqual(urlUtils.resolveUrlAsDest('javascript:0;'), 'javascript:0;');
strictEqual(urlUtils.resolveUrlAsDest('/index.html?param=value#hash'), 'https://example.com/index.html?param=value#hash');
strictEqual(urlUtils.resolveUrlAsDest('https://twitter.com/index.html?param=value#hash'), 'https://twitter.com/index.html?param=value#hash');
strictEqual(urlUtils.resolveUrlAsDest('//twitter.com/index.html?param=value#hash'), 'https://twitter.com/index.html?param=value#hash');
strictEqual(urlUtils.resolveUrlAsDest('http://g.tbcdn.cn/??kissy/k/1.4.2/seed-min.js'), 'http://g.tbcdn.cn/??kissy/k/1.4.2/seed-min.js');
});
test('isSupportedProtocol', function () {
ok(urlUtils.isSupportedProtocol('http://example.org'));
ok(urlUtils.isSupportedProtocol('https://example.org'));
ok(urlUtils.isSupportedProtocol('file:///C:/index.htm'));
ok(urlUtils.isSupportedProtocol('//example.org'));
ok(urlUtils.isSupportedProtocol('/some/path'));
ok(urlUtils.isSupportedProtocol('path'));
ok(urlUtils.isSupportedProtocol('./'));
ok(urlUtils.isSupportedProtocol('../../'));
ok(urlUtils.isSupportedProtocol('?t'));
ok(!urlUtils.isSupportedProtocol('#42'));
ok(!urlUtils.isSupportedProtocol(' data:asdasdasdasdasdasd'));
ok(!urlUtils.isSupportedProtocol('chrome-extension://google.com/image.png'));
});
test('formatUrl', function () {
strictEqual(urlUtils.formatUrl({ hostname: 'localhost', partAfterHost: '/path' }), '/path');
strictEqual(urlUtils.formatUrl({ port: '1400', partAfterHost: '/path' }), '/path');
strictEqual(urlUtils.formatUrl({
hostname: 'localhost',
port: '1400',
protocol: 'http:'
}), 'http://localhost:1400');
var parsedUrl = {
hostname: 'localhost',
port: '1400',
protocol: 'http:',
auth: 'test:test'
};
strictEqual(urlUtils.formatUrl(parsedUrl), 'http://test:test@localhost:1400');
parsedUrl = {
hostname: 'localhost',
port: '1400',
protocol: 'http:',
auth: 'test',
partAfterHost: '/path'
};
strictEqual(urlUtils.formatUrl(parsedUrl), 'http://test@localhost:1400/path');
});
test('isRelativeUrl', function () {
ok(!sharedUrlUtils.isRelativeUrl('http://example.com'));
ok(sharedUrlUtils.isRelativeUrl('/test.html'));
ok(!sharedUrlUtils.isRelativeUrl('file:///C:/index.htm'));
ok(sharedUrlUtils.isRelativeUrl('C:\\index.htm'));
});
module('parse url');
test('newline characters', function () {
var url = 'http://exa\nmple.com/?par\n=val';
var parsingResult = urlUtils.parseUrl(url);
strictEqual(parsingResult.hostname, 'example.com');
strictEqual(parsingResult.partAfterHost, '/?par=val');
});
test('tabulation characters', function () {
var url = 'http://exa\tmple.com/?par\t=val';
var parsingResult = urlUtils.parseUrl(url);
strictEqual(parsingResult.hostname, 'example.com');
strictEqual(parsingResult.partAfterHost, '/?par=val');
});
test('hash after host', function () {
var url = '//test.example.com#42';
var parsingResult = urlUtils.parseUrl(url);
ok(!parsingResult.protocol);
strictEqual(parsingResult.hostname, 'test.example.com');
strictEqual(parsingResult.partAfterHost, '#42');
});
test('question mark disappears', function () {
var url = 'http://google.ru:345/path?';
var parsedUrl = urlUtils.parseUrl(url);
strictEqual(parsedUrl.partAfterHost, '/path?');
strictEqual(urlUtils.formatUrl(parsedUrl), url);
url = 'http://yandex.ru:234/path';
parsedUrl = urlUtils.parseUrl(url);
strictEqual(parsedUrl.partAfterHost, '/path');
strictEqual(urlUtils.formatUrl(parsedUrl), url);
});
test('additional slashes after scheme (GH-739)', function () {
var url = 'http://///example.com/';
var parsingResult = urlUtils.parseUrl(url);
strictEqual(parsingResult.hostname, 'example.com');
urlUtils.parseUrl('////mail.ru');
});
test('additional slashes before path', function () {
var url = '/////example.com/';
var parsingResult = urlUtils.parseUrl(url);
strictEqual(parsingResult.hostname, 'example.com');
});
test('auth', function () {
strictEqual(urlUtils.parseUrl('http://username:password@example.com').auth, 'username:password');
strictEqual(urlUtils.parseUrl('http://username@example.com').auth, 'username');
strictEqual(urlUtils.parseUrl('http://example.com').auth, void 0);
});
test('scope', function () {
strictEqual(urlUtils.getScope('http://example.com'), '/');
strictEqual(urlUtils.getScope('http://example.com/'), '/');
strictEqual(urlUtils.getScope('http://example.com/img.gif'), '/');
strictEqual(urlUtils.getScope('http://example.com/path/to/ws.js'), '/path/to/');
strictEqual(urlUtils.getScope('http://example.com/path/?z=9'), '/path/');
strictEqual(urlUtils.getScope('http://example.com/path?z=9'), '/');
strictEqual(urlUtils.getScope('/path/?z=9'), '/path/');
strictEqual(urlUtils.getScope('../path/sw.js'), '/path/');
// GH-2524
strictEqual(urlUtils.getScope('http://example.com/path/sw.js?https://some.url/another-path'), '/path/');
strictEqual(urlUtils.getScope('/path/sw.js?v=arg=/another-path'), '/path/');
strictEqual(urlUtils.getScope('/path/sw.js?'), '/path/');
});
module('get proxy url');
test('already proxied', function () {
var destUrl = 'http://test.example.com/';
var proxyUrl = getProxyUrl(destUrl);
var newUrl = getProxyUrl(proxyUrl, 'i');
strictEqual(urlUtils.parseProxyUrl(newUrl).resourceType, 'i');
});
test('destination with query, path, hash and host', function () {
var destUrl = 'http://test.example.com/pa/th/Page?param1=value¶m2=¶m3#testHash';
var proxyUrl = getProxyUrl(destUrl);
strictEqual(proxyUrl, 'http://' + PROXY_HOST + '/sessionId/' + destUrl);
});
test('destination with host only', function () {
var destUrl = 'http://test.example.com/';
var proxyUrl = getProxyUrl(destUrl);
strictEqual(proxyUrl, 'http://' + PROXY_HOST + '/sessionId/' + destUrl);
});
test('destination with https protocol', function () {
var destUrl = 'https://test.example.com:53/';
var proxyUrl = getProxyUrl(destUrl);
strictEqual(proxyUrl, 'http://' + PROXY_HOST + '/sessionId/' + destUrl);
});
test('relative path', function () {
var proxyUrl = urlUtils.getProxyUrl('/Image1.jpg');
strictEqual(proxyUrl, 'http://' + location.host + '/sessionId/https://example.com/Image1.jpg');
var parsedUrl = urlUtils.parseUrl('share?id=1kjQMWh7IcHdTBbTv6otRvCGYr-p02q206M7aR7dmog0');
ok(!parsedUrl.hostname);
ok(!parsedUrl.host);
ok(!parsedUrl.hash);
ok(!parsedUrl.port);
ok(!parsedUrl.protocol);
strictEqual(parsedUrl.partAfterHost, 'share?id=1kjQMWh7IcHdTBbTv6otRvCGYr-p02q206M7aR7dmog0');
});
if (window.navigator.platform.toLowerCase() === 'win32' && !browserUtils.isFirefox) {
test('relative file path', function () {
var destUrl = 'C:\\index.htm';
var proxyUrl = urlUtils.getProxyUrl(destUrl);
strictEqual(proxyUrl, 'http://' + location.host + '/sessionId/file:///C:/index.htm');
});
}
test('contains successive question marks in query', function () {
var destUrl = 'http://test.example.com/??dirs/???files/';
var proxyUrl = urlUtils.getProxyUrl(destUrl, {
proxyHostname: '127.0.0.1',
proxyPort: PROXY_PORT,
sessionId: 'sessionId'
});
strictEqual(proxyUrl, 'http://' + PROXY_HOST + '/sessionId/' + destUrl);
});
test('destination with port', function () {
var destUrl = 'http://test.example.com:53/';
var proxyUrl = getProxyUrl(destUrl);
strictEqual(proxyUrl, 'http://' + PROXY_HOST + '/sessionId/' + destUrl);
});
test('undefined or null', function () {
// NOTE: In Safari, a.href = null leads to the empty url, not <current_url>/null.
if (!browserUtils.isSafari)
strictEqual(getProxyUrl(null), 'http://' + PROXY_HOST + '/sessionId/https://example.com/null');
strictEqual(getProxyUrl(void 0), 'http://' + PROXY_HOST + '/sessionId/https://example.com/undefined');
});
test('remove unnecessary slashes form the begin of the url', function () {
var proxy = urlUtils.getProxyUrl('/////example.com', {
proxyHostname: 'localhost',
proxyPort: '5555',
sessionId: 'sessionId',
resourceType: 'resourceType'
});
strictEqual(proxy, 'http://localhost:5555/sessionId!resourceType/https://example.com');
});
test('should ensure triple starting slashes in a scheme-less file URLs', function () {
var storedLocation = destLocation.getLocation();
destLocation.forceLocation(urlUtils.getProxyUrl('file:///home/testcafe/site'));
var opts = { proxyHostname: 'localhost' };
strictEqual(urlUtils.getProxyUrl('/////home/testcafe/site2', opts), 'http://localhost:2000/sessionId/file:///home/testcafe/site2');
strictEqual(urlUtils.getProxyUrl('/////D:/testcafe/site2', opts), 'http://localhost:2000/sessionId/file:///D:/testcafe/site2');
strictEqual(urlUtils.getProxyUrl('//D:/testcafe/site2', opts), 'http://localhost:2000/sessionId/file:///D:/testcafe/site2');
destLocation.forceLocation(storedLocation);
});
test('convert destination host and protocol to lower case', function () {
// BUG: GH-1
var proxy = getProxyUrl('hTtp://eXamPle.Com:123/paTh/Image?Name=Value&#Hash');
ok(proxy.indexOf('http://example.com:123/paTh/Image?Name=Value&#Hash') !== -1);
});
test('unexpected trailing slash (GH-342)', function () {
var proxyUrl = getProxyUrl('http://example.com');
ok(!/\/$/.test(proxyUrl));
proxyUrl = getProxyUrl('http://example.com/');
ok(/\/$/.test(proxyUrl));
});
test('special pages (GH-339)', function () {
sharedUrlUtils.SPECIAL_PAGES.forEach(function (url) {
var proxyUrl = getProxyUrl(url);
strictEqual(proxyUrl, 'http://' + PROXY_HOST + '/sessionId/' + url);
});
});
test('convert a charset to lower case (GH-752)', function () {
var url = 'http://example.com';
var opts = {
sessionId: 'sessionId',
charset: 'UTF-8',
proxyHostname: 'localhost',
proxyPort: '5555'
};
strictEqual(sharedUrlUtils.getProxyUrl(url, opts), 'http://localhost:5555/sessionId!utf-8/' + url);
});
test('windowId', function () {
var destUrl = 'http://example.com';
var proxyUrl = getProxyUrl(destUrl, null, null, '123456789');
strictEqual(proxyUrl, 'http://' + PROXY_HOST + '/sessionId' + '*123456789/' + destUrl);
});
test('getPageProxyUrl', function () {
var sameDomainUrl = 'https://example.com/';
var crossDomainUrl = 'https://devexpress.com/';
var proxySameDomainHost = location.host;
var proxyCrossDomainHost = location.hostname + ':' + settings.get().crossDomainProxyPort;
strictEqual(urlUtils.getPageProxyUrl(sameDomainUrl, 'windowId'),
'http://' + proxySameDomainHost + '/sessionId*windowId/' + sameDomainUrl);
strictEqual(urlUtils.getPageProxyUrl(crossDomainUrl, 'windowId'),
'http://' + proxyCrossDomainHost + '/sessionId*windowId/' + crossDomainUrl);
strictEqual(urlUtils.getPageProxyUrl('http://' + proxySameDomainHost + '/sessionId*pa/' + sameDomainUrl, 'windowId'),
'http://' + proxySameDomainHost + '/sessionId*windowId/' + sameDomainUrl);
strictEqual(urlUtils.getPageProxyUrl('http://' + proxySameDomainHost + '/sessionId*pa!if/' + sameDomainUrl, 'windowId'),
'http://' + proxySameDomainHost + '/sessionId*windowId!f/' + sameDomainUrl);
strictEqual(urlUtils.getPageProxyUrl('http://' + proxyCrossDomainHost + '/sessionId*pa!i/' + sameDomainUrl, 'windowId'),
'http://' + proxySameDomainHost + '/sessionId*windowId/' + sameDomainUrl);
});
module('https proxy protocol');
test('destination with host only', function () {
var destUrl = 'http://test.example.com/';
var proxyUrl = getProxyUrl(destUrl, '', 'https:');
strictEqual(proxyUrl, 'https://' + PROXY_HOST + '/sessionId/' + destUrl);
});
test('relative path', function () {
var proxyUrl = getProxyUrl('/Image1.jpg', '', 'https:');
strictEqual(proxyUrl, 'https://' + PROXY_HOST + '/sessionId/https://example.com/Image1.jpg');
});
test('special pages', function () {
sharedUrlUtils.SPECIAL_PAGES.forEach(function (url) {
var proxyUrl = getProxyUrl(url, '', 'https:');
strictEqual(proxyUrl, 'https://' + PROXY_HOST + '/sessionId/' + url);
});
});
test('parse proxy url', function () {
var proxyUrl = 'https://' + PROXY_HOST + '/sessionId/http://test.example.com:53/PA/TH/?#testHash';
var parsingResult = urlUtils.parseProxyUrl(proxyUrl);
strictEqual(parsingResult.destUrl, 'http://test.example.com:53/PA/TH/?#testHash');
strictEqual(parsingResult.destResourceInfo.protocol, 'http:');
strictEqual(parsingResult.destResourceInfo.host, 'test.example.com:53');
strictEqual(parsingResult.destResourceInfo.hostname, 'test.example.com');
strictEqual(parsingResult.destResourceInfo.port, '53');
strictEqual(parsingResult.destResourceInfo.partAfterHost, '/PA/TH/?#testHash');
strictEqual(parsingResult.sessionId, 'sessionId');
});
module('parse proxy url');
test('http', function () {
var proxyUrl = 'http://' + PROXY_HOST + '/sessionId/http://test.example.com:53/PA/TH/?#testHash';
var parsingResult = urlUtils.parseProxyUrl(proxyUrl);
strictEqual(parsingResult.destUrl, 'http://test.example.com:53/PA/TH/?#testHash');
strictEqual(parsingResult.destResourceInfo.protocol, 'http:');
strictEqual(parsingResult.destResourceInfo.host, 'test.example.com:53');
strictEqual(parsingResult.destResourceInfo.hostname, 'test.example.com');
strictEqual(parsingResult.destResourceInfo.port, '53');
strictEqual(parsingResult.destResourceInfo.partAfterHost, '/PA/TH/?#testHash');
strictEqual(parsingResult.sessionId, 'sessionId');
});
test('https', function () {
var proxyUrl = 'http://' + PROXY_HOST + '/sessionId/https://test.example.com:53/PA/TH/?#testHash';
var parsingResult = urlUtils.parseProxyUrl(proxyUrl);
strictEqual(parsingResult.destUrl, 'https://test.example.com:53/PA/TH/?#testHash');
strictEqual(parsingResult.destResourceInfo.protocol, 'https:');
strictEqual(parsingResult.destResourceInfo.host, 'test.example.com:53');
strictEqual(parsingResult.destResourceInfo.hostname, 'test.example.com');
strictEqual(parsingResult.destResourceInfo.port, '53');
strictEqual(parsingResult.destResourceInfo.partAfterHost, '/PA/TH/?#testHash');
strictEqual(parsingResult.sessionId, 'sessionId');
});
test('non-proxy URL', function () {
var proxyUrl = 'http://' + PROXY_HOST + '/PA/TH/?someParam=value';
var destUrlInfo = urlUtils.parseProxyUrl(proxyUrl);
ok(!destUrlInfo);
});
test('successive question marks', function () {
var proxyUrl = 'http://' + PROXY_HOST +
'/sessionId/http://test.example.com:53??dirs/???files/&#testHash';
var parsingResult = urlUtils.parseProxyUrl(proxyUrl);
strictEqual(parsingResult.destUrl, 'http://test.example.com:53??dirs/???files/&#testHash');
strictEqual(parsingResult.destResourceInfo.protocol, 'http:');
strictEqual(parsingResult.destResourceInfo.host, 'test.example.com:53');
strictEqual(parsingResult.destResourceInfo.hostname, 'test.example.com');
strictEqual(parsingResult.destResourceInfo.port, '53');
strictEqual(parsingResult.destResourceInfo.partAfterHost, '??dirs/???files/&#testHash');
strictEqual(parsingResult.sessionId, 'sessionId');
});
test('single question mark', function () {
var url = 'http://ac-gb.marketgid.com/p/j/2865/11?';
var proxyUtrl = urlUtils.getProxyUrl(url, {
proxyHostname: 'hostname',
proxyPort: 1111,
sessionId: 'sessionId'
});
strictEqual(url, urlUtils.formatUrl(urlUtils.parseProxyUrl(proxyUtrl).destResourceInfo));
});
test('special pages (GH-339)', function () {
sharedUrlUtils.SPECIAL_PAGES.forEach(function (url) {
var proxyUrl = 'http://' + PROXY_HOST + '/sessionId/' + url;
var parsingResult = urlUtils.parseProxyUrl(proxyUrl);
strictEqual(parsingResult.destUrl, url);
strictEqual(parsingResult.destResourceInfo.protocol, 'about:');
strictEqual(parsingResult.destResourceInfo.host, '');
strictEqual(parsingResult.destResourceInfo.hostname, '');
strictEqual(parsingResult.destResourceInfo.port, '');
strictEqual(parsingResult.destResourceInfo.partAfterHost, '');
strictEqual(parsingResult.sessionId, 'sessionId');
});
});
test('special page with hash (GH-1671)', function () {
var specialPageUrlWithHash = 'about:error#hash';
var proxyUrl = 'http://' + PROXY_HOST + '/sessionId/' + specialPageUrlWithHash;
var parsingResult = urlUtils.parseProxyUrl(proxyUrl);
strictEqual(parsingResult.destUrl, specialPageUrlWithHash);
strictEqual(parsingResult.destResourceInfo.protocol, 'about:');
strictEqual(parsingResult.destResourceInfo.host, '');
strictEqual(parsingResult.destResourceInfo.hostname, '');
strictEqual(parsingResult.destResourceInfo.port, '');
strictEqual(parsingResult.destResourceInfo.partAfterHost, '');
strictEqual(parsingResult.sessionId, 'sessionId');
});
test('hash with whitespace (GH-971)', function () {
var url = 'http://' + PROXY_HOST + '/sessionId/http://some.domain.com/path/#word word';
var parsingResult = urlUtils.parseProxyUrl(url);
strictEqual(parsingResult.sessionId, 'sessionId');
strictEqual(parsingResult.destUrl, 'http://some.domain.com/path/#word word');
strictEqual(parsingResult.destResourceInfo.partAfterHost, '/path/#word word');
});
test('windowId', function () {
var proxyUrl = 'http://' + PROXY_HOST + '/sessionId*123456789/http://example.com';
var parsedProxyUrl = urlUtils.parseProxyUrl(proxyUrl);
strictEqual(parsedProxyUrl.destUrl, 'http://example.com');
strictEqual(parsedProxyUrl.sessionId, 'sessionId');
strictEqual(parsedProxyUrl.resourceType, null);
strictEqual(parsedProxyUrl.windowId, '123456789');
});
module('change proxy url');
test('destination URL part', function () {
var proxyUrl = 'http://localhost:1337/sessionId/http://test.example.com:53/#testHash';
var changed = urlUtils.changeDestUrlPart(proxyUrl, nativeMethods.anchorPortSetter, '34');
strictEqual(changed, 'http://localhost:1337/sessionId/http://test.example.com:34/#testHash');
changed = urlUtils.changeDestUrlPart(proxyUrl, nativeMethods.anchorHostSetter, 'newhost:99');
strictEqual(changed, 'http://localhost:1337/sessionId/http://newhost:99/#testHash');
changed = urlUtils.changeDestUrlPart(proxyUrl, nativeMethods.anchorHostnameSetter, 'newhostname');
strictEqual(changed, 'http://localhost:1337/sessionId/http://newhostname:53/#testHash');
changed = urlUtils.changeDestUrlPart(proxyUrl, nativeMethods.anchorProtocolSetter, 'https:');
strictEqual(changed, 'http://localhost:1337/sessionId/https://test.example.com:53/#testHash');
changed = urlUtils.changeDestUrlPart(proxyUrl, nativeMethods.anchorPathnameSetter, 'test1.html');
strictEqual(changed, 'http://localhost:1337/sessionId/http://test.example.com:53/test1.html#testHash');
changed = urlUtils.changeDestUrlPart(proxyUrl, nativeMethods.anchorSearchSetter, '?hl=ru&tab=wn');
strictEqual(changed, 'http://localhost:1337/sessionId/http://test.example.com:53/?hl=ru&tab=wn#testHash');
});
module('regression');
test('location.port must return the empty string (T262593)', function () {
eval(processScript([
// NOTE: From att.com, iframesrc === https://att.com:null/?IFRAME.
'var port = (document.location.port == "") ? "" : (":" + document.location.port);',
'var iframesrc = document.location.protocol + "//" + document.location.hostname + port + "/" + "?IFRAME";'
].join('\n')));
// eslint-disable-next-line no-undef
strictEqual(iframesrc, 'https://example.com/?IFRAME');
});
test('a correct proxy URL should be obtained from a destination that has a URL in its path (GH-471)', function () {
var destUrl = 'https://example.com/path/path/sdfjhsdkjf/http://example.com/image.png';
var proxyUrl = getProxyUrl(destUrl);
strictEqual(proxyUrl, 'http://' + PROXY_HOST + '/sessionId/' + destUrl);
});
module('getProxyUrl in a document with "base" tag');
test('add, update and remove the "base" tag (GH-371)', function () {
strictEqual(getProxyUrl('image.png'), 'http://' + PROXY_HOST + '/sessionId/https://example.com/image.png');
var baseEl = document.createElement('base');
strictEqual(getProxyUrl('image.png'), 'http://' + PROXY_HOST + '/sessionId/https://example.com/image.png');
baseEl.setAttribute('href', 'http://subdomain.example.com');
document.head.appendChild(baseEl);
strictEqual(getProxyUrl('image.png'), 'http://' + PROXY_HOST + '/sessionId/http://subdomain.example.com/image.png');
baseEl.setAttribute('href', 'http://example2.com');
strictEqual(getProxyUrl('image.png'), 'http://' + PROXY_HOST + '/sessionId/http://example2.com/image.png');
baseEl.removeAttribute('href');
strictEqual(getProxyUrl('image.png'), 'http://' + PROXY_HOST + '/sessionId/https://example.com/image.png');
baseEl.parentNode.removeChild(baseEl);
strictEqual(getProxyUrl('image.png'), 'http://' + PROXY_HOST + '/sessionId/https://example.com/image.png');
});
test('recreating a document with the "base" tag (GH-371)', function () {
var src = getSameDomainPageUrl('../../data/same-domain/resolving-url-after-document-recreation.html');
return createTestIframe({ src: src })
.then(function (iframe) {
var iframeDocument = iframe.contentDocument;
var anchor = iframeDocument.getElementsByTagName('a')[0];
var proxyUrl = 'http://' + location.hostname + ':' + location.port +
'/sessionId!i/http://subdomain.example.com/index.html';
strictEqual(nativeMethods.anchorHrefGetter.call(anchor), proxyUrl);
});
});
test('setting up an href attribute for a non-added to DOM "base" tag should not cause urlResolver to update. (GH-415)', function () {
var baseEl = document.createElement('base');
baseEl.setAttribute('href', 'http://subdomain.example.com');
strictEqual(getProxyUrl('image.png'), 'http://' + PROXY_HOST + '/sessionId/https://example.com/image.png');
});
test('"base" tag with an empty href attribute (GH-422)', function () {
var base = document.createElement('base');
document.head.appendChild(base);
strictEqual(getProxyUrl('image.png'), 'http://' + PROXY_HOST + '/sessionId/https://example.com/image.png');
base.setAttribute('href', '');
strictEqual(getProxyUrl('image.png'), 'http://' + PROXY_HOST + '/sessionId/https://example.com/image.png');
});
test('"base" tag with an href attribute that is set to a relative url (GH-422)', function () {
var base = document.createElement('base');
document.head.appendChild(base);
base.setAttribute('href', '/test1/test2/test3');
strictEqual(getProxyUrl('../image.png'), 'http://' + PROXY_HOST + '/sessionId/https://example.com/test1/image.png');
base.parentNode.removeChild(base);
});
test('resolving url after writing the "base" tag (GH-526)', function () {
var src = getSameDomainPageUrl('../../data/same-domain/resolving-url-after-writing-base-tag.html');
return createTestIframe({ src: src })
.then(function (iframe) {
var anchor = iframe.contentDocument.querySelector('a');
var proxyUrl = urlUtils.getProxyUrl('http://example.com/relative', {
proxyHostname: location.hostname,
proxyPort: location.port,
sessionId: 'sessionId',
resourceType: 'i'
});
strictEqual(nativeMethods.anchorHrefGetter.call(anchor), proxyUrl);
});
});
test('"base" tag with an href attribute that is set to a protocol relative url (GH-568)', function () {
var base = document.createElement('base');
document.head.appendChild(base);
base.setAttribute('href', '//test.com');
strictEqual(getProxyUrl('/image.png'), 'http://' + PROXY_HOST + '/sessionId/https://test.com/image.png');
base.parentNode.removeChild(base);
});
test('resolving a url in a tag that is written along with a "base" tag (GH-644)', function () {
var iframe = document.createElement('iframe');
iframe.id = 'test902345';
document.body.appendChild(iframe);
iframe.contentDocument.write(
'<base href="/subpath/"/>',
'<!DOCTYPE html>',
'<html>',
'<head><script src="scripts/scr.js"><' + '/script></head>',
'...',
'</html>'
);
strictEqual(nativeMethods.scriptSrcGetter.call(iframe.contentDocument.querySelector('script')),
'http://' + location.host + '/sessionId!s/https://example.com/subpath/scripts/scr.js');
document.body.removeChild(iframe);
});
test('only first base tag should be affected', function () {
var storedProcessElement = nodeSandbox._processElement;
var nativeIframe = nativeMethods.createElement.call(document, 'iframe');
var proxiedIframe = null;
nodeSandbox._processElement = function (el) {
if (el !== nativeIframe)
storedProcessElement.call(nodeSandbox, el);
};
function checkTestCase (name, fn) {
fn(nativeIframe.contentDocument);
fn(proxiedIframe.contentDocument);
var nativeAnchor = nativeIframe.contentDocument.querySelector('a');
var proxiedAnchor = proxiedIframe.contentDocument.querySelector('a');
nativeAnchor.setAttribute('href', 'path');
proxiedAnchor.setAttribute('href', 'path');
strictEqual(urlUtils.parseProxyUrl(nativeMethods.anchorHrefGetter.call(proxiedAnchor)).destUrl, nativeAnchor.href, name);
}
return createTestIframe()
.then(function (iframe) {
proxiedIframe = iframe;
return new Promise(function (resolve) {
const nativeAddEventListener = nativeMethods.documentAddEventListener || nativeMethods.addEventListener;
nativeAddEventListener.call(nativeIframe, 'load', resolve);
nativeMethods.appendChild.call(document.body, nativeIframe);
});
})
.then(function () {
checkTestCase('append first base', function (doc) {
var anchor = doc.createElement('a');
var base = doc.createElement('base');
anchor.textContent = 'link';
base.setAttribute('href', 'https://example.com/123/');
doc.head.appendChild(base);
doc.body.appendChild(anchor);
});
checkTestCase('create base', function (doc) {
doc.createElement('base');
});
checkTestCase('create base and set attribute', function (doc) {
doc.createElement('base').setAttribute('href', 'http://example.com/base/');
});
checkTestCase('append second base', function (doc) {
var base = doc.createElement('base');
base.setAttribute('href', 'https://example.com/some/');
doc.head.appendChild(base);
});
checkTestCase('change first base', function (doc) {
var base = doc.querySelectorAll('base')[0];
base.setAttribute('href', 'https://example.com/something/');
});
checkTestCase('change second base', function (doc) {
var base = doc.querySelectorAll('base')[1];
base.setAttribute('href', 'https://example.com/something/');
});
checkTestCase('remove second base', function (doc) {
var base = doc.querySelectorAll('base')[1];
doc.head.removeChild(base);
});
checkTestCase('append second base', function (doc) {
var base = doc.createElement('base');
base.setAttribute('href', 'https://example.com/some/');
doc.head.appendChild(base);
});
checkTestCase('remove first base', function (doc) {
var base = doc.querySelectorAll('base')[0];
doc.head.removeChild(base);
});
checkTestCase('append base to fragment', function (doc) {
var fragment = doc.createDocumentFragment();
var base = doc.createElement('base');
base.setAttribute('href', 'https://example.com/fragment/');
fragment.appendChild(base);
});
checkTestCase('innerHtml', function (doc) {
doc.head.innerHTML = '<base href="https://example.com/inner-html/">';
});
checkTestCase('insert first base without href', function (doc) {
var base = doc.createElement('base');
document.head.insertBefore(base, document.head.firstChild);
});
})
.then(function () {
nativeMethods.removeChild.call(document.body, nativeIframe);
nodeSandbox._processElement = storedProcessElement;
});
});