Skip to content

Commit

Permalink
Add test for NLX rewriting
Browse files Browse the repository at this point in the history
  • Loading branch information
Viicos committed Oct 9, 2023
1 parent 8603918 commit 62d4776
Showing 1 changed file with 81 additions and 0 deletions.
81 changes: 81 additions & 0 deletions tests/test_nlx_rewriting.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
from zgw_consumers.client import build_client
from zgw_consumers.constants import AuthTypes
from zgw_consumers.test.factories import ServiceFactory


def test_request_url_and_response_data_rewritten(requests_mock):
nlx_service = ServiceFactory.create(
label="Service with NLX",
api_root="https://example.com/",
auth_type=AuthTypes.no_auth,
nlx="http://localhost:8081/:serial-number/:service/",
)
client = build_client(nlx_service)

requests_mock.get(
"http://localhost:8081/:serial-number/:service/some-resource",
json=lambda req, _: {"url": req.url},
)

with client:
response_data = client.get("some-resource").json()

assert requests_mock.last_request.method == "GET"
assert (
requests_mock.last_request.url
== "http://localhost:8081/:serial-number/:service/some-resource"
)
assert response_data == {"url": "https://example.com/some-resource"}


def test_non_json_response_data(requests_mock):
nlx_service = ServiceFactory.create(
label="Service with NLX",
api_root="https://example.com/",
auth_type=AuthTypes.no_auth,
nlx="http://localhost:8081/:serial-number/:service/",
)
client = build_client(nlx_service)

requests_mock.get(
"http://localhost:8081/:serial-number/:service/some-resource",
content=b"AAAAA",
)

with client:
response_data = client.get("some-resource").content

assert requests_mock.last_request.method == "GET"
assert (
requests_mock.last_request.url
== "http://localhost:8081/:serial-number/:service/some-resource"
)
assert response_data == b"AAAAA"


def test_service_without_nlx(requests_mock):
ServiceFactory.create(
label="Service with NLX",
api_root="https://example.com/",
auth_type=AuthTypes.no_auth,
nlx="http://localhost:8081/:serial-number/:service/",
)
normal_service = ServiceFactory.create(
label="Service without NLX",
api_root="https://second.example.com/",
auth_type=AuthTypes.no_auth,
)

client = build_client(normal_service)
requests_mock.get(
"https://second.example.com/some-resource",
json={"url": "https://example.com"},
)

with client:
response_data = client.get("some-resource").json()

assert requests_mock.last_request.method == "GET"
assert requests_mock.last_request.url, "https://second.example.com/some-resource"
# no rewriting of any sorts
assert response_data, {"url": "https://example.com"}

0 comments on commit 62d4776

Please sign in to comment.