Skip to content

Commit

Permalink
Merge pull request #1259 from c4ffein/test-case-headers
Browse files Browse the repository at this point in the history
Add default headers for NinjaClientBase instance
  • Loading branch information
vitalik authored Aug 10, 2024
2 parents fbe6443 + 1be6571 commit aa46acf
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
7 changes: 7 additions & 0 deletions docs/docs/guides/testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,10 @@ class HelloTest(TestCase):
# request.company_id will now be set within the view
response = client.get("/hello", company_id=1)
```

It is also possible to specify headers, both from the TestCase instanciation and the actual request:
```python
client = TestClient(router, headers={"A": "a", "B": "b"})
# The request will be made with {"A": "na", "B": "b", "C": "nc"} headers
response = client.get("/test-headers", headers={"A": "na", "C": "nc"})
```
12 changes: 11 additions & 1 deletion ninja/testing/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,12 @@ def build_absolute_uri(location: Optional[str] = None) -> str:
class NinjaClientBase:
__test__ = False # <- skip pytest

def __init__(self, router_or_app: Union[NinjaAPI, Router]) -> None:
def __init__(
self,
router_or_app: Union[NinjaAPI, Router],
headers: Optional[Dict[str, str]] = None,
) -> None:
self.headers = headers or {}
self.router_or_app = router_or_app

def get(
Expand Down Expand Up @@ -82,6 +87,11 @@ def request(
request_params["body"] = json_dumps(json, cls=NinjaJSONEncoder)
if data is None:
data = {}
if self.headers or request_params.get("headers"):
request_params["headers"] = {
**self.headers,
**request_params.get("headers", {}),
}
func, request, kwargs = self._resolve(method, path, data, request_params)
return self._call(func, request, kwargs) # type: ignore

Expand Down
23 changes: 23 additions & 0 deletions tests/test_test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ def simple_get(request):
return "test"


@router.get("/test-headers")
def get_headers(request):
return dict(request.headers)


client = TestClient(router)


Expand Down Expand Up @@ -78,3 +83,21 @@ def test_json_as_body():
ClientTestSchema.model_validate_json(request.body).model_dump_json()
== schema_instance.model_dump_json()
)


headered_client = TestClient(router, headers={"A": "a", "B": "b"})


def test_client_request_only_header():
r = client.get("/test-headers", headers={"A": "na"})
assert r.json() == {"A": "na"}


def test_headered_client_request_with_default_headers():
r = headered_client.get("/test-headers")
assert r.json() == {"A": "a", "B": "b"}


def test_headered_client_request_with_overwritten_and_additional_headers():
r = headered_client.get("/test-headers", headers={"A": "na", "C": "nc"})
assert r.json() == {"A": "na", "B": "b", "C": "nc"}

0 comments on commit aa46acf

Please sign in to comment.