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_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_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'})
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, 'HTTPHeaders': headers}})
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', })
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'})
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 })
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': '504', 'Message': 'Gateway Timeout' }) self.assertEqual(parsed['ResponseMetadata'], { 'HTTPStatusCode': 504, 'HTTPHeaders': headers })
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 })