Skip to content

Commit

Permalink
fix uri replace bug
Browse files Browse the repository at this point in the history
  • Loading branch information
tronicboy1 committed Jan 24, 2024
1 parent f814661 commit 243405b
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "axum_l10n"
version = "0.2.1"
version = "0.2.2"
edition = "2021"
authors = ["tronicboy1"]
description = "A crate with localization utilities for Axum"
Expand Down
23 changes: 22 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,9 @@ impl<S> LanguageIdentifierExtractor<S> {
RedirectMode::NoRedirect => unreachable!(),
};

let new_uri = uri.to_string().replace(&format!("/{}", lang_code), "");
let new_uri = uri
.to_string()
.replacen(&format!("/{}/", lang_code), "/", 1);
*uri = http::Uri::try_from(new_uri)?;

Ok(())
Expand Down Expand Up @@ -334,6 +336,25 @@ mod tests {
assert_eq!("http://localhost:3000/lists", uri.to_string().as_str());
}

#[test]
fn can_rewrite_uri_same_starting_text() {
let mut uri = "http://localhost:3000/en/enrollment/details"
.parse::<Uri>()
.unwrap();

let mut service = get_serv();
service.redirect_mode = RedirectMode::RedirectToLanguageSubPath;

let ident = LanguageIdentifier::from_str("en-US").unwrap();

service.rewrite_uri(&mut uri, &ident).unwrap();

assert_eq!(
"http://localhost:3000/enrollment/details",
uri.to_string().as_str()
);
}

#[test]
fn can_get_supported_lang_code_from_uri() {
let uri = "http://localhost:3000/ja/lists".parse::<Uri>().unwrap();
Expand Down

0 comments on commit 243405b

Please sign in to comment.