openapi-core/tests/integration/contrib/requests/conftest.py

39 lines
1.1 KiB
Python
Raw Normal View History

2020-03-02 16:05:36 +00:00
import pytest
from requests.models import Request, Response
from requests.structures import CaseInsensitiveDict
2020-03-27 08:58:45 +00:00
from six import BytesIO, b
2020-03-02 16:05:36 +00:00
from six.moves.urllib.parse import urljoin, parse_qs
2020-03-27 08:58:45 +00:00
from urllib3.response import HTTPResponse
2020-03-02 16:05:36 +00:00
@pytest.fixture
def request_factory():
schema = 'http'
server_name = 'localhost'
def create_request(method, path, subdomain=None, query_string=''):
base_url = '://'.join([schema, server_name])
url = urljoin(base_url, path)
params = parse_qs(query_string)
headers = {
'Content-Type': 'application/json',
}
return Request(method, url, params=params, headers=headers)
return create_request
@pytest.fixture
def response_factory():
def create_response(
data, status_code=200, content_type='application/json'):
2020-03-27 08:58:45 +00:00
fp = BytesIO(b(data))
raw = HTTPResponse(fp, preload_content=False)
2020-03-02 16:05:36 +00:00
resp = Response()
resp.headers = CaseInsensitiveDict({
'Content-Type': content_type,
})
resp.status_code = status_code
2020-03-27 08:58:45 +00:00
resp.raw = raw
2020-03-02 16:05:36 +00:00
return resp
return create_response