diff --git a/mollie/api/resources/base.py b/mollie/api/resources/base.py index bd0c3b59..0b619d04 100644 --- a/mollie/api/resources/base.py +++ b/mollie/api/resources/base.py @@ -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: diff --git a/tests/responses/order_error.json b/tests/responses/order_error.json new file mode 100644 index 00000000..02035834 --- /dev/null +++ b/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" + } + } +} diff --git a/tests/test_client.py b/tests/test_client.py index 9a4af4a2..06fbcddf 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -208,3 +208,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