Exemplo n.º 1
0
    def test_api_gateway_sqs_integration_with_event_source(self):
        # create target SQS stream
        aws_stack.create_sqs_queue(self.TEST_SQS_QUEUE)

        # create API Gateway and connect it to the target queue
        result = connect_api_gateway_to_sqs('test_gateway4',
                                            stage_name=self.TEST_STAGE_NAME,
                                            queue_arn=self.TEST_SQS_QUEUE,
                                            path=self.API_PATH_DATA_INBOUND)

        # create event source for sqs lambda processor
        self.create_lambda_function(self.TEST_LAMBDA_SQS_HANDLER_NAME)
        add_event_source(self.TEST_LAMBDA_SQS_HANDLER_NAME,
                         aws_stack.sqs_queue_arn(self.TEST_SQS_QUEUE), True)

        # generate test data
        test_data = {'spam': 'eggs & beans'}

        url = self.gateway_request_url(api_id=result['id'],
                                       stage_name=self.TEST_STAGE_NAME,
                                       path=self.API_PATH_DATA_INBOUND)
        result = requests.post(url, data=json.dumps(test_data))
        self.assertEqual(result.status_code, 200)

        parsed_json = xmltodict.parse(result.content)
        result = parsed_json['SendMessageResponse']['SendMessageResult']

        body_md5 = result['MD5OfMessageBody']

        self.assertEqual(body_md5, 'b639f52308afd65866c86f274c59033f')
Exemplo n.º 2
0
    def test_api_gateway_sqs_integration_with_event_source(self):
        # create target SQS stream
        aws_stack.create_sqs_queue(self.TEST_SQS_QUEUE)

        # create API Gateway and connect it to the target queue
        result = connect_api_gateway_to_sqs('test_gateway4',
                                            stage_name=self.TEST_STAGE_NAME,
                                            queue_arn=self.TEST_SQS_QUEUE,
                                            path=self.API_PATH_DATA_INBOUND)

        # create event source for sqs lambda processor
        self.create_lambda_function(self.TEST_LAMBDA_HANDLER_NAME)
        add_event_source(self.TEST_LAMBDA_HANDLER_NAME,
                         aws_stack.sqs_queue_arn(self.TEST_SQS_QUEUE), True)

        # generate test data
        test_data = {'spam': 'eggs & beans'}

        url = INBOUND_GATEWAY_URL_PATTERN.format(
            api_id=result['id'],
            stage_name=self.TEST_STAGE_NAME,
            path=self.API_PATH_DATA_INBOUND)
        result = requests.post(url, data=json.dumps(test_data))
        self.assertEqual(result.status_code, 200)

        parsed_content = parseString(result.content)
        root = parsed_content.documentElement.childNodes[1]

        attr_md5 = root.childNodes[1].lastChild.nodeValue
        body_md5 = root.childNodes[3].lastChild.nodeValue

        self.assertEqual(attr_md5, '4141913720225b35a836dd9e19fc1e55')
        self.assertEqual(body_md5, 'b639f52308afd65866c86f274c59033f')
Exemplo n.º 3
0
    def test_api_gateway_sqs_integration(self):
        # create target SQS stream
        aws_stack.create_sqs_queue(self.TEST_SQS_QUEUE)

        # create API Gateway and connect it to the target queue
        result = connect_api_gateway_to_sqs('test_gateway4',
                                            stage_name=self.TEST_STAGE_NAME,
                                            queue_arn=self.TEST_SQS_QUEUE,
                                            path=self.API_PATH_DATA_INBOUND)

        # generate test data
        test_data = {'spam': 'eggs'}

        url = INBOUND_GATEWAY_URL_PATTERN.format(
            api_id=result['id'],
            stage_name=self.TEST_STAGE_NAME,
            path=self.API_PATH_DATA_INBOUND)
        result = requests.post(url, data=json.dumps(test_data))
        self.assertEqual(result.status_code, 200)

        messages = aws_stack.sqs_receive_message(
            self.TEST_SQS_QUEUE)['Messages']
        self.assertEqual(len(messages), 1)
        self.assertEqual(json.loads(base64.b64decode(messages[0]['Body'])),
                         test_data)
