diff --git a/lychee-lib/src/utils/request.rs b/lychee-lib/src/utils/request.rs index 79b5c848e8..d367fb4d1b 100644 --- a/lychee-lib/src/utils/request.rs +++ b/lychee-lib/src/utils/request.rs @@ -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() == "file:///some/page.html#fragment")); + } + #[test] fn test_no_base_url_resolution() { let base = None;