From b89b945a0fa894740d88dc11f03c1b53110e4c79 Mon Sep 17 00:00:00 2001 From: Jim Paris Date: Wed, 31 Jul 2013 13:37:04 -0400 Subject: [PATCH] Better responses to invalid HTTP times --- nilmdb/server/server.py | 16 ++++++++++++---- tests/test_client.py | 13 +++++++++++++ 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/nilmdb/server/server.py b/nilmdb/server/server.py index b569775..89a06d7 100644 --- a/nilmdb/server/server.py +++ b/nilmdb/server/server.py @@ -84,10 +84,18 @@ class Stream(NilmApp): # Helpers def _get_times(self, start_param, end_param): (start, end) = (None, None) - if start_param is not None: - start = string_to_timestamp(start_param) - if end_param is not None: - end = string_to_timestamp(end_param) + try: + if start_param is not None: + start = string_to_timestamp(start_param) + except Exception: + raise cherrypy.HTTPError("400 Bad Request", sprintf( + "invalid start (%s): must be a numeric timestamp", start_param)) + try: + if end_param is not None: + end = string_to_timestamp(end_param) + except Exception: + raise cherrypy.HTTPError("400 Bad Request", sprintf( + "invalid end (%s): must be a numeric timestamp", end_param)) if start is not None and end is not None: if start >= end: raise cherrypy.HTTPError( diff --git a/tests/test_client.py b/tests/test_client.py index 9834e51..058f45b 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -242,6 +242,19 @@ class TestClient(object): in_("400 Bad Request", str(e.exception)) in_("start must precede end", str(e.exception)) + # Invalid times in HTTP request + with assert_raises(ClientError) as e: + client.http.put("stream/insert", "", { "path": "/newton/prep", + "start": "asdf", "end": 0 }) + in_("400 Bad Request", str(e.exception)) + in_("invalid start", str(e.exception)) + + with assert_raises(ClientError) as e: + client.http.put("stream/insert", "", { "path": "/newton/prep", + "start": 0, "end": "asdf" }) + in_("400 Bad Request", str(e.exception)) + in_("invalid end", str(e.exception)) + # Good content type with assert_raises(ClientError) as e: client.http.put("stream/insert", "",