diff --git a/client/connection.py b/client/connection.py index 864a0e4..30d52a8 100644 --- a/client/connection.py +++ b/client/connection.py @@ -19,8 +19,7 @@ def giveup_handler(_): class Session(requests.Session): def __init__(self, *args, **kwargs): - if 'hostname' in kwargs: - self.hostname = kwargs.pop('hostname') + self.hostname = kwargs.pop('hostname', None) super().__init__(*args, **kwargs) @retry diff --git a/tests/api/test_delegates.py b/tests/api/test_delegates.py index 5ac2091..ad19b05 100644 --- a/tests/api/test_delegates.py +++ b/tests/api/test_delegates.py @@ -66,3 +66,75 @@ def test_get_calls_correct_url(): assert len(responses.calls) == 1 assert responses.calls[0].request.url == 'http://127.0.0.1:4002/delegates/12345' + + +def test_blocks_calls_correct_url_with_default_params(): + delegate_id = '12345' + responses.add( + responses.GET, + 'http://127.0.0.1:4002/delegates/{}/blocks'.format(delegate_id), + json={'success': True}, + status=200 + ) + + client = ArkClient('http://127.0.0.1:4002') + client.delegates.blocks(delegate_id) + assert len(responses.calls) == 1 + assert responses.calls[0].request.url == ( + 'http://127.0.0.1:4002/delegates/12345/blocks?limit=100' + ) + + +def test_blocks_calls_correct_url_with_passed_in_params(): + delegate_id = '12345' + responses.add( + responses.GET, + 'http://127.0.0.1:4002/delegates/{}/blocks'.format(delegate_id), + json={'success': True}, + status=200 + ) + + client = ArkClient('http://127.0.0.1:4002') + client.delegates.blocks(delegate_id, page=5, limit=69) + assert len(responses.calls) == 1 + assert responses.calls[0].request.url.startswith( + 'http://127.0.0.1:4002/delegates/12345/blocks?' + ) + assert 'page=5' in responses.calls[0].request.url + assert 'limit=69' in responses.calls[0].request.url + + +def test_voters_calls_correct_url_with_default_params(): + delegate_id = '12345' + responses.add( + responses.GET, + 'http://127.0.0.1:4002/delegates/{}/voters'.format(delegate_id), + json={'success': True}, + status=200 + ) + + client = ArkClient('http://127.0.0.1:4002') + client.delegates.voters(delegate_id) + assert len(responses.calls) == 1 + assert responses.calls[0].request.url == ( + 'http://127.0.0.1:4002/delegates/12345/voters?limit=100' + ) + + +def test_voters_calls_correct_url_with_passed_in_params(): + delegate_id = '12345' + responses.add( + responses.GET, + 'http://127.0.0.1:4002/delegates/{}/voters'.format(delegate_id), + json={'success': True}, + status=200 + ) + + client = ArkClient('http://127.0.0.1:4002') + client.delegates.voters(delegate_id, page=5, limit=69) + assert len(responses.calls) == 1 + assert responses.calls[0].request.url.startswith( + 'http://127.0.0.1:4002/delegates/12345/voters?' + ) + assert 'page=5' in responses.calls[0].request.url + assert 'limit=69' in responses.calls[0].request.url diff --git a/tests/api/test_locks.py b/tests/api/test_locks.py index 798dfb1..58bfbad 100644 --- a/tests/api/test_locks.py +++ b/tests/api/test_locks.py @@ -66,3 +66,39 @@ def test_get_calls_correct_url(): assert len(responses.calls) == 1 assert responses.calls[0].request.url == 'http://127.0.0.1:4002/locks/12345' + + +def test_unlocked_calls_correct_url_with_default_params(): + responses.add( + responses.POST, + 'http://127.0.0.1:4002/locks/unlocked', + json={'success': True}, + status=200 + ) + + client = ArkClient('http://127.0.0.1:4002') + client.locks.unlocked({'ids': ['12345', '67890']}) + assert len(responses.calls) == 1 + assert responses.calls[0].request.url == 'http://127.0.0.1:4002/locks/unlocked?limit=100' + assert json.loads(responses.calls[0].request.body.decode()) == { + 'ids': ['12345', '67890'] + } + + +def test_unlocked_calls_correct_url_with_passed_in_params(): + responses.add( + responses.POST, + 'http://127.0.0.1:4002/locks/unlocked', + json={'success': True}, + status=200 + ) + + client = ArkClient('http://127.0.0.1:4002') + client.locks.unlocked({'ids': ['12345']}, page=5, limit=69) + assert len(responses.calls) == 1 + assert responses.calls[0].request.url.startswith('http://127.0.0.1:4002/locks/unlocked?') + assert 'page=5' in responses.calls[0].request.url + assert 'limit=69' in responses.calls[0].request.url + assert json.loads(responses.calls[0].request.body.decode()) == { + 'ids': ['12345'] + } diff --git a/tests/api/test_transactions.py b/tests/api/test_transactions.py index b39921a..c055cb6 100644 --- a/tests/api/test_transactions.py +++ b/tests/api/test_transactions.py @@ -134,3 +134,51 @@ def test_all_unconfirmed_calls_correct_url_with_additional_params(): assert 'page=5' in responses.calls[0].request.url 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/transactions/unconfirmed/{}'.format(transaction_id), + json={'success': True}, + status=200 + ) + + client = ArkClient('http://127.0.0.1:4002') + client.transactions.get_unconfirmed(transaction_id) + + assert len(responses.calls) == 1 + assert responses.calls[0].request.url == ( + 'http://127.0.0.1:4002/transactions/unconfirmed/12345' + ) + + +def test_types_calls_correct_url(): + responses.add( + responses.GET, + 'http://127.0.0.1:4002/transactions/types', + json={'success': True}, + status=200 + ) + + client = ArkClient('http://127.0.0.1:4002') + client.transactions.types() + + assert len(responses.calls) == 1 + assert responses.calls[0].request.url == 'http://127.0.0.1:4002/transactions/types' + + +def test_fees_calls_correct_url(): + responses.add( + responses.GET, + 'http://127.0.0.1:4002/transactions/fees', + json={'success': True}, + status=200 + ) + + client = ArkClient('http://127.0.0.1:4002') + client.transactions.fees() + + assert len(responses.calls) == 1 + assert responses.calls[0].request.url == 'http://127.0.0.1:4002/transactions/fees' diff --git a/tests/test_connection.py b/tests/test_connection.py index 60bf571..5e261af 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 @@ -14,6 +14,21 @@ def test_connection_creation_sets_default_session_headers_and_variables(): assert isinstance(connection.session, requests.Session) assert connection.session.headers['Content-Type'] == 'application/json' + +def test_session_prepare_request_prefixes_url_with_hostname(): + session = Session(hostname='http://127.0.0.1:4003') + prepared = session.prepare_request(requests.Request('GET', 'spongebob')) + assert prepared.url == 'http://127.0.0.1:4003/spongebob' + + +def test_session_prepare_request_leaves_url_unchanged_without_hostname(): + session = Session() + assert session.hostname is None + + prepared = session.prepare_request(requests.Request('GET', 'http://127.0.0.1:4003/spongebob')) + assert prepared.url == 'http://127.0.0.1:4003/spongebob' + + def test_connection_request_retry_successful(): responses.add( responses.GET,