зеркало из https://github.com/mozilla/gecko-dev.git
Bug 1838021 [wpt PR 40502] - Add basic end-to-end Private Aggregation Web Platform Tests, a=testonly
Automatic update from web-platform-tests Add basic end-to-end Private Aggregation Web Platform Tests These tests ensure that a report makes it to the expected endpoint. Only tests in Shared Storage worklets for now Bug: 1452248 Change-Id: I70ad2180c7cde93beb5ffe08fe3c1a091bf298d9 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4598993 Reviewed-by: Yao Xiao <yaoxia@chromium.org> Auto-Submit: Alex Turner <alexmt@chromium.org> Commit-Queue: Alex Turner <alexmt@chromium.org> Reviewed-by: Mason Freed <masonf@chromium.org> Cr-Commit-Position: refs/heads/main@{#1158824} -- wpt-commits: e8b5a2b60fe757f14677bb5e30cac1f48498f16d wpt-pr: 40502
This commit is contained in:
Родитель
c9c945d91e
Коммит
b58b192aa3
|
@ -0,0 +1,7 @@
|
|||
"""Endpoint to receive and return aggregatable reports."""
|
||||
from importlib import import_module
|
||||
|
||||
reports = import_module('private-aggregation.resources.reports')
|
||||
|
||||
def main(request, response):
|
||||
return reports.handle_request(request)
|
|
@ -0,0 +1,7 @@
|
|||
"""Endpoint to receive and return aggregatable reports."""
|
||||
from importlib import import_module
|
||||
|
||||
reports = import_module('private-aggregation.resources.reports')
|
||||
|
||||
def main(request, response):
|
||||
return reports.handle_request(request)
|
|
@ -0,0 +1,77 @@
|
|||
"""Methods for the report-shared-storage and report-protected-audience endpoints (including debug endpoints)"""
|
||||
import json
|
||||
from typing import List, Optional, Tuple, TypedDict
|
||||
import urllib.parse
|
||||
|
||||
from wptserve.request import Request
|
||||
from wptserve.stash import Stash
|
||||
from wptserve.utils import isomorphic_decode, isomorphic_encode
|
||||
|
||||
# Arbitrary key used to access the reports in the stash.
|
||||
REPORTS_KEY = "9d285691-4386-45ad-9a79-d2ec29557bfe"
|
||||
|
||||
Header = Tuple[str, str]
|
||||
Status = Tuple[int, str]
|
||||
Response = Tuple[Status, List[Header], str]
|
||||
|
||||
def get_request_origin(request: Request) -> str:
|
||||
return "%s://%s" % (request.url_parts.scheme,
|
||||
request.url_parts.netloc)
|
||||
|
||||
def handle_post_request(request: Request) -> Response:
|
||||
"""Handles POST request for reports.
|
||||
|
||||
Retrieves the report from the request body and stores the report in the
|
||||
stash. If clear_stash is specified in the query params, clears the stash.
|
||||
"""
|
||||
store_report(request.server.stash, get_request_origin(request),
|
||||
request.body.decode("utf-8"))
|
||||
return 200, [], ""
|
||||
|
||||
|
||||
def handle_get_request(request: Request) -> Response:
|
||||
"""Handles GET request for reports.
|
||||
|
||||
Retrieves and returns all reports from the stash.
|
||||
"""
|
||||
headers = [("Content-Type", "application/json")]
|
||||
reports = take_reports(request.server.stash, get_request_origin(request))
|
||||
headers.append(("Access-Control-Allow-Origin", "*"))
|
||||
return 200, headers, json.dumps(reports)
|
||||
|
||||
|
||||
def store_report(stash: Stash, origin: str, report: str) -> None:
|
||||
"""Stores the report in the stash. Report here is a JSON."""
|
||||
with stash.lock:
|
||||
reports_dict = stash.take(REPORTS_KEY)
|
||||
if not reports_dict:
|
||||
reports_dict = {}
|
||||
reports = reports_dict.get(origin, [])
|
||||
reports.append(report)
|
||||
reports_dict[origin] = reports
|
||||
stash.put(REPORTS_KEY, reports_dict)
|
||||
return None
|
||||
|
||||
def take_reports(stash: Stash, origin: str) -> List[str]:
|
||||
"""Takes all the reports from the stash and returns them."""
|
||||
with stash.lock:
|
||||
reports_dict = stash.take(REPORTS_KEY)
|
||||
if not reports_dict:
|
||||
reports_dict = {}
|
||||
|
||||
reports = reports_dict.pop(origin, [])
|
||||
stash.put(REPORTS_KEY, reports_dict)
|
||||
return reports
|
||||
|
||||
|
||||
def handle_request(request: Request) -> Response:
|
||||
"""Handles request to get or store reports."""
|
||||
if request.method == "POST":
|
||||
return handle_post_request(request)
|
||||
if request.method == "GET":
|
||||
return handle_get_request(request)
|
||||
|
||||
return (405, "Method Not Allowed"), [("Content-Type", "application/json")], json.dumps({
|
||||
"code": 405,
|
||||
"message": "Only GET or POST methods are supported."
|
||||
})
|
Загрузка…
Ссылка в новой задаче