Skip to content

204 Fix #213

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

Merged
merged 1 commit into from
May 27, 2025
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
14 changes: 12 additions & 2 deletions adafruit_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,12 @@ class Response:
It is still necessary to ``close`` the response object for correct management of
sockets, including doing so implicitly via ``with requests.get(...) as response``."""

def __init__(self, sock: SocketType, session: "Session") -> None:
def __init__(self, sock: SocketType, session: "Session", method: str) -> None:
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@FoamyGuy This is the only way we can also do the HEAD check (which would also have no content). Are we good with this?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense to me.

self.socket = sock
self.encoding = "utf-8"
self._cached = None
self._headers = {}
self._method = method

# _start_index and _receive_buffer are used when parsing headers.
# _receive_buffer will grow by 32 bytes everytime it is too small.
Expand Down Expand Up @@ -276,6 +277,15 @@ def _parse_headers(self) -> None:
else:
self._headers[title] = content

# does the body have a fixed length? (of zero)
if (
self.status_code == 204
or self.status_code == 304
or 100 <= self.status_code < 200 # 1xx codes
or self._method == "HEAD"
):
self._remaining = 0

def _validate_not_gzip(self) -> None:
"""gzip encoding is not supported. Raise an exception if found."""
if "content-encoding" in self.headers and self.headers["content-encoding"] == "gzip":
Expand Down Expand Up @@ -670,7 +680,7 @@ def request( # noqa: PLR0912,PLR0913,PLR0915 Too many branches,Too many argumen
if not socket:
raise OutOfRetries("Repeated socket failures") from last_exc

resp = Response(socket, self) # our response
resp = Response(socket, self, method) # our response
if allow_redirects:
if "location" in resp.headers and 300 <= resp.status_code <= 399:
# a naive handler for redirects
Expand Down
34 changes: 34 additions & 0 deletions tests/real_call_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# SPDX-FileCopyrightText: 2024 Justin Myers
#
# SPDX-License-Identifier: Unlicense

"""Real call Tests"""

import socket
import ssl

import adafruit_connection_manager
import pytest

import adafruit_requests


@pytest.mark.parametrize(
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These tests, do actual calls to httpbin. We should make sure not to add too many, but this should help us make sure we don't break anything...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with limiting the use of calls out to 3rd party services in the tests. This one is okay for now, but perhaps it would be good to add a simple httpserver script in the tests dir that the tests can then interact with. That way it can get the same functionality but be more self contained, and not reliant on actual network conditions (I had httpbin returning 503 for a moment which I think would've caused the test to fail).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@justmobilize This did end up causing the test and thus actions check to fail: https://github.com/adafruit/Adafruit_CircuitPython_Requests/actions/runs/15278252260/job/42970208909#step:2:1007

With that in mind I think rather just not have our tests rely on the 3rd party service. If you have a moment and can PR removing this test, or altering it to use a local server please do. I can circle back in a day or two and do it if you don't.

("path", "status_code", "text_result", "json_keys"),
(
("get", 200, None, {"url": "https://httpbin.org/get"}),
("status/200", 200, "", None),
("status/204", 204, "", None),
),
)
def test_gets(path, status_code, text_result, json_keys):
requests = adafruit_requests.Session(socket, ssl.create_default_context())
with requests.get(f"https://httpbin.org/{path}") as response:
assert response.status_code == status_code
if text_result is not None:
assert response.text == text_result
if json_keys is not None:
for key, value in json_keys.items():
assert response.json()[key] == value

adafruit_connection_manager.connection_manager_close_all(release_references=True)