Skip to content

Commit

Permalink
Fix crash for non-ASCII characters (#5)
Browse files Browse the repository at this point in the history
* Fixed a bug where the latin-1 encoded message was decoded with utf-8

* Removed redundant utf-8 encoding/decoding

* Added tests for non-ASCII messages
  • Loading branch information
jan1854 committed Apr 11, 2024
1 parent ff0a7d2 commit 289286e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
3 changes: 1 addition & 2 deletions ntfy_lite/ntfy.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@ def __init__(
if filepath is not None:
self._data = open(filepath, "rb")
elif message is not None:
self._data = message.encode(encoding="UTF-8", errors="replace").decode()
self._data = message.encode(encoding="latin-1", errors="replace").decode()
self._data = message.encode(encoding="latin-1", errors="replace").decode(encoding="latin-1")

def __enter__(self) -> typing.Union[typing.IO, str]:
return self._data
Expand Down
14 changes: 14 additions & 0 deletions tests/test_ntfy_lite.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,20 @@ def test_action_http_push(clear):
ntfy.push(topic, title, message=message, actions=action, dry_run=True)


def test_extended_ascii_push():
topic = "ntfy_lite_test"
title = "ntfy lite test extended ascii push"
message = "ntfy_extended_ascii_push message: (°_°)"
ntfy.push(topic, title, message=message, dry_run=True)


def test_unicode_push():
topic = "ntfy_lite_test"
title = "ntfy lite test unicode push"
message = "ntfy unicode push message: 🐋💐🪂"
ntfy.push(topic, title, message=message, dry_run=True)


@pytest.mark.parametrize("clear", [True, False])
def test_actions_view_http_push(clear):
topic = "ntfy_lite_test"
Expand Down

0 comments on commit 289286e

Please sign in to comment.