def test_serialize_with_url(self):
     event = APICallAttemptEvent(service=self.service,
                                 operation=self.operation,
                                 timestamp=self.timestamp,
                                 url=self.url)
     serialized_event_dict = self.get_serialized_event_dict(event)
     self.assertEqual(serialized_event_dict['Fqdn'], self.fqdn)
 def test_serialize_with_status_code(self):
     event = APICallAttemptEvent(service=self.service,
                                 operation=self.operation,
                                 timestamp=self.timestamp,
                                 http_status_code=200)
     serialized_event_dict = self.get_serialized_event_dict(event)
     self.assertEqual(serialized_event_dict['HttpStatusCode'], 200)
 def test_serialize_api_call_attempt_event_with_latency(self):
     event = APICallAttemptEvent(service=self.service,
                                 operation=self.operation,
                                 timestamp=self.timestamp,
                                 latency=self.latency)
     serialized_event_dict = self.get_serialized_event_dict(event)
     self.assertEqual(serialized_event_dict['AttemptLatency'], self.latency)
 def test_serialize_with_path_parameters_in_url(self):
     self.url = 'https://' + self.fqdn + '/resource'
     event = APICallAttemptEvent(service=self.service,
                                 operation=self.operation,
                                 timestamp=self.timestamp,
                                 url=self.url)
     serialized_event_dict = self.get_serialized_event_dict(event)
     self.assertEqual(serialized_event_dict['Fqdn'], self.fqdn)
 def test_serialize_with_user_agent(self):
     event = APICallAttemptEvent(
         service=self.service,
         operation=self.operation,
         timestamp=self.timestamp,
         request_headers={'User-Agent': self.user_agent})
     serialized_event_dict = self.get_serialized_event_dict(event)
     self.assertEqual(serialized_event_dict['UserAgent'], self.user_agent)
 def test_serialize_with_s3_signing(self):
     auth_value = 'AWS myaccesskey:somesignature'
     self.request_headers['Authorization'] = auth_value
     event = APICallAttemptEvent(service=self.service,
                                 operation=self.operation,
                                 timestamp=self.timestamp,
                                 request_headers=self.request_headers)
     serialized_event_dict = self.get_serialized_event_dict(event)
     self.assertEqual(serialized_event_dict['AccessKey'], 'myaccesskey')
 def test_serialize_with_session_token(self):
     self.request_headers['X-Amz-Security-Token'] = 'my-security-token'
     event = APICallAttemptEvent(service=self.service,
                                 operation=self.operation,
                                 timestamp=self.timestamp,
                                 request_headers=self.request_headers)
     serialized_event_dict = self.get_serialized_event_dict(event)
     self.assertEqual(serialized_event_dict['SessionToken'],
                      'my-security-token')
 def test_serialize_with_wire_exception(self):
     event = APICallAttemptEvent(
         service=self.service,
         operation=self.operation,
         timestamp=self.timestamp,
         wire_exception=Exception('Error on the wire'))
     serialized_event_dict = self.get_serialized_event_dict(event)
     self.assertEqual(serialized_event_dict['SdkException'], 'Exception')
     self.assertEqual(serialized_event_dict['SdkExceptionMessage'],
                      'Error on the wire')
 def test_serialize_truncates_long_user_agent(self):
     max_user_agent_length = 256
     user_agent = 'a' * (max_user_agent_length + 1)
     event = APICallAttemptEvent(service=self.service,
                                 operation=self.operation,
                                 timestamp=self.timestamp,
                                 request_headers={'User-Agent': user_agent})
     serialized_event_dict = self.get_serialized_event_dict(event)
     self.assertEqual(serialized_event_dict['UserAgent'],
                      user_agent[:max_user_agent_length])
 def test_serialize_with_service_error(self):
     event = APICallAttemptEvent(service=self.service,
                                 operation=self.operation,
                                 timestamp=self.timestamp,
                                 parsed_error={
                                     'Code': 'MyErrorCode',
                                     'Message': 'My error message'
                                 })
     serialized_event_dict = self.get_serialized_event_dict(event)
     self.assertEqual(serialized_event_dict['AwsException'], 'MyErrorCode')
     self.assertEqual(serialized_event_dict['AwsExceptionMessage'],
                      'My error message')
 def test_serialize_with_sigv4_sigining(self):
     auth_value = (
         'AWS4-HMAC-SHA256 '
         'Credential=myaccesskey/20180523/my-region-1/ec2/aws4_request,'
         'SignedHeaders=content-type;host;x-amz-date, '
         'Signature=somesignature')
     self.request_headers['Authorization'] = auth_value
     event = APICallAttemptEvent(service=self.service,
                                 operation=self.operation,
                                 timestamp=self.timestamp,
                                 request_headers=self.request_headers)
     serialized_event_dict = self.get_serialized_event_dict(event)
     self.assertEqual(serialized_event_dict['AccessKey'], 'myaccesskey')
 def test_new_api_call_attempt_event(self):
     event = APICallEvent(service='MyService',
                          operation='MyOperation',
                          timestamp=1000,
                          latency=2000,
                          attempts=[])
     attempt_event = event.new_api_call_attempt(timestamp=2000)
     self.assertEqual(
         attempt_event,
         APICallAttemptEvent(service='MyService',
                             operation='MyOperation',
                             timestamp=2000))
     self.assertEqual(event.attempts, [attempt_event])
 def test_serialize_with_request_id_headers(self):
     response_headers = {
         'x-amzn-requestid': 'id1',
         'x-amz-request-id': 'id2',
         'x-amz-id-2': 'id3',
     }
     event = APICallAttemptEvent(service=self.service,
                                 operation=self.operation,
                                 timestamp=self.timestamp,
                                 response_headers=response_headers)
     serialized_event_dict = self.get_serialized_event_dict(event)
     self.assertEqual(serialized_event_dict['XAmznRequestId'], 'id1')
     self.assertEqual(serialized_event_dict['XAmzRequestId'], 'id2')
     self.assertEqual(serialized_event_dict['XAmzId2'], 'id3')
 def test_serialize_api_call_attempt_event(self):
     event = APICallAttemptEvent(service=self.service,
                                 operation=self.operation,
                                 timestamp=self.timestamp)
     serialized_event_dict = self.get_serialized_event_dict(event)
     self.assertEqual(
         serialized_event_dict, {
             'Version': 1,
             'Type': 'ApiCallAttempt',
             'Service': self.service,
             'Api': self.operation,
             'ClientId': self.csm_client_id,
             'Timestamp': self.timestamp,
         })
 def test_serialize_filters_unwanted_response_headers(self):
     response_headers = {'filter-out': 'do-not-include-this'}
     event = APICallAttemptEvent(service=self.service,
                                 operation=self.operation,
                                 timestamp=self.timestamp,
                                 response_headers=response_headers)
     serialized_event_dict = self.get_serialized_event_dict(event)
     self.assertEqual(
         serialized_event_dict, {
             'Version': 1,
             'Type': 'ApiCallAttempt',
             'Service': self.service,
             'Api': self.operation,
             'ClientId': self.csm_client_id,
             'Timestamp': self.timestamp,
         })
    def test_feed_with_successful_response(self):
        self.feed_before_parameter_build_event(current_time=1)
        self.feed_request_created_event(current_time=2)

        self.mock_time.return_value = 3
        attempt_event = self.adapter.feed(
            'response-received', {
                'parsed_response': {
                    'ResponseMetadata': {
                        'HTTPStatusCode': self.http_status_code,
                        'HTTPHeaders': self.response_headers
                    }
                },
                'context': self.context,
                'exception': None
            })
        self.assertEqual(
            attempt_event,
            APICallAttemptEvent(
                service=self.service_id,
                operation=self.wire_name,
                timestamp=2000,
                latency=1000,
                url=self.url,
                request_headers=self.request_headers,
                http_status_code=self.http_status_code,
                response_headers=self.response_headers,
            ))

        self.mock_time.return_value = 4
        call_event = self.adapter.feed(
            'after-call', {
                'parsed': {
                    'ResponseMetadata': {
                        'HTTPStatusCode': self.http_status_code,
                        'HTTPHeaders': self.response_headers
                    }
                },
                'context': self.context
            })
        self.assertEqual(
            call_event,
            APICallEvent(service=self.service_id,
                         operation=self.wire_name,
                         timestamp=1000,
                         latency=3000,
                         attempts=[attempt_event]))
    def test_serialize_truncates_long_wire_exception(self):
        max_class_name_length = 128
        max_error_message_length = 512
        long_class_name = 'W' * (max_class_name_length + 1)
        wire_class = type(long_class_name, (Exception, ), {})
        long_error_message = 'm' * (max_error_message_length + 1)
        event = APICallAttemptEvent(
            service=self.service,
            operation=self.operation,
            timestamp=self.timestamp,
            wire_exception=wire_class(long_error_message))
        serialized_event_dict = self.get_serialized_event_dict(event)

        self.assertEqual(serialized_event_dict['SdkException'],
                         long_class_name[:max_class_name_length])
        self.assertEqual(serialized_event_dict['SdkExceptionMessage'],
                         long_error_message[:max_error_message_length])
 def test_serialize_truncates_long_service_error(self):
     max_error_code_length = 128
     max_error_message_length = 512
     long_error_code = 'c' * (max_error_code_length + 1)
     long_error_message = 'm' * (max_error_message_length + 1)
     event = APICallAttemptEvent(service=self.service,
                                 operation=self.operation,
                                 timestamp=self.timestamp,
                                 parsed_error={
                                     'Code': long_error_code,
                                     'Message': long_error_message
                                 })
     serialized_event_dict = self.get_serialized_event_dict(event)
     self.assertEqual(serialized_event_dict['AwsException'],
                      long_error_code[:max_error_code_length])
     self.assertEqual(serialized_event_dict['AwsExceptionMessage'],
                      long_error_message[:max_error_message_length])
 def test_init(self):
     url = 'https://us-east-1.myservice.amazonaws.com'
     parsed_error = {'Code': 'ErrorCode', 'Message': 'ErrorMessage'}
     wire_exception = Exception('Some wire exception')
     event = APICallAttemptEvent(service='MyService',
                                 operation='MyOperation',
                                 timestamp=1000,
                                 latency=2000,
                                 url=url,
                                 http_status_code=200,
                                 request_headers={},
                                 response_headers={},
                                 parsed_error=parsed_error,
                                 wire_exception=wire_exception)
     self.assertEqual(event.service, 'MyService')
     self.assertEqual(event.operation, 'MyOperation')
     self.assertEqual(event.timestamp, 1000)
     self.assertEqual(event.latency, 2000)
     self.assertEqual(event.url, url)
     self.assertEqual(event.http_status_code, 200)
     self.assertEqual(event.request_headers, {})
     self.assertEqual(event.response_headers, {})
     self.assertEqual(event.parsed_error, parsed_error)
     self.assertEqual(event.wire_exception, wire_exception)
    def test_feed_with_wire_exception(self):
        self.feed_before_parameter_build_event(current_time=1)
        self.feed_request_created_event(current_time=2)

        self.mock_time.return_value = 3
        wire_exception = Exception('Some wire exception')
        attempt_event = self.adapter.feed(
            'response-received', {
                'parsed_response': None,
                'context': self.context,
                'exception': wire_exception
            })
        self.assertEqual(
            attempt_event,
            APICallAttemptEvent(
                service=self.service_id,
                operation=self.wire_name,
                timestamp=2000,
                latency=1000,
                url=self.url,
                request_headers=self.request_headers,
                wire_exception=wire_exception,
            ))

        self.mock_time.return_value = 4
        call_event = self.adapter.feed('after-call-error', {
            'exception': wire_exception,
            'context': self.context
        })
        self.assertEqual(
            call_event,
            APICallEvent(service=self.service_id,
                         operation=self.wire_name,
                         timestamp=1000,
                         latency=3000,
                         attempts=[attempt_event]))