Exemplo n.º 4
0
    def test_multi_region_api_gateway(self):
        gw_1 = aws_stack.create_external_boto_client("apigateway",
                                                     region_name=REGION1)
        gw_2 = aws_stack.create_external_boto_client("apigateway",
                                                     region_name=REGION2)
        gw_3 = aws_stack.create_external_boto_client("apigateway",
                                                     region_name=REGION3)
        sqs_1 = aws_stack.create_external_boto_client("sqs",
                                                      region_name=REGION1)
        len_1 = len(gw_1.get_rest_apis()["items"])
        len_2 = len(gw_2.get_rest_apis()["items"])

        api_name1 = "a-%s" % short_uid()
        gw_1.create_rest_api(name=api_name1)
        result1 = gw_1.get_rest_apis()["items"]
        self.assertEqual(len(result1), len_1 + 1)
        self.assertEqual(len(gw_2.get_rest_apis()["items"]), len_2)

        api_name2 = "a-%s" % short_uid()
        gw_2.create_rest_api(name=api_name2)
        result2 = gw_2.get_rest_apis()["items"]
        self.assertEqual(len(gw_1.get_rest_apis()["items"]), len_1 + 1)
        self.assertEqual(len(result2), len_2 + 1)

        api_name3 = "a-%s" % short_uid()
        queue_name1 = "q-%s" % short_uid()
        result = sqs_1.create_queue(QueueName=queue_name1)
        queue_arn = aws_stack.sqs_queue_arn(queue_name1, region_name=REGION1)
        result = connect_api_gateway_to_sqs(api_name3,
                                            stage_name="test",
                                            queue_arn=queue_arn,
                                            path="/data",
                                            region_name=REGION3)
        api_id = result["id"]
        result = gw_3.get_rest_apis()["items"]
        self.assertEqual(result[-1]["name"], api_name3)

        # post message and receive from SQS
        url = self._gateway_request_url(api_id=api_id,
                                        stage_name="test",
                                        path="/data")
        test_data = {"foo": "bar"}
        result = requests.post(url, data=json.dumps(test_data))
        self.assertEqual(result.status_code, 200)
        messages = aws_stack.sqs_receive_message(queue_arn)["Messages"]
        self.assertEqual(len(messages), 1)
        self.assertEqual(
            json.loads(to_str(base64.b64decode(to_str(messages[0]["Body"])))),
            test_data)
Exemplo n.º 5
0
    def test_api_gateway_sqs_integration_with_event_source(self):
        # create target SQS stream
        queue_name = 'queue-%s' % short_uid()
        queue_url = aws_stack.create_sqs_queue(queue_name)['QueueUrl']

        # create API Gateway and connect it to the target queue
        result = connect_api_gateway_to_sqs('test_gateway4',
                                            stage_name=self.TEST_STAGE_NAME,
                                            queue_arn=queue_name,
                                            path=self.API_PATH_DATA_INBOUND)

        # create event source for sqs lambda processor
        self.create_lambda_function(self.TEST_LAMBDA_SQS_HANDLER_NAME)
        event_source_data = {
            'FunctionName': self.TEST_LAMBDA_SQS_HANDLER_NAME,
            'EventSourceArn': aws_stack.sqs_queue_arn(queue_name),
            'Enabled': True
        }
        add_event_source(event_source_data)

        # generate test data
        test_data = {'spam': 'eggs & beans'}

        url = gateway_request_url(api_id=result['id'],
                                  stage_name=self.TEST_STAGE_NAME,
                                  path=self.API_PATH_DATA_INBOUND)
        result = requests.post(url, data=json.dumps(test_data))
        self.assertEqual(result.status_code, 200)

        parsed_json = xmltodict.parse(result.content)
        result = parsed_json['SendMessageResponse']['SendMessageResult']

        body_md5 = result['MD5OfMessageBody']

        self.assertEqual(body_md5, 'b639f52308afd65866c86f274c59033f')

        # clean up
        sqs_client = aws_stack.connect_to_service('sqs')
        sqs_client.delete_queue(QueueUrl=queue_url)

        lambda_client = aws_stack.connect_to_service('lambda')
        lambda_client.delete_function(
            FunctionName=self.TEST_LAMBDA_SQS_HANDLER_NAME)