forked from cashubtc/nutshell
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
sanitize mint URL before adding (cashubtc#606)
- Loading branch information
Showing
3 changed files
with
84 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
def sanitize_url(url: str) -> str: | ||
# extract host from url and lower case it, remove trailing slash from url | ||
protocol = url.split("://")[0] | ||
host = url.split("://")[1].split("/")[0].lower() | ||
path = ( | ||
url.split("://")[1].split("/", 1)[1].rstrip("/") | ||
if "/" in url.split("://")[1] | ||
else "" | ||
) | ||
return f"{protocol}://{host}{'/' + path if path else ''}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
from typing import List, Union | ||
|
||
from cashu.core.errors import CashuError | ||
from cashu.wallet.utils import sanitize_url | ||
|
||
|
||
async def assert_err(f, msg: Union[str, CashuError]): | ||
"""Compute f() and expect an error message 'msg'.""" | ||
try: | ||
await f | ||
except Exception as exc: | ||
error_message: str = str(exc.args[0]) | ||
if isinstance(msg, CashuError): | ||
if msg.detail not in error_message: | ||
raise Exception( | ||
f"CashuError. Expected error: {msg.detail}, got: {error_message}" | ||
) | ||
return | ||
if msg not in error_message: | ||
raise Exception(f"Expected error: {msg}, got: {error_message}") | ||
return | ||
raise Exception(f"Expected error: {msg}, got no error") | ||
|
||
|
||
async def assert_err_multiple(f, msgs: List[str]): | ||
"""Compute f() and expect an error message 'msg'.""" | ||
try: | ||
await f | ||
except Exception as exc: | ||
for msg in msgs: | ||
if msg in str(exc.args[0]): | ||
return | ||
raise Exception(f"Expected error: {msgs}, got: {exc.args[0]}") | ||
raise Exception(f"Expected error: {msgs}, got no error") | ||
|
||
|
||
def test_sanitize_url(): | ||
url = "https://localhost:3338" | ||
assert sanitize_url(url) == "https://localhost:3338" | ||
|
||
url = "https://mint.com:3338" | ||
assert sanitize_url(url) == "https://mint.com:3338" | ||
|
||
url = "https://Mint.com:3338" | ||
assert sanitize_url(url) == "https://mint.com:3338" | ||
|
||
url = "https://mint.com:3338/" | ||
assert sanitize_url(url) == "https://mint.com:3338" | ||
|
||
url = "https://mint.com:3338/abc" | ||
assert sanitize_url(url) == "https://mint.com:3338/abc" | ||
|
||
url = "https://mint.com:3338/Abc" | ||
assert sanitize_url(url) == "https://mint.com:3338/Abc" | ||
|
||
url = "https://mint.com:3338/abc/" | ||
assert sanitize_url(url) == "https://mint.com:3338/abc" | ||
|
||
url = "https://mint.com:3338/Abc/" | ||
assert sanitize_url(url) == "https://mint.com:3338/Abc" | ||
|
||
url = "https://Mint.com:3338/Abc/def" | ||
assert sanitize_url(url) == "https://mint.com:3338/Abc/def" |