diff --git a/setra_client/client.py b/setra_client/client.py index bfe4c6cbc2b11f997a0659251def2a0f2a793e42..8c0fee87c6deee4f507da1a310470847a9a6d37a 100644 --- a/setra_client/client.py +++ b/setra_client/client.py @@ -370,7 +370,7 @@ class SetraClient(object): if response.status_code == 202: return ResponseStatusEnum.ACCEPTED, {'code': 202, 'content': None} elif response.status_code == 409: - return ResponseStatusEnum.CONFLICT, {'code': 409, 'content': None} + return ResponseStatusEnum.CONFLICT, {'code': 409, 'content': content} else: return ResponseStatusEnum.UNKNOWN, { 'code': response.status_code, 'content': content} @@ -397,7 +397,7 @@ class SetraClient(object): if response.status_code == 204: return ResponseStatusEnum.ACCEPTED, {'code': 204, 'content': None} elif response.status_code == 409: - return ResponseStatusEnum.CONFLICT, {'code': 409, 'content': None} + return ResponseStatusEnum.CONFLICT, {'code': 409, 'content': content} else: return ResponseStatusEnum.UNKNOWN, { 'code': response.status_code, 'content': content} @@ -526,15 +526,17 @@ class SetraClient(object): data=abworder.json(), headers=headers, return_response=True) + + try: + content = response.json() + except ValueError: + content = response.content + if response.status_code == 202: - return ResponseStatusEnum.ACCEPTED, {'code': 202, 'content': None} + return ResponseStatusEnum.ACCEPTED, {'code': 202, 'content': content} elif response.status_code == 409: - return ResponseStatusEnum.CONFLICT, {'code': 409, 'content': None} + return ResponseStatusEnum.CONFLICT, {'code': 409, 'content': content} else: - try: - content = response.json() - except ValueError: - content = response.content response.raise_for_status() return ResponseStatusEnum.UNKNOWN, { 'code': response.status_code, 'content': content} diff --git a/tests/test_client.py b/tests/test_client.py index c9426bf27e9bee078a5a1d13b2a9386a17e1b845..a551dbe4548ab6af91fdacd9a53b7775f6cfc9e7 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -141,10 +141,10 @@ def test_get_failing_batch_without_return_response2( (because we expect json from setra, and it will give error with text content)""" requests_mock.get(batch_url, text="some content", status_code=200) - with pytest.raises(JSONDecodeError) as err: + with pytest.raises(requests.exceptions.JSONDecodeError) as err: client.call(method_name="GET", url=batch_url, return_response=False) - assert err.type == JSONDecodeError + assert err.type == requests.exceptions.JSONDecodeError # Test post method @@ -336,7 +336,7 @@ def test_successfully_post_batch_with_voucher(client, batch_with_voucher_fixture state, data = client.post_new_batch(batch) # we get a response object back assert state == 'Accepted' - assert data == {} + assert data == {'code': 202, 'content': None} def test_successfully_post_batch_with_voucher_and_response( @@ -350,7 +350,7 @@ def test_successfully_post_batch_with_voucher_and_response( batch = InputBatch.from_dict(batch_with_voucher_fixture) state, data = client.post_new_batch(batch) # we get a response object back assert state == 'Accepted' - assert data == {} + assert data == {'code': 202, 'content': None} def test_conflicting_post_new_batch(client, batch_with_voucher_fixture, requests_mock, baseurl): @@ -359,7 +359,7 @@ def test_conflicting_post_new_batch(client, batch_with_voucher_fixture, requests batch = InputBatch.from_dict(batch_with_voucher_fixture) state, data = client.post_new_batch(batch) assert state == 'Conflict' - assert data == {'error': 'batch is being processed'} + assert data == {'code': 409, 'content': {'error': 'batch is being processed'}} def test_unknown_post_new_batch_state(client, batch_with_voucher_fixture, requests_mock, baseurl): @@ -530,8 +530,11 @@ def test_send_in_abworder(client, requests_mock, baseurl, complete_abw_order_fix response = client.post_add_abw_order(abworder) if isinstance(response, tuple): - assert response[0].decode("utf-8") == json.dumps(resp) - assert response[1] == "Accepted" + assert response[0] == "Accepted" + assert response[1] == {'code': 202, 'content': { + 'responsible': 'responsible2', + 'interface': 'testinterface', + 'client': 'testclient'}} def test_send_in_abworder_failure_conflict( @@ -549,8 +552,8 @@ def test_send_in_abworder_failure_conflict( response = client.post_add_abw_order(abworder) assert isinstance(response, tuple) - assert response[0].decode("utf-8") == json.dumps(resp) - assert response[1] == "Conflict" + assert response[1]['content'] == resp + assert response[0] == "Conflict" def test_send_in_abworder_failure(