async def test_request_headers_should_get_the_same_headers_as_the_server_cors(
        page: Page, server, is_webkit, is_win):
    if is_webkit and is_win:
        pytest.xfail("Curl does not show accept-encoding and accept-language")
    await page.goto(server.PREFIX + "/empty.html")
    server_request_headers_future: Future[Dict[str, str]] = asyncio.Future()

    def handle_something(request):
        normalized_headers = {
            key.decode().lower(): value[0].decode()
            for key, value in request.requestHeaders.getAllRawHeaders()
        }
        server_request_headers_future.set_result(normalized_headers)
        request.setHeader("Access-Control-Allow-Origin", "*")
        request.write(b"done")
        request.finish()

    server.set_route("/something", handle_something)

    text = None
    async with page.expect_request("**/*") as request_info:
        text = await page.evaluate(
            """async url => {
                const data = await fetch(url);
                return data.text();
            }""",
            server.CROSS_PROCESS_PREFIX + "/something",
        )
    request = await request_info.value
    assert text == "done"
    server_headers = await server_request_headers_future
    assert await request.all_headers() == server_headers
Beispiel #2
0
async def test_should_set_bodysize_to_0(page: Page, server: Server):
    await page.goto(server.EMPTY_PAGE)
    async with page.expect_request("*/**") as request_info:
        await page.evaluate("() => fetch('./get').then(r => r.text())")
    request = await request_info.value
    sizes = await request.sizes()
    assert sizes["requestBodySize"] == 0
    assert sizes["requestHeadersSize"] >= 200
Beispiel #3
0
async def test_should_set_bodysize_and_headersize(page: Page, server: Server):
    await page.goto(server.EMPTY_PAGE)
    async with page.expect_request("*/**") as request_info:
        await page.evaluate(
            "() => fetch('./get', { method: 'POST', body: '12345'}).then(r => r.text())"
        )
    request = await request_info.value
    sizes = await request.sizes()
    assert sizes["requestBodySize"] == 5
    assert sizes["requestHeadersSize"] >= 300
Beispiel #4
0
async def test_page_events_request_should_report_requests_and_responses_handled_by_service_worker(
        page: Page, server):
    await page.goto(server.PREFIX + "/serviceworkers/fetchdummy/sw.html")
    await page.evaluate("() => window.activationPromise")
    sw_response = None
    async with page.expect_request("**/*") as request_info:
        sw_response = await page.evaluate('() => fetchDummy("foo")')
    request = await request_info.value
    assert sw_response == "responseFromServiceWorker:foo"
    assert request.url == server.PREFIX + "/serviceworkers/fetchdummy/foo"
    response = await request.response()
    assert response.url == server.PREFIX + "/serviceworkers/fetchdummy/foo"
    assert await response.text() == "responseFromServiceWorker:foo"
async def test_should_report_request_headers_array(page: Page, server: Server,
                                                   is_win: bool,
                                                   browser_name: str) -> None:
    if is_win and browser_name == "webkit":
        pytest.skip(
            "libcurl does not support non-set-cookie multivalue headers")
    expected_headers = []

    def handle(request: http.Request):
        for name, values in request.requestHeaders.getAllRawHeaders():
            for value in values:
                expected_headers.append({
                    "name": name.decode().lower(),
                    "value": value.decode()
                })
        request.finish()

    server.set_route("/headers", handle)
    await page.goto(server.EMPTY_PAGE)
    async with page.expect_request("*/**") as request_info:
        await page.evaluate("""() => fetch('/headers', {
            headers: [
                ['header-a', 'value-a'],
                ['header-b', 'value-b'],
                ['header-a', 'value-a-1'],
                ['header-a', 'value-a-2'],
            ]
            })
        """)
    request = await request_info.value
    sorted_pw_request_headers = sorted(
        list(
            map(
                lambda header: {
                    "name": header["name"].lower(),
                    "value": header["value"],
                },
                await request.headers_array(),
            )),
        key=lambda header: header["name"],
    )
    sorted_expected_headers = sorted(expected_headers,
                                     key=lambda header: header["name"])
    assert sorted_pw_request_headers == sorted_expected_headers
    assert await request.header_value("Header-A"
                                      ) == "value-a, value-a-1, value-a-2"
    assert await request.header_value("not-there") is None
async def test_should_amend_http_headers(page: Page, server: Server) -> None:
    values = []

    async def handler(route: Route):
        values.append(route.request.headers.get("foo"))
        values.append(await route.request.header_value("FOO"))
        await route.continue_()

    await page.route("**/sleep.zzz", handler)

    async def handler_with_header_mods(route: Route):
        await route.fallback(headers={**route.request.headers, "FOO": "bar"})

    await page.route("**/*", handler_with_header_mods)

    await page.goto(server.EMPTY_PAGE)
    async with page.expect_request("/sleep.zzz") as request_info:
        await page.evaluate("() => fetch('/sleep.zzz')")
    request = await request_info.value
    values.append(request.headers.get("foo"))
    assert values == ["bar", "bar", "bar"]