Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

strip Request mimetype #553

Merged
merged 2 commits into from
Apr 11, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion openapi_core/contrib/requests/requests.py
Original file line number Diff line number Diff line change
@@ -80,7 +80,7 @@ def mimetype(self) -> str:
return str(
self.request.headers.get("Content-Type")
or self.request.headers.get("Accept")
)
).split(";")[0]


class RequestsOpenAPIWebhookRequest(RequestsOpenAPIRequest):
10 changes: 8 additions & 2 deletions tests/unit/contrib/requests/conftest.py
Original file line number Diff line number Diff line change
@@ -14,12 +14,18 @@ def request_factory():
schema = "http"
server_name = "localhost"

def create_request(method, path, subdomain=None, query_string=""):
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": "application/json",
"Content-Type": content_type,
}
return Request(method, url, params=params, headers=headers)

27 changes: 27 additions & 0 deletions tests/unit/contrib/requests/test_requests_requests.py
Original file line number Diff line number Diff line change
@@ -115,3 +115,30 @@ def test_hash_param(self, request_factory, request):
assert openapi_request.path == "/browse/#12"
assert openapi_request.body == prepared.body
assert openapi_request.mimetype == "application/json"

def test_content_type_with_charset(self, request_factory, request):
request = request_factory(
"GET",
"/",
subdomain="www",
content_type="application/json; charset=utf-8",
)

openapi_request = RequestsOpenAPIRequest(request)

path = {}
query = ImmutableMultiDict([])
headers = Headers(dict(request.headers))
cookies = {}
prepared = request.prepare()
assert openapi_request.parameters == RequestParameters(
path=path,
query=query,
header=headers,
cookie=cookies,
)
assert openapi_request.method == request.method.lower()
assert openapi_request.host_url == "http://localhost"
assert openapi_request.path == "/"
assert openapi_request.body == prepared.body
assert openapi_request.mimetype == "application/json"