FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

fix(net): preserve custom content type regardless of header casing by HarshRajSinghania · Pull Request #97 · pythonnative/pythonnative · GitHub

Repository navigation

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (2) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
5 changes: 3 additions & 2 deletions src/pythonnative/net.py
This file contains hidden or 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
Expand Up @@ -147,7 +147,7 @@ async def fetch(

Args:
url: Target URL. Relative URLs are not supported.
method: HTTP method (``"GET"``, ``"POST"``, ``"PUT"`` …).
method: HTTP method (``GET``, ``POST``, ``PUT`` …).
headers: Optional request headers.
body: Request body. ``bytes`` are sent as-is; ``str`` is
UTF-8 encoded; ``dict`` is JSON-encoded with a
Expand Down Expand Up @@ -203,7 +203,8 @@ def _build_request(
payload = body.encode("utf-8")
elif isinstance(body, Mapping):
payload = json.dumps(body, default=str).encode("utf-8")
header_dict.setdefault("Content-Type", "application/json")
if not any(name.lower() == "content-type" for name in header_dict):
header_dict["Content-Type"] = "application/json"
else:
raise TypeError(f"Unsupported body type: {type(body)!r}")

Expand Down
36 changes: 35 additions & 1 deletion tests/test_net.py
This file contains hidden or 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
Expand Up @@ -11,7 +11,7 @@

import pytest

from pythonnative.net import HTTPError, Response, fetch
from pythonnative.net import HTTPError, Response, _build_request, fetch

# ======================================================================
# Mini HTTP server fixture
Expand Down Expand Up @@ -198,3 +198,37 @@ async def run() -> Response:

with pytest.raises(OSError):
asyncio.run(run())


@pytest.mark.parametrize(
"header_name",
["Content-Type", "content-type", "CONTENT-TYPE", "Content-type"],
)
def test_custom_content_type_is_preserved_regardless_of_casing(header_name: str) -> None:
original_headers = {header_name: "application/merge-patch+json"}
request = _build_request(
url="https://example.com/users",
method="POST",
headers=original_headers,
body={"name": "Ada"},
params=None,
)
content_types = [value for name, value in request.header_items() if name.lower() == "content-type"]
assert content_types == ["application/merge-patch+json"]
assert original_headers == {header_name: "application/merge-patch+json"}
assert isinstance(request.data, bytes)
assert json.loads(request.data.decode("utf-8")) == {"name": "Ada"}


def test_mapping_body_defaults_to_application_json() -> None:
request = _build_request(
url="https://example.com/users",
method="POST",
headers=None,
body={"name": "Ada"},
params=None,
)
content_types = [value for name, value in request.header_items() if name.lower() == "content-type"]
assert content_types == ["application/json"]
assert isinstance(request.data, bytes)
assert json.loads(request.data.decode("utf-8")) == {"name": "Ada"}
Loading

Back | FazBrowse Home | New Git URL