Ejemplo n.º 1
0
 def testApiCallEventOrderedRequestParams(self):
     backing_data = {
         'api_call': {
             'expect_request': {
                 'uri': 'https://example.com?a=foo&b=bar&c=foobar',
                 'method': 'GET',
                 'headers': {
                     'a': 'b',
                     'c': 'd'
                 },
                 'body': {
                     'json': {
                         'ba': 'bb',
                         'bc': 'bd'
                     }
                 },
             },
             'return_response': {
                 'headers': {
                     'e': 'f',
                     'g': 'h'
                 },
                 'body': 'response body',
             }
         }
     }
     e = events.ApiCallEvent.FromData(backing_data)
     request = events.Request('https://example.com?a=foo&b=bar&c=foobar',
                              'GET', {
                                  b'a': b'b',
                                  b'c': b'd'
                              }, '{"ba": "bb", "bc": "bd"}')
     self.assertEqual([], e.Handle(request))
     request = events.Request('https://example.com?b=bar&c=foobar&a=foo',
                              'GET', {
                                  b'a': b'b',
                                  b'c': b'd'
                              }, '{"ba": "bb", "bc": "bd"}')
     self.assertEqual([], e.Handle(request))
     request = events.Request('https://example.com?c=foobar&b=bar&a=foo',
                              'GET', {
                                  b'a': b'b',
                                  b'c': b'd'
                              }, '{"ba": "bb", "bc": "bd"}')
     self.assertEqual([], e.Handle(request))
Ejemplo n.º 2
0
 def testComplexAssertionUpdates(self):
     backing_data = {
         'api_call': {
             'expect_request': {
                 'uri': 'https://example.com',
                 'method': 'GET',
                 'headers': {
                     'a': {
                         'is_none': True
                     },
                     'c': {
                         'matches': r'\w'
                     },
                     'e': {
                         'in': ['d', 'e', 'f']
                     },
                 },
                 'body': {
                     'text': {
                         'matches': 'q.*'
                     }
                 },
             },
             'return_response': {
                 'headers': {},
                 'body': '',
             }
         }
     }
     e = events.ApiCallEvent.FromData(backing_data)
     request = events.Request('https://foo.com', 'POST', {
         b'a': b'b',
         b'c': b' ',
         b'e': b'1',
     }, 'asdf')
     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': {
                 'a': 'b',
                 'c': ' ',
                 'e': '1'
             },
             'body': {
                 'text': 'asdf'
             }
         }, backing_data['api_call']['expect_request'])
Ejemplo n.º 3
0
    def testApiCallBodies(self, assertion, actual, new, has_failures):
        backing_data = {
            'api_call': {
                'expect_request': {
                    'uri': 'https://example.com',
                    'method': 'GET',
                    'headers': {}
                }
            }
        }
        backing_data['api_call']['expect_request'].update(assertion)

        e = events.ApiCallEvent.FromData(backing_data)
        request = events.Request('https://example.com', 'GET', {}, actual)
        failures = e.Handle(request)
        self.assertEqual(bool(failures), has_failures)
        for f in failures:
            f.Update([updates.Mode.API_REQUESTS])
        self.assertEqual(new,
                         backing_data['api_call']['expect_request']['body'])
Ejemplo n.º 4
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'])
Ejemplo n.º 5
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)
Ejemplo n.º 6
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'])
Ejemplo n.º 7
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'])
Ejemplo n.º 8
0
 def RequestFromArgs(self, operation_model, request_dict):
   """Returns a Request object from the args used in a request call."""
   self.url = request_dict['url']
   # pylint: disable=protected-access
   return events_lib.Request(request_dict['url'], request_dict['method'],
                             request_dict['headers'], request_dict['body'])
Ejemplo n.º 9
0
 def RequestFromArgs(self, method, uri, headers=None, data=None, **kwargs):
   """Returns a Request object from the args used in a request call."""
   del kwargs  # Unused
   # params is not used by apitools
   return events_lib.Request(uri, method, headers or {}, data)
Ejemplo n.º 10
0
 def RequestFromArgs(self, uri, method='GET', body=None, headers=None,
                     redirections=5, connection_type=None, **kwargs):
   """Returns a Request object from the args used in a request call."""
   del redirections, connection_type, kwargs  # Unused
   return events_lib.Request(uri, method, headers, body)