Skip to content

Commit c430d5e

Browse files
committed
test: add tests for getters
1 parent c00f47d commit c430d5e

File tree

1 file changed

+38
-17
lines changed

1 file changed

+38
-17
lines changed

src/lib.rs

Lines changed: 38 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,22 @@ impl Drop for Url {
125125
}
126126

127127
impl Url {
128+
pub fn parse<U: AsRef<str>>(url: U) -> Result<Url, Error> {
129+
let url_with_0_terminate = std::ffi::CString::new(url.as_ref()).unwrap();
130+
unsafe {
131+
let url_aggregator = ffi::ada_parse(url_with_0_terminate.as_ptr());
132+
133+
if ffi::ada_is_valid(url_aggregator) {
134+
Ok(Url {
135+
origin: None,
136+
url: url_aggregator,
137+
})
138+
} else {
139+
Err(Error::ParseUrl(url.as_ref().to_owned()))
140+
}
141+
}
142+
}
143+
128144
pub fn can_parse(input: &str, base: Option<&str>) -> bool {
129145
unsafe {
130146
ffi::ada_can_parse(
@@ -182,28 +198,33 @@ impl Url {
182198
}
183199
}
184200

185-
pub fn parse<U: AsRef<str>>(url: U) -> Result<Url, Error> {
186-
let url_with_0_terminate = std::ffi::CString::new(url.as_ref()).unwrap();
187-
unsafe {
188-
let url_aggregator = ffi::ada_parse(url_with_0_terminate.as_ptr());
189-
190-
if ffi::ada_is_valid(url_aggregator) {
191-
Ok(Url {
192-
origin: None,
193-
url: url_aggregator,
194-
})
195-
} else {
196-
Err(Error::ParseUrl(url.as_ref().to_owned()))
197-
}
198-
}
199-
}
200-
201201
#[cfg(test)]
202202
mod test {
203203
use super::*;
204204

205205
#[test]
206206
fn should_parse_simple_url() {
207-
assert!(parse("https://google.com").is_ok());
207+
let mut out = Url::parse("https://username:[email protected]:9090/search?query#hash")
208+
.expect("Should have parsed a simple url");
209+
assert_eq!(out.origin(), "https://google.com:9090");
210+
assert_eq!(
211+
out.href(),
212+
"https://username:[email protected]:9090/search?query#hash"
213+
);
214+
assert_eq!(out.username(), "username");
215+
assert_eq!(out.password(), "password");
216+
assert_eq!(out.port(), "9090");
217+
assert_eq!(out.hash(), "#hash");
218+
assert_eq!(out.host(), "google.com:9090");
219+
assert_eq!(out.hostname(), "google.com");
220+
assert_eq!(out.pathname(), "/search");
221+
assert_eq!(out.search(), "?query");
222+
assert_eq!(out.protocol(), "https:");
223+
}
224+
225+
#[test]
226+
fn can_parse_simple_url() {
227+
assert!(Url::can_parse("https://google.com", None));
228+
assert!(Url::can_parse("/helo", Some("https://www.google.com")));
208229
}
209230
}

0 commit comments

Comments
 (0)