示例#1
0
  def ResponseFromTransportResponse(self, response):
    """Returns a Response object from the transport's raw response."""

    def Default(value):
      if isinstance(value, datetime.datetime):
        return {'_datetime': value.strftime(_BOTOCORE_DATE_FORMAT)}
      if isinstance(value, botocore.response.StreamingBody):
        return {'_streamingbody': http_encoding.Decode(value.read())}
      return json.JSONEncoder.default(json.JSONEncoder, value)

    http, parsed_response = response

    if isinstance(parsed_response, dict):
      parsed_response.pop('ResponseMetadata', None)
      parsed_response = json.dumps(
          parsed_response, default=Default, sort_keys=True)

    return events_lib.Response(http.status_code, http.headers, parsed_response)
示例#2
0
    def testApiCallEventMissingText(self):
        e = events.ApiCallEvent.ForMissing(('line', 'col'))
        self.assertEqual(events.Response(httplib.OK, {}, ''), e.GetResponse())

        request = events.Request('https://foo.com', 'POST', {b'y': b'z'},
                                 'asdf')
        failures = e.Handle(request)
        self.assertEqual(4, len(failures))
        for f in failures:
            f.Update([updates.Mode.API_REQUESTS])
        self.assertEqual(
            {
                'uri': 'https://foo.com',
                'method': 'POST',
                'headers': {},
                'body': {
                    'text': 'asdf'
                }
            },  # Generates the text assertion.
            e.UpdateContext().BackingData()['api_call']['expect_request'])
示例#3
0
 def testUpdateResponsePayload(self):
     backing_data = {
         'api_call': {
             'expect_request': {
                 'uri': 'https://example.com',
                 'method': 'GET',
                 'headers': {},
                 'body': None,
             },
             'return_response': {
                 'omit_fields': ['bad_field'],
                 'headers': {},
                 'body': '',
             }
         }
     }
     e = events.ApiCallEvent.FromData(backing_data)
     response = events.Response(
         httplib.OK, {},
         json.dumps({
             'status': 'RUNNING',
             'progress': '0',
             'bad_field': 'asdf'
         }))
     failures = e.UpdateResponsePayload(response)
     self.assertEqual(1, len(failures))
     failures[0].Update([updates.Mode.API_RESPONSE_PAYLOADS])
     self.assertEqual(
         {
             'status': httplib.OK,
             'headers': {},
             'body': {
                 'status': 'RUNNING',
                 'progress': '0'
             },
             'omit_fields': ['bad_field']
         },
         e.UpdateContext().BackingData()['api_call']['return_response'])
示例#4
0
 def testApiCallResponseAssertions(self, return_response, expect_response):
     return_response['body'] = 'response body'
     expect_response['body'] = {'text': {'matches': 'res.*'}}
     backing_data = {
         'api_call': {
             'expect_request': {
                 'uri': {
                     'equals': 'https://example.com'
                 },
                 'method': 'GET',
                 'headers': {
                     'a': 'b'
                 },
                 'body': {
                     'json': {
                         'ba': 'bb'
                     }
                 },
             },
             'return_response': return_response,
             'expect_response': expect_response
         }
     }
     e = events.ApiCallEvent.FromData(backing_data)
     self.assertEqual(events.EventType.API_CALL, e.EventType())
     self.assertEqual(updates.Mode.API_REQUESTS,
                      e.UpdateContext()._update_mode)
     request = events.Request('https://example.com', 'GET', {b'a': b'b'},
                              '{"ba": "bb"}')
     request_failures = e.Handle(request)
     self.assertEqual([], request_failures)
     response = e.GetResponse()
     self.assertEqual(
         events.Response(httplib.OK, {'e': 'f'}, 'response body'), response)
     response_failures = e.HandleResponse(response, None)
     self.assertEqual([], response_failures)
