示例#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')
示例#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')
示例#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)
示例#4
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)