-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathvalidate.py
33 lines (26 loc) · 1.1 KB
/
validate.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
from .errors import ContentTypeError
ALLOWABLE_PAIRS = [
("image/tiff", "image/tiff; application=geotiff; profile=cloud-optimized"),
("image/tiff", "image/tiff; application=geotiff"),
("text/xml", "application/xml"),
]
IGNORED_CONTENT_TYPES = ["binary/octet-stream", "application/octet-stream"]
def content_type(actual: str, expected: str) -> None:
"""Validates that the actual content type matches the expected.
This is normally a simple string comparison, but has some extra rules:
* COGs are allowed in place of TIFFs, and vice versa
* Responses with ``binary/octet-stream`` and ``application/octet-stream``
are always allowed
Args:
actual: The actual content type
expected: The expected content type
Raises:
ContentTypeError: Raised if the actual doesn't match the expected.
"""
if (
actual != expected
and actual not in IGNORED_CONTENT_TYPES
and (actual, expected) not in ALLOWABLE_PAIRS
and (expected, actual) not in ALLOWABLE_PAIRS
):
raise ContentTypeError(actual=actual, expected=expected)