def test_headers(): headers = {"X-Header1": ["h1"], "X-Header2": "h2"} req = Request("GET", "http://go.com/", "", headers) assert req.headers == {"X-Header1": "h1", "X-Header2": "h2"} req.add_header("X-Header1", "h11") assert req.headers == {"X-Header1": "h11", "X-Header2": "h2"}
def test_headers(): headers = {'X-Header1': ['h1'], 'X-Header2': 'h2'} req = Request('GET', 'http://go.com/', '', headers) assert req.headers == {'X-Header1': 'h1', 'X-Header2': 'h2'} req.add_header('X-Header1', 'h11') assert req.headers == {'X-Header1': 'h11', 'X-Header2': 'h2'}
def test_remove_json_post_data_parameters(): body = b'{"id": "secret", "foo": "bar", "baz": "qux"}' request = Request('POST', 'http://google.com', body, {}) request.add_header('Content-Type', 'application/json') remove_post_data_parameters(request, ['id']) request_body_json = json.loads(request.body.decode('utf-8')) expected_json = json.loads(b'{"foo": "bar", "baz": "qux"}'.decode('utf-8')) assert request_body_json == expected_json
class VCRConnection(object): # A reference to the cassette that's currently being patched in cassette = None def _port_postfix(self): """ Returns empty string for the default port and ':port' otherwise """ port = self.real_connection.port default_port = {'https': 433, 'http': 80}[self._protocol] return ':{0}'.format(port) if port != default_port else '' def _uri(self, url): """Returns request absolute URI""" uri = "{0}://{1}{2}{3}".format( self._protocol, self.real_connection.host, self._port_postfix(), url, ) return uri def _url(self, uri): """Returns request selector url from absolute URI""" prefix = "{0}://{1}{2}".format( self._protocol, self.real_connection.host, self._port_postfix(), ) return uri.replace(prefix, '', 1) def request(self, method, url, body=None, headers=None): '''Persist the request metadata in self._vcr_request''' self._vcr_request = Request( method=method, uri=self._uri(url), body=body, headers=headers or {} ) log.debug('Got {0}'.format(self._vcr_request)) # Note: The request may not actually be finished at this point, so # I'm not sending the actual request until getresponse(). This # allows me to compare the entire length of the response to see if it # exists in the cassette. def putrequest(self, method, url, *args, **kwargs): """ httplib gives you more than one way to do it. This is a way to start building up a request. Usually followed by a bunch of putheader() calls. """ self._vcr_request = Request( method=method, uri=self._uri(url), body="", headers={} ) log.debug('Got {0}'.format(self._vcr_request)) def putheader(self, header, *values): for value in values: self._vcr_request.add_header(header, value) def send(self, data): ''' This method is called after request(), to add additional data to the body of the request. So if that happens, let's just append the data onto the most recent request in the cassette. ''' self._vcr_request.body = (self._vcr_request.body or '') + data def close(self): # Note: the real connection will only close if it's open, so # no need to check that here. self.real_connection.close() def endheaders(self, *args, **kwargs): """ Normally, this would atually send the request to the server. We are not sending the request until getting the response, so bypass this method for now. """ pass def getresponse(self, _=False): '''Retrieve the response''' # Check to see if the cassette has a response for this request. If so, # then return it if self.cassette.can_play_response_for(self._vcr_request): log.info( "Playing response for {0} from cassette".format( self._vcr_request ) ) response = self.cassette.play_response(self._vcr_request) return VCRHTTPResponse(response) else: if self.cassette.write_protected and self.cassette.filter_request(self._vcr_request): raise CannotOverwriteExistingCassetteException( "Can't overwrite existing cassette (%r) in " "your current record mode (%r)." % (self.cassette._path, self.cassette.record_mode) ) # Otherwise, we should send the request, then get the response # and return it. log.info( "{0} not in cassette, sending to real server".format( self._vcr_request ) ) self.real_connection.request( method=self._vcr_request.method, url=self._url(self._vcr_request.uri), body=self._vcr_request.body, headers=self._vcr_request.headers, ) # get the response response = self.real_connection.getresponse() # put the response into the cassette response = { 'status': { 'code': response.status, 'message': response.reason }, 'headers': serialize_headers(response), 'body': {'string': response.read()}, } self.cassette.append(self._vcr_request, response) return VCRHTTPResponse(response) def set_debuglevel(self, *args, **kwargs): self.real_connection.set_debuglevel(*args, **kwargs) def connect(self, *args, **kwargs): """ httplib2 uses this. Connects to the server I'm assuming. Only pass to the baseclass if we don't have a recorded response and are not write-protected. """ if hasattr(self, '_vcr_request') and \ self.cassette.can_play_response_for(self._vcr_request): # We already have a response we are going to play, don't # actually connect return if self.cassette.write_protected: # Cassette is write-protected, don't actually connect return return self.real_connection.connect(*args, **kwargs) @property def sock(self): if self.real_connection.sock: return self.real_connection.sock return VCRFakeSocket() @sock.setter def sock(self, value): if self.real_connection.sock: self.real_connection.sock = value def __init__(self, *args, **kwargs): if six.PY3: kwargs.pop('strict', None) # apparently this is gone in py3 # need to temporarily reset here because the real connection # inherits from the thing that we are mocking out. Take out # the reset if you want to see what I mean :) from vcr.patch import force_reset with force_reset(): self.real_connection = self._baseclass(*args, **kwargs)
class VCRConnection: # A reference to the cassette that's currently being patched in cassette = None def _port_postfix(self): """ Returns empty string for the default port and ':port' otherwise """ port = self.real_connection.port default_port = {'https': 433, 'http': 80}[self._protocol] return ':{0}'.format(port) if port != default_port else '' def _uri(self, url): """Returns request absolute URI""" uri = "{0}://{1}{2}{3}".format( self._protocol, self.real_connection.host, self._port_postfix(), url, ) return uri def _url(self, uri): """Returns request selector url from absolute URI""" prefix = "{0}://{1}{2}".format( self._protocol, self.real_connection.host, self._port_postfix(), ) return uri.replace(prefix, '', 1) def request(self, method, url, body=None, headers=None): '''Persist the request metadata in self._vcr_request''' self._vcr_request = Request(method=method, uri=self._uri(url), body=body, headers=headers or {}) log.debug('Got {0}'.format(self._vcr_request)) # Note: The request may not actually be finished at this point, so # I'm not sending the actual request until getresponse(). This # allows me to compare the entire length of the response to see if it # exists in the cassette. def putrequest(self, method, url, *args, **kwargs): """ httplib gives you more than one way to do it. This is a way to start building up a request. Usually followed by a bunch of putheader() calls. """ self._vcr_request = Request(method=method, uri=self._uri(url), body="", headers={}) log.debug('Got {0}'.format(self._vcr_request)) def putheader(self, header, *values): for value in values: self._vcr_request.add_header(header, value) def send(self, data): ''' This method is called after request(), to add additional data to the body of the request. So if that happens, let's just append the data onto the most recent request in the cassette. ''' self._vcr_request.body = (self._vcr_request.body or '') + data def close(self): # Note: the real connection will only close if it's open, so # no need to check that here. self.real_connection.close() def endheaders(self, *args, **kwargs): """ Normally, this would atually send the request to the server. We are not sending the request until getting the response, so bypass this method for now. """ pass def getresponse(self, _=False): '''Retrieve a the response''' # Check to see if the cassette has a response for this request. If so, # then return it if self.cassette.can_play_response_for(self._vcr_request): log.info("Playing response for {0} from cassette".format( self._vcr_request)) response = self.cassette.play_response(self._vcr_request) return VCRHTTPResponse(response) else: if self.cassette.write_protected and self.cassette._filter_request( self._vcr_request): raise CannotOverwriteExistingCassetteException( "Can't overwrite existing cassette (%r) in " "your current record mode (%r)." % (self.cassette._path, self.cassette.record_mode)) # Otherwise, we should send the request, then get the response # and return it. log.info("{0} not in cassette, sending to real server".format( self._vcr_request)) self.real_connection.request( method=self._vcr_request.method, url=self._url(self._vcr_request.uri), body=self._vcr_request.body, headers=self._vcr_request.headers, ) # get the response response = self.real_connection.getresponse() # put the response into the cassette response = { 'status': { 'code': response.status, 'message': response.reason }, 'headers': serialize_headers(response), 'body': { 'string': response.read() }, } self.cassette.append(self._vcr_request, response) return VCRHTTPResponse(response) def set_debuglevel(self, *args, **kwargs): self.real_connection.set_debuglevel(*args, **kwargs) def connect(self, *args, **kwargs): """ httplib2 uses this. Connects to the server I'm assuming. Only pass to the baseclass if we don't have a recorded response and are not write-protected. """ if hasattr(self, '_vcr_request') and \ self.cassette.can_play_response_for(self._vcr_request): # We already have a response we are going to play, don't # actually connect return if self.cassette.write_protected: # Cassette is write-protected, don't actually connect return return self.real_connection.connect(*args, **kwargs) @property def sock(self): if self.real_connection.sock: return self.real_connection.sock return VCRFakeSocket() @sock.setter def sock(self, value): if self.real_connection.sock: self.real_connection.sock = value def __init__(self, *args, **kwargs): if six.PY3: kwargs.pop('strict', None) # apparently this is gone in py3 # need to temporarily reset here because the real connection # inherits from the thing that we are mocking out. Take out # the reset if you want to see what I mean :) from vcr.patch import install, reset reset() self.real_connection = self._baseclass(*args, **kwargs) install(self.cassette)
def test_remove_nonexistent_json_post_data_parameters(): body = b'{}' request = Request('POST', 'http://google.com', body, {}) request.add_header('Content-Type', 'application/json') remove_post_data_parameters(request, ['id']) assert request.body == b'{}'
def test_remove_all_json_post_data_parameters(): body = b'{"id": "secret", "foo": "bar"}' request = Request('POST', 'http://google.com', body, {}) request.add_header('Content-Type', 'application/json') remove_post_data_parameters(request, ['id', 'foo']) assert request.body == b'{}'
class VCRConnection: # A reference to the cassette that's currently being patched in cassette = None def request(self, method, url, body=None, headers=None): '''Persist the request metadata in self._vcr_request''' self._vcr_request = Request( protocol=self._protocol, host=self.real_connection.host, port=self.real_connection.port, method=method, path=url, body=body, headers=headers or {} ) # Note: The request may not actually be finished at this point, so # I'm not sending the actual request until getresponse(). This # allows me to compare the entire length of the response to see if it # exists in the cassette. def putrequest(self, method, url, *args, **kwargs): """ httplib gives you more than one way to do it. This is a way to start building up a request. Usually followed by a bunch of putheader() calls. """ self._vcr_request = Request( protocol=self._protocol, host=self.real_connection.host, port=self.real_connection.port, method=method, path=url, body="", headers={} ) def putheader(self, header, *values): for value in values: self._vcr_request.add_header(header, value) def send(self, data): ''' This method is called after request(), to add additional data to the body of the request. So if that happens, let's just append the data onto the most recent request in the cassette. ''' self._vcr_request.body = (self._vcr_request.body or '') + data def close(self): # Note: the real connection will only close if it's open, so # no need to check that here. self.real_connection.close() def endheaders(self, *args, **kwargs): """ Normally, this would atually send the request to the server. We are not sending the request until getting the response, so bypass this method for now. """ pass def getresponse(self, _=False): '''Retrieve a the response''' # Check to see if the cassette has a response for this request. If so, # then return it if self._vcr_request in self.cassette and \ self.cassette.record_mode != "all" and \ self.cassette.rewound: response = self.cassette.play_response(self._vcr_request) return VCRHTTPResponse(response) else: if self.cassette.write_protected: raise CannotOverwriteExistingCassetteException( "Can't overwrite existing cassette (%r) in " "your current record mode (%r)." % (self.cassette._path, self.cassette.record_mode) ) # Otherwise, we should send the request, then get the response # and return it. self.real_connection.request( method=self._vcr_request.method, url=self._vcr_request.path, body=self._vcr_request.body, headers=dict(self._vcr_request.headers or {}) ) # get the response response = self.real_connection.getresponse() # put the response into the cassette response = { 'status': { 'code': response.status, 'message': response.reason }, 'headers': compat.get_headers(response), 'body': {'string': response.read()}, } self.cassette.append(self._vcr_request, response) return VCRHTTPResponse(response) def set_debuglevel(self, *args, **kwargs): self.real_connection.set_debuglevel(*args, **kwargs) def connect(self, *args, **kwargs): """ httplib2 uses this. Connects to the server I'm assuming. Only pass to the baseclass if we don't have a recorded response and are not write-protected. """ if hasattr(self, '_vcr_request') and \ self._vcr_request in self.cassette and \ self.cassette.record_mode != "all" and \ self.cassette.rewound: # We already have a response we are going to play, don't # actually connect return if self.cassette.write_protected: # Cassette is write-protected, don't actually connect return return self.real_connection.connect(*args, **kwargs) def __init__(self, *args, **kwargs): # need to temporarily reset here because the real connection # inherits from the thing that we are mocking out. Take out # the reset if you want to see what I mean :) from vcr.patch import install, reset reset() self.real_connection = self._baseclass(*args, **kwargs) install(self.cassette)