Skip to content

Commit

Permalink
ETag fixes for aio-libs#5298
Browse files Browse the repository at this point in the history
  • Loading branch information
greshilov committed Mar 24, 2021
1 parent 1a4126a commit 6756810
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 9 deletions.
7 changes: 3 additions & 4 deletions aiohttp/web_fileresponse.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,16 +109,15 @@ async def _sendfile(
def _strong_etag_match(etag_value: str, etags: Tuple[ETag, ...]) -> bool:
if len(etags) == 1 and etags[0].value == ETAG_ANY:
return True
else:
return any(etag.value == etag_value for etag in etags if not etag.is_weak)
return any(etag.value == etag_value for etag in etags if not etag.is_weak)

async def _not_modified(
self, request: "BaseRequest", etag_value: str, last_modified: float
) -> Optional[AbstractStreamWriter]:
self.set_status(HTTPNotModified.status_code)
self._length_check = False
self.etag = etag_value # type: ignore
self.last_modified = last_modified # type: ignore
self.etag = etag_value # type: ignore[assignment]
self.last_modified = last_modified # type: ignore[assignment]
# Delete any Content-Length headers provided by user. HTTP 304
# should always have empty response body
return await super().prepare(request)
Expand Down
4 changes: 2 additions & 2 deletions tests/test_web_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -826,14 +826,14 @@ async def invalid_handler_1(request):


@pytest.mark.parametrize(
"header,header_attr",
["header", "header_attr"],
[
pytest.param("If-Match", "if_match"),
pytest.param("If-None-Match", "if_none_match"),
],
)
@pytest.mark.parametrize(
"header_val,expected",
["header_val", "expected"],
[
pytest.param(
'"67ab43", W/"54ed21", "7892,dd"',
Expand Down
6 changes: 3 additions & 3 deletions tests/test_web_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ def test_etag_string() -> None:


@pytest.mark.parametrize(
"etag,expected_header",
["etag", "expected_header"],
(
(ETag(value="0123-weak-kotik", is_weak=True), 'W/"0123-weak-kotik"'),
(ETag(value="0123-strong-kotik", is_weak=False), '"0123-strong-kotik"'),
Expand Down Expand Up @@ -303,7 +303,7 @@ def test_etag_any() -> None:
)
def test_etag_invalid_value_set(invalid_value) -> None:
resp = StreamResponse()
with pytest.raises(ValueError):
with pytest.raises(ValueError, match="is not a valid etag"):
resp.etag = invalid_value


Expand All @@ -323,7 +323,7 @@ def test_etag_invalid_value_get(header) -> None:
@pytest.mark.parametrize("invalid", (123, ETag(value=123, is_weak=True)))
def test_etag_invalid_value_class(invalid) -> None:
resp = StreamResponse()
with pytest.raises(ValueError):
with pytest.raises(ValueError, match="Unsupported etag type"):
resp.etag = invalid


Expand Down

0 comments on commit 6756810

Please sign in to comment.