Skip to content

Commit 96aee26

Browse files
committed
add tests that compare URI against nodejs/standard URL
1 parent bea884b commit 96aee26

1 file changed

Lines changed: 79 additions & 6 deletions

File tree

src/vs/base/test/node/uri.test.ts

Lines changed: 79 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import * as assert from 'assert';
66
import { URI, UriComponents } from 'vs/base/common/uri';
77
import { isWindows } from 'vs/base/common/platform';
8-
import { pathToFileURL } from 'url';
8+
import { pathToFileURL, fileURLToPath, URL } from 'url';
99

1010
suite('URI', () => {
1111
test('file#toString', () => {
@@ -455,7 +455,7 @@ suite('URI', () => {
455455
assert.equal(uri1.path, uri2.path);
456456
assert.equal(uri1.query, uri2.query);
457457
assert.equal(uri1.fragment, uri2.fragment);
458-
assert.equal(strIn, strOut); // fails here!!
458+
assert.equal(strIn, strOut);
459459
});
460460

461461
test('Uri#parse can break path-component #45515', function () {
@@ -469,7 +469,7 @@ suite('URI', () => {
469469
assert.equal(uri1.path, uri2.path);
470470
assert.equal(uri1.query, uri2.query);
471471
assert.equal(uri1.fragment, uri2.fragment);
472-
assert.equal(strIn, strOut); // fails here!!
472+
assert.equal(strIn, strOut);
473473
});
474474

475475
test('URI - (de)serialize', function () {
@@ -502,14 +502,27 @@ suite('URI', () => {
502502
// console.profileEnd();
503503
});
504504

505-
function assertFileUri(path: string): void {
506-
// check that our uri aligns with nodejs
505+
// ------ check against standard URL and nodejs-file-url utils
506+
507+
function assertFileUri(path: string, recurse = true): void {
507508
const actual = URI.file(path).toString();
508509
const expected = pathToFileURL(path).href;
509510
assert.equal(actual, expected);
511+
if (recurse) {
512+
assertFsPath(expected, false);
513+
}
514+
}
515+
516+
function assertFsPath(uri: string, recurse = true): void {
517+
const actual = URI.parse(uri).fsPath;
518+
const expected = fileURLToPath(uri);
519+
assert.equal(actual, expected);
520+
if (recurse) {
521+
assertFileUri(actual, false);
522+
}
510523
}
511524

512-
test('URI.file vs pathToFileURL', function () {
525+
test('URI.file and pathToFileURL', function () {
513526
assertFileUri('/foo/bar');
514527
assertFileUri('/foo/%2e.txt'); // %2e -> .
515528
assertFileUri('/foo/%A0.txt'); // %A0 -> invalid
@@ -520,4 +533,64 @@ suite('URI', () => {
520533
assertFileUri('/c\\win\\path');
521534
}
522535
});
536+
537+
test('URI.fsPath and fileURLToPath', function () {
538+
assertFsPath('file:///foo/bar');
539+
assertFsPath('file:///fo%25/bar');
540+
assertFsPath('file:///foo/b ar/text.cs');
541+
assertFsPath('file:///foö/bar');
542+
assertFsPath('file:///fo%C3%B6/bar');
543+
assertFsPath('file:///');
544+
545+
// assertFsPath('file:///fo%2f/bar'); not allowed in nodejs
546+
547+
if (isWindows) {
548+
// nodejs doesn't create UNC-paths on non-windows
549+
assertFsPath('file://unc-host/foö/bar');
550+
assertFsPath('file://unc-host/');
551+
552+
// nodejs prepends c: with / on non-windows
553+
assertFsPath('file:///c:/bar/foo');
554+
}
555+
556+
if (!isWindows) {
557+
//
558+
assertFsPath('file:///foo%5cbar');
559+
assertFsPath('file:///foo%5Cbar');
560+
assertFsPath('file:///foo%5C%5cbar');
561+
}
562+
});
563+
564+
function assertToString(uri: string): void {
565+
const actual = URI.parse(uri).toString();
566+
const expected = new URL(uri).href;
567+
assert.equal(actual, expected);
568+
}
569+
570+
test('URI.toString and URL.href', function () {
571+
assertToString('before:some/file/path');
572+
assertToString('scheme://authority/path');
573+
assertToString('scheme:/path');
574+
assertToString('foo:bar/path');
575+
// assertToString('http:/api/files/test.me?t=1234'); // URL make api the hostname,
576+
assertToString('http://api/files/test.me?t=1234');
577+
// assertToString('file:///c:/test/me'); // we encode the colon
578+
// assertToString('file:///c:/Source/Z%C3%BCrich%20or%20Zurich%20(%CB%88zj%CA%8A%C9%99r%C9%AAk,/Code/resources/app/plugins/c%23/plugin.json');
579+
// assertToString('file:///c:/test %25/path');
580+
assertToString('file://shares/files/c%23/p.cs');
581+
assertToString('inmemory:');
582+
assertToString('foo:api/files/test');
583+
assertToString('file:?q');
584+
assertToString('file:#d');
585+
assertToString('f3ile:#d');
586+
assertToString('foo+bar:path');
587+
assertToString('foo-bar:path');
588+
assertToString('foo.bar:path');
589+
assertToString('file:///_:/path');
590+
assertToString('https://firebasestorage.googleapis.com/v0/b/brewlangerie.appspot.com/o/products%2FzVNZkudXJyq8bPGTXUxx%2FBetterave-Sesame.jpg?alt=media&token=0b2310c4-3ea6-4207-bbde-9c3710ba0437');
591+
assertToString('https://myhost.com/Redirect?url=http%3A%2F%2Fwww.bing.com%3Fsearch%3Dtom');
592+
assertToString('debug:internalModule.js?session=aDebugSessionId&ref=11');
593+
assertToString('debug:internalModule.js?session%3DaDebugSessionId%26ref%3D11');
594+
assertToString('https://github.com/microsoft/vscode/issues/33746#issuecomment-545345356');
595+
});
523596
});

0 commit comments

Comments
 (0)