Skip to content

Commit 2cbe6c3

Browse files
♻️ fix improper test & improper enqueueing for localresponse (#237)
1 parent 011b38b commit 2cbe6c3

File tree

2 files changed

+21
-6
lines changed

2 files changed

+21
-6
lines changed

mindee/input/local_response.py

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import hmac
33
import io
44
import json
5+
import os
56
from pathlib import Path
67
from typing import Any, BinaryIO, Dict, Union
78

@@ -15,18 +16,31 @@ class LocalResponse:
1516
"""File object of the local response."""
1617

1718
def __init__(self, input_file: Union[BinaryIO, str, Path, bytes]):
18-
if isinstance(input_file, BinaryIO):
19-
self._file = input_file
19+
if isinstance(input_file, (BinaryIO, io.BufferedReader)):
20+
str_stripped = (
21+
input_file.read().decode("utf-8").replace("\r", "").replace("\n", "")
22+
)
23+
self._file = io.BytesIO(str_stripped.encode("utf-8"))
2024
self._file.seek(0)
21-
elif isinstance(input_file, (str, Path)):
25+
elif isinstance(input_file, Path) or (
26+
isinstance(input_file, str) and os.path.exists(input_file)
27+
):
2228
with open(input_file, "r", encoding="utf-8") as file:
2329
self._file = io.BytesIO(
2430
file.read().replace("\r", "").replace("\n", "").encode()
2531
)
32+
elif isinstance(input_file, str):
33+
self._file = io.BytesIO(
34+
input_file.replace("\r", "").replace("\n", "").encode("utf-8")
35+
)
2636
elif isinstance(input_file, bytes):
27-
self._file = io.BytesIO(input_file)
37+
str_stripped = (
38+
input_file.decode("utf-8").replace("\r", "").replace("\n", "")
39+
)
40+
self._file = io.BytesIO(str_stripped.encode("utf-8"))
41+
self._file.seek(0)
2842
else:
29-
raise MindeeError("Incompatible type for input.")
43+
raise MindeeError(f"Incompatible type for input '{type(input_file)}'.")
3044

3145
@property
3246
def as_dict(self) -> Dict[str, Any]:

tests/Input/test_local_response.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ def file_path():
2222

2323

2424
def test_valid_file_local_response(dummy_secret_key, signature, file_path):
25-
local_response = LocalResponse(file_path)
25+
with open(file_path, "rb") as file:
26+
local_response = LocalResponse(file)
2627
assert local_response._file is not None
2728
assert not local_response.is_valid_hmac_signature(
2829
dummy_secret_key, "invalid signature"

0 commit comments

Comments
 (0)