|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the VS Code Swift open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2024 the VS Code Swift project authors |
| 6 | +// Licensed under Apache License v2.0 |
| 7 | +// |
| 8 | +// See LICENSE.txt for license information |
| 9 | +// See CONTRIBUTORS.txt for the list of VS Code Swift project authors |
| 10 | +// |
| 11 | +// SPDX-License-Identifier: Apache-2.0 |
| 12 | +// |
| 13 | +//===----------------------------------------------------------------------===// |
| 14 | + |
| 15 | +import * as assert from "assert"; |
| 16 | +import * as vscode from "vscode"; |
| 17 | +import { uriConverters } from "../../../src/sourcekit-lsp/uriConverters"; |
| 18 | + |
| 19 | +/// Check that decoding the given URI string and re-encoding it results in the original string and that the decoded Uri |
| 20 | +/// does not cause any assertion failures in `verifyUri`. |
| 21 | +function checkUri(input: string, verifyUri: (uri: vscode.Uri) => void) { |
| 22 | + const uri = uriConverters.protocol2Code(input); |
| 23 | + verifyUri(uri); |
| 24 | + assert.equal(uriConverters.code2Protocol(uri), input); |
| 25 | +} |
| 26 | + |
| 27 | +suite.only("uriConverters Suite", () => { |
| 28 | + suite("Default Coding", () => { |
| 29 | + test("Space in host", () => { |
| 30 | + checkUri("file://host%20with%20space/", uri => { |
| 31 | + assert.equal(uri.authority, "host with space"); |
| 32 | + }); |
| 33 | + }); |
| 34 | + |
| 35 | + test("Space in path", () => { |
| 36 | + checkUri("file://host/with%20space", uri => { |
| 37 | + assert.equal(uri.path, "/with space"); |
| 38 | + }); |
| 39 | + }); |
| 40 | + |
| 41 | + test("Query does not round-trip", () => { |
| 42 | + // If this test starts passing, the underlying VS Code issue that requires us to have custom URI coding |
| 43 | + // has been fixed and we should be able to remove our custom uri converter. |
| 44 | + const uri = uriConverters.protocol2Code("scheme://host?outer=inner%3Dvalue"); |
| 45 | + assert.equal( |
| 46 | + uri.toString(/*skipEncoding*/ false), |
| 47 | + "scheme://host?outer%3Dinner%3Dvalue" |
| 48 | + ); |
| 49 | + assert.equal(uri.toString(/*skipEncoding*/ true), "scheme://host?outer=inner=value"); |
| 50 | + }); |
| 51 | + }); |
| 52 | + |
| 53 | + suite("Custom Coding", () => { |
| 54 | + test("Basic", () => { |
| 55 | + checkUri("sourcekit-lsp://host?outer=inner%3Dvalue", uri => { |
| 56 | + assert.equal(uri.query, "outer=inner%3Dvalue"); |
| 57 | + }); |
| 58 | + }); |
| 59 | + |
| 60 | + test("Percent-encoded hash in query", () => { |
| 61 | + checkUri("sourcekit-lsp://host?outer=with%23hash", uri => { |
| 62 | + assert.equal(uri.query, "outer=with%23hash"); |
| 63 | + }); |
| 64 | + }); |
| 65 | + |
| 66 | + test("Query and fragment", () => { |
| 67 | + checkUri("sourcekit-lsp://host?outer=with%23hash#fragment", uri => { |
| 68 | + assert.equal(uri.query, "outer=with%23hash"); |
| 69 | + assert.equal(uri.fragment, "fragment"); |
| 70 | + }); |
| 71 | + }); |
| 72 | + |
| 73 | + test("Percent encoding in host", () => { |
| 74 | + // Technically, it would be nice to percent-decode the authority and path here but then we get into |
| 75 | + // ambiguities around username in the authority (see the `Encoded '@' in host` test). |
| 76 | + // For now, rely on SourceKit-LSP not using any characters that need percent-encoding here. |
| 77 | + checkUri("sourcekit-lsp://host%20with%20space", uri => { |
| 78 | + assert.equal(uri.authority, "host%20with%20space"); |
| 79 | + }); |
| 80 | + }); |
| 81 | + |
| 82 | + test("Encoded '@' in host", () => { |
| 83 | + checkUri("sourcekit-lsp://user%40with-at@host%40with-at", uri => { |
| 84 | + assert.equal(uri.authority, "user%40with-at@host%40with-at"); |
| 85 | + }); |
| 86 | + }); |
| 87 | + |
| 88 | + test("Percent encoding in path", () => { |
| 89 | + checkUri("sourcekit-lsp://host/with%20space", uri => { |
| 90 | + assert.equal(uri.path, "/with%20space"); |
| 91 | + }); |
| 92 | + }); |
| 93 | + |
| 94 | + test("No query", () => { |
| 95 | + checkUri("sourcekit-lsp://host/with/path", uri => { |
| 96 | + assert.equal(uri.query, ""); |
| 97 | + }); |
| 98 | + }); |
| 99 | + |
| 100 | + test("With username", () => { |
| 101 | + checkUri("sourcekit-lsp://user@host", uri => { |
| 102 | + assert.equal(uri.authority, "user@host"); |
| 103 | + }); |
| 104 | + }); |
| 105 | + |
| 106 | + test("With username and password", () => { |
| 107 | + checkUri("sourcekit-lsp://user:pass@host", uri => { |
| 108 | + assert.equal(uri.authority, "user:pass@host"); |
| 109 | + }); |
| 110 | + }); |
| 111 | + }); |
| 112 | +}); |
0 commit comments