|
20 | 20 | from reportportal_client.core.rp_responses import RPResponse, AsyncRPResponse
|
21 | 21 |
|
22 | 22 |
|
| 23 | +class JSONDecodeError(ValueError): |
| 24 | + pass |
| 25 | + |
| 26 | + |
23 | 27 | def json_error():
|
24 | 28 | raise json.JSONDecodeError('Expecting value: line 1 column 1 (char 0)', '<html />', 0)
|
25 | 29 |
|
26 | 30 |
|
| 31 | +def custom_error(): |
| 32 | + raise JSONDecodeError('Expecting value: line 1 column 1 (char 0)') |
| 33 | + |
| 34 | + |
27 | 35 | @mock.patch('reportportal_client.core.rp_responses.logging.Logger.error')
|
28 |
| -def test_json_decode_error(error_log): |
| 36 | +@pytest.mark.parametrize('ok, response_code, error_function, expected_message', [ |
| 37 | + (False, 404, json_error, 'Unable to decode JSON response, got failed response with code "404" please check your ' |
| 38 | + 'endpoint configuration or API key'), |
| 39 | + (True, 200, json_error, 'Unable to decode JSON response, got passed response with code "200" please check your ' |
| 40 | + 'endpoint configuration or API key'), |
| 41 | + (True, 200, custom_error, 'Unable to decode JSON response, got passed response with code "200" please check your ' |
| 42 | + 'endpoint configuration or API key'), |
| 43 | +]) |
| 44 | +def test_custom_decode_error(error_log, ok, response_code, error_function, expected_message): |
29 | 45 | response = mock.Mock()
|
30 |
| - response.ok = False |
| 46 | + response.ok = ok |
31 | 47 | del response.status
|
32 |
| - response.status_code = 404 |
33 |
| - response.json.side_effect = json_error |
| 48 | + response.status_code = response_code |
| 49 | + response.json.side_effect = error_function |
34 | 50 |
|
35 | 51 | rp_response = RPResponse(response)
|
36 | 52 | assert rp_response.json is None
|
37 | 53 | 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') |
| 54 | + assert error_log.call_args_list[0][0][0] == expected_message |
40 | 55 |
|
41 | 56 |
|
42 | 57 | @pytest.mark.skipif(sys.version_info < (3, 8),
|
43 | 58 | reason='the test requires AsyncMock which was introduced in Python 3.8')
|
44 | 59 | @mock.patch('reportportal_client.core.rp_responses.logging.Logger.error')
|
45 | 60 | @pytest.mark.asyncio
|
46 |
| -async def test_json_decode_error_async(error_log): |
| 61 | +@pytest.mark.parametrize('ok, response_code, error_function, expected_message', [ |
| 62 | + (False, 404, json_error, 'Unable to decode JSON response, got failed response with code "404" please check your ' |
| 63 | + 'endpoint configuration or API key'), |
| 64 | + (True, 200, json_error, 'Unable to decode JSON response, got passed response with code "200" please check your ' |
| 65 | + 'endpoint configuration or API key'), |
| 66 | + (True, 200, custom_error, 'Unable to decode JSON response, got passed response with code "200" please check your ' |
| 67 | + 'endpoint configuration or API key'), |
| 68 | +]) |
| 69 | +async def test_json_decode_error_async(error_log, ok, response_code, error_function, expected_message): |
47 | 70 | response = mock.AsyncMock()
|
48 |
| - response.ok = False |
49 |
| - response.status = 403 |
50 |
| - response.json.side_effect = json_error |
| 71 | + response.ok = ok |
| 72 | + response.status = response_code |
| 73 | + response.json.side_effect = error_function |
51 | 74 |
|
52 | 75 | rp_response = AsyncRPResponse(response)
|
53 | 76 | assert await rp_response.json is None
|
54 | 77 | 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') |
| 78 | + assert error_log.call_args_list[0][0][0] == expected_message |
0 commit comments