-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathuri_normalize_test.dart
154 lines (132 loc) · 4.51 KB
/
uri_normalize_test.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import "package:expect/expect.dart";
testNormalizePath() {
test(String expected, String path, {String? scheme, String? host}) {
var uri = new Uri(scheme: scheme, host: host, path: path);
Expect.equals(expected, uri.toString());
if (scheme == null && host == null) {
Expect.equals(expected, uri.path);
}
}
var unreserved =
"-._~0123456789"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz";
test("A", "%41");
test("AB", "%41%42");
test("%40AB", "%40%41%42");
test("a", "%61");
test("ab", "%61%62");
test("%60ab", "%60%61%62");
test(unreserved, unreserved);
var x = new StringBuffer();
for (int i = 32; i < 128; i++) {
if (unreserved.indexOf(new String.fromCharCode(i)) != -1) {
x.writeCharCode(i);
} else {
x.write("%");
x.write(i.toRadixString(16));
}
}
Expect.equals(
x.toString().toUpperCase(),
new Uri(path: x.toString()).toString().toUpperCase(),
);
// Normalized paths.
// Full absolute path normalization for absolute paths.
test("/a/b/c/", "/../a/./b/z/../c/d/..");
test("/a/b/c/", "/./a/b/c/");
test("/a/b/c/", "/./../a/b/c/");
test("/a/b/c/", "/./../a/b/c/.");
test("/a/b/c/", "/./../a/b/c/z/./..");
test("/", "/a/..");
// Full absolute path normalization for URIs with scheme.
test("s:a/b/c/", "../a/./b/z/../c/d/..", scheme: "s");
test("s:a/b/c/", "./a/b/c/", scheme: "s");
test("s:a/b/c/", "./../a/b/c/", scheme: "s");
test("s:a/b/c/", "./../a/b/c/.", scheme: "s");
test("s:a/b/c/", "./../a/b/c/z/./..", scheme: "s");
test("s:/", "/a/..", scheme: "s");
test("s:/", "a/..", scheme: "s");
// Full absolute path normalization for URIs with authority.
test("//h/a/b/c/", "../a/./b/z/../c/d/..", host: "h");
test("//h/a/b/c/", "./a/b/c/", host: "h");
test("//h/a/b/c/", "./../a/b/c/", host: "h");
test("//h/a/b/c/", "./../a/b/c/.", host: "h");
test("//h/a/b/c/", "./../a/b/c/z/./..", host: "h");
test("//h/", "/a/..", host: "h");
test("//h/", "a/..", host: "h");
// Partial relative normalization (allowing leading .. or ./ for current dir).
test("../a/b/c/", "../a/./b/z/../c/d/..");
test("a/b/c/", "./a/b/c/");
test("../a/b/c/", "./../a/b/c/");
test("../a/b/c/", "./../a/b/c/.");
test("../a/b/c/", "./../a/b/c/z/./..");
test("/", "/a/..");
test("./", "a/..");
}
void testNormalizeResolve() {
var uri = Uri.parse("scheme://user:pass@example.com:1/path/?query#fragment");
var nonCanon = NonCanonicalizingUri();
nonCanon
..fragment = "fr%61gment"
..hasFragment = true;
Expect.equals(uri, uri.resolveUri(nonCanon));
nonCanon
..query = "qu%65ry"
..hasQuery = true;
Expect.equals(uri, uri.resolveUri(nonCanon));
nonCanon..path = "/p%61th/";
Expect.equals(uri, uri.resolveUri(nonCanon));
nonCanon..path = "../p%61th/";
Expect.equals(uri, uri.resolveUri(nonCanon));
nonCanon
..hasAuthority = true
..hasUserInfo = true
..userInfo = "us%65r:pass"
..host = "ex%41mple.com"
..hasPort = true
..port = 1;
Expect.equals(uri, uri.resolveUri(nonCanon));
nonCanon
..hasScheme = true
..scheme = "schEme";
Expect.equals(uri, uri.resolveUri(nonCanon));
}
main() {
testNormalizePath();
testNormalizeResolve();
}
class NonCanonicalizingUri implements Uri {
String scheme = "";
String userInfo = "";
String host = "";
int port = 0;
String path = "";
String query = "";
String fragment = "";
bool hasScheme = false;
bool hasAuthority = false;
bool hasUserInfo = false;
bool hasPort = false;
bool hasQuery = false;
bool hasFragment = false;
bool get hasEmptyPath => path.isEmpty;
bool get hasAbsolutePath => path.startsWith("/") || isScheme("file");
bool isScheme(String scheme) =>
scheme.toLowerCase() == Uri.decodeComponent(this.scheme).toLowerCase();
Uri normalize() => this; // Na-ah!
List<String> get pathSegments => path.split("/");
String toString() =>
"${hasScheme ? "$scheme:" : ""}"
"${hasAuthority ? "//${hasUserInfo ? "$userInfo@" : ""}"
"$host"
"${hasPort ? ":$port" : ""}" : ""}"
"$path${hasQuery ? "?$query" : ""}${hasFragment ? "#$fragment" : ""}";
int get hashCode => toString().hashCode;
bool operator ==(Object other) =>
other is Uri && toString() == other.toString();
Object? noSuchMethod(i) => super.noSuchMethod(i);
}