Beispiel #1
0
async def test_should_report_if_request_was_from_service_worker(
        page: Page, server: Server) -> None:
    response = await page.goto(server.PREFIX + "/serviceworkers/fetch/sw.html")
    assert response
    assert not response.from_service_worker
    await page.evaluate("() => window.activationPromise")
    async with page.expect_response("**/example.txt") as response_info:
        await page.evaluate("() => fetch('/example.txt')")
    response = await response_info.value
    assert response.from_service_worker
async def test_should_report_response_headers_array(page: Page, server: Server,
                                                    is_win,
                                                    browser_name) -> None:
    if is_win and browser_name == "webkit":
        pytest.skip(
            "libcurl does not support non-set-cookie multivalue headers")
    expected_headers = {
        "header-a": ["value-a", "value-a-1", "value-a-2"],
        "header-b": ["value-b"],
        "set-cookie": ["a=b", "c=d"],
    }

    def handle(request: http.Request):
        for key in expected_headers:
            for value in expected_headers[key]:
                request.responseHeaders.addRawHeader(key, value)
        request.finish()

    server.set_route("/headers", handle)
    await page.goto(server.EMPTY_PAGE)
    async with page.expect_response("*/**") as response_info:
        await page.evaluate("""() => fetch('/headers')
        """)
    response = await response_info.value
    actual_headers = {}
    for header in await response.headers_array():
        name = header["name"].lower()
        value = header["value"]
        if not actual_headers.get(name):
            actual_headers[name] = []
        actual_headers[name].append(value)

    for key in ["Keep-Alive", "Connection", "Date", "Transfer-Encoding"]:
        if key in actual_headers:
            actual_headers.pop(key)
        if key.lower() in actual_headers:
            actual_headers.pop(key.lower())
    assert actual_headers == expected_headers
    assert await response.header_value("not-there") is None
    assert await response.header_value("set-cookie") == "a=b\nc=d"
    assert await response.header_value("header-a"
                                       ) == "value-a, value-a-1, value-a-2"
    assert await response.header_values("set-cookie") == ["a=b", "c=d"]