Skip to content

Commit 6e076ee

Browse files
committed
Merge pull request #168 from jairhenrique/build_absolute_uri
Implements fake build_absolute_uri on test client
2 parents c682a6c + 118fb9d commit 6e076ee

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

ninja/testing/client.py

+12-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from json import dumps as json_dumps, loads as json_loads
2-
from typing import Any, Callable, Dict, List, Tuple, Union
2+
from typing import Any, Callable, Dict, List, Optional, Tuple, Union
33
from unittest.mock import Mock
4+
from urllib.parse import urljoin
45

56
import django
67
from django.http import QueryDict, StreamingHttpResponse
@@ -10,6 +11,15 @@
1011
from ninja.responses import Response as HttpResponse
1112

1213

14+
def build_absolute_uri(location: Optional[str] = None) -> str:
15+
base = "http://testlocation/"
16+
17+
if location:
18+
base = urljoin(base, location)
19+
20+
return base
21+
22+
1323
# TODO: this should be changed
1424
# maybe add here urlconf object and add urls from here
1525
class NinjaClientBase:
@@ -89,6 +99,7 @@ def _build_request(
8999
request.COOKIES = {}
90100
request._dont_enforce_csrf_checks = True
91101
request.is_secure.return_value = False
102+
request.build_absolute_uri = build_absolute_uri
92103

93104
if "user" not in request_params:
94105
request.user.is_authenticated = False

tests/test_test_client.py

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import pytest
2+
3+
from http import HTTPStatus
4+
from ninja.testing import TestClient
5+
from ninja import Router
6+
7+
router = Router()
8+
9+
@router.get("/request/build_absolute_uri")
10+
def request_build_absolute_uri(request):
11+
return request.build_absolute_uri()
12+
13+
14+
@router.get("/request/build_absolute_uri/location")
15+
def request_build_absolute_uri_location(request):
16+
return request.build_absolute_uri('location')
17+
18+
19+
client = TestClient(router)
20+
21+
22+
@pytest.mark.parametrize('path,expected_status,expected_response', [
23+
('/request/build_absolute_uri', HTTPStatus.OK, 'http://testlocation/'),
24+
('/request/build_absolute_uri/location', HTTPStatus.OK, 'http://testlocation/location'),
25+
])
26+
def test_sync_build_absolute_uri(path, expected_status, expected_response):
27+
response = client.get(path)
28+
29+
assert response.status_code == expected_status
30+
assert response.json() == expected_response

0 commit comments

Comments
 (0)