uri.test.ts 23.8 KB
Newer Older
E
Erich Gamma 已提交
1 2 3 4 5
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/
import * as assert from 'assert';
J
Johannes Rieken 已提交
6
import { URI, UriComponents } from 'vs/base/common/uri';
7
import { isWindows } from 'vs/base/common/platform';
8

E
Erich Gamma 已提交
9 10 11

suite('URI', () => {
	test('file#toString', () => {
J
Johannes Rieken 已提交
12 13 14 15
		assert.equal(URI.file('c:/win/path').toString(), 'file:///c%3A/win/path');
		assert.equal(URI.file('C:/win/path').toString(), 'file:///c%3A/win/path');
		assert.equal(URI.file('c:/win/path/').toString(), 'file:///c%3A/win/path/');
		assert.equal(URI.file('/c:/win/path').toString(), 'file:///c%3A/win/path');
E
Erich Gamma 已提交
16 17
	});

18 19
	test('URI.file (win-special)', () => {
		if (isWindows) {
J
Johannes Rieken 已提交
20 21
			assert.equal(URI.file('c:\\win\\path').toString(), 'file:///c%3A/win/path');
			assert.equal(URI.file('c:\\win/path').toString(), 'file:///c%3A/win/path');
22 23 24
		} else {
			assert.equal(URI.file('c:\\win\\path').toString(), 'file:///c%3A%5Cwin%5Cpath');
			assert.equal(URI.file('c:\\win/path').toString(), 'file:///c%3A%5Cwin/path');
J
Johannes Rieken 已提交
25

26 27 28 29 30 31 32 33 34 35 36 37
		}
	});

	test('file#fsPath (win-special)', () => {
		if (isWindows) {
			assert.equal(URI.file('c:\\win\\path').fsPath, 'c:\\win\\path');
			assert.equal(URI.file('c:\\win/path').fsPath, 'c:\\win\\path');

			assert.equal(URI.file('c:/win/path').fsPath, 'c:\\win\\path');
			assert.equal(URI.file('c:/win/path/').fsPath, 'c:\\win\\path\\');
			assert.equal(URI.file('C:/win/path').fsPath, 'c:\\win\\path');
			assert.equal(URI.file('/c:/win/path').fsPath, 'c:\\win\\path');
38
			assert.equal(URI.file('./c/win/path').fsPath, '\\.\\c\\win\\path');
39 40 41 42 43
		} else {
			assert.equal(URI.file('c:/win/path').fsPath, 'c:/win/path');
			assert.equal(URI.file('c:/win/path/').fsPath, 'c:/win/path/');
			assert.equal(URI.file('C:/win/path').fsPath, 'c:/win/path');
			assert.equal(URI.file('/c:/win/path').fsPath, 'c:/win/path');
44
			assert.equal(URI.file('./c/win/path').fsPath, '/./c/win/path');
45
		}
E
Erich Gamma 已提交
46 47
	});

48 49 50
	test('URI#fsPath - no `fsPath` when no `path`', () => {
		const value = URI.parse('file://%2Fhome%2Fticino%2Fdesktop%2Fcpluscplus%2Ftest.cpp');
		assert.equal(value.authority, '/home/ticino/desktop/cpluscplus/test.cpp');
51
		assert.equal(value.path, '/');
J
Johannes Rieken 已提交
52 53 54 55 56
		if (isWindows) {
			assert.equal(value.fsPath, '\\');
		} else {
			assert.equal(value.fsPath, '/');
		}
57 58
	});

E
Erich Gamma 已提交
59
	test('http#toString', () => {
60 61 62
		assert.equal(URI.from({ scheme: 'http', authority: 'www.msft.com', path: '/my/path' }).toString(), 'http://www.msft.com/my/path');
		assert.equal(URI.from({ scheme: 'http', authority: 'www.msft.com', path: '/my/path' }).toString(), 'http://www.msft.com/my/path');
		assert.equal(URI.from({ scheme: 'http', authority: 'www.MSFT.com', path: '/my/path' }).toString(), 'http://www.msft.com/my/path');
63
		assert.equal(URI.from({ scheme: 'http', authority: '', path: 'my/path' }).toString(), 'http:/my/path');
64
		assert.equal(URI.from({ scheme: 'http', authority: '', path: '/my/path' }).toString(), 'http:/my/path');
65
		//http://a-test-site.com/#test=true
66 67
		assert.equal(URI.from({ scheme: 'http', authority: 'a-test-site.com', path: '/', query: 'test=true' }).toString(), 'http://a-test-site.com/?test%3Dtrue');
		assert.equal(URI.from({ scheme: 'http', authority: 'a-test-site.com', path: '/', query: '', fragment: 'test=true' }).toString(), 'http://a-test-site.com/#test%3Dtrue');
68 69 70
	});

	test('http#toString, encode=FALSE', () => {
71 72
		assert.equal(URI.from({ scheme: 'http', authority: 'a-test-site.com', path: '/', query: 'test=true' }).toString(true), 'http://a-test-site.com/?test=true');
		assert.equal(URI.from({ scheme: 'http', authority: 'a-test-site.com', path: '/', query: '', fragment: 'test=true' }).toString(true), 'http://a-test-site.com/#test=true');
73
		assert.equal(URI.from({ scheme: 'http', path: '/api/files/test.me', query: 't=1234' }).toString(true), 'http:/api/files/test.me?t=1234');
74

M
Matt Bierner 已提交
75
		const value = URI.parse('file://shares/pröjects/c%23/#l12');
76 77 78 79
		assert.equal(value.authority, 'shares');
		assert.equal(value.path, '/pröjects/c#/');
		assert.equal(value.fragment, 'l12');
		assert.equal(value.toString(), 'file://shares/pr%C3%B6jects/c%23/#l12');
80
		assert.equal(value.toString(true), 'file://shares/pröjects/c%23/#l12');
81

M
Matt Bierner 已提交
82 83
		const uri2 = URI.parse(value.toString(true));
		const uri3 = URI.parse(value.toString());
84 85 86 87
		assert.equal(uri2.authority, uri3.authority);
		assert.equal(uri2.path, uri3.path);
		assert.equal(uri2.query, uri3.query);
		assert.equal(uri2.fragment, uri3.fragment);
E
Erich Gamma 已提交
88 89
	});

90
	test('with, identity', () => {
91 92
		let uri = URI.parse('foo:bar/path');

93
		let uri2 = uri.with(null!);
94
		assert.ok(uri === uri2);
95
		uri2 = uri.with(undefined!);
96 97 98 99 100 101 102
		assert.ok(uri === uri2);
		uri2 = uri.with({});
		assert.ok(uri === uri2);
		uri2 = uri.with({ scheme: 'foo', path: 'bar/path' });
		assert.ok(uri === uri2);
	});

103
	test('with, changes', () => {
104
		assert.equal(URI.parse('before:some/file/path').with({ scheme: 'after' }).toString(), 'after:some/file/path');
105 106 107 108 109 110
		assert.equal(URI.from({ scheme: 's' }).with({ scheme: 'http', path: '/api/files/test.me', query: 't=1234' }).toString(), 'http:/api/files/test.me?t%3D1234');
		assert.equal(URI.from({ scheme: 's' }).with({ scheme: 'http', authority: '', path: '/api/files/test.me', query: 't=1234', fragment: '' }).toString(), 'http:/api/files/test.me?t%3D1234');
		assert.equal(URI.from({ scheme: 's' }).with({ scheme: 'https', authority: '', path: '/api/files/test.me', query: 't=1234', fragment: '' }).toString(), 'https:/api/files/test.me?t%3D1234');
		assert.equal(URI.from({ scheme: 's' }).with({ scheme: 'HTTP', authority: '', path: '/api/files/test.me', query: 't=1234', fragment: '' }).toString(), 'HTTP:/api/files/test.me?t%3D1234');
		assert.equal(URI.from({ scheme: 's' }).with({ scheme: 'HTTPS', authority: '', path: '/api/files/test.me', query: 't=1234', fragment: '' }).toString(), 'HTTPS:/api/files/test.me?t%3D1234');
		assert.equal(URI.from({ scheme: 's' }).with({ scheme: 'boo', authority: '', path: '/api/files/test.me', query: 't=1234', fragment: '' }).toString(), 'boo:/api/files/test.me?t%3D1234');
E
Erich Gamma 已提交
111 112
	});

113
	test('with, remove components #8465', () => {
114 115 116 117 118 119 120 121 122
		assert.equal(URI.parse('scheme://authority/path').with({ authority: '' }).toString(), 'scheme:/path');
		assert.equal(URI.parse('scheme:/path').with({ authority: 'authority' }).with({ authority: '' }).toString(), 'scheme:/path');
		assert.equal(URI.parse('scheme:/path').with({ authority: 'authority' }).with({ authority: null }).toString(), 'scheme:/path');
		assert.equal(URI.parse('scheme:/path').with({ authority: 'authority' }).with({ path: '' }).toString(), 'scheme://authority');
		assert.equal(URI.parse('scheme:/path').with({ authority: 'authority' }).with({ path: null }).toString(), 'scheme://authority');
		assert.equal(URI.parse('scheme:/path').with({ authority: '' }).toString(), 'scheme:/path');
		assert.equal(URI.parse('scheme:/path').with({ authority: null }).toString(), 'scheme:/path');
	});

123
	test('with, validation', () => {
124 125 126 127 128 129 130
		let uri = URI.parse('foo:bar/path');
		assert.throws(() => uri.with({ scheme: 'fai:l' }));
		assert.throws(() => uri.with({ scheme: 'fäil' }));
		assert.throws(() => uri.with({ authority: 'fail' }));
		assert.throws(() => uri.with({ path: '//fail' }));
	});

E
Erich Gamma 已提交
131
	test('parse', () => {
M
Matt Bierner 已提交
132
		let value = URI.parse('http:/api/files/test.me?t=1234');
E
Erich Gamma 已提交
133 134 135 136 137 138 139 140 141 142 143 144 145
		assert.equal(value.scheme, 'http');
		assert.equal(value.authority, '');
		assert.equal(value.path, '/api/files/test.me');
		assert.equal(value.query, 't=1234');
		assert.equal(value.fragment, '');

		value = URI.parse('http://api/files/test.me?t=1234');
		assert.equal(value.scheme, 'http');
		assert.equal(value.authority, 'api');
		assert.equal(value.path, '/files/test.me');
		assert.equal(value.query, 't=1234');
		assert.equal(value.fragment, '');

J
Johannes Rieken 已提交
146
		value = URI.parse('file:///c:/test/me');
E
Erich Gamma 已提交
147 148 149 150 151
		assert.equal(value.scheme, 'file');
		assert.equal(value.authority, '');
		assert.equal(value.path, '/c:/test/me');
		assert.equal(value.fragment, '');
		assert.equal(value.query, '');
152
		assert.equal(value.fsPath, isWindows ? 'c:\\test\\me' : 'c:/test/me');
E
Erich Gamma 已提交
153 154 155 156 157 158 159

		value = URI.parse('file://shares/files/c%23/p.cs');
		assert.equal(value.scheme, 'file');
		assert.equal(value.authority, 'shares');
		assert.equal(value.path, '/files/c#/p.cs');
		assert.equal(value.fragment, '');
		assert.equal(value.query, '');
160
		assert.equal(value.fsPath, isWindows ? '\\\\shares\\files\\c#\\p.cs' : '//shares/files/c#/p.cs');
E
Erich Gamma 已提交
161 162

		value = URI.parse('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');
J
Johannes Rieken 已提交
163 164
		assert.equal(value.scheme, 'file');
		assert.equal(value.authority, '');
E
Erich Gamma 已提交
165
		assert.equal(value.path, '/c:/Source/Zürich or Zurich (ˈzjʊərɪk,/Code/resources/app/plugins/c#/plugin.json');
J
Johannes Rieken 已提交
166 167
		assert.equal(value.fragment, '');
		assert.equal(value.query, '');
E
Erich Gamma 已提交
168 169 170 171

		value = URI.parse('file:///c:/test %25/path');
		assert.equal(value.scheme, 'file');
		assert.equal(value.authority, '');
J
Johannes Rieken 已提交
172
		assert.equal(value.path, '/c:/test %/path');
E
Erich Gamma 已提交
173
		assert.equal(value.fragment, '');
J
Johannes Rieken 已提交
174
		assert.equal(value.query, '');
E
Erich Gamma 已提交
175 176 177 178 179 180 181 182

		value = URI.parse('inmemory:');
		assert.equal(value.scheme, 'inmemory');
		assert.equal(value.authority, '');
		assert.equal(value.path, '');
		assert.equal(value.query, '');
		assert.equal(value.fragment, '');

183 184
		value = URI.parse('foo:api/files/test');
		assert.equal(value.scheme, 'foo');
E
Erich Gamma 已提交
185 186 187 188 189
		assert.equal(value.authority, '');
		assert.equal(value.path, 'api/files/test');
		assert.equal(value.query, '');
		assert.equal(value.fragment, '');

J
Johannes Rieken 已提交
190 191 192
		value = URI.parse('file:?q');
		assert.equal(value.scheme, 'file');
		assert.equal(value.authority, '');
193
		assert.equal(value.path, '/');
J
Johannes Rieken 已提交
194 195 196 197 198 199
		assert.equal(value.query, 'q');
		assert.equal(value.fragment, '');

		value = URI.parse('file:#d');
		assert.equal(value.scheme, 'file');
		assert.equal(value.authority, '');
200
		assert.equal(value.path, '/');
J
Johannes Rieken 已提交
201 202
		assert.equal(value.query, '');
		assert.equal(value.fragment, 'd');
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

		value = URI.parse('f3ile:#d');
		assert.equal(value.scheme, 'f3ile');
		assert.equal(value.authority, '');
		assert.equal(value.path, '');
		assert.equal(value.query, '');
		assert.equal(value.fragment, 'd');

		value = URI.parse('foo+bar:path');
		assert.equal(value.scheme, 'foo+bar');
		assert.equal(value.authority, '');
		assert.equal(value.path, 'path');
		assert.equal(value.query, '');
		assert.equal(value.fragment, '');

		value = URI.parse('foo-bar:path');
		assert.equal(value.scheme, 'foo-bar');
		assert.equal(value.authority, '');
		assert.equal(value.path, 'path');
		assert.equal(value.query, '');
		assert.equal(value.fragment, '');

		value = URI.parse('foo.bar:path');
		assert.equal(value.scheme, 'foo.bar');
		assert.equal(value.authority, '');
		assert.equal(value.path, 'path');
		assert.equal(value.query, '');
		assert.equal(value.fragment, '');
J
Johannes Rieken 已提交
231
	});
E
Erich Gamma 已提交
232

J
Johannes Rieken 已提交
233
	test('parse, disallow //path when no authority', () => {
E
Erich Gamma 已提交
234
		assert.throws(() => URI.parse('file:////shares/files/p.cs'));
J
Johannes Rieken 已提交
235
	});
E
Erich Gamma 已提交
236

237 238
	test('URI#file, win-speciale', () => {
		if (isWindows) {
M
Matt Bierner 已提交
239
			let value = URI.file('c:\\test\\drive');
240
			assert.equal(value.path, '/c:/test/drive');
J
Johannes Rieken 已提交
241
			assert.equal(value.toString(), 'file:///c%3A/test/drive');
242 243 244 245 246 247 248 249 250 251 252 253

			value = URI.file('\\\\shäres\\path\\c#\\plugin.json');
			assert.equal(value.scheme, 'file');
			assert.equal(value.authority, 'shäres');
			assert.equal(value.path, '/path/c#/plugin.json');
			assert.equal(value.fragment, '');
			assert.equal(value.query, '');
			assert.equal(value.toString(), 'file://sh%C3%A4res/path/c%23/plugin.json');

			value = URI.file('\\\\localhost\\c$\\GitDevelopment\\express');
			assert.equal(value.scheme, 'file');
			assert.equal(value.path, '/c$/GitDevelopment/express');
J
Johannes Rieken 已提交
254
			assert.equal(value.fsPath, '\\\\localhost\\c$\\GitDevelopment\\express');
255 256
			assert.equal(value.query, '');
			assert.equal(value.fragment, '');
257
			assert.equal(value.toString(), 'file://localhost/c%24/GitDevelopment/express');
258 259 260

			value = URI.file('c:\\test with %\\path');
			assert.equal(value.path, '/c:/test with %/path');
J
Johannes Rieken 已提交
261
			assert.equal(value.toString(), 'file:///c%3A/test%20with%20%25/path');
262 263 264

			value = URI.file('c:\\test with %25\\path');
			assert.equal(value.path, '/c:/test with %25/path');
J
Johannes Rieken 已提交
265
			assert.equal(value.toString(), 'file:///c%3A/test%20with%20%2525/path');
266 267 268

			value = URI.file('c:\\test with %25\\c#code');
			assert.equal(value.path, '/c:/test with %25/c#code');
J
Johannes Rieken 已提交
269
			assert.equal(value.toString(), 'file:///c%3A/test%20with%20%2525/c%23code');
270 271 272 273 274 275 276 277 278 279 280 281

			value = URI.file('\\\\shares');
			assert.equal(value.scheme, 'file');
			assert.equal(value.authority, 'shares');
			assert.equal(value.path, '/'); // slash is always there

			value = URI.file('\\\\shares\\');
			assert.equal(value.scheme, 'file');
			assert.equal(value.authority, 'shares');
			assert.equal(value.path, '/');
		}
	});
282

J
Johannes Rieken 已提交
283 284
	test('VSCode URI module\'s driveLetterPath regex is incorrect, #32961', function () {
		let uri = URI.parse('file:///_:/path');
R
Ramya Achutha Rao 已提交
285
		assert.equal(uri.fsPath, isWindows ? '\\_:\\path' : '/_:/path');
J
Johannes Rieken 已提交
286 287
	});

288
	test('URI#file, no path-is-uri check', () => {
289 290

		// we don't complain here
291
		let value = URI.file('file://path/to/file');
292 293 294
		assert.equal(value.scheme, 'file');
		assert.equal(value.authority, '');
		assert.equal(value.path, '/file://path/to/file');
E
Erich Gamma 已提交
295 296
	});

297 298
	test('URI#file, always slash', () => {

M
Matt Bierner 已提交
299
		let value = URI.file('a.file');
300 301 302 303 304 305 306 307 308 309 310 311
		assert.equal(value.scheme, 'file');
		assert.equal(value.authority, '');
		assert.equal(value.path, '/a.file');
		assert.equal(value.toString(), 'file:///a.file');

		value = URI.parse(value.toString());
		assert.equal(value.scheme, 'file');
		assert.equal(value.authority, '');
		assert.equal(value.path, '/a.file');
		assert.equal(value.toString(), 'file:///a.file');
	});

E
Erich Gamma 已提交
312
	test('URI.toString, only scheme and query', () => {
313 314
		const value = URI.parse('stuff:?qüery');
		assert.equal(value.toString(), 'stuff:?q%C3%BCery');
E
Erich Gamma 已提交
315 316 317
	});

	test('URI#toString, upper-case percent espaces', () => {
318 319
		const value = URI.parse('file://sh%c3%a4res/path');
		assert.equal(value.toString(), 'file://sh%C3%A4res/path');
E
Erich Gamma 已提交
320 321
	});

322
	test('URI#toString, lower-case windows drive letter', () => {
323 324
		assert.equal(URI.parse('untitled:c:/Users/jrieken/Code/abc.txt').toString(), 'untitled:c%3A/Users/jrieken/Code/abc.txt');
		assert.equal(URI.parse('untitled:C:/Users/jrieken/Code/abc.txt').toString(), 'untitled:c%3A/Users/jrieken/Code/abc.txt');
325 326
	});

E
Erich Gamma 已提交
327
	test('URI#toString, escape all the bits', () => {
328

M
Matt Bierner 已提交
329
		const value = URI.file('/Users/jrieken/Code/_samples/18500/Mödel + Other Thîngß/model.js');
330
		assert.equal(value.toString(), 'file:///Users/jrieken/Code/_samples/18500/M%C3%B6del%20%2B%20Other%20Th%C3%AEng%C3%9F/model.js');
E
Erich Gamma 已提交
331 332 333
	});

	test('URI#toString, don\'t encode port', () => {
M
Matt Bierner 已提交
334
		let value = URI.parse('http://localhost:8080/far');
335
		assert.equal(value.toString(), 'http://localhost:8080/far');
E
Erich Gamma 已提交
336

337
		value = URI.from({ scheme: 'http', authority: 'löcalhost:8080', path: '/far', query: undefined, fragment: undefined });
338
		assert.equal(value.toString(), 'http://l%C3%B6calhost:8080/far');
E
Erich Gamma 已提交
339 340
	});

341
	test('URI#toString, user information in authority', () => {
342 343 344 345 346 347 348 349 350 351 352 353 354 355
		let value = URI.parse('http://foo:bar@localhost/far');
		assert.equal(value.toString(), 'http://foo:bar@localhost/far');

		value = URI.parse('http://foo@localhost/far');
		assert.equal(value.toString(), 'http://foo@localhost/far');

		value = URI.parse('http://foo:bAr@localhost:8080/far');
		assert.equal(value.toString(), 'http://foo:bAr@localhost:8080/far');

		value = URI.parse('http://foo@localhost:8080/far');
		assert.equal(value.toString(), 'http://foo@localhost:8080/far');

		value = URI.from({ scheme: 'http', authority: 'föö:bör@löcalhost:8080', path: '/far', query: undefined, fragment: undefined });
		assert.equal(value.toString(), 'http://f%C3%B6%C3%B6:b%C3%B6r@l%C3%B6calhost:8080/far');
356 357
	});

E
Erich Gamma 已提交
358 359
	test('correctFileUriToFilePath2', () => {

M
Matt Bierner 已提交
360 361
		const test = (input: string, expected: string) => {
			const value = URI.parse(input);
E
Erich Gamma 已提交
362
			assert.equal(value.fsPath, expected, 'Result for ' + input);
M
Matt Bierner 已提交
363
			const value2 = URI.file(value.fsPath);
E
Erich Gamma 已提交
364 365 366 367
			assert.equal(value2.fsPath, expected, 'Result for ' + input);
			assert.equal(value.toString(), value2.toString());
		};

368 369 370 371
		test('file:///c:/alex.txt', isWindows ? 'c:\\alex.txt' : 'c:/alex.txt');
		test('file:///c:/Source/Z%C3%BCrich%20or%20Zurich%20(%CB%88zj%CA%8A%C9%99r%C9%AAk,/Code/resources/app/plugins', isWindows ? 'c:\\Source\\Zürich or Zurich (ˈzjʊərɪk,\\Code\\resources\\app\\plugins' : 'c:/Source/Zürich or Zurich (ˈzjʊərɪk,/Code/resources/app/plugins');
		test('file://monacotools/folder/isi.txt', isWindows ? '\\\\monacotools\\folder\\isi.txt' : '//monacotools/folder/isi.txt');
		test('file://monacotools1/certificates/SSL/', isWindows ? '\\\\monacotools1\\certificates\\SSL\\' : '//monacotools1/certificates/SSL/');
E
Erich Gamma 已提交
372 373
	});

J
Johannes Rieken 已提交
374
	test('URI - http, query & toString', function () {
375 376 377 378

		let uri = URI.parse('https://go.microsoft.com/fwlink/?LinkId=518008');
		assert.equal(uri.query, 'LinkId=518008');
		assert.equal(uri.toString(true), 'https://go.microsoft.com/fwlink/?LinkId=518008');
379
		assert.equal(uri.toString(), 'https://go.microsoft.com/fwlink/?LinkId%3D518008');
380 381 382 383 384 385 386 387

		let uri2 = URI.parse(uri.toString());
		assert.equal(uri2.query, 'LinkId=518008');
		assert.equal(uri2.query, uri.query);

		uri = URI.parse('https://go.microsoft.com/fwlink/?LinkId=518008&foö&ké¥=üü');
		assert.equal(uri.query, 'LinkId=518008&foö&ké¥=üü');
		assert.equal(uri.toString(true), 'https://go.microsoft.com/fwlink/?LinkId=518008&foö&ké¥=üü');
388
		assert.equal(uri.toString(), 'https://go.microsoft.com/fwlink/?LinkId%3D518008%26fo%C3%B6%26k%C3%A9%C2%A5%3D%C3%BC%C3%BC');
389 390 391 392

		uri2 = URI.parse(uri.toString());
		assert.equal(uri2.query, 'LinkId=518008&foö&ké¥=üü');
		assert.equal(uri2.query, uri.query);
J
Johannes Rieken 已提交
393 394 395 396

		// #24849
		uri = URI.parse('https://twitter.com/search?src=typd&q=%23tag');
		assert.equal(uri.toString(true), 'https://twitter.com/search?src=typd&q=%23tag');
J
Johannes Rieken 已提交
397 398 399
	});


J
Johannes Rieken 已提交
400 401 402 403 404 405 406
	test('class URI cannot represent relative file paths #34449', function () {

		let path = '/foo/bar';
		assert.equal(URI.file(path).path, path);
		path = 'foo/bar';
		assert.equal(URI.file(path).path, '/foo/bar');
		path = './foo/bar';
407
		assert.equal(URI.file(path).path, '/./foo/bar'); // missing normalization
J
Johannes Rieken 已提交
408 409 410 411 412 413 414 415 416 417 418

		const fileUri1 = URI.parse(`file:foo/bar`);
		assert.equal(fileUri1.path, '/foo/bar');
		assert.equal(fileUri1.authority, '');
		const uri = fileUri1.toString();
		assert.equal(uri, 'file:///foo/bar');
		const fileUri2 = URI.parse(uri);
		assert.equal(fileUri2.path, '/foo/bar');
		assert.equal(fileUri2.authority, '');
	});

J
Johannes Rieken 已提交
419 420 421 422 423 424 425 426 427 428
	test('Ctrl click to follow hash query param url gets urlencoded #49628', function () {
		let input = 'http://localhost:3000/#/foo?bar=baz';
		let uri = URI.parse(input);
		assert.equal(uri.toString(true), input);

		input = 'http://localhost:3000/foo?bar=baz';
		uri = URI.parse(input);
		assert.equal(uri.toString(true), input);
	});

429 430
	test('Unable to open \'%A0.txt\': URI malformed #76506', function () {

431 432 433 434 435 436 437 438 439
		let uri = URI.file('/foo/%A0.txt');
		let uri2 = URI.parse(uri.toString());
		assert.equal(uri.scheme, uri2.scheme);
		assert.equal(uri.path, uri2.path);

		uri = URI.file('/foo/%2e.txt');
		uri2 = URI.parse(uri.toString());
		assert.equal(uri.scheme, uri2.scheme);
		assert.equal(uri.path, uri2.path);
440
	});
441

442 443 444 445
	test('Unable to open \'%A0.txt\': URI malformed #76506', function () {
		assert.equal(URI.parse('file://some/%.txt'), 'file://some/%25.txt');
		assert.equal(URI.parse('file://some/%A0.txt'), 'file://some/%25A0.txt');
	});
446

447
	test('Links in markdown are broken if url contains encoded parameters #79474', function () {
448
		this.skip();
449 450 451 452
		let strIn = 'https://myhost.com/Redirect?url=http%3A%2F%2Fwww.bing.com%3Fsearch%3Dtom';
		let uri1 = URI.parse(strIn);
		let strOut = uri1.toString();
		let uri2 = URI.parse(strOut);
453 454 455 456 457 458 459

		assert.equal(uri1.scheme, uri2.scheme);
		assert.equal(uri1.authority, uri2.authority);
		assert.equal(uri1.path, uri2.path);
		assert.equal(uri1.query, uri2.query);
		assert.equal(uri1.fragment, uri2.fragment);
		assert.equal(strIn, strOut); // fails here!!
460 461
	});

462
	test('Uri#parse can break path-component #45515', function () {
463
		this.skip();
464 465 466 467
		let strIn = 'https://firebasestorage.googleapis.com/v0/b/brewlangerie.appspot.com/o/products%2FzVNZkudXJyq8bPGTXUxx%2FBetterave-Sesame.jpg?alt=media&token=0b2310c4-3ea6-4207-bbde-9c3710ba0437';
		let uri1 = URI.parse(strIn);
		let strOut = uri1.toString();
		let uri2 = URI.parse(strOut);
468

469 470 471 472 473 474
		assert.equal(uri1.scheme, uri2.scheme);
		assert.equal(uri1.authority, uri2.authority);
		assert.equal(uri1.path, uri2.path);
		assert.equal(uri1.query, uri2.query);
		assert.equal(uri1.fragment, uri2.fragment);
		assert.equal(strIn, strOut); // fails here!!
475 476
	});

477
	test('URI - (de)serialize', function () {
E
Erich Gamma 已提交
478

M
Matt Bierner 已提交
479
		const values = [
E
Erich Gamma 已提交
480 481
			URI.parse('http://localhost:8080/far'),
			URI.file('c:\\test with %25\\c#code'),
482
			URI.file('\\\\shäres\\path\\c#\\plugin.json'),
E
Erich Gamma 已提交
483 484 485 486 487 488 489 490
			URI.parse('http://api/files/test.me?t=1234'),
			URI.parse('http://api/files/test.me?t=1234#fff'),
			URI.parse('http://api/files/test.me#fff'),
		];

		// console.profile();
		// let c = 100000;
		// while (c-- > 0) {
J
Johannes Rieken 已提交
491
		for (let value of values) {
J
Johannes Rieken 已提交
492
			let data = value.toJSON() as UriComponents;
493
			let clone = URI.revive(data);
E
Erich Gamma 已提交
494 495 496 497 498 499 500 501 502 503 504 505

			assert.equal(clone.scheme, value.scheme);
			assert.equal(clone.authority, value.authority);
			assert.equal(clone.path, value.path);
			assert.equal(clone.query, value.query);
			assert.equal(clone.fragment, value.fragment);
			assert.equal(clone.fsPath, value.fsPath);
			assert.equal(clone.toString(), value.toString());
		}
		// }
		// console.profileEnd();
	});
506 507 508 509 510 511 512 513 514
	function assertJoined(base: string, fragment: string, expected: string, checkWithUrl: boolean = true) {
		const baseUri = URI.parse(base);
		const newUri = URI.joinPath(baseUri, fragment);
		const actual = newUri.toString(true);
		assert.equal(actual, expected);

		if (checkWithUrl) {
			const actualUrl = new URL(fragment, base).href;
			assert.equal(actualUrl, expected, 'DIFFERENT from URL');
515
		}
516 517
	}
	test('URI#joinPath', function () {
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

		assertJoined(('file:///foo/'), '../../bazz', 'file:///bazz');
		assertJoined(('file:///foo'), '../../bazz', 'file:///bazz');
		assertJoined(('file:///foo'), '../../bazz', 'file:///bazz');
		assertJoined(('file:///foo/bar/'), './bazz', 'file:///foo/bar/bazz');
		assertJoined(('file:///foo/bar'), './bazz', 'file:///foo/bar/bazz', false);
		assertJoined(('file:///foo/bar'), 'bazz', 'file:///foo/bar/bazz', false);

		// "auto-path" scheme
		assertJoined(('file:'), 'bazz', 'file:///bazz');
		assertJoined(('http://domain'), 'bazz', 'http://domain/bazz');
		assertJoined(('https://domain'), 'bazz', 'https://domain/bazz');
		assertJoined(('http:'), 'bazz', 'http:/bazz', false);
		assertJoined(('https:'), 'bazz', 'https:/bazz', false);

		// no "auto-path" scheme with and w/o paths
		assertJoined(('foo:/'), 'bazz', 'foo:/bazz');
		assertJoined(('foo://bar/'), 'bazz', 'foo://bar/bazz');

		// no "auto-path" + no path -> error
		assert.throws(() => assertJoined(('foo:'), 'bazz', ''));
		assert.throws(() => new URL('bazz', 'foo:'));
		assert.throws(() => assertJoined(('foo://bar'), 'bazz', ''));
		assert.throws(() => new URL('bazz', 'foo://bar'));
	});
543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566

	test('URI#joinPath (posix)', function () {
		if (isWindows) {
			this.skip();
		}
		assertJoined(('file:///c:/foo/'), '../../bazz', 'file:///bazz', false);
		assertJoined(('file://server/share/c:/'), '../../bazz', 'file://server/bazz', false);
		assertJoined(('file://server/share/c:'), '../../bazz', 'file://server/bazz', false);

		assertJoined(('file://ser/foo/'), '../../bazz', 'file://ser/bazz');
		assertJoined(('file://ser/foo'), '../../bazz', 'file://ser/bazz');
	});

	test('URI#joinPath (windows)', function () {
		if (!isWindows) {
			this.skip();
		}
		assertJoined(('file:///c:/foo/'), '../../bazz', 'file:///c:/bazz', false);
		assertJoined(('file://server/share/c:/'), '../../bazz', 'file://server/share/bazz', false);
		assertJoined(('file://server/share/c:'), '../../bazz', 'file://server/share/bazz', false);

		assertJoined(('file://ser/foo/'), '../../bazz', 'file://ser/foo/bazz', false);
		assertJoined(('file://ser/foo'), '../../bazz', 'file://ser/foo/bazz', false);
	});
567
});