-
Notifications
You must be signed in to change notification settings - Fork 476
/
test.js
2060 lines (1747 loc) · 78.1 KB
/
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
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
(function() {
'use strict';
/*global window, document, location, URI, URI_pre_lib, IPv6, IPv6_pre_lib, URITemplate, URITemplate_pre_lib, SecondLevelDomains, SecondLevelDomains_pre_lib, urls, test, ok, equal, strictEqual, deepEqual, raises */
// FIXME: v2.0.0 renamce non-camelCase properties to uppercase
/*jshint camelcase: false, loopfunc: true */
test('loaded', function() {
ok(window.URI);
});
module('constructing');
test('URI()', function() {
var u = URI();
ok(u instanceof URI, 'instanceof URI');
equal(u.toString(), window.location && window.location.href || '', 'is location (browser) or empty string (node)');
});
test('URI(undefined)', function() {
raises(function() {
URI(undefined);
}, TypeError, 'Failing undefined input');
});
test('URI(null)', function() {
raises(function() {
URI(null);
}, TypeError, 'Failing undefined input');
});
test('new URI(string)', function() {
var u = new URI('http://example.org/');
ok(u instanceof URI, 'instanceof URI');
ok(u._parts.hostname !== undefined, 'host undefined');
});
test('new URI(object)', function() {
var u = new URI({protocol: 'http', hostname: 'example.org'});
ok(u instanceof URI, 'instanceof URI');
ok(u._parts.hostname !== undefined, 'host undefined');
});
test('new URI(object)', function() {
var u = new URI({
protocol: 'http',
hostname: 'example.org',
query: {
foo: 'bar',
bar: 'foo',
},
});
ok(u instanceof URI, 'instanceof URI');
ok(typeof u.query() === 'string', 'query is string');
equal(u.query(), 'foo=bar&bar=foo', 'query has right value');
equal(u.search(), '?foo=bar&bar=foo', 'search has right value');
deepEqual(u.query(true), { foo: 'bar', bar: 'foo' }, 'query(true) value');
deepEqual(u.search(true), { foo: 'bar', bar: 'foo' }, 'search(true) value');
});
test('new URI(object)', function() {
var u = new URI({
protocol: 'http',
hostname: 'example.org',
query: 'foo=bar&bar=foo',
});
ok(u instanceof URI, 'instanceof URI');
ok(typeof u.query() === 'string', 'query is string');
equal(u.query(), 'foo=bar&bar=foo', 'query has right value');
equal(u.search(), '?foo=bar&bar=foo', 'search has right value');
deepEqual(u.query(true), { foo: 'bar', bar: 'foo' }, 'query(true) value');
deepEqual(u.search(true), { foo: 'bar', bar: 'foo' }, 'search(true) value');
});
test('new URI(object)', function() {
var u = new URI({
protocol: 'http',
hostname: 'example.org',
query: '?foo=bar&bar=foo',
});
ok(u instanceof URI, 'instanceof URI');
ok(typeof u.query() === 'string', 'query is string');
equal(u.query(), 'foo=bar&bar=foo', 'query has right value');
equal(u.search(), '?foo=bar&bar=foo', 'search has right value');
deepEqual(u.query(true), { foo: 'bar', bar: 'foo' }, 'query(true) value');
deepEqual(u.search(true), { foo: 'bar', bar: 'foo' }, 'search(true) value');
});
test('new URI(Location)', function () {
var u = new URI(location);
equal(u.href(), String(location.href), 'location object');
});
test('new URI(undefined)', function() {
var u = new URI();
ok(u instanceof URI, 'instanceof URI');
equal(u.toString(), window.location && window.location.href || '', 'is location (browser) or empty string (node)');
raises(function() {
new URI(undefined);
}, TypeError, 'Failing undefined input');
});
(function() {
var element;
function testDomAttribute(element, attribute) {
test('new URI(Element ' + element.nodeName + ')', function() {
element[attribute] = 'http://example.org/foobar.html';
var u = new URI(element);
equal(u.scheme(), 'http', 'scheme');
equal(u.host(), 'example.org', 'host');
equal(u.path(), '/foobar.html', 'path');
element[attribute] = 'file:///C:/foo/bar.html';
u = new URI(element);
equal(u.href(), element[attribute], 'file');
});
}
function testUnsupportedDomAttribute(element, attribute) {
test('new URI(unsupported Element ' + element.nodeName + ')', function() {
element[attribute] = 'http://example.org/foobar.html';
var u = new URI(element);
equal(u.scheme(), '', 'scheme');
equal(u.host(), '', 'host');
equal(u.path(), '', 'path');
element[attribute] = 'file:///C:/foo/bar.html';
u = new URI(element);
equal(u.href(), '', 'file');
});
}
for (var nodeName in URI.domAttributes) {
if (!Object.prototype.hasOwnProperty.call(URI.domAttributes, nodeName) || nodeName === 'input') {
continue;
}
element = document.createElement(nodeName);
testDomAttribute(element, URI.domAttributes[nodeName]);
}
element = document.createElement('input');
element.type = 'image';
testDomAttribute(element, 'src');
element = document.createElement('input');
testUnsupportedDomAttribute(element, 'src');
element = document.createElement('div');
testUnsupportedDomAttribute(element, 'src');
})();
test('new URI(URI)', function() {
var u = new URI(new URI({protocol: 'http', hostname: 'example.org'}));
ok(u instanceof URI, 'instanceof URI');
ok(u._parts.hostname !== undefined, 'host undefined');
});
test('new URI(new Date())', function() {
raises(function() {
new URI(new Date());
}, TypeError, 'Failing unknown input');
});
test('new URI(undefined)', function() {
raises(function() {
new URI(undefined);
}, TypeError, 'Failing undefined input');
});
test('new URI()', function() {
var u = new URI();
ok(u instanceof URI, 'instanceof URI');
ok(u._parts.hostname === location.hostname || u._parts.hostname === null && location.hostname === '',
'hostname == location.hostname');
});
test('function URI(string)', function() {
var u = new URI('http://example.org/');
ok(u instanceof URI, 'instanceof URI');
ok(u._parts.hostname !== undefined, 'host undefined');
});
test('function URI(string) with invalid port "port" throws', function () {
raises(function () {
new URI('http://example.org:port');
}, TypeError, "throws TypeError");
});
test('function URI(string) with invalid port "0" throws', function () {
raises(function () {
new URI('http://example.org:0');
}, TypeError, "throws TypeError");
});
test('function URI(string) with invalid port "65536" throws', function () {
raises(function () {
new URI('http://example.org:65536');
}, TypeError, "throws TypeError");
});
test('function URI(string) with protocol and without hostname should throw', function () {
new URI('http://');
URI.preventInvalidHostname = true;
raises(function () {
new URI('http://');
}, TypeError, "throws TypeError");
URI.preventInvalidHostname = false;
new URI('http://');
});
test('new URI(string, string)', function() {
// see http://dvcs.w3.org/hg/url/raw-file/tip/Overview.html#constructor
var u = new URI('../foobar.html', 'http://example.org/hello/world.html');
equal(u+'', 'http://example.org/foobar.html', 'resolve on construct');
});
module('parsing');
// [].forEach() no IE, lacking interest in polyfilling this...
for (var i = 0, t; (t = urls[i]); i++) {
(function(t){
test('parse ' + t.name, function() {
var u = new URI(t.url),
key;
// test URL built from parts
equal(u + '', t._url || t.url, 'toString');
// test parsed parts
for (key in t.parts) {
if (Object.hasOwnProperty.call(t.parts, key)) {
equal(u._parts[key], t.parts[key], 'part: ' + key);
}
}
// test accessors
for (key in t.accessors) {
if (Object.hasOwnProperty.call(t.accessors, key)) {
equal(u[key](), t.accessors[key], 'accessor: ' + key);
}
}
// test is()
for (key in t.is) {
if (Object.hasOwnProperty.call(t.is, key)) {
equal(u.is(key), t.is[key], 'is: ' + key);
}
}
});
})(t);
}
module('serializing');
test('scheme and relative path', function() {
var u = new URI('')
.protocol('food')
.path('test/file.csv')
.toString()
equal(u.toString(), 'food:///test/file.csv', 'relative-path with scheme but no authority');
});
module('mutating basics');
test('protocol', function() {
var u = new URI('http://example.org/foo.html');
u.protocol('ftp');
equal(u.protocol(), 'ftp', 'ftp protocol');
equal(u+'', 'ftp://example.org/foo.html', 'ftp url');
u.protocol('');
equal(u.protocol(), '', 'relative protocol');
equal(u+'', '//example.org/foo.html', 'relative-scheme url');
u.protocol('f.t-p+0');
equal(u.protocol(), 'f.t-p+0', 'character profile');
try {
u.protocol('f:t');
ok(false, 'do not accept invalid protocol');
} catch(e) {}
u.protocol(null);
equal(u.protocol(), '', 'missing protocol');
equal(u+'', '//example.org/foo.html', 'missing-scheme url');
});
test('username', function() {
var u = new URI('http://example.org/foo.html');
u.username('hello');
equal(u.username(), 'hello', 'changed username hello');
equal(u.password(), '', 'changed passowrd hello');
equal(u+'', 'http://hello@example.org/foo.html', 'changed url hello');
u.username('');
equal(u.username(), '', 'changed username ""');
equal(u.password(), '', 'changed passowrd ""');
equal(u+'', 'http://example.org/foo.html', 'changed url ""');
});
test('password', function() {
var u = new URI('http://hello@example.org/foo.html');
u.password('world');
equal(u.username(), 'hello', 'changed username world');
equal(u.password(), 'world', 'changed passowrd world');
equal(u+'', 'http://hello:world@example.org/foo.html', 'changed url world');
u.password('');
equal(u.username(), 'hello', 'changed username ""');
equal(u.password(), '', 'changed passowrd ""');
equal(u+'', 'http://hello@example.org/foo.html', 'changed url ""');
u.username('').password('hahaha');
equal(u.username(), '', 'changed username - password without username');
equal(u.password(), 'hahaha', 'changed password - password without username');
equal(u+'', 'http://:hahaha@example.org/foo.html', 'changed url - password without username');
});
test('hostname', function() {
var u = new URI('http://example.org/foo.html');
u.hostname('abc.foobar.lala');
equal(u.hostname(), 'abc.foobar.lala', 'hostname changed');
equal(u+'', 'http://abc.foobar.lala/foo.html', 'hostname changed url');
u.hostname('some_where.exa_mple.org');
equal(u.hostname(), 'some_where.exa_mple.org', 'hostname changed');
equal(u+'', 'http://some_where.exa_mple.org/foo.html', 'hostname changed url');
raises(function() {
u.hostname('foo\\bar.com');
}, TypeError, 'Failing backslash detection in hostname');
// instance does not fall back to global setting
URI.preventInvalidHostname = true;
u.hostname('');
u.hostname(null);
URI.preventInvalidHostname = false;
u.preventInvalidHostname(true);
raises(function() {
u.hostname('');
}, TypeError, "Trying to set an empty hostname with http(s) protocol throws a TypeError");
raises(function() {
u.hostname(null);
}, TypeError, "Trying to set hostname to null with http(s) protocol throws a TypeError");
});
test('port', function() {
var u = new URI('http://example.org/foo.html');
u.port('80');
equal(u.port(), '80', 'changing port 80');
equal(u+'', 'http://example.org:80/foo.html', 'changing url 80');
u.port('');
equal(u.port(), '', 'changing port ""');
equal(u+'', 'http://example.org/foo.html', 'changing url ""');
});
test('path', function() {
var u = new URI('http://example.org/foobar.html?query=string');
u.pathname('/some/path/file.suffix');
equal(u.pathname(), '/some/path/file.suffix', 'changing pathname "/some/path/file.suffix"');
equal(u+'', 'http://example.org/some/path/file.suffix?query=string', 'changing url "/some/path/file.suffix"');
u.pathname('');
equal(u.pathname(), '/', 'changing pathname ""');
equal(u+'', 'http://example.org/?query=string', 'changing url ""');
u.pathname('/~userhome/@mine;is %2F and/');
equal(u.pathname(), '/~userhome/@mine;is%20%2F%20and/', 'path encoding');
equal(u.pathname(true), '/~userhome/@mine;is %2F and/', 'path decoded');
u = new URI('/a/b/c/').relativeTo('/a/b/c/');
equal(u.pathname(), '', 'empty relative path');
equal(u.toString(), '', 'empty relative path to string');
u.pathname('/');
equal(u.pathname(), '/', 'empty absolute path');
equal(u.toString(), '/', 'empty absolute path to string');
});
test('URN paths', function() {
var u = new URI('urn:uuid:6e8bc430-9c3a-11d9-9669-0800200c9a66?foo=bar');
u.pathname('uuid:de305d54-75b4-431b-adb2-eb6b9e546013');
equal(u.pathname(), 'uuid:de305d54-75b4-431b-adb2-eb6b9e546013');
equal(u + '', 'urn:uuid:de305d54-75b4-431b-adb2-eb6b9e546013?foo=bar');
u.pathname('');
equal(u.pathname(), '', 'changing pathname ""');
equal(u+'', 'urn:?foo=bar', 'changing url ""');
u.pathname('music:classical:Béla Bártok%3a Concerto for Orchestra');
equal(u.pathname(), 'music:classical:B%C3%A9la%20B%C3%A1rtok%3A%20Concerto%20for%20Orchestra', 'path encoding');
equal(u.pathname(true), 'music:classical:Béla Bártok%3A Concerto for Orchestra', 'path decoded');
});
test('query', function() {
var u = new URI('http://example.org/foo.html');
u.query('foo=bar=foo');
equal(u.query(), 'foo=bar=foo', 'query: foo=bar=foo');
equal(u.search(), '?foo=bar=foo', 'query: foo=bar=foo - search');
u.query('?bar=foo');
equal(u.query(), 'bar=foo', 'query: ?bar=foo');
equal(u.search(), '?bar=foo', 'query: ?bar=foo - search');
u.query('');
equal(u.query(), '', 'query: ""');
equal(u.search(), '', 'query: "" - search');
equal(u.toString(), 'http://example.org/foo.html');
u.search('foo=bar=foo');
equal(u.query(), 'foo=bar=foo', 'search: foo=bar=foo');
equal(u.search(), '?foo=bar=foo', 'search: foo=bar=foo - query');
u.search('?bar=foo');
equal(u.query(), 'bar=foo', 'search: ?bar=foo');
equal(u.search(), '?bar=foo', 'search: ?bar=foo - query');
u.search('');
equal(u.query(), '', 'search: ""');
equal(u.search(), '', 'search: "" - query');
u.query('?foo');
equal(u.query(), 'foo', 'search: ""');
equal(u.search(), '?foo', 'search: "" - query');
u.search('foo=&foo=bar');
equal(u.query(), 'foo=&foo=bar', 'search: foo=&foo=bar');
equal(JSON.stringify(u.query(true)), JSON.stringify({foo: ['', 'bar']}), 'parsed query: {foo:["", "bar"]}');
u.search('foo=bar&foo=');
equal(u.query(), 'foo=bar&foo=', 'search: foo=bar&foo=');
equal(JSON.stringify(u.query(true)), JSON.stringify({foo: ['bar', '']}), 'parsed query: {foo:["bar", ""]}');
u.search('foo=bar&foo');
equal(u.query(), 'foo=bar&foo', 'search: foo=bar&foo');
equal(JSON.stringify(u.query(true)), JSON.stringify({foo: ['bar', null]}), 'parsed query: {foo:["bar", null]}');
u.search('foo&foo=bar');
equal(u.query(), 'foo&foo=bar', 'search: foo&foo=bar');
equal(JSON.stringify(u.query(true)), JSON.stringify({foo: [null, 'bar']}), 'parsed query: {foo:[null, "bar"]}');
u.search('__proto__=hasOwnProperty&__proto__=eviltwin&uuid');
equal(u.query(), '__proto__=hasOwnProperty&__proto__=eviltwin&uuid', 'search: __proto__=hasOwnProperty&__proto__=eviltwin&uuid');
equal(JSON.stringify(u.query(true)), '{"uuid":null}', 'parsed query: {uuid: null}');
// parsing empty query
var t;
t = u.query('?').query(true);
t = u.query('').query(true);
t = u.href('http://example.org').query(true);
});
test('fragment', function() {
var u = new URI('http://example.org/foo.html');
u.fragment('foo');
equal(u.fragment(), 'foo', 'fragment: foo');
equal(u.hash(), '#foo', 'fragment: foo - hash');
u.fragment('#bar');
equal(u.fragment(), 'bar', 'fragment: #bar');
equal(u.hash(), '#bar', 'fragment: #bar - hash');
u.fragment('');
equal(u.fragment(), '', 'fragment: ""');
equal(u.hash(), '', 'fragment: "" - hash');
equal(u.toString(), 'http://example.org/foo.html');
u.hash('foo');
equal(u.fragment(), 'foo', 'hash: foo');
equal(u.hash(), '#foo', 'hash: foo - fragment');
u.hash('#bar');
equal(u.fragment(), 'bar', 'hash: #bar');
equal(u.hash(), '#bar', 'hash: #bar - fragment');
u.hash('');
equal(u.fragment(), '', 'hash: ""');
equal(u.hash(), '', 'hash: "" - fragment');
});
module('mutating compounds');
test('host', function() {
var u = new URI('http://foo.bar/foo.html');
u.host('example.org:80');
equal(u.hostname(), 'example.org', 'host changed hostname');
equal(u.port(), '80', 'host changed port');
equal(u+'', 'http://example.org:80/foo.html', 'host changed url');
u.host('some-domain.com');
equal(u.hostname(), 'some-domain.com', 'host modified hostname');
equal(u.port(), '', 'host removed port');
equal(u+'', 'http://some-domain.com/foo.html', 'host modified url');
u.host('some_where.exa_mple.org:44');
equal(u.hostname(), 'some_where.exa_mple.org', 'host modified hostname #2');
equal(u.port(), '44', 'port restored');
equal(u+'', 'http://some_where.exa_mple.org:44/foo.html', 'host modified url #2');
raises(function() {
u.host('foo\\bar.com');
}, TypeError, 'Failing backslash detection in host');
});
test('origin', function () {
var u = new URI('http://foo.bar/foo.html');
equal(u.origin(), 'http://foo.bar', 'invalid origin');
u.origin('http://bar.foo/bar.html');
equal(u.origin(), 'http://bar.foo', 'origin didnt change');
equal(u+'', 'http://bar.foo/foo.html', 'origin path changed');
});
test('authority', function() {
var u = new URI('http://foo.bar/foo.html');
u.authority('username:password@example.org:80');
equal(u.username(), 'username', 'authority changed username');
equal(u.password(), 'password', 'authority changed password');
equal(u.hostname(), 'example.org', 'authority changed hostname');
equal(u.port(), '80', 'authority changed port');
equal(u+'', 'http://username:password@example.org:80/foo.html', 'authority changed url');
u.authority('some-domain.com');
equal(u.username(), '', 'authority removed username');
equal(u.password(), '', 'authority removed password');
equal(u.hostname(), 'some-domain.com', 'authority modified hostname');
equal(u.port(), '', 'authority removed port');
equal(u+'', 'http://some-domain.com/foo.html', 'authority modified url');
raises(function() {
u.authority('username:password@foo\\bar.com:80');
}, TypeError, 'Failing backslash detection in authority');
});
test('userinfo', function() {
var u = new URI('http://foo.bar/foo.html');
u.userinfo('username:password');
equal(u.username(), 'username', 'userinfo changed username-only');
equal(u.password(), 'password', 'userinfo changed password');
equal(u+'', 'http://username:password@foo.bar/foo.html', 'userinfo changed url');
u.userinfo('walter');
equal(u.username(), 'walter', 'userinfo removed password');
equal(u.password(), '', 'userinfo removed password');
equal(u+'', 'http://walter@foo.bar/foo.html', 'userinfo changed url');
u.userinfo('');
equal(u.username(), '', 'userinfo removed username');
equal(u.password(), '', 'userinfo removed password');
equal(u+'', 'http://foo.bar/foo.html', 'userinfo changed url');
});
test('href', function() {
var u = new URI('http://foo.bar/foo.html');
u.href('ftp://u:p@example.org:123/directory/file.suffix?query=string#fragment');
equal(u.protocol(), 'ftp', 'href changed protocol');
equal(u.username(), 'u', 'href changed username');
equal(u.password(), 'p', 'href changed password');
equal(u.hostname(), 'example.org', 'href changed hostname');
equal(u.port(), '123', 'href changed port');
equal(u.pathname(), '/directory/file.suffix', 'href changed pathname');
equal(u.search(), '?query=string', 'href changed search');
equal(u.hash(), '#fragment', 'href changed hash');
equal(u.href(), 'ftp://u:p@example.org:123/directory/file.suffix?query=string#fragment', 'href removed url');
u.href('../path/index.html');
equal(u.protocol(), '', 'href removed protocol');
equal(u.username(), '', 'href removed username');
equal(u.password(), '', 'href removed password');
equal(u.hostname(), '', 'href removed hostname');
equal(u.port(), '', 'href removed port');
equal(u.pathname(), '../path/index.html', 'href removed pathname');
equal(u.search(), '', 'href removed search');
equal(u.hash(), '', 'href removed hash');
equal(u.href(), '../path/index.html', 'href removed url');
/*jshint -W053 */
u.href(new String('/narf'));
/*jshint +W053 */
equal(u.pathname(), '/narf', 'href from String instance');
});
test('resource', function() {
var u = new URI('http://foo.bar/foo.html?hello#world');
equal(u.resource(), '/foo.html?hello#world', 'get resource');
u.resource('/foo.html?hello#world');
equal(u.href(), 'http://foo.bar/foo.html?hello#world', 'set resource');
u.resource('/world.html');
equal(u.href(), 'http://foo.bar/world.html', 'set resource path');
equal(u.resource(), '/world.html', 'get resource path');
u.resource('?query');
equal(u.href(), 'http://foo.bar/?query', 'set resource query');
equal(u.resource(), '/?query', 'get resource query');
u.resource('#fragment');
equal(u.href(), 'http://foo.bar/#fragment', 'set resource fragment');
equal(u.resource(), '/#fragment', 'get resource fragment');
u.resource('?hello#world');
equal(u.href(), 'http://foo.bar/?hello#world', 'set resource query+fragment');
equal(u.resource(), '/?hello#world', 'get resource query+fragment');
u.resource('/mars.txt?planet=123');
equal(u.href(), 'http://foo.bar/mars.txt?planet=123', 'set resource path+query');
equal(u.resource(), '/mars.txt?planet=123', 'get resource path+query');
u.resource('/neptune.txt#foo');
equal(u.href(), 'http://foo.bar/neptune.txt#foo', 'set resource path+fragment');
equal(u.resource(), '/neptune.txt#foo', 'get resource path+fragment');
});
module('mutating fractions');
test('subdomain', function() {
var u = new URI('http://www.example.org/foo.html');
u.subdomain('foo.bar');
equal(u.hostname(), 'foo.bar.example.org', 'changed subdomain foo.bar');
equal(u+'', 'http://foo.bar.example.org/foo.html', 'changed url foo.bar');
u.subdomain('');
equal(u.hostname(), 'example.org', 'changed subdomain ""');
equal(u+'', 'http://example.org/foo.html', 'changed url ""');
u.subdomain('foo.');
equal(u.hostname(), 'foo.example.org', 'changed subdomain foo.');
equal(u+'', 'http://foo.example.org/foo.html', 'changed url foo.');
u.subdomain('foo_bar');
equal(u.hostname(), 'foo_bar.example.org', 'changed subdomain foo_bar');
equal(u+'', 'http://foo_bar.example.org/foo.html', 'changed url foo_bar');
});
test('domain', function() {
var u = new URI('http://www.example.org/foo.html');
u.domain('foo.bar');
equal(u.hostname(), 'www.foo.bar', 'changed hostname foo.bar');
equal(u+'', 'http://www.foo.bar/foo.html', 'changed url foo.bar');
raises(function() {
u.domain('');
}, TypeError, 'Failing empty input');
u.hostname('www.example.co.uk');
equal(u.domain(), 'example.co.uk', 'domain after changed hostname www.example.co.uk');
equal(u+'', 'http://www.example.co.uk/foo.html', 'url after changed hostname www.example.co.uk');
equal(u.domain(true), 'co.uk', 'domain after changed hostname www.example.co.uk (TLD of SLD)');
u.domain('example.org');
equal(u.domain(), 'example.org', 'domain after changed domain example.org');
equal(u+'', 'http://www.example.org/foo.html', 'url after changed domain example.org');
u.domain('example.co.uk');
equal(u.domain(), 'example.co.uk', 'domain after changed domain example.co.uk');
equal(u+'', 'http://www.example.co.uk/foo.html', 'url after changed domain example.co.uk');
u.href('http://test/');
equal(u.domain(), 'test', 'domain (dot-less)');
equal(u.subdomain(), '', 'subdomain (dot-less)');
u.subdomain('foo');
equal(u.href(), 'http://foo.test/', 'subdomain set on (dot-less)');
u.subdomain('bar');
equal(u.href(), 'http://bar.foo.test/', 'subdomain set on foo.test');
u.domain('exam_ple.org');
equal(u.domain(), 'exam_ple.org', 'domain after changed domain exam_ple.org');
equal(u+'', 'http://bar.exam_ple.org/', 'url after changed domain exam_ple.org');
});
test('tld', function() {
var u = new URI('http://www.example.org/foo.html');
u.tld('mine');
equal(u.tld(), 'mine', 'tld changed');
equal(u+'', 'http://www.example.mine/foo.html', 'changed url mine');
raises(function() {
u.tld('');
}, TypeError, 'Failing empty input');
raises(function() {
u.tld('foo.bar');
}, TypeError, 'Failing "foo.bar"');
u.tld('co.uk');
equal(u.tld(), 'co.uk', 'tld changed to sld');
equal(u+'', 'http://www.example.co.uk/foo.html', 'changed url to sld');
equal(u.tld(true), 'uk', 'TLD of SLD');
u.tld('org');
equal(u.tld(), 'org', 'sld changed to tld');
equal(u+'', 'http://www.example.org/foo.html', 'changed url to tld');
u.hostname('www.examplet.se');
equal(u.tld(), 'se', 'se tld');
});
test('sld', function() {
var u = new URI('http://www.example.ch/foo.html')
equal(u.is('sld'), false, 'is() www.example.ch');
equal(u.domain(), 'example.ch', 'domain() www.example.ch');
equal(u.subdomain(), 'www', 'subdomain() www.example.ch');
u = new URI('http://www.example.com/foo.html')
equal(u.is('sld'), false, 'is() www.example.com');
equal(u.domain(), 'example.com', 'domain() www.example.com');
equal(u.subdomain(), 'www', 'subdomain() www.example.com');
u = new URI('http://www.example.eu.com/foo.html')
equal(u.is('sld'), true, 'is() www.example.eu.com');
equal(u.domain(), 'example.eu.com', 'domain() www.example.eu.com');
equal(u.subdomain(), 'www', 'subdomain() www.example.eu.com');
});
test('directory', function() {
var u = new URI('http://www.example.org/some/directory/foo.html');
u.directory('/');
equal(u.path(), '/foo.html', 'changed path '/'');
equal(u+'', 'http://www.example.org/foo.html', 'changed url '/'');
u.directory('');
equal(u.path(), '/foo.html', 'changed path ""');
equal(u+'', 'http://www.example.org/foo.html', 'changed url ""');
u.directory('/bar');
equal(u.path(), '/bar/foo.html', 'changed path "/bar"');
equal(u+'', 'http://www.example.org/bar/foo.html', 'changed url "/bar"');
u.directory('baz');
equal(u.path(), '/baz/foo.html', 'changed path "baz"');
equal(u+'', 'http://www.example.org/baz/foo.html', 'changed url "baz"');
// relative paths
u = new URI('../some/directory/foo.html');
u.directory('../other/');
equal(u.path(), '../other/foo.html', 'changed path "../other/"');
equal(u+'', '../other/foo.html', 'changed url "../other/"');
u.directory('mine');
equal(u.path(), 'mine/foo.html', 'changed path "mine"');
equal(u+'', 'mine/foo.html', 'changed url "mine"');
u.directory('/');
equal(u.path(), '/foo.html', 'changed path "/"');
equal(u+'', '/foo.html', 'changed url "/"');
u.directory('');
equal(u.path(), 'foo.html', 'changed path ""');
equal(u+'', 'foo.html', 'changed url ""');
u.directory('../blubb');
equal(u.path(), '../blubb/foo.html', 'changed path "../blubb"');
equal(u+'', '../blubb/foo.html', 'changed url "../blubb"');
// encoding
u.path('/some/directory/foo.html');
u.directory('/~userhome/@mine;is %2F and/');
equal(u.path(), '/~userhome/@mine;is%20%2F%20and/foo.html', 'directory encoding');
equal(u.directory(true), '/~userhome/@mine;is %2F and', 'directory decoded');
});
test('filename', function() {
var u = new URI('http://www.example.org/some/directory/foo.html');
u.filename('hello.world');
equal(u.path(), '/some/directory/hello.world', 'changed path "hello.world"');
equal(u+'', 'http://www.example.org/some/directory/hello.world', 'changed url "hello.world"');
u.filename('hello');
equal(u.path(), '/some/directory/hello', 'changed path "hello"');
equal(u+'', 'http://www.example.org/some/directory/hello', 'changed url "hello"');
u.filename('');
equal(u.path(), '/some/directory/', 'changed path ""');
equal(u+'', 'http://www.example.org/some/directory/', 'changed url ""');
u.filename('world');
equal(u.path(), '/some/directory/world', 'changed path "world"');
equal(u+'', 'http://www.example.org/some/directory/world', 'changed url "world"');
// encoding
u.path('/some/directory/foo.html');
u.filename('hällo wörld.html');
equal(u.path(), '/some/directory/h%C3%A4llo%20w%C3%B6rld.html', 'filename encoding');
equal(u.filename(true), 'hällo wörld.html', 'filename decoded');
});
test('suffix', function() {
var u = new URI('http://www.example.org/some/directory/foo.html');
u.suffix('xml');
equal(u.path(), '/some/directory/foo.xml', 'changed path "xml"');
equal(u+'', 'http://www.example.org/some/directory/foo.xml', 'changed url "xml"');
u.suffix('');
equal(u.path(), '/some/directory/foo', 'changed path ""');
equal(u+'', 'http://www.example.org/some/directory/foo', 'changed url ""');
u.suffix('html');
equal(u.path(), '/some/directory/foo.html', 'changed path "html"');
equal(u+'', 'http://www.example.org/some/directory/foo.html', 'changed url "html"');
// encoding
u.suffix('cört');
equal(u.path(), '/some/directory/foo.c%C3%B6rt', 'suffix encoding');
equal(u.suffix(), 'c%C3%B6rt', 'suffix encoded'); // suffix is expected to be alnum!
equal(u.suffix(true), 'cört', 'suffix decoded'); // suffix is expected to be alnum!
});
test('segment', function() {
var u = new URI('http://www.example.org/some/directory/foo.html'),
s = u.segment();
equal(s.join('||'), 'some||directory||foo.html', 'segment get array');
u.segment(['hello', 'world', 'foo.html']);
equal(u.path(), '/hello/world/foo.html', 'segment set array');
equal(u.segment(0), 'hello', 'segment get 0');
equal(u.segment(2), 'foo.html', 'segment get 2');
equal(u.segment(3), undefined, 'segment get 3');
u.segment(0, 'goodbye');
equal(u.path(), '/goodbye/world/foo.html', 'segment set 0');
u.segment(2, 'bar.html');
equal(u.path(), '/goodbye/world/bar.html', 'segment set 2');
u.segment(3, 'zupp');
equal(u.path(), '/goodbye/world/bar.html/zupp', 'segment set 3');
u.segment('zapp');
equal(u.path(), '/goodbye/world/bar.html/zupp/zapp', 'segment append');
u.segment(3, '');
equal(u.path(), '/goodbye/world/bar.html/zapp', 'segment del 3 ""');
u.segment(3, null);
equal(u.path(), '/goodbye/world/bar.html', 'segment del 3 null');
u = new URI('http://www.example.org/some/directory/foo.html');
equal(u.segment(-1), 'foo.html', 'segment get -1');
u.segment(-1, 'world.html');
equal(u.path(), '/some/directory/world.html', 'segment set -1');
u = new URI('someurn:foo:bar:baz');
equal(u.segment().join('||'), 'foo||bar||baz', 'segment get array URN');
u.segment(1, 'mars');
equal(u.path(), 'foo:mars:baz', 'segment set 1 URN');
equal(u.toString(), 'someurn:foo:mars:baz', 'segment set 1 URN');
u = new URI('/foo/');
equal(u.segment().join('||'), 'foo||', 'segment get array trailing empty');
u.segment('test');
equal(u.path(), '/foo/test', 'segment append trailing empty');
u.segment('');
equal(u.path(), '/foo/test/', 'segment append empty trailing');
u.segment('');
equal(u.path(), '/foo/test/', 'segment append empty trailing unchanged');
u.segment(['', '', 'foo', '', '', 'bar', '', '']);
equal(u.path(), '/foo/bar/', 'segment collapsing empty parts');
u = new URI('https://google.com');
u.segment('//font.ttf//');
equal(u.path(), '/font.ttf', 'segment removes trailing and leading slashes');
u.segment(['/hello', '/world/', '//foo.html']);
equal(u.path(), '/hello/world/foo.html', 'segment set array trimming slashes');
u.segment(1, '/mars/');
equal(u.path(), '/hello/mars/foo.html', 'segment set index trimming slashes');
});
test('segmentCoded', function() {
var u = new URI('http://www.example.org/some%20thing/directory/foo.html'),
s = u.segmentCoded();
equal(s.join('||'), 'some thing||directory||foo.html', 'segmentCoded get array');
u.segmentCoded(['hello/world']);
equal(u.path(), '/hello%2Fworld', 'escape in array');
u.segmentCoded('hello/world');
equal(u.path(), '/hello%2Fworld/hello%2Fworld', 'escape appended value');
u.segmentCoded(['hello world', 'mars', 'foo.html']);
equal(u.path(), '/hello%20world/mars/foo.html', 'segmentCoded set array');
equal(u.segmentCoded(0), 'hello world', 'segmentCoded get 0');
equal(u.segmentCoded(2), 'foo.html', 'segmentCoded get 2');
equal(u.segmentCoded(3), undefined, 'segmentCoded get 3');
u.segmentCoded('zapp zerapp');
equal(u.path(), '/hello%20world/mars/foo.html/zapp%20zerapp', 'segmentCoded append');
u.segmentCoded(2, '');
equal(u.path(), '/hello%20world/mars/zapp%20zerapp', 'segmentCoded del 3 ""');
u.segmentCoded(2, null);
equal(u.path(), '/hello%20world/mars', 'segmentCoded del 3 null');
u.segmentCoded('');
equal(u.path(), '/hello%20world/mars/', 'segmentCoded append empty trailing');
u.segmentCoded('');
equal(u.path(), '/hello%20world/mars/', 'segmentCoded append empty trailing unchanged');
});
module('mutating query strings');
test('mutating object', function() {
var u = new URI('?foo=bar&baz=bam&baz=bau'),
q = u.query(true);
q.something = ['new', 'and', 'funky'];
u.query(q);
equal(u.query(), 'foo=bar&baz=bam&baz=bau&something=new&something=and&something=funky', 'adding array');
q.foo = undefined;
u.query(q);
equal(u.query(), 'baz=bam&baz=bau&something=new&something=and&something=funky', 'removing field');
q.baz = undefined;
u.query(q);
equal(u.query(), 'something=new&something=and&something=funky', 'removing array');
});
test('query callback', function() {
var u = URI('?foo=bar');
u.query(function(data) {
data.foo = 'bam';
});
equal(u.query(), 'foo=bam', 'augment argument');
u.query(function() {
return {
bla: 'blubb'
};
});
equal(u.query(), 'bla=blubb', 'overwrite returned value');
});
test('setQuery', function() {
var u = URI('?foo=bar');
u.setQuery('foo', 'bam');
equal(u.query(), 'foo=bam', 'set name, value');
u.setQuery('array', ['one', 'two']);
equal(u.query(), 'foo=bam&array=one&array=two', 'set name, array');
u.query('?foo=bar');
u.setQuery({'obj': 'bam', foo: 'baz'});
equal(u.query(), 'foo=baz&obj=bam', 'set {name: value}');
u.setQuery({'foo': 'foo', bar: ['1', '2']});
equal(u.query(), 'foo=foo&obj=bam&bar=1&bar=2', 'set {name: array}');
u.query('?foo=bar');
u.setQuery({'bam': null, 'baz': ''});
equal(u.query(), 'foo=bar&bam&baz=', 'set {name: null}');
u.query('?foo=bar');
u.setQuery('empty');
equal(u.query(), 'foo=bar&empty', 'set undefined');
u.query('?foo=bar');
u.setQuery('empty', '');
equal(u.query(), 'foo=bar&empty=', 'set empty string');
u.query('');
u.setQuery('some value', 'must be encoded because of = and ? and #');
equal(u.query(), 'some+value=must+be+encoded+because+of+%3D+and+%3F+and+%23', 'encoding');
equal(u.query(true)['some value'], 'must be encoded because of = and ? and #', 'decoding');
u.query('?foo=bar');
u.setQuery('__proto__', 'hasOwnProperty');
equal(u.query(), 'foo=bar', 'set __proto__');
});
test('addQuery', function() {
var u = URI('?foo=bar');
u.addQuery('baz', 'bam');
equal(u.query(), 'foo=bar&baz=bam', 'add name, value');
u.addQuery('array', ['one', 'two']);
equal(u.query(), 'foo=bar&baz=bam&array=one&array=two', 'add name, array');
u.query('?foo=bar');
u.addQuery({'obj': 'bam', foo: 'baz'});
equal(u.query(), 'foo=bar&foo=baz&obj=bam', 'add {name: value}');
u.addQuery({'foo': 'bam', bar: ['1', '2']});
equal(u.query(), 'foo=bar&foo=baz&foo=bam&obj=bam&bar=1&bar=2', 'add {name: array}');
u.query('?foo=bar');
u.addQuery({'bam': null, 'baz': ''});
equal(u.query(), 'foo=bar&bam&baz=', 'add {name: null}');
u.query('?foo=bar');
u.addQuery('empty');
equal(u.query(), 'foo=bar&empty', 'add undefined');
u.query('?foo=bar');
u.addQuery('empty', '');
equal(u.query(), 'foo=bar&empty=', 'add empty string');
u.query('?foo');
u.addQuery('foo', 'bar');
equal(u.query(), 'foo=bar', 'add to null value');
u.query('');
u.addQuery('some value', 'must be encoded because of = and ? and #');
equal(u.query(), 'some+value=must+be+encoded+because+of+%3D+and+%3F+and+%23', 'encoding');
equal(u.query(true)['some value'], 'must be encoded because of = and ? and #', 'decoding');
});
test('removeQuery', function() {
var u = new URI('?foo=bar&foo=baz&foo=bam&obj=bam&bar=1&bar=2&bar=3');
u.removeQuery('foo', 'bar');
equal(u.query(), 'foo=baz&foo=bam&obj=bam&bar=1&bar=2&bar=3', 'removing name, value');
u.removeQuery('foo');
equal(u.query(), 'obj=bam&bar=1&bar=2&bar=3', 'removing name');
u.removeQuery('bar', ['1', '3']);
equal(u.query(), 'obj=bam&bar=2', 'removing name, array');
u.query('?obj=bam&bar=1&bar=2');
u.removeQuery('bar', ['2']);
equal(u.query(), 'obj=bam&bar=1', 'removing name, singleton array');
u.removeQuery('bar', ['1']);
equal(u.query(), 'obj=bam', 'removing the last value via name, singleton array');
u.query('?foo=one&foo=two').removeQuery('foo', ['one', 'two']);
equal(u.query(), '', 'removing name, array, finishes empty');