-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathurl_test.mjs
1487 lines (1217 loc) · 54.1 KB
/
url_test.mjs
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
import './internal_test_init.mjs'
import * as t from '../test.mjs'
import * as l from '../lang.mjs'
import * as u from '../url.mjs'
const LONG = `scheme://user:pass@host:123/path?key=val#hash`
t.test(function test_query() {l.reqInst(u.query(), u.Query)})
t.test(function test_toQuery() {
function same(val) {
l.reqInst(val, u.Query)
t.is(u.toQuery(val), val)
}
function make(src) {
const out = u.toQuery(src)
l.reqInst(out, u.Query)
t.isnt(out, src)
t.is(u.query(src).toString(), u.query(out).toString())
}
make(undefined)
make(``)
make(`one=two`)
same(u.query())
same(u.query(`one=two`))
})
t.test(function test_url() {l.reqInst(u.url(), u.Url)})
t.test(function test_toUrl() {
function same(val) {
l.reqInst(val, u.Url)
t.is(u.toUrl(val), val)
}
function make(src) {
const out = u.toUrl(src)
l.reqInst(out, u.Url)
t.isnt(out, src)
t.is(u.url(src).toString(), u.url(out).toString())
}
make(undefined)
make(``)
make(`/one?two=three#four`)
same(u.url())
same(u.url(`/one?two=three#four`))
})
t.test(function test_urlJoin() {
l.reqInst(u.urlJoin(), u.Url)
testStr(u.urlJoin(), ``)
testStr(u.urlJoin(`one`), `one`)
testStr(u.urlJoin(`one`, `two`), `one/two`)
testStr(u.urlJoin(`/one`, `two`), `/one/two`)
testStr(u.urlJoin(`/one?two=three#four`, `five`, `six`), `/one/five/six?two=three#four`)
testStr(u.urlJoin(`./one?two=three#four`, `five`, `six`), `./one/five/six?two=three#four`)
testStr(u.urlJoin(`../one?two=three#four`, `five`, `six`), `../one/five/six?two=three#four`)
testStr(u.urlJoin(`../one/two?three=four#five`, `six`), `../one/two/six?three=four#five`)
})
t.test(function test_Query() {
t.test(function test_decoding() {
function test(src, exp) {t.eq(u.query(src), exp)}
t.test(function test_decoding_general() {
function empty(src) {test(src, u.query())}
empty(undefined)
empty(null)
empty(``)
empty(`?`)
test(`one=two`, u.query({one: `two`}))
test(`?one=two`, u.query({one: [`two`]}))
test(`??`, u.query({'?': [``]}))
test(`??one`, u.query({'?one': [``]}))
test(`??one=two`, u.query({'?one': [`two`]}))
test(`???one=two`, u.query({'??one': [`two`]}))
test(
`one=two&one=three&four=five&five=six&seven=eight&nine=ten&nine=eleven`,
u.query({
one: [`two`, `three`],
four: `five`,
five: [`six`],
seven: `eight`,
nine: [`ten`, `eleven`],
})
)
})
t.test(function test_decoding_plus() {
test(
`one+two=three+four++five`,
u.query({'one two': `three four five`}),
)
})
t.test(function test_decoding_unicode() {
test(
`?one=%F0%9F%99%82%F0%9F%98%81%F0%9F%98%9B`,
u.query({one: [`🙂😁😛`]}),
)
})
})
t.test(function test_encoding() {
t.test(function test_encoding_from_structured() {
testStr(u.query(), ``)
testStr(u.query([]), ``)
testStr(u.query({}), ``)
testStr(u.query({one: []}), ``)
testStr(u.query({one: undefined}), ``)
testStr(u.query({one: ``}), `one=`)
testStr(u.query({one: [``]}), `one=`)
testStr(u.query({one: `two`}), `one=two`)
testStr(u.query({one: `two`, three: undefined}), `one=two`)
testStr(u.query({one: `two`, three: []}), `one=two`)
testStr(u.query({one: `two`, three: ``}), `one=two&three=`)
testStr(u.query({one: `two`, three: [``]}), `one=two&three=`)
testStr(u.query({one: `two`, three: [`four`]}), `one=two&three=four`)
})
t.test(function test_encoding_from_decoded() {
function test(src, exp) {testStr(u.query(src), exp)}
function same(src) {test(src, src)}
same(``)
same(`one=two`)
same(`one=two&three=four`)
same(`one=two&one=three&four=five&five=six&seven=eight&nine=ten&nine=eleven`)
test(`?`, ``)
test(`??`, `%3F=`)
test(`?one=two`, `one=two`)
test(`??one=two`, `%3Fone=two`)
// Must be reversible.
test(`%3Fone=two`, `%3Fone=two`)
test(`?%3Fone=two`, `%3Fone=two`)
})
t.test(function test_encoding_date() {
const val = new Date(1024)
testStr(u.query().append(`key`, val), `key=1970-01-01T00%3A00%3A01.024Z`)
testStr(u.query({key: val}), `key=1970-01-01T00%3A00%3A01.024Z`)
testStr(u.query([[`key`, val]]), `key=1970-01-01T00%3A00%3A01.024Z`)
})
t.test(function test_encoding_plus() {
testStr(
u.query().append(`one two`, `three four five`),
`one+two=three+four++five`,
)
})
t.test(function test_encoding_unicode() {
testStr(
u.query().append(`one`, `🙂😁😛`),
`one=%F0%9F%99%82%F0%9F%98%81%F0%9F%98%9B`,
)
})
})
t.test(function test_toStringFull() {
t.is(u.query().toStringFull(), ``)
t.is(u.query(`one=two`).toStringFull(), `?one=two`)
})
t.test(function test_reset_from_str() {
testSearchResetWith(l.id)
})
t.test(function test_mut_from_str() {
testSearchMutWith(l.id)
})
t.test(function test_reset_from_Query() {
testSearchResetWith(u.query)
})
t.test(function test_mut_from_Query() {
testSearchMutWith(u.query)
})
t.test(function test_reset_from_URLSearchParams() {
testSearchResetWith(function parse(src) {return new URLSearchParams(src)})
})
t.test(function test_mut_from_URLSearchParams() {
testSearchMutWith(function parse(src) {return new URLSearchParams(src)})
})
t.test(function test_toURLSearchParams() {
function test(str) {
const src = u.query(str)
const out = src.toURLSearchParams()
const exp = new URLSearchParams(str)
t.is(src.toString(), exp.toString())
t.is(out.toString(), exp.toString())
t.eq(out, exp)
t.eq([...out], [...exp])
}
test(``)
test(`one=two&one=three&two=four&two=five`)
test(`one+two=three+four++five`)
test(`%F0%9F%99%82=%F0%9F%98%81`)
})
t.test(function test_keys() {
function test(src, exp) {t.eq([...src.keys()], exp)}
test(u.query(), [])
test(
u.query(`one=two&one=three&four=five&four=six&seven=eight`),
[`one`, `four`, `seven`],
)
test(
u.query().set(`one two`, `three four five`),
[`one two`],
)
test(
u.query(`%F0%9F%99%82=%F0%9F%98%81`),
[`🙂`],
)
// For comparison.
t.eq(
[...new URLSearchParams(`one=two&one=three&four=five&four=six&seven=eight`).keys()],
[`one`, `one`, `four`, `four`, `seven`],
)
})
t.test(function test_values() {
function test(src, exp) {t.eq([...src.values()], exp)}
test(u.query(), [])
test(
u.query(`one=two&one=three&four=five&four=six&seven=eight`),
[[`two`, `three`], [`five`, `six`], [`eight`]],
)
test(
u.query().set(`one two`, `three four five`),
[[`three four five`]],
)
test(
u.query(`%F0%9F%99%82=%F0%9F%98%81`),
[[`😁`]],
)
// For comparison.
t.eq(
[...new URLSearchParams(`one=two&one=three&four=five&four=six&seven=eight`).values()],
[`two`, `three`, `five`, `six`, `eight`],
)
})
t.test(function test_entries() {
function test(src, exp) {t.eq([...src.entries()], exp)}
test(u.query(), [])
test(
u.query(`one=two&one=three&four=five&four=six&seven=eight`),
[[`one`, [`two`, `three`]], [`four`, [`five`, `six`]], [`seven`, [`eight`]]],
)
test(
u.query().set(`one two`, `three four five`),
[[`one two`, [`three four five`]]],
)
test(
u.query(`%F0%9F%99%82=%F0%9F%98%81`),
[[`🙂`, [`😁`]]],
)
// For comparison.
t.eq(
[...new URLSearchParams(`one=two&one=three&four=five&four=six&seven=eight`).entries()],
[[`one`, `two`], [`one`, `three`], [`four`, `five`], [`four`, `six`], [`seven`, `eight`]],
)
})
t.test(function test_toJSON() {
t.is(u.query().toJSON(), null)
t.is(u.query(``).toJSON(), null)
t.is(u.query(`?`).toJSON(), null)
t.is(u.query(`one=two`).toJSON(), `one=two`)
})
})
function testSearchResetWith(fun) {
testReset(u.query(), fun(), ``)
testReset(u.query(), fun(``), ``)
testReset(u.query(), fun(`one=two`), `one=two`)
testReset(u.query(`one=two`), fun(`one=three`), `one=three`)
testReset(u.query(`one=two`), fun(`three=four`), `three=four`)
testReset(u.query(`one=two`), fun(`three=four&five=six`), `three=four&five=six`)
}
function testSearchMutWith(fun) {
testMut(u.query(), fun(), ``)
testMut(u.query(), fun(``), ``)
testMut(u.query(), fun(`one=two`), `one=two`)
testMut(u.query(`one=two`), fun(`one=three`), `one=three`)
testMut(u.query(`one=two`), fun(`three=four`), `one=two&three=four`)
testMut(u.query(`one=two`), fun(`three=four&five=six`), `one=two&three=four&five=six`)
}
/*
TODO more tests for IPv4.
TODO more tests for IPv6.
*/
t.test(function test_Url() {
t.test(function test_structure() {
t.eq(
new u.Url(`one://two:[email protected]:123/six?seven=eight#nine`),
Object.assign(Object.create(u.Url.prototype), {
[u.schemeKey]: `one`,
[u.slashKey]: `//`,
[u.usernameKey]: `two`,
[u.passwordKey]: `three`,
[u.hostnameKey]: `four.five`,
[u.portKey]: `123`,
[u.pathnameKey]: `/six`,
[u.queryKey]: `seven=eight`,
[u.hashKey]: `nine`,
}),
)
})
t.test(function test_scheme() {
t.throws(() => u.url().scheme = 10, TypeError, `unable to convert 10 to scheme`)
t.throws(() => u.url().scheme = `https:`, SyntaxError, `unable to convert "https:" to scheme`)
t.throws(() => u.url().scheme = `https://`, SyntaxError, `unable to convert "https://" to scheme`)
t.throws(() => u.url().scheme = `one two`, SyntaxError, `unable to convert "one two" to scheme`)
function test(src, exp) {t.is(u.url(src).scheme, exp)}
test(``, ``)
test(`one:`, `one`)
test(`one:two`, `one`)
test(`one:two/three`, `one`)
test(`one:/two/three`, `one`)
test(`one://two/three`, `one`)
test(`one://two.three`, `one`)
test(`one://two.three/four`, `one`)
})
// TODO validate that username and password get updated.
t.test(function test_setScheme() {
function test(src, val, exp) {testStr(u.url(src).setScheme(val), exp)}
test(``, ``, ``)
test(`scheme:path`, ``, `path`)
test(`scheme://host`, ``, ``)
test(`scheme://host/path`, ``, `/path`)
test(`scheme://host/path?key=val#hash`, ``, `/path?key=val#hash`)
test(``, `scheme`, `scheme:`)
test(`scheme:path`, `mailto`, `mailto:path`)
test(`scheme://host`, `https`, `https://host`)
})
t.test(function test_slash() {
t.throws(() => u.url().slash = 10, TypeError, `unable to convert 10 to slash`)
t.throws(() => u.url().slash = `str`, SyntaxError, `unable to convert "str" to slash`)
function test(src, exp) {t.is(u.url(src).slash, exp)}
test(``, ``)
test(`one:`, ``)
test(`one:two`, ``)
test(`one:/two`, ``)
test(`one://two`, `//`)
})
// TODO validate that username and password get updated.
t.test(function test_setSlash() {
function test(src, val, exp) {testStr(u.url(src).setSlash(val), exp)}
test(``, ``, ``)
test(``, `/`, `/`)
test(``, `//`, `//`)
test(``, `///`, `///`)
test(`one:two`, `//`, `one://two`)
test(`one:/two`, `/`, `one://two`)
test(`one:/two`, `//`, `one:///two`)
test(`one:/two`, ``, `one:/two`)
test(`scheme:val`, `//`, `scheme://val`)
// For the most part, we don't support or allow this.
// TODO consider avoiding.
test(`/path`, `//`, `///path`)
test(`scheme://`, ``, `scheme:`)
test(`scheme://host`, ``, `scheme:`)
test(`scheme://host/path`, ``, `scheme:/path`)
test(`scheme://host/path?key=val#hash`, ``, `scheme:/path?key=val#hash`)
})
t.test(function test_username() {
t.throws(() => u.url().username = 10, TypeError, `unable to convert 10 to username`)
t.throws(() => u.url().username = `/`, SyntaxError, `unable to convert "/" to username`)
t.throws(() => u.url().username = `user`, SyntaxError, `username is forbidden in URL without protocol double slash`)
{
const url = u.url(`scheme:path`)
t.throws(() => url.username = `user`, SyntaxError, `username is forbidden in URL without protocol double slash`)
}
function test(src, exp) {t.is(u.url(src).username, exp)}
testEmpty(test)
// User info is allowed only after scheme with //.
test(`scheme:nonuser:nonpass@nondomain`, ``)
test(`scheme:nonuser:@nondomain`, ``)
test(`scheme::nonpass@nondomain`, ``)
test(`scheme://domain`, ``)
test(`scheme://@domain`, ``)
test(`scheme://user:pass@domain`, `user`)
test(`scheme://user:@domain`, `user`)
test(`scheme://:pass@domain`, ``)
})
t.test(function test_setUsername() {
function test(src, val, exp) {testStr(u.url(src).setUsername(val), exp)}
test(``, ``, ``)
test(`scheme://@domain`, ``, `scheme://domain`)
test(`scheme://user@domain`, ``, `scheme://domain`)
test(`scheme://:pass@domain`, ``, `scheme://:pass@domain`)
test(`scheme://user:pass@domain`, ``, `scheme://:pass@domain`)
test(`scheme://domain`, `user`, `scheme://user@domain`)
test(`scheme://user@domain`, `username`, `scheme://username@domain`)
test(`scheme://:pass@domain`, `user`, `scheme://user:pass@domain`)
test(`scheme://user:pass@domain`, `username`, `scheme://username:pass@domain`)
})
t.test(function test_password() {
t.throws(() => u.url().password = 10, TypeError, `unable to convert 10 to password`)
t.throws(() => u.url().password = `/`, SyntaxError, `unable to convert "/" to password`)
t.throws(() => u.url().password = `pass`, SyntaxError, `password is forbidden in URL without protocol double slash`)
{
const url = u.url(`scheme:path`)
t.throws(() => url.password = `pass`, SyntaxError, `password is forbidden in URL without protocol double slash`)
}
function test(src, exp) {t.is(u.url(src).password, exp)}
testEmpty(test)
// User info is allowed only after scheme with //.
test(`scheme:nonuser:nonpass@nondomain`, ``)
test(`scheme:nonuser:@nondomain`, ``)
test(`scheme::nonpass@nondomain`, ``)
test(`scheme://domain`, ``)
test(`scheme://@domain`, ``)
test(`scheme://user:pass@domain`, `pass`)
test(`scheme://user:@domain`, ``)
test(`scheme://:pass@domain`, `pass`)
})
t.test(function test_setPassword() {
function test(src, val, exp) {testStr(u.url(src).setPassword(val), exp)}
test(``, ``, ``)
test(`scheme://@domain`, ``, `scheme://domain`)
test(`scheme://user@domain`, ``, `scheme://user@domain`)
test(`scheme://:pass@domain`, ``, `scheme://domain`)
test(`scheme://user:pass@domain`, ``, `scheme://user@domain`)
test(`scheme://domain`, `pass`, `scheme://:pass@domain`)
test(`scheme://user@domain`, `pass`, `scheme://user:pass@domain`)
test(`scheme://:pass@domain`, `password`, `scheme://:password@domain`)
test(`scheme://user:pass@domain`, `password`, `scheme://user:password@domain`)
})
t.test(function test_hostname() {
t.throws(() => u.url().hostname = `/`, SyntaxError, `unable to convert "/" to hostname`)
t.throws(() => u.url().hostname = `?`, SyntaxError, `unable to convert "?" to hostname`)
t.throws(() => u.url().hostname = `#`, SyntaxError, `unable to convert "#" to hostname`)
t.throws(() => u.url().hostname = `one/two`, SyntaxError, `unable to convert "one/two" to hostname`)
t.throws(() => u.url().hostname = `one?two`, SyntaxError, `unable to convert "one?two" to hostname`)
t.throws(() => u.url().hostname = `one#two`, SyntaxError, `unable to convert "one#two" to hostname`)
t.throws(() => u.url().hostname = `one two`, SyntaxError, `unable to convert "one two" to hostname`)
t.throws(() => u.url().hostname = `one`, SyntaxError, `hostname is forbidden in URL without protocol double slash`)
{
const url = u.url(`scheme:path`)
t.throws(() => url.hostname = `one`, SyntaxError, `hostname is forbidden in URL without protocol double slash`)
}
function test(src, exp) {t.is(u.url(src).hostname, exp)}
testEmpty(test)
test(`one:two`, ``)
test(`one:two.three`, ``)
test(`one:two.three.four`, ``)
test(`one://two`, `two`)
test(`one://two.three`, `two.three`)
test(`one://two.three.four`, `two.three.four`)
test(`http://1.2.3.4:5678`, `1.2.3.4`)
test(`http://12.23.34.45:5678`, `12.23.34.45`)
test(`http://[::1]:1234`, `[::1]`)
test(`http://[2001:0db8:0000:0000:0000:8a2e:0370:7334]:1234`, `[2001:0db8:0000:0000:0000:8a2e:0370:7334]`)
test(`http://[2001:db8::8a2e:370:7334]:1234`, `[2001:db8::8a2e:370:7334]`)
})
t.test(function test_setHostname() {
function test(src, val, exp) {testStr(u.url(src).setHostname(val), exp)}
test(``, ``, ``)
test(`scheme:path`, ``, `scheme:path`)
test(`scheme://host`, ``, `scheme://`)
test(`scheme://host:123`, ``, `scheme://`)
test(`scheme://host:123/path`, ``, `scheme:///path`)
test(`scheme://host`, `hostname`, `scheme://hostname`)
test(`scheme://host/path`, `hostname`, `scheme://hostname/path`)
test(`scheme://host:123`, `hostname`, `scheme://hostname:123`)
test(`scheme://host:123/path`, `hostname`, `scheme://hostname:123/path`)
})
t.test(function test_port() {
t.throws(() => u.url().port = `/`, SyntaxError, `unable to convert "/" to port`)
t.throws(() => u.url().port = `123`, SyntaxError, `port is forbidden in URL without protocol double slash`)
function test(src, exp) {t.is(u.url(src).port, exp)}
testEmpty(test)
test(`scheme:[email protected]:123`, ``)
test(`scheme://host`, ``)
test(`scheme://host/path`, ``)
test(`scheme://host:123`, `123`)
test(`scheme://one.two:123`, `123`)
test(`scheme://one.two.three:123`, `123`)
})
t.test(function test_setPort() {
function test(src, val, exp) {testStr(u.url(src).setPort(val), exp)}
test(``, ``, ``)
test(`scheme:path`, ``, `scheme:path`)
test(`scheme://host`, ``, `scheme://host`)
test(`scheme://host:123`, ``, `scheme://host`)
test(`scheme://host`, `234`, `scheme://host:234`)
test(`scheme://host:123`, `234`, `scheme://host:234`)
test(`scheme://host`, 0, `scheme://host:0`)
test(`scheme://host:123`, 0, `scheme://host:0`)
test(`scheme://host`, 234, `scheme://host:234`)
test(`scheme://host:123`, 234, `scheme://host:234`)
})
t.test(function test_pathname() {
t.throws(() => u.url().pathname = `?`, SyntaxError, `unable to convert "?" to pathname`)
t.throws(() => u.url().pathname = `#`, SyntaxError, `unable to convert "#" to pathname`)
t.throws(() => u.url().pathname = `one?two`, SyntaxError, `unable to convert "one?two" to pathname`)
t.throws(() => u.url().pathname = `one#two`, SyntaxError, `unable to convert "one#two" to pathname`)
// Known inconsistency with `URL`, which supports idempotent transcoding
// from non-URL-safe to URL-safe. We only allow URL-safe inputs here.
t.throws(() => u.url().pathname = `one two`, SyntaxError, `unable to convert "one two" to pathname`)
function test(src, exp) {t.is(u.url(src).pathname, exp)}
testEmpty(test)
test(undefined, ``)
test(null, ``)
test(``, ``)
test(`one:`, ``)
test(`one:two`, `two`)
test(`one:two.three`, `two.three`)
test(`one:two.three.four`, `two.three.four`)
test(`scheme:`, ``)
test(`scheme:[email protected]`, `[email protected]`)
test(`scheme:nonuser:nonpass@nondomain`, `nonuser:nonpass@nondomain`)
test(`scheme:nonuser:nonpass@nondomain?key=val#hash`, `nonuser:nonpass@nondomain`)
test(`scheme://user:pass@domain/path?key=val#hash`, `/path`)
test(`scheme://user:pass@domain/?key=val#hash`, `/`)
test(`scheme://user:pass@domain?key=val#hash`, ``)
test(`./one`, `./one`)
test(`../one`, `../one`)
test(`one/../two`, `one/../two`)
test(`/one/../two`, `/one/../two`)
test(`./one/../two`, `./one/../two`)
test(`../one/../two`, `../one/../two`)
test(`scheme://user:pass@domain/../one`, `/../one`)
test(`scheme://user:pass@domain/one/../two`, `/one/../two`)
})
t.test(function test_setPathname() {
function test(src, val, exp) {testStr(u.url(src).setPathname(val), exp)}
test(``, ``, ``)
test(``, `path`, `path`)
test(``, `/path`, `/path`)
test(`scheme:path`, ``, `scheme:`)
test(`scheme:`, `path`, `scheme:path`)
test(`scheme:`, `/path`, `scheme:/path`)
test(`scheme://`, `path`, `scheme://path`)
test(`scheme://`, `/path`, `scheme:///path`)
test(`scheme://host`, `path`, `scheme://host/path`)
test(`scheme://host`, `/path`, `scheme://host/path`)
test(`scheme://host/path`, `pathname`, `scheme://host/pathname`)
test(`scheme://host/path`, `/pathname`, `scheme://host/pathname`)
test(`scheme://host:123/path`, `/pathname`, `scheme://host:123/pathname`)
test(`scheme://host/path?search#hash`, `/pathname`, `scheme://host/pathname?search#hash`)
})
t.test(function test_search() {
t.throws(() => u.url().search = `#`, SyntaxError, `unable to convert "#" to Query`)
t.throws(() => u.url().search = `one#two`, SyntaxError, `unable to convert "one#two" to Query`)
t.throws(() => u.url().search = 10, TypeError, `expected variant of isStr, got 10`)
t.throws(() => u.url().search = u.query(), TypeError, `expected variant of isStr, got [object Query]`)
t.throws(() => u.url().search = u.url(), TypeError, `expected variant of isStr, got [object Url`)
t.throws(() => u.url().search = {key: `val`}, TypeError, `expected variant of isStr, got {key: "val"}`)
function test(src, exp) {t.is(u.url(src).search, exp)}
testUrlSearchGet(test)
test(`scheme://?one=two?three`, `one=two?three`)
test(`scheme://host/path?one=two?three`, `one=two?three`)
test(`scheme://host/path?one=two?three#four`, `one=two?three`)
test(`scheme://host/path?one=two?three#four?five`, `one=two?three`)
test(`scheme://host/path?one=two?three#four?five#six`, `one=two?three`)
})
t.test(function test_setSearch() {
t.throws(() => u.url().setSearch(10), TypeError, `expected variant of isStr, got 10`)
t.throws(() => u.url().setSearch(u.query()), TypeError, `expected variant of isStr, got [object Query]`)
t.throws(() => u.url().setSearch({key: `val`}), TypeError, `expected variant of isStr, got {key: "val"}`)
testUrlSearchSet(function test(src, val, exp) {
testStr(u.url(src).setSearch(val), exp)
})
})
t.test(function test_searchParams() {
t.throws(() => u.url().searchParams = `#`, SyntaxError, `unable to convert "#" to Query`)
t.throws(() => u.url().searchParams = `one#two`, SyntaxError, `unable to convert "one#two" to Query`)
t.throws(() => u.url().searchParams = 10, TypeError, `unable to convert 10 to Query`)
l.reqInst(u.url().searchParams, u.Query)
l.reqInst(u.url(`?key=val`).searchParams, u.Query)
t.eq(u.url(`?key=val`).searchParams, u.query(`key=val`))
function test(src, exp) {
t.is(u.url(src).searchParams.toString(), exp)
t.eq(u.url(src).searchParams, u.query(exp))
}
testUrlSearchGet(test)
test(`scheme://?one=two?three`, `one=two%3Fthree`)
test(`scheme://host/path?one=two?three`, `one=two%3Fthree`)
test(`scheme://host/path?one=two?three#four`, `one=two%3Fthree`)
test(`scheme://host/path?one=two?three#four?five`, `one=two%3Fthree`)
test(`scheme://host/path?one=two?three#four?five#six`, `one=two%3Fthree`)
})
t.test(function test_setSearchParams() {
t.throws(() => u.url().setSearchParams(10), TypeError, `unable to convert 10 to Query`)
function test(src, val, exp) {testStr(u.url(src).setSearchParams(val), exp)}
testUrlSearchSet(test)
test(``, new URLSearchParams(`key=val`), `?key=val`)
test(``, u.query(`key=val`), `?key=val`)
test(``, {key: `val`}, `?key=val`)
test(`?one=two`, u.query(`three=four`), `?three=four`)
})
t.test(function test_query() {
const url = u.url()
t.is(url.query, url.searchParams)
url.searchParams = `one=two`
t.is(url.query, url.searchParams)
})
t.test(function test_setQuery() {
function test(src, val, exp) {testStr(u.url(src).setQuery(val), exp)}
test(``, u.query(`key=val`), `?key=val`)
test(`?one=two`, u.query(`three=four`), `?three=four`)
})
t.test(function test_queryMut() {
function test(src, val, exp) {testStr(u.url(src).queryMut(val), exp)}
test(``, u.query(`key=val`), `?key=val`)
test(`?one=two`, u.query(`three=four`), `?one=two&three=four`)
})
t.test(function test_querySet() {
function test(src, key, val, exp) {
testStr(u.url(src).querySet(key, val), exp)
}
test(``, `one`, `two`, `?one=two`)
test(`?one=two`, `three`, `four`, `?one=two&three=four`)
test(`/one?two=three`, `four`, `five`, `/one?two=three&four=five`)
})
t.test(function test_hash() {
t.throws(() => u.url().hash = 10, TypeError, `unable to convert 10 to hash`)
t.throws(() => u.url().hash = `one two`, SyntaxError, `unable to convert "one two" to hash`)
function test(src, exp) {t.is(u.url(src).hash, exp)}
testEmpty(test)
test(`scheme:path?key=val`, ``)
test(`scheme:path?key=val#hash`, `hash`)
test(`scheme:path#hash`, `hash`)
test(`scheme://user:pass@domain/path?key=val`, ``)
test(`scheme://user:pass@domain/path?key=val#`, ``)
test(`scheme://user:pass@domain/path?key=val##`, `#`)
test(`scheme://user:pass@domain/path?key=val#hash`, `hash`)
test(`scheme://user:pass@domain/path?key=val##hash`, `#hash`)
test(`scheme://user:pass@domain/path?key=val#hash?one`, `hash?one`)
test(`scheme://user:pass@domain/path?key=val##hash?one`, `#hash?one`)
test(`scheme://user:pass@domain/path?key=val#hash#two`, `hash#two`)
test(`scheme://user:pass@domain/path?key=val##hash#two`, `#hash#two`)
test(`scheme://user:pass@domain/path?key=val#hash?one#two`, `hash?one#two`)
test(`scheme://user:pass@domain/path?key=val##hash?one#two`, `#hash?one#two`)
})
t.test(function test_setHash() {
t.throws(() => u.url().setHash(10), TypeError, `unable to convert 10 to hash`)
t.throws(() => u.url().setHash(`one two`), SyntaxError, `unable to convert "one two" to hash`)
function test(src, val, exp) {testStr(u.url(src).setHash(val), exp)}
/*
Should behave identically to existing APIs such as `URL` and `Location`,
where getter `.hash` idempotently prepends `#` and setter `.hash`
automatically strips one level of leading `#`. Without this stripping, it
would be WAY too easy to accidentally stack up `###`. However, only one
level of leading `#` is stripped, preserving additional hashes if any,
allowing the getter and setter to be mostly reversible. We also provide
`.setHashExact` to bypass this mechanism.
*/
test(``, ``, ``)
test(``, `hash`, `#hash`)
test(``, `#hash`, `#hash`)
test(``, `##hash`, `##hash`)
test(``, `###hash`, `###hash`)
test(`scheme:`, ``, `scheme:`)
test(`scheme:`, `hash`, `scheme:#hash`)
test(`scheme:path`, `hash`, `scheme:path#hash`)
test(`scheme:/path`, `hash`, `scheme:/path#hash`)
test(`scheme://`, ``, `scheme://`)
test(`scheme://`, `hash`, `scheme://#hash`)
test(`scheme://host`, `hash`, `scheme://host#hash`)
test(`scheme://host/path`, `hash`, `scheme://host/path#hash`)
test(`scheme://host/path?key=val`, `hash`, `scheme://host/path?key=val#hash`)
test(`scheme://host/path?key=val#one`, `two`, `scheme://host/path?key=val#two`)
test(`scheme://host/path?key=val#one?two#three`, `four`, `scheme://host/path?key=val#four`)
test(`scheme://host/path?key=val#one?two#three`, `four?five#six`, `scheme://host/path?key=val#four?five#six`)
})
t.test(function test_setHashExact() {
t.throws(() => u.url().setHashExact(10), TypeError, `unable to convert 10 to hash`)
t.throws(() => u.url().setHashExact(`one two`), SyntaxError, `unable to convert "one two" to hash`)
function test(src, val, exp) {testStr(u.url(src).setHashExact(val), exp)}
test(``, ``, ``)
test(``, `hash`, `#hash`)
test(``, `#hash`, `##hash`)
test(``, `##hash`, `###hash`)
})
t.test(function test_protocol() {
t.throws(() => u.url().protocol = 10, TypeError, `expected variant of isStr, got 10`)
t.throws(() => u.url().protocol = `/`, SyntaxError, `unable to convert "/" to protocol`)
t.throws(() => u.url().protocol = `one`, SyntaxError, `unable to convert "one" to protocol`)
function test(src, exp) {t.is(u.url(src).protocol, exp)}
testEmpty(test)
test(``, ``)
test(`scheme:`, `scheme:`)
test(`scheme:path`, `scheme:`)
test(`scheme:path:morepath`, `scheme:`)
test(`scheme:/path:morepath`, `scheme:`)
test(`scheme://host`, `scheme://`)
test(`scheme://host.host`, `scheme://`)
test(`scheme://host.host/path`, `scheme://`)
})
t.test(function test_setProtocol() {
t.throws(() => u.url().setProtocol(10), TypeError, `expected variant of isStr, got 10`)
t.throws(() => u.url().setProtocol(`/`), SyntaxError, `unable to convert "/" to protocol`)
t.throws(() => u.url().setProtocol(`one`), SyntaxError, `unable to convert "one" to protocol`)
function test(src, val, exp) {testStr(u.url(src).setProtocol(val), exp)}
test(``, ``, ``)
test(`/path`, ``, `/path`)
test(`scheme:path`, ``, `path`)
test(`scheme:/path`, ``, `/path`)
test(`scheme://host`, ``, ``)
test(`scheme://host/path?key=val#hash`, ``, `/path?key=val#hash`)
test(``, `scheme:`, `scheme:`)
test(``, `scheme://`, `scheme://`)
test(`path`, `scheme:`, `scheme:path`)
test(`host`, `scheme://`, `scheme://host`)
test(`scheme:path`, `mailto:`, `mailto:path`)
test(`scheme:path?key=val#hash`, `mailto:`, `mailto:path?key=val#hash`)
test(`scheme://host`, `mailto:`, `mailto:`)
test(`scheme://host/path?key=val#hash`, `mailto:`, `mailto:/path?key=val#hash`)
test(`scheme:val`, `https://`, `https://val`)
test(`scheme:val?key=val#hash`, `https://`, `https://val?key=val#hash`)
test(`scheme://host`, `https://`, `https://host`)
test(`scheme://host/path?key=val#hash`, `https://`, `https://host/path?key=val#hash`)
})
t.test(function test_host() {
t.throws(() => u.url().host = 10, SyntaxError, `host is forbidden in URL without protocol double slash`)
t.throws(() => u.url().host = `one`, SyntaxError, `host is forbidden in URL without protocol double slash`)
t.throws(() => u.url(`scheme://`).host = 10, TypeError, `expected variant of isStr, got 10`)
t.throws(() => u.url(`scheme://`).host = `/`, SyntaxError, `unable to convert "/" to host`)
function test(src, exp) {t.is(u.url(src).host, exp)}
testEmpty(test)
test(`scheme:`, ``)
test(`scheme:path`, ``)
test(`scheme:/path`, ``)
test(`scheme:path:morepath`, ``)
test(`scheme:/path:morepath`, ``)
test(`scheme://`, ``)
test(`scheme://:123`, ``)
test(`scheme://host`, `host`)
test(`scheme://host:123`, `host:123`)
test(`scheme://host/path`, `host`)
test(`scheme://host:123/path`, `host:123`)
test(`scheme://host/path:morepath`, `host`)
test(`scheme://host:123/path:morepath`, `host:123`)
})
t.test(function test_setHost() {
t.throws(() => u.url().setHost(10), SyntaxError, `host is forbidden in URL without protocol double slash`)
t.throws(() => u.url().setHost(`one`), SyntaxError, `host is forbidden in URL without protocol double slash`)
t.throws(() => u.url(`scheme://`).setHost(10), TypeError, `expected variant of isStr, got 10`)
t.throws(() => u.url(`scheme://`).setHost(`/`), SyntaxError, `unable to convert "/" to host`)
function test(src, val, exp) {testStr(u.url(src).setHost(val), exp)}
test(``, ``, ``)
test(`/path`, ``, `/path`)
test(`scheme:path`, ``, `scheme:path`)
test(`scheme:path?key=val#hash`, ``, `scheme:path?key=val#hash`)
test(`scheme://host`, ``, `scheme://`)
test(`scheme://host?key=val#hash`, ``, `scheme://?key=val#hash`)
test(`scheme://host/path?key=val#hash`, ``, `scheme:///path?key=val#hash`)
test(`scheme://`, `host`, `scheme://host`)
test(`scheme:///path`, `host:123`, `scheme://host:123/path`)
test(`scheme://?key=val#hash`, `host:123`, `scheme://host:123?key=val#hash`)
test(`scheme:///path?key=val#hash`, `host:123`, `scheme://host:123/path?key=val#hash`)
})
t.test(function test_origin() {
t.throws(() => u.url().origin = 10, TypeError, `expected variant of isStr, got 10`)
t.throws(() => u.url().origin = `/`, SyntaxError, `unable to convert "/" to origin`)
t.throws(() => u.url().origin = `one`, SyntaxError, `unable to convert "one" to origin`)
t.throws(() => u.url().origin = `scheme:path`, SyntaxError, `unable to convert "scheme:path" to origin`)
t.throws(() => u.url().origin = `scheme://host/path`, SyntaxError, `unable to convert "scheme://host/path" to origin`)
function test(src, exp) {t.is(u.url(src).origin, exp)}
testEmpty(test)
test(`one://two.three/four/five`, `one://two.three`)
test(`one://two.three:123/four/five`, `one://two.three:123`)
test(`one:two.three/four/five`, ``)
test(`one:two.three:123/four/five`, ``)
})
// TODO validate that username and password get updated.
t.test(function test_setOrigin() {
t.throws(() => u.url().setOrigin(10), TypeError, `expected variant of isStr, got 10`)
t.throws(() => u.url().setOrigin(`/`), SyntaxError, `unable to convert "/" to origin`)
t.throws(() => u.url().setOrigin(`one`), SyntaxError, `unable to convert "one" to origin`)
t.throws(() => u.url().setOrigin(`scheme:path`), SyntaxError, `unable to convert "scheme:path" to origin`)
t.throws(() => u.url().setOrigin(`scheme://host/path`), SyntaxError, `unable to convert "scheme://host/path" to origin`)
function test(src, val, exp) {testStr(u.url(src).setOrigin(val), exp)}
test(``, ``, ``)
test(``, `scheme:`, `scheme:`)
test(``, `scheme://`, `scheme://`)
test(``, `scheme://host`, `scheme://host`)
test(``, `scheme://host:123`, `scheme://host:123`)
test(`?key=val#hash`, ``, `?key=val#hash`)
test(`?key=val#hash`, `scheme:`, `scheme:?key=val#hash`)
test(`?key=val#hash`, `scheme://`, `scheme://?key=val#hash`)
test(`?key=val#hash`, `scheme://host`, `scheme://host?key=val#hash`)
test(`?key=val#hash`, `scheme://host:123`, `scheme://host:123?key=val#hash`)
test(`/path?key=val#hash`, ``, `/path?key=val#hash`)
test(`/path?key=val#hash`, `scheme:`, `scheme:/path?key=val#hash`)
test(`/path?key=val#hash`, `scheme://`, `scheme:///path?key=val#hash`)
test(`/path?key=val#hash`, `scheme://host`, `scheme://host/path?key=val#hash`)
test(`/path?key=val#hash`, `scheme://host:123`, `scheme://host:123/path?key=val#hash`)
test(`mailto:/path?key=val#hash`, ``, `/path?key=val#hash`)
test(`mailto:/path?key=val#hash`, `scheme:`, `scheme:/path?key=val#hash`)
test(`mailto:/path?key=val#hash`, `scheme://`, `scheme:///path?key=val#hash`)
test(`mailto:/path?key=val#hash`, `scheme://host`, `scheme://host/path?key=val#hash`)
test(`mailto:/path?key=val#hash`, `scheme://host:123`, `scheme://host:123/path?key=val#hash`)
test(`https://prev/path?key=val#hash`, ``, `/path?key=val#hash`)
test(`https://prev/path?key=val#hash`, `scheme:`, `scheme:/path?key=val#hash`)
test(`https://prev/path?key=val#hash`, `scheme://`, `scheme:///path?key=val#hash`)
test(`https://prev/path?key=val#hash`, `scheme://host`, `scheme://host/path?key=val#hash`)
test(`https://prev/path?key=val#hash`, `scheme://host:123`, `scheme://host:123/path?key=val#hash`)
test(`https://prev:234/path?key=val#hash`, ``, `/path?key=val#hash`)
test(`https://prev:234/path?key=val#hash`, `scheme:`, `scheme:/path?key=val#hash`)
test(`https://prev:234/path?key=val#hash`, `scheme://`, `scheme:///path?key=val#hash`)
test(`https://prev:234/path?key=val#hash`, `scheme://host`, `scheme://host/path?key=val#hash`)
test(`https://prev:234/path?key=val#hash`, `scheme://host:123`, `scheme://host:123/path?key=val#hash`)
})
t.test(function test_href() {
t.throws(() => u.url().href = 10, TypeError, `unable to convert 10 to Url`)
t.throws(() => u.url().href = `one two`, SyntaxError, `unable to convert "one two" to url`)
function test(src, exp) {t.is(u.url(src).href, exp)}
function same(src) {test(src, src)}
// Known cases of information loss / normalization.
// For other URLs, decoding and encoding should be reversible.
test(`scheme://user@domain`, `scheme://user@domain`)
test(`scheme://user:@domain`, `scheme://user@domain`)
same(`one:two.three/four/five`)
same(`one:two.three:123/four/five`)
same(`one:/two.three/four/five`)
same(`one:/two.three:123/four/five`)
same(`one:/two.three:123/four/five?six=seven`)
same(`one:/two.three:123/four/five?six=seven#eight`)
same(`one:/two.three:123/four/five?six=seven#eight?nine`)
same(`one:/two.three:123/four/five?six=seven#eight?nine=ten`)
same(`one:/two.three:123/four/five?six=seven#eight?nine=ten#eleven`)
same(`scheme://user:pass@domain`)
same(`scheme://:pass@domain`)
same(`scheme://user:pass@domain:123`)
same(`scheme://:pass@domain:123`)
same(`scheme://user:pass@domain:123/path?key=val`)
same(`scheme://user:pass@domain:123/path?key=val##`)
same(`scheme://user:pass@domain:123/path?key=val#hash`)
same(`scheme://user:pass@domain:123/path?key=val##hash`)
})
t.test(function test_setHref() {
t.throws(() => u.url().setHref(10), TypeError, `unable to convert 10 to Url`)
t.throws(() => u.url().setHref(`one two`), SyntaxError, `unable to convert "one two" to url`)
function test(src, val, exp) {testStr(u.url(src).setHref(val), exp)}