Skip to content

Commit f9e223e

Browse files
committed
Separate validator for date and date format in query API [skip ci]
1 parent fd79135 commit f9e223e

File tree

1 file changed

+33
-3
lines changed

1 file changed

+33
-3
lines changed

src/mock_vws/_mock_web_query_api.py

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -278,14 +278,14 @@ def validate_include_target_data(
278278

279279

280280
@wrapt.decorator
281-
def validate_date(
281+
def validate_date_format(
282282
wrapped: Callable[..., str],
283283
instance: Any, # pylint: disable=unused-argument
284284
args: Tuple[_RequestObjectProxy, _Context],
285285
kwargs: Dict,
286286
) -> str:
287287
"""
288-
Validate the date header given to the query endpoint.
288+
Validate the format of the date header given to the query endpoint.
289289
290290
Args:
291291
wrapped: An endpoint function for `requests_mock`.
@@ -296,7 +296,6 @@ def validate_date(
296296
Returns:
297297
The result of calling the endpoint.
298298
An `UNAUTHORIZED` response if the date is in the wrong format.
299-
A `FORBIDDEN` response if the date is out of range.
300299
"""
301300
request, context = args
302301

@@ -313,6 +312,36 @@ def validate_date(
313312
context.headers['Content-Type'] = content_type
314313
return text
315314

315+
return wrapped(*args, **kwargs)
316+
317+
318+
@wrapt.decorator
319+
def validate_date(
320+
wrapped: Callable[..., str],
321+
instance: Any, # pylint: disable=unused-argument
322+
args: Tuple[_RequestObjectProxy, _Context],
323+
kwargs: Dict,
324+
) -> str:
325+
"""
326+
Validate date in the date header given to the query endpoint.
327+
328+
Args:
329+
wrapped: An endpoint function for `requests_mock`.
330+
instance: The class that the endpoint function is in.
331+
args: The arguments given to the endpoint function.
332+
kwargs: The keyword arguments given to the endpoint function.
333+
334+
Returns:
335+
The result of calling the endpoint.
336+
A `FORBIDDEN` response if the date is out of range.
337+
"""
338+
request, context = args
339+
340+
date_from_header = datetime.datetime.strptime(
341+
request.headers['Date'],
342+
'%a, %d %b %Y %H:%M:%S GMT',
343+
)
344+
316345
gmt = pytz.timezone('GMT')
317346
now = datetime.datetime.now(tz=gmt)
318347
date_from_header = date_from_header.replace(tzinfo=gmt)
@@ -527,6 +556,7 @@ def decorator(method: Callable[..., str]) -> Callable[..., str]:
527556
decorators = [
528557
validate_authorization,
529558
validate_date,
559+
validate_date_format,
530560
validate_date_header_given,
531561
validate_include_target_data,
532562
validate_max_num_results,

0 commit comments

Comments
 (0)