Skip to content

Commit 5c6021d

Browse files
authored
Merge pull request #183 from ayame113/http-scheme-encode
2 parents 2145ca9 + b32cadd commit 5c6021d

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

lib/convert.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ export default class Convert {
1717
* @returns The Uri corresponding to the path. e.g. file:///a/b/c.txt
1818
*/
1919
public static pathToUri(filePath: string): string {
20+
if (new URL(filePath, "file://").protocol !== "file:") {
21+
return filePath
22+
}
2023
let newPath = filePath.replace(/\\/g, "/")
2124
if (newPath[0] !== "/") {
2225
newPath = `/${newPath}`

test/convert.test.ts

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,18 @@ describe("Convert", () => {
2525
})
2626

2727
describe("pathToUri", () => {
28+
it("does not convert path other than file:", () => {
29+
expect(Convert.pathToUri("http://atom.io/a")).toBe("http://atom.io/a")
30+
expect(Convert.pathToUri("https://atom.io/b")).toBe("https://atom.io/b")
31+
expect(Convert.pathToUri("deno:/hello.js")).toBe("deno:/hello.js")
32+
})
33+
34+
it("does not convert non-alphanumeric path other than file:", () => {
35+
expect(Convert.pathToUri("http://atom.io/a%40%E3%81%82")).toBe("http://atom.io/a%40%E3%81%82")
36+
expect(Convert.pathToUri("https://atom.io/b?foo=bar")).toBe("https://atom.io/b?foo=bar")
37+
expect(Convert.pathToUri("deno:/hello%40%E3%81%82.js")).toBe("deno:/hello%40%E3%81%82.js")
38+
})
39+
2840
it("prefixes an absolute path with file://", () => {
2941
expect(Convert.pathToUri("/a/b/c/d.txt")).toBe("file:///a/b/c/d.txt")
3042
})
@@ -38,6 +50,11 @@ describe("Convert", () => {
3850
})
3951

4052
it("does not encode Windows drive specifiers", () => {
53+
// This test only succeeds on windows. (Because of the difference in the processing method of drive characters)
54+
// However, it is enough to test the windows drive character only on windows.
55+
if (process.platform !== "win32") {
56+
pending("Only test on windows")
57+
}
4158
expect(Convert.pathToUri("d:\\ee\\ff.txt")).toBe("file:///d:/ee/ff.txt")
4259
})
4360

@@ -47,10 +64,18 @@ describe("Convert", () => {
4764
})
4865

4966
describe("uriToPath", () => {
50-
it("does not convert http: and https: uri's", () => {
67+
it("does not convert uri other than file:", () => {
5168
setProcessPlatform("darwin")
5269
expect(Convert.uriToPath("http://atom.io/a")).toBe("http://atom.io/a")
5370
expect(Convert.uriToPath("https://atom.io/b")).toBe("https://atom.io/b")
71+
expect(Convert.uriToPath("deno:/hello.js")).toBe("deno:/hello.js")
72+
})
73+
74+
it("does not convert non-alphanumeric uri other than file:", () => {
75+
setProcessPlatform("darwin")
76+
expect(Convert.uriToPath("http://atom.io/a%40%E3%81%82")).toBe("http://atom.io/a%40%E3%81%82")
77+
expect(Convert.uriToPath("https://atom.io/b?foo=bar")).toBe("https://atom.io/b?foo=bar")
78+
expect(Convert.uriToPath("deno:/hello%40%E3%81%82.js")).toBe("deno:/hello%40%E3%81%82.js")
5479
})
5580

5681
it("converts a file:// path to an absolute path", () => {

0 commit comments

Comments
 (0)