Skip to content

Commit

Permalink
Fix example
Browse files Browse the repository at this point in the history
  • Loading branch information
mre committed May 29, 2022
1 parent c58f492 commit bd75531
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 7 deletions.
2 changes: 1 addition & 1 deletion lychee-lib/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 17 additions & 5 deletions lychee-lib/src/remap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,15 @@ 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
/// - Testing URIs behind a proxy
///
/// 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)>);

Expand All @@ -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<Uri> {
let mut uri = uri;
for (pattern, new_uri) in &self.0 {
Expand Down Expand Up @@ -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);
}
}
2 changes: 1 addition & 1 deletion lychee-lib/src/types/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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}")]
Expand Down

0 comments on commit bd75531

Please sign in to comment.