diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 97eca57..9908546 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -31,4 +31,4 @@ jobs: run: | . venv/bin/activate mkdir -p test-reports - pytest -v -s --junitxml=test-reports/junit.xml --cov=client --cov-config=.coveragerc --cov-report xml + pytest -v -s --junitxml=test-reports/junit.xml --cov=client --cov-config=.coveragerc --cov-report xml --cov-fail-under=100 diff --git a/client/connection.py b/client/connection.py index 0df0f20..1fd1f35 100644 --- a/client/connection.py +++ b/client/connection.py @@ -25,8 +25,11 @@ class ClientHosts(TypedDict): class Session(requests.Session): def __init__(self, *args, **kwargs): - if 'hostname' in kwargs: - self.hostname = kwargs.pop('hostname') + if 'hostname' not in kwargs: + raise ValueError('hostname is required') + + self.hostname = kwargs.pop('hostname') + super().__init__(*args, **kwargs) @retry @@ -37,8 +40,8 @@ def send(self, request, **kwargs): return super().send(request, **kwargs) def prepare_request(self, request): - if self.hostname is not None: - request.url = f'{self.hostname}/{request.url}' + request.url = f'{self.hostname}/{request.url}' + return super().prepare_request(request) class Connection(object): diff --git a/tests/api/test_transactions.py b/tests/api/test_transactions.py index fbc5412..4ffcffb 100644 --- a/tests/api/test_transactions.py +++ b/tests/api/test_transactions.py @@ -135,6 +135,23 @@ def test_all_unconfirmed_calls_correct_url_with_additional_params(): assert 'limit=69' in responses.calls[0].request.url assert 'orderBy=timestamp.epoch' in responses.calls[0].request.url + +def test_get_unconfirmed_calls_correct_url(): + transaction_id = '12345' + + responses.add( + responses.GET, + 'http://127.0.0.1:4002/api/transactions/unconfirmed/{}'.format(transaction_id), + json={'success': True}, + status=200 + ) + + client = ArkClient('http://127.0.0.1:4002/api') + client.transactions.get_unconfirmed(transaction_id) + assert len(responses.calls) == 1 + assert responses.calls[0].request.url == 'http://127.0.0.1:4002/api/transactions/unconfirmed/12345' + + def test_configuration_calls_correct_url(): responses.add( responses.GET, diff --git a/tests/test_connection.py b/tests/test_connection.py index dab0964..26dbf07 100644 --- a/tests/test_connection.py +++ b/tests/test_connection.py @@ -4,7 +4,7 @@ import responses -from client.connection import Connection +from client.connection import Connection, Session from client.exceptions import ArkHTTPException @@ -162,3 +162,31 @@ def test_http_methods_call_correct_url_with_params_and_return_correct_response(m assert data == {'success': True} assert len(responses.calls) == 1 assert responses.calls[0].request.url == 'http://127.0.0.1:4003/spongebob?foo=bar' + + +def test_session_detects_hostname_correctly(): + session = Session(hostname="test.com") + assert session.hostname == "test.com" + assert isinstance(session, requests.Session) + + +def test_session_throws_error_when_missing_hostname(): + with pytest.raises(ValueError) as exception: + Session() + + assert exception.value == 'hostname is required' + + +def test_session_prepends_hostname_to_url(): + responses.add( + responses.GET, + 'http://test.com/spongebob', + json={'success': True}, + status=200 + ) + + session = Session(hostname="http://test.com") + + session.get('spongebob') + assert len(responses.calls) == 1 + assert responses.calls[0].request.url == 'http://test.com/spongebob'