Skip to content

Commit

Permalink
Add test for unicode responses from API, and fix the bug that trigger…
Browse files Browse the repository at this point in the history
  • Loading branch information
Tom Hendrikx committed Sep 19, 2018
1 parent 7ca721c commit 83e13d2
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
2 changes: 2 additions & 0 deletions mollie/api/resources/base.py
Expand Up @@ -72,6 +72,8 @@ def list(self, **params):

def perform_api_call(self, http_method, path, data=None, params=None):
resp = self.client.perform_http_call(http_method, path, data, params)
# Mollie returns unicode data in responses, but doesn't set the Content-Type header correctly
resp.encoding = 'utf-8'
try:
result = resp.json() if resp.status_code != 204 else {}
except Exception:
Expand Down
12 changes: 12 additions & 0 deletions tests/responses/order_error.json
@@ -0,0 +1,12 @@
{
"status": 422,
"title": "Unprocessable Entity",
"detail": "Order line 1 is invalid. VAT amount is off. Expected VAT amount to be €3.47 (21.00% over €20.00), got €3.10",
"field": "lines.1.vatAmount",
"_links": {
"documentation": {
"href": "https://docs.mollie.com/guides/handling-errors",
"type": "text/html"
}
}
}
13 changes: 13 additions & 0 deletions tests/test_client.py
Expand Up @@ -207,3 +207,16 @@ def test_client_error_including_field_response(client, response):
client.payments.create(**params)
assert excinfo.match('The amount is higher than the maximum')
assert excinfo.value.field == 'amount'


def test_client_unicode_error(client, response):
"""An error response containing Unicode characters should also be processed correctly."""
response.post('https://api.mollie.com/v2/orders', 'order_error', status=422)
with pytest.raises(UnprocessableEntityError) as err:
# actual POST data for creating an order can be found in test_orders.py
client.orders.create({})

# printing the result should work even when utf-8 characters are in the response.
expected = "UnprocessableEntityError('Order line 1 is invalid. VAT amount is off. Expected VAT amount " \
"to be €3.47 (21.00% over €20.00), got €3.10',)"
assert repr(err.value) == expected

0 comments on commit 83e13d2

Please sign in to comment.