Skip to content

Commit

Permalink
Replace response mimetype with content_type
Browse files Browse the repository at this point in the history
  • Loading branch information
p1c2u committed Oct 21, 2023
1 parent 519dafd commit d99c7e4
Show file tree
Hide file tree
Showing 16 changed files with 36 additions and 30 deletions.
2 changes: 1 addition & 1 deletion openapi_core/contrib/aiohttp/responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def status_code(self) -> int:
return self.response.status

@property
def mimetype(self) -> str:
def content_type(self) -> str:
return self.response.content_type or ""

@property
Expand Down
2 changes: 1 addition & 1 deletion openapi_core/contrib/django/responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def headers(self) -> Headers:
return Headers(self.response.headers.items())

@property
def mimetype(self) -> str:
def content_type(self) -> str:
content_type = self.response.get("Content-Type", "")
assert isinstance(content_type, str)
return content_type
10 changes: 5 additions & 5 deletions openapi_core/contrib/falcon/responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ def status_code(self) -> int:
return int(self.response.status[:3])

@property
def mimetype(self) -> str:
mimetype = ""
def content_type(self) -> str:
content_type = ""
if self.response.content_type:
mimetype = self.response.content_type.partition(";")[0]
content_type = self.response.content_type
else:
mimetype = self.response.options.default_media_type
return mimetype
content_type = self.response.options.default_media_type
return content_type

@property
def headers(self) -> Headers:
Expand Down
2 changes: 1 addition & 1 deletion openapi_core/contrib/requests/responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def status_code(self) -> int:
return int(self.response.status_code)

@property
def mimetype(self) -> str:
def content_type(self) -> str:
return str(self.response.headers.get("Content-Type", ""))

@property
Expand Down
4 changes: 2 additions & 2 deletions openapi_core/contrib/starlette/responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ def status_code(self) -> int:
return self.response.status_code

@property
def mimetype(self) -> str:
return self.response.media_type or ""
def content_type(self) -> str:
return self.response.headers.get("Content-Type") or ""

@property
def headers(self) -> Headers:
Expand Down
2 changes: 1 addition & 1 deletion openapi_core/contrib/werkzeug/responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def status_code(self) -> int:
return self.response._status_code

@property
def mimetype(self) -> str:
def content_type(self) -> str:
return str(self.response.mimetype)

@property
Expand Down
6 changes: 3 additions & 3 deletions openapi_core/protocols.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,8 @@ class Response(Protocol):
The status code as integer.
headers
Response headers as Headers.
mimetype
Lowercase content type without charset.
content_type
The content type with parameters and always lowercase.
"""

@property
Expand All @@ -128,7 +128,7 @@ def status_code(self) -> int:
...

@property
def mimetype(self) -> str:
def content_type(self) -> str:
...

@property
Expand Down
4 changes: 2 additions & 2 deletions openapi_core/testing/responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ def __init__(
data: str,
status_code: int = 200,
headers: Optional[Dict[str, Any]] = None,
mimetype: str = "application/json",
content_type: str = "application/json",
):
self.data = data
self.status_code = status_code
self.headers = Headers(headers or {})
self.mimetype = mimetype
self.content_type = content_type
4 changes: 2 additions & 2 deletions openapi_core/unmarshalling/response/unmarshallers.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def _unmarshal(

try:
validated_data = self._get_data(
response.data, response.mimetype, operation_response
response.data, response.content_type, operation_response
)
except DataValidationError as exc:
validated_data = None
Expand Down Expand Up @@ -106,7 +106,7 @@ def _unmarshal_data(

try:
validated = self._get_data(
response.data, response.mimetype, operation_response
response.data, response.content_type, operation_response
)
except DataValidationError as exc:
validated = None
Expand Down
14 changes: 10 additions & 4 deletions openapi_core/validation/response/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,10 @@ def iter_errors(
return

yield from self._iter_data_errors(
response.status_code, response.data, response.mimetype, operation
response.status_code,
response.data,
response.content_type,
operation,
)


Expand Down Expand Up @@ -273,7 +276,7 @@ def iter_errors(
response.status_code,
response.data,
response.headers,
response.mimetype,
response.content_type,
operation,
)

Expand All @@ -292,7 +295,10 @@ def iter_errors(
return

yield from self._iter_data_errors(
response.status_code, response.data, response.mimetype, operation
response.status_code,
response.data,
response.content_type,
operation,
)


Expand Down Expand Up @@ -331,7 +337,7 @@ def iter_errors(
response.status_code,
response.data,
response.headers,
response.mimetype,
response.content_type,
operation,
)

Expand Down
4 changes: 2 additions & 2 deletions tests/integration/test_petstore.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ def test_get_pets_response_no_schema(self, spec):

data = b"<html></html>"
response = MockResponse(
data, status_code=404, mimetype="text/html; charset=utf-8"
data, status_code=404, content_type="text/html; charset=utf-8"
)

response_result = unmarshal_response(request, response, spec=spec)
Expand Down Expand Up @@ -1372,7 +1372,7 @@ def test_get_pet_wildcard(self, spec):
assert result.body is None

data = b"imagedata"
response = MockResponse(data, mimetype="image/png")
response = MockResponse(data, content_type="image/png")

response_result = unmarshal_response(request, response, spec=spec)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def test_invalid_response(self, response_unmarshaller):

def test_invalid_content_type(self, response_unmarshaller):
request = MockRequest(self.host_url, "get", "/v1/pets")
response = MockResponse("Not Found", mimetype="text/csv")
response = MockResponse("Not Found", content_type="text/csv")

result = response_unmarshaller.unmarshal(request, response)

Expand Down
2 changes: 1 addition & 1 deletion tests/integration/validation/test_response_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def test_invalid_response(self, response_validator):

def test_invalid_content_type(self, response_validator):
request = MockRequest(self.host_url, "get", "/v1/pets")
response = MockResponse("Not Found", mimetype="text/csv")
response = MockResponse("Not Found", content_type="text/csv")

with pytest.raises(DataValidationError) as exc_info:
response_validator.validate(request, response)
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/contrib/django/test_django.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ def test_stream_response(self, response_factory):

assert openapi_response.data == "foo\nbar\nbaz\n"
assert openapi_response.status_code == response.status_code
assert openapi_response.mimetype == response["Content-Type"]
assert openapi_response.content_type == response["Content-Type"]

def test_redirect_response(self, response_factory):
data = "/redirected/"
Expand All @@ -194,4 +194,4 @@ def test_redirect_response(self, response_factory):

assert openapi_response.data == data
assert openapi_response.status_code == response.status_code
assert openapi_response.mimetype == response["Content-Type"]
assert openapi_response.content_type == response["Content-Type"]
2 changes: 1 addition & 1 deletion tests/unit/contrib/flask/test_flask_responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ def test_invalid_server(self, response_factory):

assert openapi_response.data == data
assert openapi_response.status_code == status_code
assert openapi_response.mimetype == response.mimetype
assert openapi_response.content_type == response.mimetype
2 changes: 1 addition & 1 deletion tests/unit/contrib/requests/test_requests_responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ def test_invalid_server(self, response_factory):
assert openapi_response.data == data
assert openapi_response.status_code == status_code
mimetype = response.headers.get("Content-Type")
assert openapi_response.mimetype == mimetype
assert openapi_response.content_type == mimetype

0 comments on commit d99c7e4

Please sign in to comment.