Skip to content

Commit

Permalink
FIX: pass headers for async request definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
Bart Veraart authored and eigenein committed Apr 17, 2023
1 parent 80a58cc commit 38a5785
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 6 deletions.
1 change: 1 addition & 0 deletions combadge/support/httpx/backends/async_.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ async def __call__(self, request: Request, response_type: Type[ResponseT]) -> Re
json=request.json_,
data=request.form_data,
params=request.query_params,
headers=request.headers,
)
if self._raise_for_status:
response.raise_for_status()
Expand Down
48 changes: 48 additions & 0 deletions tests/integration/cassettes/test_httpbin/test_headers_async.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
interactions:
- request:
body: '{}'
headers:
accept:
- '*/*'
accept-encoding:
- gzip, deflate
connection:
- keep-alive
content-length:
- '2'
content-type:
- application/json
host:
- httpbin.org
user-agent:
- python-httpx/0.23.3
x-bar:
- barval
x-foo:
- fooval
method: GET
uri: https://httpbin.org/headers
response:
content: "{\n \"headers\": {\n \"Accept\": \"*/*\", \n \"Accept-Encoding\":
\"gzip, deflate\", \n \"Content-Length\": \"2\", \n \"Content-Type\":
\"application/json\", \n \"Host\": \"httpbin.org\", \n \"User-Agent\":
\"python-httpx/0.23.3\", \n \"X-Amzn-Trace-Id\": \"Root=1-643d0f74-258491ed49f5affd6aa5cd03\",
\n \"X-Bar\": \"barval\", \n \"X-Foo\": \"fooval\"\n }\n}\n"
headers:
Access-Control-Allow-Credentials:
- 'true'
Access-Control-Allow-Origin:
- '*'
Connection:
- keep-alive
Content-Length:
- '339'
Content-Type:
- application/json
Date:
- Mon, 17 Apr 2023 09:20:56 GMT
Server:
- gunicorn/19.9.0
http_version: HTTP/1.1
status_code: 200
version: 1
35 changes: 29 additions & 6 deletions tests/integration/test_httpbin.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
from abc import abstractmethod
from typing import Any, Dict

from httpx import Client
from httpx import AsyncClient, Client
from pydantic import BaseModel
from pytest import mark
from typing_extensions import Annotated, Protocol

from combadge.core.interfaces import SupportsService
from combadge.support.http.markers import FormData, FormField, Header, QueryParam, http_method, path
from combadge.support.httpx.backends.sync import HttpxBackend
from combadge.support.httpx.backends.async_ import HttpxBackend as AsyncHttpxBackend
from combadge.support.httpx.backends.sync import HttpxBackend as SyncHttpxBackend


@mark.vcr
Expand All @@ -31,7 +32,7 @@ def post_anything(
) -> Response:
...

service = SupportsHttpbin.bind(HttpxBackend(Client(base_url="https://httpbin.org")))
service = SupportsHttpbin.bind(SyncHttpxBackend(Client(base_url="https://httpbin.org")))
response = service.post_anything(data=Data(foo=42), bar=100500, qux=100501)

assert response == Response(form={"foo": "42", "barqux": ["100500", "100501"]})
Expand All @@ -53,14 +54,14 @@ def get_anything(
) -> Response:
...

service = SupportsHttpbin.bind(HttpxBackend(Client(base_url="https://httpbin.org")))
service = SupportsHttpbin.bind(SyncHttpxBackend(Client(base_url="https://httpbin.org")))
response = service.get_anything(foo=100500, bar=100501)

assert response == Response(args={"foobar": ["100500", "100501"]})


@mark.vcr
def test_headers() -> None:
def test_headers_sync() -> None:
class Response(BaseModel):
headers: Dict[str, Any]

Expand All @@ -75,7 +76,29 @@ def get_headers(
) -> Response:
...

service = SupportsHttpbin.bind(HttpxBackend(Client(base_url="https://httpbin.org")))
service = SupportsHttpbin.bind(SyncHttpxBackend(Client(base_url="https://httpbin.org")))
response = service.get_headers(foo="fooval")
assert response.headers["X-Foo"] == "fooval"
assert response.headers["X-Bar"] == "barval"


@mark.vcr
async def test_headers_async() -> None:
class Response(BaseModel):
headers: Dict[str, Any]

class SupportsHttpbin(SupportsService, Protocol):
@http_method("GET")
@path("/headers")
@abstractmethod
async def get_headers(
self,
foo: Annotated[str, Header("x-foo")],
bar: Annotated[str, Header("x-bar")] = "barval",
) -> Response:
...

service = SupportsHttpbin.bind(AsyncHttpxBackend(AsyncClient(base_url="https://httpbin.org")))
response = await service.get_headers(foo="fooval")
assert response.headers["X-Foo"] == "fooval"
assert response.headers["X-Bar"] == "barval"

0 comments on commit 38a5785

Please sign in to comment.