mirror of
https://gitlab.archlinux.org/archlinux/aurweb.git
synced 2025-02-03 10:43:03 +01:00
feat(rpc): support the If-None-Match request header
If the If-None-Match header is supplied with a previously obtained ETag from the same query, a 304 Not Modified is returned with no content. This allows clients to completely leverage the ETag header. Signed-off-by: Kevin Morris <kevr@0cost.org>
This commit is contained in:
parent
2cc44e8f28
commit
61f3cb938c
2 changed files with 15 additions and 2 deletions
|
@ -113,6 +113,11 @@ async def rpc(request: Request,
|
|||
"ETag": f'"{etag}"'
|
||||
}
|
||||
|
||||
if_none_match = request.headers.get("If-None-Match", str())
|
||||
if if_none_match and if_none_match.strip("\t\n\r\" ") == etag:
|
||||
return Response(headers=headers,
|
||||
status_code=int(HTTPStatus.NOT_MODIFIED))
|
||||
|
||||
if callback:
|
||||
content = f"/**/{callback}({content.decode()})"
|
||||
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
import re
|
||||
|
||||
from http import HTTPStatus
|
||||
from typing import Dict
|
||||
from unittest import mock
|
||||
|
||||
import orjson
|
||||
|
@ -28,9 +29,9 @@ from aurweb.redis import redis_connection
|
|||
from aurweb.testing import setup_test_db
|
||||
|
||||
|
||||
def make_request(path):
|
||||
def make_request(path, headers: Dict[str, str] = {}):
|
||||
with TestClient(app) as request:
|
||||
return request.get(path)
|
||||
return request.get(path, headers=headers)
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
|
@ -539,6 +540,13 @@ def test_rpc_search():
|
|||
result = data.get("results")[0]
|
||||
assert result.get("Name") == "big-chungus"
|
||||
|
||||
# Test the If-None-Match headers.
|
||||
etag = response.headers.get("ETag").strip('"')
|
||||
headers = {"If-None-Match": etag}
|
||||
response = make_request("/rpc?v=5&type=search&arg=big", headers=headers)
|
||||
assert response.status_code == int(HTTPStatus.NOT_MODIFIED)
|
||||
assert response.content == b''
|
||||
|
||||
# No args on non-m by types return an error.
|
||||
response = make_request("/rpc?v=5&type=search")
|
||||
assert response.json().get("error") == "No request type/data specified."
|
||||
|
|
Loading…
Add table
Reference in a new issue