Skip to content

Commit d064386

Browse files
committed
Handle missing MIME types in MediaTypeFinder
It's possible for a response not to have a MIME type (mimetype=None). Previously, this would result in an exception from within fnmatch(): TypeError: expected str, bytes or os.PathLike object, not NoneType This change guards against that condition and raises MediaTypeNotFound instead. Tests have also been added for this module.
1 parent 4ce032c commit d064386

File tree

2 files changed

+51
-3
lines changed

2 files changed

+51
-3
lines changed

openapi_core/templating/media_types/finders.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@ def find(self, request):
1212
if request.mimetype in self.content:
1313
return self.content / request.mimetype, request.mimetype
1414

15-
for key, value in self.content.items():
16-
if fnmatch.fnmatch(request.mimetype, key):
17-
return value, key
15+
if request.mimetype:
16+
for key, value in self.content.items():
17+
if fnmatch.fnmatch(request.mimetype, key):
18+
return value, key
1819

1920
raise MediaTypeNotFound(request.mimetype, list(self.content.keys()))
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import pytest
2+
3+
from openapi_core.spec.paths import SpecPath
4+
from openapi_core.templating.media_types.exceptions import MediaTypeNotFound
5+
from openapi_core.templating.media_types.finders import MediaTypeFinder
6+
from openapi_core.testing import MockResponse
7+
8+
9+
class TestMediaTypes:
10+
@pytest.fixture(scope="class")
11+
def spec(self):
12+
return {
13+
"application/json": {"schema": {"type": "object"}},
14+
"text/*": {"schema": {"type": "object"}},
15+
}
16+
17+
@pytest.fixture(scope="class")
18+
def content(self, spec):
19+
return SpecPath.from_spec(spec)
20+
21+
@pytest.fixture(scope="class")
22+
def finder(self, content):
23+
return MediaTypeFinder(content)
24+
25+
def test_exact(self, finder, content):
26+
response = MockResponse("", mimetype="application/json")
27+
28+
_, mimetype = finder.find(response)
29+
assert mimetype == "application/json"
30+
31+
def test_match(self, finder, content):
32+
response = MockResponse("", mimetype="text/html")
33+
34+
_, mimetype = finder.find(response)
35+
assert mimetype == "text/*"
36+
37+
def test_not_found(self, finder, content):
38+
response = MockResponse("", mimetype="unknown")
39+
40+
with pytest.raises(MediaTypeNotFound):
41+
finder.find(response)
42+
43+
def test_missing(self, finder, content):
44+
response = MockResponse("", mimetype=None)
45+
46+
with pytest.raises(MediaTypeNotFound):
47+
finder.find(response)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy