def test_wrapNspError(self):
     'It should wrap the NspError with the expected attributes'
     nspError = NspError(NspError.THING_NOT_FOUND, 'message', ['causes'])
     e = HttpError.wrap(nspError)
     self.assertEqual(e.message, nspError.message)
     self.assertEqual(e.causes, nspError.causes)
     self.assertEqual(e.timestamp, nspError.timestamp)
 def test_wrapException(self):
     'It should wrap an Exception with the expected attributes'
     try:
         raise (Exception('hello'))
     except Exception as exception:
         e = HttpError.wrap(exception)
         self.assertEqual(e.statusCode, HttpError.INTERNAL_SERVER_ERROR)
         self.assertEqual(
             e.message,
             repr(exception) + ':\n' +
             ''.join(traceback.format_tb(sys.exc_info()[2])))
         self.assertEqual(e.causes, [])
         self.assertIsInstance(e.timestamp, datetime.datetime)
 def testWithNspError(self):
     '''APIGateway.createErrorResponse() should wrap the passed NspError in an HttpError and then create an error
     response with it'''
     event = {}
     sut = APIGateway(mockLoggerFactory, event)
     error = NspError(NspError.THING_NOT_FOUND, 'message')
     httpError = HttpError.wrap(error)
     httpError.method = sut.getHttpMethod()
     httpError.resource = sut.getHttpResource()
     response = sut.createErrorResponse(error)
     self.assertEqual(response, {
         'statusCode': httpError.statusCode,
         'headers': {'Access-Control-Allow-Origin': '*'},
         'body': json.dumps(httpError.__dict__, default=jsonutils.dumpdefault)
     })
 def testWithException(self):
     '''APIGateway.createErrorResponse() should wrap the passed Exception in an HttpError and the create an error
     response with it'''
     event = {}
     sut = APIGateway(mockLoggerFactory, event)
     error = KeyError('unknown')
     httpError = HttpError.wrap(error)
     httpError.method = sut.getHttpMethod()
     httpError.resource = sut.getHttpResource()
     response = sut.createErrorResponse(error)
     self.assertEqual(response['statusCode'], httpError.statusCode)
     self.assertEqual(response['headers'], {'Access-Control-Allow-Origin': '*'})
     body = json.loads(response['body'])
     self.assertEqual(body['message'], httpError.message)
     self.assertEqual(body['method'], httpError.method)
     self.assertEqual(body['resource'], httpError.resource)
     self.assertEqual(body['causes'], [])
 def test_wrap_THING_UNPROCESSABLE(self):
     'It should wrap the THING_UNPROCESSABLE NspError'
     e = HttpError.wrap(NspError(NspError.THING_UNPROCESSABLE, 'message'))
     self.assertEqual(e.statusCode, 422)
     self.assertEqual(e.message, 'message')
 def test_wrap_THING_NOT_FOUND(self):
     'It should wrap the THING_NOT_FOUND NspError'
     e = HttpError.wrap(NspError(NspError.THING_NOT_FOUND, 'message'))
     self.assertEqual(e.statusCode, 404)
     self.assertEqual(e.message, 'message')
 def test_wrap_INTERNAL_SERVER_ERROR(self):
     'It should wrap the INTERNAL_SERVER_ERROR NspError'
     e = HttpError.wrap(NspError(NspError.INTERNAL_SERVER_ERROR, 'message'))
     self.assertEqual(e.statusCode, 500)
     self.assertEqual(e.message, 'message')
 def test_wrap_FORBIDDEN(self):
     'It should wrap the FORBIDDEN NspError'
     e = HttpError.wrap(NspError(NspError.FORBIDDEN, 'message'))
     self.assertEqual(e.statusCode, 403)
     self.assertEqual(e.message, 'message')
Пример #9
0
 def createErrorResponse(self, error):
     if not isinstance(error, HttpError):
         error = HttpError.wrap(error)
     error.method = self.getHttpMethod()
     error.resource = self.getHttpResource()
     return createResponse(error.statusCode, {}, error.__dict__)