CommentClientError now has sane subclasses that are meaningfully distinct, and each subclass is handled appropriately. Errors raised by the requests library are no longer handled by turning them into CommentClientErrors, since there is no meaningful handling we can do, and this way we will get more visibility into why errors are occurring. Also, HTTP status codes from the comments service indicating client error are correctly passed through to the client.
40 lines
1.7 KiB
Python
40 lines
1.7 KiB
Python
import django.http
|
|
from django.test import TestCase
|
|
import json
|
|
|
|
import comment_client
|
|
import django_comment_client.middleware as middleware
|
|
|
|
|
|
class AjaxExceptionTestCase(TestCase):
|
|
def setUp(self):
|
|
self.a = middleware.AjaxExceptionMiddleware()
|
|
self.request1 = django.http.HttpRequest()
|
|
self.request0 = django.http.HttpRequest()
|
|
self.exception1 = comment_client.CommentClientRequestError('{}', 401)
|
|
self.exception2 = comment_client.CommentClientRequestError('Foo!', 404)
|
|
self.exception0 = comment_client.CommentClient500Error("Holy crap the server broke!")
|
|
self.request1.META['HTTP_X_REQUESTED_WITH'] = "XMLHttpRequest"
|
|
self.request0.META['HTTP_X_REQUESTED_WITH'] = "SHADOWFAX"
|
|
|
|
def test_process_exception(self):
|
|
response1 = self.a.process_exception(self.request1, self.exception1)
|
|
self.assertIsInstance(response1, middleware.JsonError)
|
|
self.assertEqual(self.exception1.status_code, response1.status_code)
|
|
self.assertEqual(
|
|
{"errors": json.loads(self.exception1.message)},
|
|
json.loads(response1.content)
|
|
)
|
|
|
|
response2 = self.a.process_exception(self.request1, self.exception2)
|
|
self.assertIsInstance(response2, middleware.JsonError)
|
|
self.assertEqual(self.exception2.status_code, response2.status_code)
|
|
self.assertEqual(
|
|
{"errors": [self.exception2.message]},
|
|
json.loads(response2.content)
|
|
)
|
|
|
|
self.assertIsNone(self.a.process_exception(self.request1, self.exception0))
|
|
self.assertIsNone(self.a.process_exception(self.request0, self.exception1))
|
|
self.assertIsNone(self.a.process_exception(self.request0, self.exception0))
|