Skip to content

Commit

Permalink
Made NinjaTestClient accept COOKIES + documentation (#1311)
Browse files Browse the repository at this point in the history
  • Loading branch information
c4ffein authored Oct 4, 2024
1 parent de40a7f commit 8e45421
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
10 changes: 10 additions & 0 deletions docs/docs/guides/testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ It is also possible to access the deserialized data using the `data` property:
self.assertEqual(response.data, {"msg": "Hello World"})
```

## Attributes
Arbitrary attributes can be added to the request object by passing keyword arguments to the client request methods:
```python
class HelloTest(TestCase):
Expand All @@ -47,9 +48,18 @@ class HelloTest(TestCase):
response = client.get("/hello", company_id=1)
```

### Headers
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"})
```

### Cookies
It is also possible to specify cookies, both from the TestCase instanciation and the actual request:
```python
client = TestClient(router, COOKIES={"A": "a", "B": "b"})
# The request will be made with {"A": "na", "B": "b", "C": "nc"} cookies
response = client.get("/test-cookies", COOKIES={"A": "na", "C": "nc"})
```
7 changes: 7 additions & 0 deletions ninja/testing/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,10 @@ def __init__(
self,
router_or_app: Union[NinjaAPI, Router],
headers: Optional[Dict[str, str]] = None,
COOKIES: Optional[Dict[str, str]] = None,
) -> None:
self.headers = headers or {}
self.cookies = COOKIES or {}
self.router_or_app = router_or_app

def get(
Expand Down Expand Up @@ -92,6 +94,11 @@ def request(
**self.headers,
**request_params.get("headers", {}),
}
if self.cookies or request_params.get("COOKIES"):
request_params["COOKIES"] = {
**self.cookies,
**request_params.get("COOKIES", {}),
}
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 @@ -32,6 +32,11 @@ def get_headers(request):
return dict(request.headers)


@router.get("/test-cookies")
def get_cookies(request):
return dict(request.COOKIES)


client = TestClient(router)


Expand Down Expand Up @@ -101,3 +106,21 @@ def test_headered_client_request_with_default_headers():
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"}


cookied_client = TestClient(router, COOKIES={"A": "a", "B": "b"})


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


def test_headered_client_request_with_default_cookies():
r = cookied_client.get("/test-cookies")
assert r.json() == {"A": "a", "B": "b"}


def test_headered_client_request_with_overwritten_and_additional_cookies():
r = cookied_client.get("/test-cookies", COOKIES={"A": "na", "C": "nc"})
assert r.json() == {"A": "na", "B": "b", "C": "nc"}

0 comments on commit 8e45421

Please sign in to comment.