1 package urlx
2
3 import (
4 "runtime"
5 "testing"
6
7 "github.com/stretchr/testify/assert"
8 )
9
10 func TestGetURLFilePath(t *testing.T) {
11 type testData struct {
12 urlStr string
13 expectedUnix string
14 expectedWindows string
15 shouldSucceed bool
16 }
17 var testURLs = []testData{
18 {"File:///home/test/file1.txt", "/home/test/file1.txt", "\\home\\test\\file1.txt", true},
19 {"fIle:/home/test/file2.txt", "/home/test/file2.txt", "\\home\\test\\file2.txt", true},
20 {"fiLe:///../test/update/file3.txt", "/../test/update/file3.txt", "\\..\\test\\update\\file3.txt", true},
21 {"filE://../test/update/file4.txt", "../test/update/file4.txt", "..\\test\\update\\file4.txt", true},
22 {"file://C:/users/test/file5.txt", "/C:/users/test/file5.txt", "C:\\users\\test\\file5.txt", true},
23 {"file:///C:/users/test/file5b.txt", "/C:/users/test/file5b.txt", "C:\\users\\test\\file5b.txt", true},
24 {"file://anotherhost/share/users/test/file6.txt", "/share/users/test/file6.txt", "\\\\anotherhost\\share\\users\\test\\file6.txt", false},
25 {"file://file7.txt", "file7.txt", "file7.txt", true},
26 {"file://path/file8.txt", "path/file8.txt", "path\\file8.txt", true},
27 {"file://C:\\Users\\RUNNER~1\\AppData\\Local\\Temp\\9ccf9f68-121c-451a-8a73-2aa360925b5a386398343/access-rules.json", "/C:\\Users\\RUNNER~1\\AppData\\Local\\Temp\\9ccf9f68-121c-451a-8a73-2aa360925b5a386398343/access-rules.json", "C:\\Users\\RUNNER~1\\AppData\\Local\\Temp\\9ccf9f68-121c-451a-8a73-2aa360925b5a386398343\\access-rules.json", true},
28 {"file:///C:\\Users\\RUNNER~1\\AppData\\Local\\Temp\\9ccf9f68-121c-451a-8a73-2aa360925b5a386398343/access-rules.json", "/C:\\Users\\RUNNER~1\\AppData\\Local\\Temp\\9ccf9f68-121c-451a-8a73-2aa360925b5a386398343/access-rules.json", "C:\\Users\\RUNNER~1\\AppData\\Local\\Temp\\9ccf9f68-121c-451a-8a73-2aa360925b5a386398343\\access-rules.json", true},
29 {"file8.txt", "file8.txt", "file8.txt", true},
30 {"../file9.txt", "../file9.txt", "..\\file9.txt", true},
31 {"./file9b.txt", "./file9b.txt", ".\\file9b.txt", true},
32 {"file://./file9c.txt", "./file9c.txt", ".\\file9c.txt", true},
33 {"file://./folder/.././file9d.txt", "./folder/.././file9d.txt", ".\\folder\\..\\.\\file9d.txt", true},
34 {"..\\file10.txt", "..\\file10.txt", "..\\file10.txt", true},
35 {"C:\\file11.txt", "/C:\\file11.txt", "C:\\file11.txt", true},
36 {"\\\\hostname\\share\\file12.txt", "/share/file12.txt", "\\\\hostname\\share\\file12.txt", true},
37 {"file:///home/test/file 13.txt", "/home/test/file 13.txt", "\\home\\test\\file 13.txt", true},
38 {"file:///home/test/file%2014.txt", "/home/test/file 14.txt", "\\home\\test\\file 14.txt", true},
39 {"http://server:80/test/file%2015.txt", "/test/file 15.txt", "/test/file 15.txt", true},
40 {"file:///dir/file\\ with backslash", "/dir/file\\ with backslash", "\\dir\\file\\ with backslash", true},
41 {"file://dir/file\\ with backslash", "dir/file\\ with backslash", "dir\\file\\ with backslash", true},
42 {"file:///dir/file with windows path forbidden chars \\<>:\"|%3F*", "/dir/file with windows path forbidden chars \\<>:\"|?*", "\\dir\\file with windows path forbidden chars \\<>:\"|?*", true},
43 {"file://dir/file with windows path forbidden chars \\<>:\"|%3F*", "dir/file with windows path forbidden chars \\<>:\"|?*", "dir\\file with windows path forbidden chars \\<>:\"|?*", true},
44 {"file:///path/file?query=1", "/path/file", "\\path\\file", true},
45 {"http://host:80/path/file?query=1", "/path/file", "/path/file", true},
46 {"file://////C:/file.txt", "////C:/file.txt", "C:\\file.txt", true},
47 {"file://////C:\\file.txt", "////C:\\file.txt", "C:\\file.txt", true},
48 }
49 for _, td := range testURLs {
50 u, err := Parse(td.urlStr)
51 assert.NoError(t, err)
52 if err != nil {
53 continue
54 }
55 p := GetURLFilePath(u)
56 if runtime.GOOS == "windows" {
57 if td.shouldSucceed {
58 assert.Equal(t, td.expectedWindows, p)
59 } else {
60 assert.NotEqual(t, td.expectedWindows, p)
61 }
62 } else {
63 if td.shouldSucceed {
64 assert.Equal(t, td.expectedUnix, p)
65 } else {
66 assert.NotEqual(t, td.expectedUnix, p)
67 }
68 }
69 }
70 assert.Empty(t, GetURLFilePath(nil))
71 }
72
View as plain text