|
| 1 | +# Copyright (c) 2023 EPAM Systems |
| 2 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | +# you may not use this file except in compliance with the License. |
| 4 | +# You may obtain a copy of the License at |
| 5 | +# |
| 6 | +# https://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +# |
| 8 | +# Unless required by applicable law or agreed to in writing, software |
| 9 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +# See the License for the specific language governing permissions and |
| 12 | +# limitations under the License |
| 13 | + |
| 14 | +import json |
| 15 | +import sys |
| 16 | +import pytest |
| 17 | + |
| 18 | +from unittest import mock |
| 19 | + |
| 20 | +from reportportal_client.core.rp_responses import RPResponse, AsyncRPResponse |
| 21 | + |
| 22 | + |
| 23 | +def json_error(): |
| 24 | + raise json.JSONDecodeError('Expecting value: line 1 column 1 (char 0)', '<html />', 0) |
| 25 | + |
| 26 | + |
| 27 | +@mock.patch('reportportal_client.core.rp_responses.logging.Logger.error') |
| 28 | +def test_json_decode_error(error_log): |
| 29 | + response = mock.Mock() |
| 30 | + response.ok = False |
| 31 | + del response.status |
| 32 | + response.status_code = 404 |
| 33 | + response.json.side_effect = json_error |
| 34 | + |
| 35 | + rp_response = RPResponse(response) |
| 36 | + assert rp_response.json is None |
| 37 | + error_log.assert_called_once() |
| 38 | + assert error_log.call_args_list[0][0][0] == ('Unable to decode JSON response, got failed response with code "404" ' |
| 39 | + 'please check your endpoint configuration or API key') |
| 40 | + |
| 41 | + |
| 42 | +@pytest.mark.skipif(sys.version_info < (3, 8), |
| 43 | + reason='the test requires AsyncMock which was introduced in Python 3.8') |
| 44 | +@mock.patch('reportportal_client.core.rp_responses.logging.Logger.error') |
| 45 | +@pytest.mark.asyncio |
| 46 | +async def test_json_decode_error_async(error_log): |
| 47 | + response = mock.AsyncMock() |
| 48 | + response.ok = False |
| 49 | + response.status = 403 |
| 50 | + response.json.side_effect = json_error |
| 51 | + |
| 52 | + rp_response = AsyncRPResponse(response) |
| 53 | + assert await rp_response.json is None |
| 54 | + error_log.assert_called_once() |
| 55 | + assert error_log.call_args_list[0][0][0] == ('Unable to decode JSON response, got failed response with code "403" ' |
| 56 | + 'please check your endpoint configuration or API key') |
0 commit comments