Debian Codegraph @

src/cmd/go/internal/web/url_windows_test.go

raw
1 // Copyright 2019 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package web
6
7 var urlTests = []struct {
8 url string
9 filePath string
10 canonicalURL string // If empty, assume equal to url.
11 wantErr string
12 }{
13 // Examples from https://blogs.msdn.microsoft.com/ie/2006/12/06/file-uris-in-windows/:
14
15 {
16 url: `file://laptop/My%20Documents/FileSchemeURIs.doc`,
17 filePath: `\\laptop\My Documents\FileSchemeURIs.doc`,
18 },
19 {
20 url: `file:///C:/Documents%20and%20Settings/davris/FileSchemeURIs.doc`,
21 filePath: `C:\Documents and Settings\davris\FileSchemeURIs.doc`,
22 },
23 {
24 url: `file:///D:/Program%20Files/Viewer/startup.htm`,
25 filePath: `D:\Program Files\Viewer\startup.htm`,
26 },
27 {
28 url: `file:///C:/Program%20Files/Music/Web%20Sys/main.html?REQUEST=RADIO`,
29 filePath: `C:\Program Files\Music\Web Sys\main.html`,
30 canonicalURL: `file:///C:/Program%20Files/Music/Web%20Sys/main.html`,
31 },
32 {
33 url: `file://applib/products/a-b/abc_9/4148.920a/media/start.swf`,
34 filePath: `\\applib\products\a-b\abc_9\4148.920a\media\start.swf`,
35 },
36 {
37 url: `file:////applib/products/a%2Db/abc%5F9/4148.920a/media/start.swf`,
38 wantErr: "file URL missing drive letter",
39 },
40 {
41 url: `C:\Program Files\Music\Web Sys\main.html?REQUEST=RADIO`,
42 wantErr: "non-file URL",
43 },
44
45 // The example "file://D:\Program Files\Viewer\startup.htm" errors out in
46 // url.Parse, so we substitute a slash-based path for testing instead.
47 {
48 url: `file://D:/Program Files/Viewer/startup.htm`,
49 wantErr: "file URL encodes volume in host field: too few slashes?",
50 },
51
52 // The blog post discourages the use of non-ASCII characters because they
53 // depend on the user's current codepage. However, when we are working with Go
54 // strings we assume UTF-8 encoding, and our url package refuses to encode
55 // URLs to non-ASCII strings.
56 {
57 url: `file:///C:/exampleㄓ.txt`,
58 filePath: `C:\exampleㄓ.txt`,
59 canonicalURL: `file:///C:/example%E3%84%93.txt`,
60 },
61 {
62 url: `file:///C:/example%E3%84%93.txt`,
63 filePath: `C:\exampleㄓ.txt`,
64 },
65
66 // Examples from RFC 8089:
67
68 // We allow the drive-letter variation from section E.2, because it is
69 // simpler to support than not to. However, we do not generate the shorter
70 // form in the reverse direction.
71 {
72 url: `file:c:/path/to/file`,
73 filePath: `c:\path\to\file`,
74 canonicalURL: `file:///c:/path/to/file`,
75 },
76
77 // We encode the UNC share name as the authority following section E.3.1,
78 // because that is what the Microsoft blog post explicitly recommends.
79 {
80 url: `file://host.example.com/Share/path/to/file.txt`,
81 filePath: `\\host.example.com\Share\path\to\file.txt`,
82 },
83
84 // We decline the four- and five-slash variations from section E.3.2.
85 // The paths in these URLs would change meaning under path.Clean.
86 {
87 url: `file:////host.example.com/path/to/file`,
88 wantErr: "file URL missing drive letter",
89 },
90 {
91 url: `file://///host.example.com/path/to/file`,
92 wantErr: "file URL missing drive letter",
93 },
94 }