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
|
from io import BytesIO
from urllib.parse import parse_qs
from urllib.parse import urljoin
import pytest
from requests.models import Request
from requests.models import Response
from requests.structures import CaseInsensitiveDict
from urllib3.response import HTTPResponse
@pytest.fixture
def request_factory():
schema = "http"
server_name = "localhost"
def create_request(
method,
path,
subdomain=None,
query_string="",
content_type="application/json",
):
base_url = "://".join([schema, server_name])
url = urljoin(base_url, path)
params = parse_qs(query_string)
headers = {
"Content-Type": content_type,
}
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"
):
fp = BytesIO(data)
raw = HTTPResponse(fp, preload_content=False)
resp = Response()
resp.headers = CaseInsensitiveDict(
{
"Content-Type": content_type,
}
)
resp.status_code = status_code
resp.raw = raw
return resp
return create_response
|