Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 Fix encoding data with None values and more #119

Merged
merged 6 commits into from
May 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions src/niquests/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -742,9 +742,12 @@ def _encode_params(
result = []
for k, vs in to_key_val_list(data):
iterable_vs: typing.Iterable[str | bytes]
if isinstance(vs, (str, bytes, int, float, bool)):
# not officially supported, but some people maybe passing ints, float or bool.
if isinstance(vs, (str, bytes)) is False:
if isinstance(vs, (str, bytes, int, float, bool)) or not hasattr(
vs, "__iter__"
):
# not officially supported, but some people maybe passing ints, float, bool,
# or other string serializable classes.
if vs is not None and not isinstance(vs, (str, bytes,)):
iterable_vs = [str(vs)]
else:
iterable_vs = [vs]
Expand Down
34 changes: 34 additions & 0 deletions tests/test_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -2540,6 +2540,40 @@ def test_json_encodes_as_bytes():
assert isinstance(p.body, bytes)


def test_data_encodes_noniterables(httpbin):
class Class:
pass

class ClassStringable:
def __str__(self):
return "🔥arbClassStringable🔥"

ClassObj = Class()

body = {
"string": "string",
Ousret marked this conversation as resolved.
Show resolved Hide resolved
"bytes": b"string",
"float": 0.01,
"int": 100,
"bool": True,
"None": None,
"plain_class": ClassObj,
"stringable_class": ClassStringable(),
}

expected_response = {
"bool": "True",
"float": "0.01",
"int": "100",
"plain_class": str(ClassObj),
"string": "string",
Ousret marked this conversation as resolved.
Show resolved Hide resolved
"bytes": "string",
"stringable_class": "🔥arbClassStringable🔥",
}
resp = niquests.post(httpbin("post"), data=body)
assert resp.json()["form"] == expected_response


def test_requests_are_updated_each_time(httpbin):
session = RedirectSession([303, 307])
prep = niquests.Request("POST", httpbin("post")).prepare()
Expand Down
Loading