Skip to content

Commit 01f66eb

Browse files
committed
refactor gix-url tests
1 parent 530257f commit 01f66eb

File tree

12 files changed

+26
-11
lines changed

12 files changed

+26
-11
lines changed

gix-url/src/parse.rs

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -114,19 +114,26 @@ pub(crate) fn url(input: &BStr, protocol_end: usize) -> Result<crate::Url, Error
114114
Ok(crate::Url {
115115
serialize_alternative_form: false,
116116
scheme,
117-
user: url_user(&url),
118-
password: url.password().map(percent_decoded_utf8),
117+
user: url_user(&url, UrlKind::Url)?,
118+
password: url
119+
.password()
120+
.map(|s| percent_decoded_utf8(s, UrlKind::Url))
121+
.transpose()?,
119122
host: url.host_str().map(Into::into),
120123
port: url.port(),
121124
path: url.path().into(),
122125
})
123126
}
124127

125-
fn percent_decoded_utf8(s: &str) -> String {
126-
percent_decode_str(s)
128+
fn percent_decoded_utf8(s: &str, kind: UrlKind) -> Result<String, Error> {
129+
Ok(percent_decode_str(s)
127130
.decode_utf8()
128-
.expect("it's not possible to sneak illegal UTF8 into a URL")
129-
.into_owned()
131+
.map_err(|err| Error::Utf8 {
132+
url: s.into(),
133+
kind,
134+
source: err,
135+
})?
136+
.into_owned())
130137
}
131138

132139
pub(crate) fn scp(input: &BStr, colon: usize) -> Result<crate::Url, Error> {
@@ -157,19 +164,22 @@ pub(crate) fn scp(input: &BStr, colon: usize) -> Result<crate::Url, Error> {
157164
Ok(crate::Url {
158165
serialize_alternative_form: true,
159166
scheme: url.scheme().into(),
160-
user: url_user(&url),
161-
password: url.password().map(percent_decoded_utf8),
167+
user: url_user(&url, UrlKind::Scp)?,
168+
password: url
169+
.password()
170+
.map(|s| percent_decoded_utf8(s, UrlKind::Scp))
171+
.transpose()?,
162172
host: url.host_str().map(Into::into),
163173
port: url.port(),
164174
path: path.into(),
165175
})
166176
}
167177

168-
fn url_user(url: &url::Url) -> Option<String> {
178+
fn url_user(url: &url::Url, kind: UrlKind) -> Result<Option<String>, Error> {
169179
if url.username().is_empty() && url.password().is_none() {
170-
None
180+
Ok(None)
171181
} else {
172-
Some(percent_decoded_utf8(url.username()))
182+
Ok(Some(percent_decoded_utf8(url.username(), kind)?))
173183
}
174184
}
175185

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
BBi%BBBBB,@}:m\
File renamed without changes.

gix-url/tests/baseline.rs renamed to gix-url/tests/url/baseline.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ fn assert_urls_equal(expected: &baseline::GitDiagUrl<'_>, actual: &gix_url::Url)
164164
assert_eq!(actual.path, expected.path.unwrap_or_default());
165165
}
166166

167+
#[allow(clippy::module_inception)]
167168
mod baseline {
168169
use bstr::{BStr, BString, ByteSlice};
169170
use gix_testtools::once_cell::sync::Lazy;
File renamed without changes.

gix-url/tests/fuzzed.rs renamed to gix-url/tests/url/fuzzed.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use bstr::ByteSlice;
55
#[test]
66
fn fuzzed() {
77
for name in [
8+
"illegal-utf8",
89
"short-panic",
910
"very-long-abort2",
1011
"very-long-abort",

gix-url/tests/url.rs renamed to gix-url/tests/url/main.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,7 @@ pub type Error = Box<dyn std::error::Error>;
22
pub type Result = std::result::Result<(), Error>;
33

44
mod access;
5+
mod baseline;
56
mod expand_path;
7+
mod fuzzed;
68
mod parse;
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)