-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.rs
93 lines (84 loc) · 2.16 KB
/
utils.rs
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
use url::Url;
/// Url has some rather irritatingly precise ideas about joining URLs together:
///
/// "Note: a trailing slash is significant. Without it, the last path component is considered to
/// be a “file” name to be removed to get at the “directory” that is used as the base"
///
/// That means that if a user specifies a log URL with a path that doesn't end in a slash, the
/// URLs we produce will be broken. Rather than try to educate users on the vagaries of URL
/// handling, we'll just add a slash ourselves if necessary.
#[must_use]
pub fn fix_url(mut url: Url) -> Url {
if !url.path().ends_with('/') {
url.set_path(&format!("{}/", url.path()));
};
url
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn fixed_url_mangles_unslashed_base_domain() {
assert_eq!(
"https://example.com/",
fix_url(Url::parse("https://example.com").unwrap()).to_string()
);
}
#[test]
fn fixed_url_leaves_slashed_base_domain() {
assert_eq!(
"https://example.com/",
fix_url(Url::parse("https://example.com/").unwrap()).to_string()
);
}
#[test]
fn fixed_url_joins_to_base_domain_correctly() {
assert_eq!(
"https://example.com/foo/bar/baz",
fix_url(Url::parse("https://example.com").unwrap())
.join("foo/bar/baz")
.unwrap()
.to_string()
);
}
#[test]
fn fixed_url_joins_to_slashed_base_domain_correctly() {
assert_eq!(
"https://example.com/foo/bar/baz",
fix_url(Url::parse("https://example.com/").unwrap())
.join("foo/bar/baz")
.unwrap()
.to_string()
);
}
#[test]
fn fixed_url_joins_to_pathed_domain_correctly() {
assert_eq!(
"https://example.com/foo/bar/baz",
fix_url(Url::parse("https://example.com/foo/bar").unwrap())
.join("baz")
.unwrap()
.to_string()
);
}
#[test]
fn fixed_url_joins_to_slashed_pathed_domain_correctly() {
assert_eq!(
"https://example.com/foo/bar/baz",
fix_url(Url::parse("https://example.com/foo/bar/").unwrap())
.join("baz")
.unwrap()
.to_string()
);
}
#[test]
fn crate_example() {
assert_eq!(
"https://example.net/a/b/c.png",
fix_url(Url::parse("https://example.net/a/b/").unwrap())
.join("c.png")
.unwrap()
.to_string()
);
}
}