Exemplo n.º 1
0
 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)
Exemplo n.º 2
0
 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
             }
         })
Exemplo n.º 3
0
 def test_empty_rest_json_response(self):
     headers = {'x-amzn-requestid': 'request-id'}
     parser = parsers.RestJSONParser()
     output_shape = None
     parsed = parser.parse(
         {'body': b'', 'headers': headers, 'status_code': 200},
         output_shape)
     self.assertEqual(
         parsed,
         {'ResponseMetadata': {'RequestId': 'request-id',
                               'HTTPStatusCode': 200}})
Exemplo n.º 4
0
 def test_can_parse_with_case_insensitive_keys(self):
     body = (b'{"Code":"AccessDeniedException","type":"Client","Message":'
             b'"Access denied"}')
     headers = {
          'x-amzn-requestid': 'request-id'
     }
     parser = parsers.RestJSONParser()
     parsed = parser.parse(
         {'body': body, 'headers': headers, 'status_code': 400}, None)
     self.assertEqual(parsed['Error'], {'Message': 'Access denied',
                                        'Code': 'AccessDeniedException'})
Exemplo n.º 5
0
 def test_can_parse_rest_json_modeled_fields(self):
     body = (b'{"ModeledField":"Some modeled field",'
             b'"Message":"Some message"}')
     parser = parsers.RestJSONParser()
     response_dict = {
         'status_code': 400,
         'headers': {},
         'body': body,
     }
     parsed = parser.parse(response_dict, self.error_shape)
     expected_parsed = {
         'ModeledField': 'Some modeled field',
     }
     self.assertEqual(parsed, expected_parsed)
Exemplo n.º 6
0
 def test_can_parse_restjson_error_code(self):
     body = b'''{
         "status": "error",
         "errors": [{"message": "[*Deprecated*: blah"}],
         "adds": 0,
         "__type": "#WasUnableToParseThis",
         "message": "blah",
         "deletes": 0}'''
     headers = {
          'x-amzn-requestid': 'request-id'
     }
     parser = parsers.RestJSONParser()
     parsed = parser.parse(
         {'body': body, 'headers': headers, 'status_code': 400}, None)
     self.assertEqual(parsed['Error'], {'Message': 'blah',
                                        'Code': 'WasUnableToParseThis'})
Exemplo n.º 7
0
    def test_error_response_with_no_body_rest_json(self):
        parser = parsers.RestJSONParser()
        response = b''
        headers = {'content-length': '0', 'connection': 'keep-alive'}
        output_shape = None
        parsed = parser.parse({'body': response, 'headers': headers,
                               'status_code': 504}, output_shape)

        self.assertIn('Error', parsed)
        self.assertEqual(parsed['Error'], {
            'Code': '',
            'Message': ''
        })
        self.assertEqual(parsed['ResponseMetadata'], {
            'HTTPStatusCode': 504,
        })
Exemplo n.º 8
0
 def test_can_parse_rest_json_errors(self):
     body = (
         '{"Message":"Function not found: foo","Type":"User"}'
     ).encode('utf-8')
     headers = {
         'x-amzn-requestid': 'request-id',
         'x-amzn-errortype': 'ResourceNotFoundException:http://url/',
     }
     parser = parsers.RestJSONParser()
     parsed = parser.parse({
         'body': body, 'headers': headers, 'status_code': 400}, None)
     self.assertIn('Error', parsed)
     self.assertEqual(parsed['Error'], {
         'Code': 'ResourceNotFoundException',
         'Message': 'Function not found: foo',
     })
Exemplo n.º 9
0
    def test_error_response_with_string_body_rest_json(self):
        parser = parsers.RestJSONParser()
        response = b'HTTP content length exceeded 1049600 bytes.'
        headers = {'content-length': '0', 'connection': 'keep-alive'}
        output_shape = None
        parsed = parser.parse({'body': response, 'headers': headers,
                               'status_code': 413}, output_shape)

        self.assertIn('Error', parsed)
        self.assertEqual(parsed['Error'], {
            'Code': '413',
            'Message': response.decode('utf-8')
        })
        self.assertEqual(parsed['ResponseMetadata'], {
            'HTTPStatusCode': 413,
            'HTTPHeaders': headers
        })
Exemplo n.º 10
0
    def test_error_response_with_xml_body_rest_json(self):
        parser = parsers.RestJSONParser()
        response = (
            '<AccessDeniedException>'
            '   <Message>Unable to determine service/operation name to be authorized</Message>'
            '</AccessDeniedException>'
        ).encode('utf-8')
        headers = {'content-length': '0', 'connection': 'keep-alive'}
        output_shape = None
        parsed = parser.parse({'body': response, 'headers': headers,
                               'status_code': 403}, output_shape)

        self.assertIn('Error', parsed)
        self.assertEqual(parsed['Error'], {
            'Code': '403',
            'Message': response.decode('utf-8')
        })
        self.assertEqual(parsed['ResponseMetadata'], {
            'HTTPStatusCode': 403,
            'HTTPHeaders': headers
        })