|
| 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 MockRequest |
| 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 | + request = MockRequest( |
| 27 | + "http://petstore.swagger.io", |
| 28 | + "get", |
| 29 | + "/pets", |
| 30 | + mimetype="application/json", |
| 31 | + ) |
| 32 | + |
| 33 | + _, mimetype = finder.find(request) |
| 34 | + assert mimetype == "application/json" |
| 35 | + |
| 36 | + def test_match(self, finder, content): |
| 37 | + request = MockRequest( |
| 38 | + "http://petstore.swagger.io", "get", "/pets", mimetype="text/html" |
| 39 | + ) |
| 40 | + |
| 41 | + _, mimetype = finder.find(request) |
| 42 | + assert mimetype == "text/*" |
| 43 | + |
| 44 | + def test_not_found(self, finder, content): |
| 45 | + request = MockRequest( |
| 46 | + "http://petstore.swagger.io", "get", "/pets", mimetype="unknown" |
| 47 | + ) |
| 48 | + |
| 49 | + with pytest.raises(MediaTypeNotFound): |
| 50 | + finder.find(request) |
| 51 | + |
| 52 | + def test_missing(self, finder, content): |
| 53 | + request = MockRequest( |
| 54 | + "http://petstore.swagger.io", "get", "/pets", mimetype=None |
| 55 | + ) |
| 56 | + |
| 57 | + with pytest.raises(MediaTypeNotFound): |
| 58 | + finder.find(request) |
0 commit comments