From b5f3a548388931c3245473b61f6ea4b96341a1a6 Mon Sep 17 00:00:00 2001 From: Lorenz Leitner Date: Thu, 14 Oct 2021 19:16:16 +0200 Subject: Fix URL shortening of some WA inputs --- src/util/web.rs | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) (limited to 'src/util') diff --git a/src/util/web.rs b/src/util/web.rs index 77e20a3..bfa7003 100644 --- a/src/util/web.rs +++ b/src/util/web.rs @@ -1,5 +1,10 @@ use anyhow::{Context, Error, Result}; use reqwest::{get, Url}; +use urlparse::quote_plus as urlparse_quote_plus; + +pub(crate) fn quote_plus(text: &str) -> Result { + Ok(urlparse_quote_plus(text, b"")?) +} // TODO: Either catinator should have a URL shortening utility module, // or we should start our own service @@ -18,3 +23,27 @@ pub(crate) async fn shorten_url(url: &str) -> Result { Ok(short_url) } + +#[cfg(test)] +mod tests { + use super::{quote_plus}; + use anyhow::{Error, Result}; + + #[test] + fn test_quote_plus_1() -> Result<(), Error> { + assert_eq!(quote_plus("5/10")?, "5%2F10"); + Ok(()) + } + + #[test] + fn test_quote_plus_2() -> Result<(), Error> { + assert_eq!(quote_plus("1 * 2")?, "1+%2A+2"); + Ok(()) + } + + #[test] + fn test_quote_plus_3() -> Result<(), Error> { + assert_eq!(quote_plus("e_plus("1 * 2")?)?, "1%2B%252A%2B2"); + Ok(()) + } +} -- cgit v1.2.3