Coverage for vcr.stubs : 67%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
'''Stubs for patching HTTP and HTTPS requests'''
HTTPConnection, HTTPSConnection, HTTPMessage, HTTPResponse, )
""" A socket that doesn't do anything! Used when playing back casssettes, when there is no actual open socket. """
pass
""" This is kinda crappy. requests will watch this descriptor and make sure it's not closed. Return file descriptor 0 since that's stdin. """ return 0 # wonder how bad this is....
""" Convert headers from our serialized dict with lists for keys to a HTTPMessage """ key.encode('utf-8') + b":" + v.encode('utf-8') + b"\r\n"
out = {} for key, values in compat.get_headers(response.msg): out.setdefault(key, []) out[key].extend(values) return out
""" Stub reponse class that gets returned instead of a HTTPResponse """
def closed(self): # in python3, I can't change the value of self.closed. So I' # twiddling self._closed and using this property to shadow the real # self.closed from the superclas
return self._content.readline(*args, **kwargs)
return self.status
return parse_headers(self.recorded_response['headers'])
message = parse_headers(self.recorded_response['headers']) return list(compat.get_header_items(message))
values = [v for (k, v) in self.getheaders() if k.lower() == header.lower()]
if values: return ', '.join(values) else: return default
# A reference to the cassette that's currently being patched in
""" Returns empty string for the default port and ':port' otherwise """
"""Returns request absolute URI""" self._protocol, self.real_connection.host, self._port_postfix(), url, )
"""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)
'''Persist the request metadata in self._vcr_request''' method=method, uri=self._uri(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.
""" 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))
for value in values: self._vcr_request.add_header(header, value)
''' 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
# Note: the real connection will only close if it's open, so # no need to check that here.
""" 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
'''Retrieve the response''' # Check to see if the cassette has a response for this request. If so, # then return it "Playing response for {0} from cassette".format( self._vcr_request ) ) else: if self.cassette.write_protected and self.cassette.filter_request( self._vcr_request ): raise CannotOverwriteExistingCassetteException( "No match for the request (%r) was found. " "Can't overwrite existing cassette (%r) in " "your current record mode (%r)." % (self._vcr_request, 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 ) ) # This is imported here to avoid circular import. # TODO(@IvanMalison): Refactor to allow normal import. from vcr.patch import force_reset with force_reset(): 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)
self.real_connection.set_debuglevel(*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)
def sock(self): return self.real_connection.sock
def sock(self, value): if self.real_connection.sock: self.real_connection.sock = value
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 :)
'''A Mocked class for HTTP requests'''
'''A Mocked class for HTTPS requests''' |