|
| 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