def test_metadata_always_exists_on_rest_json_response(self):
     # ResponseMetadata is used for more than just the request id. It
     # should always get populated, even if the request doesn't seem to
     # have an id.
     parser = parsers.RestJSONParser()
     response = b'{"Str": "mystring"}'
     headers = {}
     output_shape = model.StructureShape(
         'OutputShape', {
             'type': 'structure',
             'members': {
                 'Str': {
                     'shape': 'StringType',
                 }
             }
         }, model.ShapeResolver({'StringType': {
             'type': 'string'
         }}))
     parsed = parser.parse(
         {
             'body': response,
             'headers': headers,
             'status_code': 200
         }, output_shape)
     expected = {
         'Str': 'mystring',
         'ResponseMetadata': {
             'HTTPStatusCode': 200,
             'HTTPHeaders': headers
         }
     }
     self.assertEqual(parsed, expected)
 def test_response_metadata_on_rest_json_response(self):
     parser = parsers.RestJSONParser()
     response = b'{"Str": "mystring"}'
     headers = {'x-amzn-requestid': 'request-id'}
     output_shape = model.StructureShape(
         'OutputShape', {
             'type': 'structure',
             'members': {
                 'Str': {
                     'shape': 'StringType',
                 }
             }
         }, model.ShapeResolver({'StringType': {
             'type': 'string'
         }}))
     parsed = parser.parse(
         {
             'body': response,
             'headers': headers,
             'status_code': 200
         }, output_shape)
     # Note that the response metadata is normalized to match the query
     # protocol, even though this is not how it appears in the output.
     self.assertEqual(
         parsed, {
             'Str': 'mystring',
             'ResponseMetadata': {
                 'RequestId': 'request-id',
                 'HTTPStatusCode': 200,
                 'HTTPHeaders': headers
             }
         })
 def test_response_no_initial_event_stream(self):
     parser = parsers.JSONParser()
     output_shape = model.StructureShape(
         'OutputShape', {
             'type': 'structure',
             'members': {
                 'Payload': {
                     'shape': 'Payload'
                 }
             }
         },
         model.ShapeResolver({
             'Payload': {
                 'type': 'structure',
                 'members': [],
                 'eventstream': True
             }
         }))
     with self.assertRaises(parsers.ResponseParserError):
         response_dict = {
             'status_code': 200,
             'headers': {},
             'body': RawResponse(b''),
             'context': {
                 'operation_name': 'TestOperation'
             }
         }
         parser.parse(response_dict, output_shape)
 def test_metadata_always_exists_for_ec2(self):
     # ResponseMetadata is used for more than just the request id. It
     # should always get populated, even if the request doesn't seem to
     # have an id.
     parser = parsers.EC2QueryParser()
     response = ('<OperationNameResponse>'
                 '  <Str>myname</Str>'
                 '</OperationNameResponse>').encode('utf-8')
     output_shape = model.StructureShape(
         'OutputShape', {
             'type': 'structure',
             'members': {
                 'Str': {
                     'shape': 'StringType',
                 }
             }
         }, model.ShapeResolver({'StringType': {
             'type': 'string'
         }}))
     parsed = parser.parse(
         {
             'headers': {},
             'body': response,
             'status_code': 200
         }, output_shape)
     expected = {
         'Str': 'myname',
         'ResponseMetadata': {
             'HTTPStatusCode': 200,
             'HTTPHeaders': {}
         }
     }
     self.assertEqual(parsed, expected)
 def test_response_metadata_parsed_for_ec2(self):
     parser = parsers.EC2QueryParser()
     response = ('<OperationNameResponse>'
                 '  <Str>myname</Str>'
                 '  <requestId>request-id</requestId>'
                 '</OperationNameResponse>').encode('utf-8')
     output_shape = model.StructureShape(
         'OutputShape', {
             'type': 'structure',
             'members': {
                 'Str': {
                     'shape': 'StringType',
                 }
             }
         }, model.ShapeResolver({'StringType': {
             'type': 'string'
         }}))
     parsed = parser.parse(
         {
             'headers': {},
             'body': response,
             'status_code': 200
         }, output_shape)
     # Note that the response metadata is normalized to match the query
     # protocol, even though this is not how it appears in the output.
     self.assertEqual(
         parsed, {
             'Str': 'myname',
             'ResponseMetadata': {
                 'RequestId': 'request-id',
                 'HTTPStatusCode': 200,
                 'HTTPHeaders': {}
             }
         })
 def create_arbitary_output_shape(self):
     output_shape = model.StructureShape(
         'OutputShape', {
             'type': 'structure',
             'members': {
                 'Str': {
                     'shape': 'StringType',
                 }
             }
         }, model.ShapeResolver({'StringType': {
             'type': 'string'
         }}))
     return output_shape
 def test_response_metadata_parsed_for_query_service(self):
     parser = parsers.QueryParser()
     response = (
         '<OperationNameResponse>'
         '  <OperationNameResult><Str>myname</Str></OperationNameResult>'
         '  <ResponseMetadata>'
         '    <RequestId>request-id</RequestId>'
         '  </ResponseMetadata>'
         '</OperationNameResponse>').encode('utf-8')
     output_shape = model.StructureShape(
         'OutputShape', {
             'type': 'structure',
             'resultWrapper': 'OperationNameResult',
             'members': {
                 'Str': {
                     'shape': 'StringType',
                 },
                 'Num': {
                     'shape': 'IntegerType',
                 }
             }
         },
         model.ShapeResolver({
             'StringType': {
                 'type': 'string',
             },
             'IntegerType': {
                 'type': 'integer',
             }
         }))
     parsed = parser.parse(
         {
             'body': response,
             'headers': {},
             'status_code': 200
         }, output_shape)
     self.assertEqual(
         parsed, {
             'Str': 'myname',
             'ResponseMetadata': {
                 'RequestId': 'request-id',
                 'HTTPStatusCode': 200,
                 'HTTPHeaders': {}
             }
         })
 def setUp(self):
     self.parser = parsers.EventStreamXMLParser()
     self.output_shape = model.StructureShape(
         'EventStream', {
             'eventstream': True,
             'type': 'structure',
             'members': {
                 'EventA': {
                     'shape': 'EventAStructure'
                 },
                 'EventB': {
                     'shape': 'EventBStructure'
                 },
                 'EventC': {
                     'shape': 'EventCStructure'
                 },
                 'EventD': {
                     'shape': 'EventDStructure'
                 },
                 'EventException': {
                     'shape': 'ExceptionShape'
                 },
             }
         },
         model.ShapeResolver({
             'EventAStructure': {
                 'event': True,
                 'type': 'structure',
                 'members': {
                     'Stats': {
                         'shape': 'StatsStructure',
                         'eventpayload': True
                     },
                     'Header': {
                         'shape': 'IntShape',
                         'eventheader': True
                     }
                 }
             },
             'EventBStructure': {
                 'event': True,
                 'type': 'structure',
                 'members': {
                     'Body': {
                         'shape': 'BlobShape',
                         'eventpayload': True
                     }
                 }
             },
             'EventCStructure': {
                 'event': True,
                 'type': 'structure',
                 'members': {
                     'Body': {
                         'shape': 'StringShape',
                         'eventpayload': True
                     }
                 }
             },
             'EventDStructure': {
                 'event': True,
                 'type': 'structure',
                 'members': {
                     'StringField': {
                         'shape': 'StringShape'
                     },
                     'IntField': {
                         'shape': 'IntShape'
                     },
                     'Header': {
                         'shape': 'IntShape',
                         'eventheader': True
                     }
                 }
             },
             'StatsStructure': {
                 'type': 'structure',
                 'members': {
                     'StringField': {
                         'shape': 'StringShape'
                     },
                     'IntField': {
                         'shape': 'IntShape'
                     }
                 }
             },
             'BlobShape': {
                 'type': 'blob'
             },
             'StringShape': {
                 'type': 'string'
             },
             'IntShape': {
                 'type': 'integer'
             },
             'ExceptionShape': {
                 'exception': True,
                 'type': 'structure',
                 'members': {
                     'message': {
                         'shape': 'StringShape'
                     }
                 }
             },
         }))