示例#5
0
    def testApiCallEventComplexAssertions(self):
        backing_data = {
            'api_call': {
                'expect_request': {
                    'uri': 'https://example.com',
                    'method': 'GET',
                    'headers': {
                        'a': {
                            'is_none': False
                        },
                        'c': {
                            'matches': r'\w'
                        },
                        'e': {
                            'in': ['d', 'e', 'f']
                        },
                    },
                    'body': {
                        'json': {
                            'ba': 'bb',
                            'bc': 'bd'
                        }
                    },
                },
                'return_response': {
                    'headers': {
                        'e': 'f',
                        'g': 'h'
                    },
                    'body': 'response body',
                }
            }
        }
        e = events.ApiCallEvent.FromData(backing_data)
        self.assertEqual(events.EventType.API_CALL, e.EventType())
        self.assertEqual(updates.Mode.API_REQUESTS,
                         e.UpdateContext()._update_mode)
        request = events.Request('https://example.com', 'GET', {
            b'a': b'b',
            b'c': b'd',
            b'e': b'f'
        }, '{"ba": "bb", "bc": "bd"}')
        self.assertEqual([], e.Handle(request))
        self.assertEqual(
            events.Response(httplib.OK, {
                'e': 'f',
                'g': 'h'
            }, 'response body'), e.GetResponse())

        # Headers and body contain 2 assertions each, plus uri and method.
        request = events.Request('https://foo.com', 'POST', {b'y': b'z'},
                                 '{"be": "bf"}')
        failures = e.Handle(request)
        self.assertEqual(7, len(failures))
        for f in failures:
            f.Update([updates.Mode.API_REQUESTS])
        self.assertEqual(
            {
                'uri': 'https://foo.com',
                'method': 'POST',
                'headers': {},
                'body': {
                    'json': {}
                }
            }, backing_data['api_call']['expect_request'])
示例#6
0
    def testApiCallEvent(self):
        backing_data = {
            'api_call': {
                'expect_request': {
                    'uri': {
                        'equals': 'https://example.com'
                    },
                    'method': 'GET',
                    'headers': {
                        'a': 'b',
                        'c': 'd'
                    },
                    'body': {
                        'json': {
                            'ba': 'bb',
                            'bc': 'bd'
                        }
                    },
                },
                'expect_response': {
                    'headers': {
                        'e': 'f'
                    },
                    'body': {
                        'text': {
                            'matches': 'res.*'
                        }
                    },
                },
                'return_response': {
                    'headers': {
                        'e': 'f',
                        'g': 'h'
                    },
                    'body': 'response body',
                }
            }
        }
        e = events.ApiCallEvent.FromData(backing_data)
        self.assertEqual(events.EventType.API_CALL, e.EventType())
        self.assertEqual(updates.Mode.API_REQUESTS,
                         e.UpdateContext()._update_mode)
        request = events.Request('https://example.com', 'GET', {
            b'a': b'b',
            b'c': b'd'
        }, '{"ba": "bb", "bc": "bd"}')
        self.assertEqual([], e.Handle(request))
        response = e.GetResponse()
        self.assertEqual(
            events.Response(httplib.OK, {
                'e': 'f',
                'g': 'h'
            }, 'response body'), response)
        self.assertEqual([], e.HandleResponse(response, None))

        # Headers and body contain 2 assertions each, plus uri and method.
        request = events.Request('https://foo.com', 'POST', {b'y': b'z'},
                                 '{"be": "bf"}')
        failures = e.Handle(request)
        self.assertEqual(6, len(failures))
        for f in failures:
            f.Update([updates.Mode.API_REQUESTS])
        self.assertEqual(
            {
                'uri': 'https://foo.com',
                'method': 'POST',
                'headers': {},
                'body': {
                    'json': {}
                }
            }, backing_data['api_call']['expect_request'])

        failures = e.HandleResponse(
            events.Response(httplib.OK, {'e': 'z'}, b'blah'), None)
        self.assertEqual(2, len(failures))
        for f in failures:
            f.Update([updates.Mode.API_REQUESTS])
        self.assertEqual({
            'headers': {
                'e': 'z'
            },
            'body': {
                'text': 'blah'
            }
        }, backing_data['api_call']['expect_response'])

        response = events.Response(httplib.OK, {'e': 'z'}, 'blah')
        failures = e.UpdateResponsePayload(response)
        self.assertEqual(1, len(failures))
        for f in failures:
            f.Update([updates.Mode.API_RESPONSE_PAYLOADS])
        self.assertEqual(
            {
                'headers': {
                    'e': 'z'
                },
                'body': 'blah',
                'status': httplib.OK
            }, backing_data['api_call']['return_response'])
示例#7
0
 def ResponseFromTransportResponse(self, response):
   """Returns a Response object from the transport's raw response."""
   return events_lib.Response(response.status_code, response.headers,
                              response.content.decode('utf-8'))
示例#8
0
 def ResponseFromTransportResponse(self, response):
   """Returns a Response object from the transport's raw response."""
   info, content = response
   headers = info.copy()
   status = int(headers.pop('status', httplib.OK))
   return events_lib.Response(status, headers, content.decode('utf-8'))