def __init__(self, headers): self.fp = True if six.PY2: self.msg = httplib.HTTPMessage(StringIO()) else: self.msg = httplib.HTTPMessage() for (k, v) in headers.items(): self.msg[k] = v
def encode_HarResponse_to_HTTPResponse(self, har): # headers = dict(map(utils.pair_from_obj, har.headers)) if har is None: return None class DummySocket(object): bufsize = 1024 def close(self): pass def sendall(self, data): pass def readline(self, bufsize=1024): return '' def read(self, bufsize=1024): return '' def makefile(self, mode, bufsize=1024): self.bufsize = bufsize return self resp = http_client.HTTPResponse(DummySocket()) resp.status = har.status resp.reason = har.statusText resp.version = utils.parse_http_version(har.httpVersion) resp.length = 0 resp.msg = http_client.HTTPMessage(resp.fp) resp.msg.startofheaders = har._statusLineSize resp.msg.startofbody = har.headersSize return resp
def test_incomplete_download_sets_content_length_correctly(self): expected_content_length = 100 bytes_returned_by_server = 'byte count less than content length' http_response = mock.Mock(spec=http_client.HTTPResponse) http_response.reason = 'reason' http_response.version = 'version' http_response.status = http_client.OK http_response.read.side_effect = [bytes_returned_by_server, ''] http_response.getheaders.return_value = [('Content-Length', expected_content_length)] # This method is only called with content-length: http_response.getheader.return_value = expected_content_length headers_stream = io.BytesIO(b'Content-Length:%d' % expected_content_length) if six.PY2: http_response.msg = http_client.HTTPMessage(headers_stream) else: http_response.msg = http_client.parse_headers(headers_stream) mock_connection = mock.Mock(spec=http_client.HTTPConnection) mock_connection.getresponse.return_value = http_response http = HttpWithDownloadStream() http.stream = mock.Mock(spec=io.BufferedIOBase) http.stream.mode = 'wb' http._conn_request(mock_connection, 'uri', 'GET', 'body', 'headers') # To resume this download correctly, the response needs to have the number # of bytes we actually received, not the number of bytes in the original # content-length header. self.assertEqual(int(http_response.msg['content-length']), len(bytes_returned_by_server))
def __init__(self, cache_location, url, set_cache_header=True): self.cache_location = cache_location hpath, bpath = calculate_cache_path(cache_location, url) StringIO.__init__(self, open(bpath, "rb").read()) self.url = url self.code = 200 self.msg = "OK" headerbuf = open(hpath, "rb").read() if set_cache_header: headerbuf += "x-local-cache: %s\r\n" % (bpath) self.headers = httplib.HTTPMessage(StringIO(headerbuf))
def __enter__(self): self.request_context = mock.patch.object(http_client.HTTPConnection, 'request') self.request_context.return_value = None self.request_mock = self.request_context.__enter__() self.fixture_file = open(join(dirname(__file__), 'fixtures', self.fixture), 'rb') # Read through the request. preamble_line = self.fixture_file.readline().strip() try: self.method, self.uri, http_version = preamble_line.split(None, 2) self.method = self.method.decode() self.uri = self.uri.decode() except ValueError: raise ValueError("Couldn't parse preamble line from fixture file %r; does it have a fixture in it?" % self.fixture) # Read request headers def read_headers(fp): while True: try: line = fp.readline() except EOFError: return if not line or line == six.b('\n'): return yield line if six.PY2: msg = http_client.HTTPMessage(self.fixture_file, 0) self.headers = dict((k, v.strip()) for k, v in (header.split(':', 1) for header in msg.headers)) else: # http.client.HTTPMessage doesn't have importing headers from file msg = http_client.HTTPMessage() headers = email.message_from_bytes(six.b('').join(read_headers(self.fixture_file))) self.headers = dict((k, v.strip()) for k, v in headers._headers) # self.headers = {k: v for k, v in headers._headers} msg.fp = None # Read through to the vertical space. def nextline(fp): while True: try: line = fp.readline() except EOFError: return if not line or line.startswith(six.b('\x16')): return yield line body = six.b('').join(nextline(self.fixture_file)) # exhaust the request either way self.body = None if self.method in ('PUT', 'POST'): if 'Content-Type' in self.headers: if 'application/xml' in self.headers['Content-Type']: self.body = xml(body) else: self.body = body # Set up the response returner. sock = mock.Mock() sock.makefile = mock.Mock(return_value=self.fixture_file) response = http_client.HTTPResponse(sock, method=self.method) response.begin() self.response_context = mock.patch.object(http_client.HTTPConnection, 'getresponse', lambda self: response) self.response_mock = self.response_context.__enter__() return self