def test(self): 'APIGateway.createLocationHeader() should return getHttpResource() with the parameter appended' event = {} sut = APIGateway(mockLoggerFactory, event) sut.getHttpResource = MagicMock(return_value='http-resource') uuid = 'uuid' location = sut.createLocationHeader(uuid) self.assertEqual(location, {'Location': 'http-resource/uuid'})
def testNonStandardHttp(self): 'APIGateway.getHttpResource() should return a standard port http resource' event = { 'headers': { 'X-Forwarded-Port': '8080', 'X-Forwarded-Proto': 'http', 'Host': 'localhost' }, 'path': '/path' } sut = APIGateway(mockLoggerFactory, event) httpResource = sut.getHttpResource() self.assertEqual(httpResource, 'http://localhost:8080/path')
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 testContextPath(self): 'APIGateway.getHttpResource() should insert a context path if the host is an API Gateway API' event = { 'headers': { 'X-Forwarded-Port': '443', 'X-Forwarded-Proto': 'https', 'Host': '12345678.execute-api.eu-west-1.amazonaws.com' }, 'path': '/path', 'requestContext': { 'stage': 'stage' } } sut = APIGateway(mockLoggerFactory, event) httpResource = sut.getHttpResource() self.assertEqual(httpResource, 'https://12345678.execute-api.eu-west-1.amazonaws.com/stage/path')
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 testMissingElemenst(self): 'APIGateway.getHttpResource() should return a default value for needed elements are missing' event = {} sut = APIGateway(mockLoggerFactory, event) httpResource = sut.getHttpResource() self.assertEqual(httpResource, 'UNKNOWN_PROTOCOL://UNKNOWN_HOST:UNKNOWN_PORT/UNKNOWN_PATH')