Skip to content

feat(headers): raw request headers #263

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion playwright/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ def __init__(
"responseStart": -1,
"responseEnd": -1,
}
self._headers: Dict[str, str] = parse_headers(self._initializer["headers"])

@property
def url(self) -> str:
Expand Down Expand Up @@ -97,7 +98,7 @@ def postDataBuffer(self) -> Optional[bytes]:

@property
def headers(self) -> Dict[str, str]:
return parse_headers(self._initializer["headers"])
return self._headers

async def response(self) -> Optional["Response"]:
return from_nullable_channel(await self._channel.send("response"))
Expand Down Expand Up @@ -209,6 +210,7 @@ def __init__(
self._request._timing["connectEnd"] = timing["connectEnd"]
self._request._timing["requestStart"] = timing["requestStart"]
self._request._timing["responseStart"] = timing["responseStart"]
self._request._headers = parse_headers(self._initializer["requestHeaders"])

@property
def url(self) -> str:
Expand Down
35 changes: 31 additions & 4 deletions tests/async/test_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@
import asyncio
import json
from asyncio.futures import Future
from typing import Dict, List
from typing import Dict, List, cast

import pytest

from playwright import Error
from playwright.async_api import Page, Request
from playwright.async_api import Page, Request, Response


async def test_request_fulfill(page, server):
Expand Down Expand Up @@ -148,9 +148,32 @@ async def test_request_headers_should_work(
assert "WebKit" in response.request.headers["user-agent"]


@pytest.mark.only_browser("firefox")
async def test_request_headers_should_get_the_same_headers_as_the_server(
page: Page, server
page: Page, server, is_webkit, is_win
):
server_request_headers_future: Future[Dict[str, str]] = asyncio.Future()

def handle(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.write(b"done")
request.finish()

server.set_route("/empty.html", handle)
response = await page.goto(server.EMPTY_PAGE)
server_headers = await server_request_headers_future
if is_webkit and is_win:
# Curl does not show accept-encoding and accept-language
server_headers.pop("accept-encoding")
server_headers.pop("accept-language")
assert cast(Response, response).request.headers == server_headers


async def test_request_headers_should_get_the_same_headers_as_the_server_cors(
page: Page, server, is_webkit, is_win
):
await page.goto(server.PREFIX + "/empty.html")
server_request_headers_future: Future[Dict[str, str]] = asyncio.Future()
Expand Down Expand Up @@ -178,6 +201,10 @@ def handle_something(request):
request: Request = await requestPromise
assert text == "done"
server_headers = await server_request_headers_future
if is_webkit and is_win:
# Curl does not show accept-encoding and accept-language
server_headers.pop("accept-encoding")
server_headers.pop("accept-language")
assert request.headers == server_headers


Expand Down