Skip to content

Commit

Permalink
refactor (response): remove deprecated attributes (#2317)
Browse files Browse the repository at this point in the history
* refactor (response): Remove deprecated attributes

* refactor (response): Organize imports with ruff check --fix

* test (response): Refactor test_response_set_stream test

* test (response): Fix set stream test

* test (response): Improve assertion
  • Loading branch information
aarcex3 authored Sep 3, 2024
1 parent 4f31356 commit f20c3cc
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 25 deletions.
17 changes: 0 additions & 17 deletions falcon/response.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,6 @@
from falcon.util.uri import encode_check_escaped as uri_encode
from falcon.util.uri import encode_value_check_escaped as uri_encode_value

_STREAM_LEN_REMOVED_MSG = (
'The deprecated stream_len property was removed in Falcon 3.0. '
'Please use Response.set_stream() or Response.content_length instead.'
)

_RESERVED_CROSSORIGIN_VALUES = frozenset({'anonymous', 'use-credentials'})

_RESERVED_SAMESITE_VALUES = frozenset({'lax', 'strict', 'none'})
Expand Down Expand Up @@ -243,18 +238,6 @@ def media(self, value):
self._media = value
self._media_rendered = _UNSET

@property
def stream_len(self):
# NOTE(kgriffs): Provide some additional information by raising the
# error explicitly.
raise AttributeError(_STREAM_LEN_REMOVED_MSG)

@stream_len.setter
def stream_len(self, value):
# NOTE(kgriffs): We explicitly disallow setting the deprecated attribute
# so that apps relying on it do not fail silently.
raise AttributeError(_STREAM_LEN_REMOVED_MSG)

def render_body(self):
"""Get the raw bytestring content for the response body.
Expand Down
20 changes: 12 additions & 8 deletions tests/test_response.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from io import BytesIO
from unittest.mock import MagicMock

import pytest
Expand Down Expand Up @@ -53,14 +54,6 @@ def test_response_attempt_to_set_read_only_headers(resp):
assert headers['x-things3'] == 'thing-3a, thing-3b'


def test_response_removed_stream_len(resp):
with pytest.raises(AttributeError):
resp.stream_len = 128

with pytest.raises(AttributeError):
resp.stream_len


def test_response_option_mimetype_init(monkeypatch):
mock = MagicMock()
mock.inited = False
Expand All @@ -81,3 +74,14 @@ def test_response_option_mimetype_init(monkeypatch):
assert ro.static_media_types['.js'] == 'text/javascript'
assert ro.static_media_types['.json'] == 'application/json'
assert ro.static_media_types['.mjs'] == 'text/javascript'


@pytest.mark.parametrize('content', [b'', b'dummy content'])
def test_response_set_stream(resp, content):
stream = BytesIO(content)
content_length = len(content)

resp.set_stream(stream, content_length)

assert resp.stream is stream
assert resp.headers['content-length'] == str(content_length)

0 comments on commit f20c3cc

Please sign in to comment.