From bd755311e656c44e87b8ef83d1b029ddf8e42eec Mon Sep 17 00:00:00 2001 From: Matthias Date: Mon, 16 May 2022 23:23:47 +0200 Subject: [PATCH] Fix example --- lychee-lib/src/client.rs | 2 +- lychee-lib/src/remap.rs | 22 +++++++++++++++++----- lychee-lib/src/types/error.rs | 2 +- 3 files changed, 19 insertions(+), 7 deletions(-) diff --git a/lychee-lib/src/client.rs b/lychee-lib/src/client.rs index 738bc9c1b3..8083676846 100644 --- a/lychee-lib/src/client.rs +++ b/lychee-lib/src/client.rs @@ -79,7 +79,7 @@ pub struct ClientBuilder { /// Remap URIs matching a pattern to a different URI /// /// This makes it possible to remap any HTTP/HTTPS endpoint to a different - /// HTTP/HTTPS endpoint. Also, this feature could also be used to proxy + /// HTTP/HTTPS endpoint. This feature could also be used to proxy /// certain requests. /// /// Use with caution because a large set of remapping rules may cause diff --git a/lychee-lib/src/remap.rs b/lychee-lib/src/remap.rs index 8b099b3ea4..124b847bfc 100644 --- a/lychee-lib/src/remap.rs +++ b/lychee-lib/src/remap.rs @@ -6,7 +6,7 @@ use reqwest::Url; use crate::Uri; -/// Remaps allow mapping from a URI pattern to a different URI +/// Remaps allow mapping from a URI pattern to a specified URI /// /// Some use-cases are /// - Testing URIs prior to production deployment @@ -14,8 +14,7 @@ use crate::Uri; /// /// Be careful when using this feature because checking every link against a /// large set of regular expressions has a performance impact. Also there are no -/// constraints on the URI mapping, so the rules might contradict with each -/// other. +/// constraints on the URI mapping, so the rules might contradict each other. #[derive(Debug, Clone)] pub struct Remaps(Vec<(Regex, Url)>); @@ -30,7 +29,7 @@ impl Remaps { /// /// # Errors /// - /// Returns an error if the remapping value is not a URI + /// Returns an error if the remapping value is not a valid URI pub fn remap(&self, uri: Uri) -> Result { let mut uri = uri; for (pattern, new_uri) in &self.0 { @@ -106,9 +105,22 @@ mod tests { let uri = Uri::try_from("https://example.com").unwrap(); let remaps = Remaps::new(vec![(pattern, uri.clone().url)]); - let input = Uri::try_from("../../issues").unwrap(); + let input = Uri::try_from("file://../../issues").unwrap(); let remapped = remaps.remap(input).unwrap(); assert_eq!(remapped, uri); } + + #[test] + fn test_remap_skip() { + let pattern = Regex::new("https://example.com").unwrap(); + let uri = Uri::try_from("http://127.0.0.1:8080").unwrap(); + let remaps = Remaps::new(vec![(pattern, uri.url)]); + + let input = Uri::try_from("https://unrelated.example.com").unwrap(); + let remapped = remaps.remap(input.clone()).unwrap(); + + // URI was not modified + assert_eq!(remapped, input); + } } diff --git a/lychee-lib/src/types/error.rs b/lychee-lib/src/types/error.rs index 792f27a4ad..fd14c811fc 100644 --- a/lychee-lib/src/types/error.rs +++ b/lychee-lib/src/types/error.rs @@ -64,7 +64,7 @@ pub enum ErrorKind { #[error("Error with base dir `{0}` : {1}")] InvalidBase(String, String), /// The given input can not be parsed into a valid URI remapping - #[error("Error with URI remap expression `{0}`")] + #[error("Error handling URI remap expression. Cannot parse into URI remapping: `{0}`")] InvalidUriRemap(String), /// The given path does not resolve to a valid file #[error("Cannot find local file {0}")]