Skip to content

Commit 1a332a8

Browse files
committed
Passing test on mock for PNG
1 parent 0bfa532 commit 1a332a8

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

src/mock_vws/_mock_web_query_api.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,60 @@ def validate_image_format(
8383
)
8484

8585

86+
@wrapt.decorator
87+
def validate_image_file_contents(
88+
wrapped: Callable[..., str],
89+
instance: Any, # pylint: disable=unused-argument
90+
args: Tuple[_RequestObjectProxy, _Context],
91+
kwargs: Dict,
92+
) -> str:
93+
"""
94+
Validate the format of the image given to the query endpoint.
95+
96+
Args:
97+
wrapped: An endpoint function for `requests_mock`.
98+
instance: The class that the endpoint function is in.
99+
args: The arguments given to the endpoint function.
100+
kwargs: The keyword arguments given to the endpoint function.
101+
102+
Returns:
103+
The result of calling the endpoint.
104+
An `UNPROCESSABLE_ENTITY` response if the image is given and is not
105+
either a PNG or a JPEG.
106+
"""
107+
request, context = args
108+
body_file = io.BytesIO(request.body)
109+
110+
_, pdict = cgi.parse_header(request.headers['Content-Type'])
111+
parsed = cgi.parse_multipart(
112+
fp=body_file,
113+
pdict={
114+
'boundary': pdict['boundary'].encode(),
115+
},
116+
)
117+
118+
[image] = parsed['image']
119+
120+
image_file = io.BytesIO(image)
121+
try:
122+
Image.open(image_file).verify()
123+
except SyntaxError:
124+
context.status_code = codes.UNPROCESSABLE_ENTITY
125+
transaction_id = uuid.uuid4().hex
126+
result_code = ResultCodes.BAD_IMAGE.value
127+
128+
# The response has an unusual format of separators, so we construct it
129+
# manually.
130+
return (
131+
'{"transaction_id": '
132+
f'"{transaction_id}",'
133+
f'"result_code":"{result_code}"'
134+
'}'
135+
)
136+
137+
return wrapped(*args, **kwargs)
138+
139+
86140
@wrapt.decorator
87141
def validate_date_header_given(
88142
wrapped: Callable[..., str],
@@ -476,6 +530,7 @@ def decorator(method: Callable[..., str]) -> Callable[..., str]:
476530
validate_date_header_given,
477531
validate_include_target_data,
478532
validate_max_num_results,
533+
validate_image_file_contents,
479534
validate_image_format,
480535
validate_image_field_given,
481536
validate_extra_fields,

0 commit comments

Comments
 (0)