Skip to content

Commit b728167

Browse files
authored
Merge pull request #371 from jparise/media_type_finder
Handle missing MIME type in MediaTypeFinder
2 parents 4ce032c + d064386 commit b728167

File tree

2 files changed

+51
-3
lines changed

2 files changed

+51
-3
lines changed

openapi_core/templating/media_types/finders.py

+4-3
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()))
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)