Skip to content

Commit

Permalink
Unit tests both at the same time
Browse files Browse the repository at this point in the history
  • Loading branch information
trask committed Dec 1, 2024
1 parent 1ce48cf commit 393919e
Showing 1 changed file with 75 additions and 0 deletions.
75 changes: 75 additions & 0 deletions lychee-lib/src/utils/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,81 @@ mod tests {
.any(|r| r.uri.url.as_str() == "file:///some/page.html#fragment"));
}

#[test]
fn test_relative_url_resolution_from_root_path_and_base_url() {
let root_path = Some(PathBuf::from("/tmp/lychee"));
let base = Some(Base::try_from("https://example.com/path/page.html").unwrap());
let source = InputSource::FsPath(PathBuf::from("/some/page.html"));

let uris = vec![RawUri::from("relative.html")];
let requests = create(uris, &source, &root_path, &base, &None);

assert_eq!(requests.len(), 1);
assert!(requests
.iter()
.any(|r| r.uri.url.as_str() == "https://example.com/path/relative.html"));
}

#[test]
fn test_absolute_url_resolution_from_root_path_and_base_url() {
let root_path = Some(PathBuf::from("/tmp/lychee"));
let base = Some(Base::try_from("https://example.com/path/page.html").unwrap());
let source = InputSource::FsPath(PathBuf::from("/some/page.html"));

let uris = vec![RawUri::from("https://another.com/page")];
let requests = create(uris, &source, &root_path, &base, &None);

assert_eq!(requests.len(), 1);
assert!(requests
.iter()
.any(|r| r.uri.url.as_str() == "https://another.com/page"));
}

#[test]
fn test_root_relative_url_resolution_from_root_path_and_base_url() {
let root_path = Some(PathBuf::from("/tmp/lychee"));
let base = Some(Base::try_from("https://example.com/path/page.html").unwrap());
let source = InputSource::FsPath(PathBuf::from("/some/page.html"));

let uris = vec![RawUri::from("/root-relative")];
let requests = create(uris, &source, &root_path, &base, &None);

assert_eq!(requests.len(), 1);
assert!(requests
.iter()
.any(|r| r.uri.url.as_str() == "https://example.com/tmp/lychee/root-relative"));
}

#[test]
fn test_parent_directory_url_resolution_from_root_path_and_base_url() {
let root_path = Some(PathBuf::from("/tmp/lychee"));
let base = Some(Base::try_from("https://example.com/path/page.html").unwrap());
let source = InputSource::FsPath(PathBuf::from("/some/page.html"));

let uris = vec![RawUri::from("../parent")];
let requests = create(uris, &source, &root_path, &base, &None);

assert_eq!(requests.len(), 1);
assert!(requests
.iter()
.any(|r| r.uri.url.as_str() == "https://example.com/parent"));
}

#[test]
fn test_fragment_url_resolution_from_root_path_and_base_url() {
let root_path = Some(PathBuf::from("/tmp/lychee"));
let base = Some(Base::try_from("https://example.com/path/page.html").unwrap());
let source = InputSource::FsPath(PathBuf::from("/some/page.html"));

let uris = vec![RawUri::from("#fragment")];
let requests = create(uris, &source, &root_path, &base, &None);

assert_eq!(requests.len(), 1);
assert!(requests
.iter()
.any(|r| r.uri.url.as_str() == "https://example.com/path/page.html#fragment"));
}

#[test]
fn test_no_base_url_resolution() {
let base = None;
Expand Down

0 comments on commit 393919e

Please sign in to comment.