From 8cb505e531da42ba37774c92cd2a8b333d429a66 Mon Sep 17 00:00:00 2001 From: richardbarran Date: Thu, 8 Apr 2010 17:07:56 +0000 Subject: [PATCH] Bug fix: some people were playing around with the AJAX interface - sending AJAX requests with no time - and causing server errors. Trap these errors and return a more helpful error message! git-svn-id: http://django-jqchat.googlecode.com/svn/trunk@15 8369a704-5b4a-11de-992f-fdd7e25b9163 --- jqchat/tests.py | 13 +++++++++++++ jqchat/views.py | 5 ++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/jqchat/tests.py b/jqchat/tests.py index 812df52..479c8a3 100644 --- a/jqchat/tests.py +++ b/jqchat/tests.py @@ -124,6 +124,12 @@ class AJAXGetTest(TestCase): messages = payload['messages'] self.assert_(len(messages) == 0) + def test_no_time(self): + """All requests should include the time.""" + + response = self.client.get('/jqchat/room/1/ajax/') + self.assert_(response.status_code == 400, response.status_code) + def test_room_2(self): """Retrieve messages for room 2 - should be a different list to room 1. Additionally, set the time so as to retrieve only the latest 2 messages in that room - @@ -188,6 +194,13 @@ class AJAXPostTest(TestCase): 'message': 'rhubarb'}) self.assert_(response.status_code == 400, response.status_code) + def test_no_time(self): + """All requests should include the time.""" + + response = self.client.get('/jqchat/room/1/ajax/', {'action': 'postmsg', + 'message': 'rhubarb'}) + self.assert_(response.status_code == 400, response.status_code) + def test_empty_message(self): """Post an empty message to the server - it will be ignored.""" diff --git a/jqchat/views.py b/jqchat/views.py index ea9a2b0..873297a 100644 --- a/jqchat/views.py +++ b/jqchat/views.py @@ -81,7 +81,10 @@ class Ajax(object): StatusCode = 0 # Default status code is 0 i.e. no new data. self.request = request - self.request_time = int(self.request.REQUEST['time']) + try: + self.request_time = int(self.request.REQUEST['time']) + except: + return HttpResponseBadRequest("What's the time?") self.ThisRoom = Room.objects.get(id=id) NewDescription = None -- 2.39.5