File size: 1,562 Bytes
079c32c |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
import json
from contextlib import contextmanager
from typing import Optional, Mapping, Any
import pytest
import requests
import responses
from requests import HTTPError
class _HTTPErrorGenerator:
@classmethod
def _generate_exception(
cls, code: int, message: str, data: Optional[Mapping[str, Any]] = None, success: bool = False
):
@contextmanager
def _yield_func():
with responses.RequestsMock(assert_all_requests_are_fired=False) as rsp:
rsp.add(
**{
'method': responses.GET,
'url': 'http://example.com/path',
'body': json.dumps(
{
"success": not not success,
"code": int(code),
"message": str(message),
"data": data or {},
}
),
'status': 400,
'content_type': 'application/json',
}
)
yield
@responses.activate
def _get_exception():
try:
with _yield_func():
response = requests.get('http://example.com/path')
response.raise_for_status()
except HTTPError as err:
return err
else:
pytest.fail('Should not reach here.')
return _get_exception()
|