Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
11 changes: 7 additions & 4 deletions client/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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):
Expand Down
17 changes: 17 additions & 0 deletions tests/api/test_transactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
30 changes: 29 additions & 1 deletion tests/test_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import responses

from client.connection import Connection
from client.connection import Connection, Session
from client.exceptions import ArkHTTPException


Expand Down Expand Up @@ -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'