def _encode_gzip(iter): data = StringIO() with GzipFile(fileobj=data, mode='w') as gzo: for item in iter: gzo.write(item) gzo.write('\n') return data.getvalue()
def test_iter_lines(self): lines = (0, 2, 10, 100) for i in lines: r = get(httpbin('stream', str(i)), prefetch=False) lines = list(r.iter_lines()) len_lines = len(lines) self.assertEqual(i, len_lines) # Tests that trailing whitespaces within lines do not get stripped. # Tests that a trailing non-terminated line does not get stripped. quote = ('''Agamemnon \n''' '''\tWhy will he not upon our fair request\r\n''' '''\tUntent his person and share the air with us?''') # Make a request and monkey-patch its contents r = get(httpbin('get'), prefetch=False) r.raw = StringIO(quote) lines = list(r.iter_lines()) len_lines = len(lines) self.assertEqual(len_lines, 3) joined = lines[0] + '\n' + lines[1] + '\r\n' + lines[2] self.assertEqual(joined, quote)
def nlst(self, path, request): '''Executes the FTP NLST command on the given path.''' data = StringIO() # Alias the close method. data.release_conn = data.close self.conn.cwd(path) code = self.conn.retrbinary('NLST', data_callback_factory(data)) # When that call has finished executing, we'll have all our data. response = build_text_response(request, data, code) # Close the connection. self.conn.close() return response
def add_report(self, key, content, content_type='text/plain'): from requests.compat import StringIO params = { 'project': self.project.id, 'job': self.id, 'key': key, 'content_type': content_type, } files = {'content': ('report', StringIO(content))} self._post('reports_add', 'json', params, files=files)
def build_response(request, data, code, encoding): response = Response() response.encoding = encoding # Fill in some useful fields. raw = StringIO() raw.write(data) raw.seek(0) response.raw = raw response.url = request.url response.request = request response.status_code = code # Run the response hook. response = dispatch_hook('response', request.hooks, response) return response
def list(self, path, request): '''Executes the FTP LIST command on the given path.''' data = StringIO() # To ensure the StringIO gets cleaned up, we need to alias its close # method to the release_conn() method. This is a dirty hack, but there # you go. data.release_conn = data.close self.conn.cwd(path) code = self.conn.retrbinary('LIST', data_callback_factory(data)) # When that call has finished executing, we'll have all our data. response = build_text_response(request, data, code) # Close the connection. self.conn.close() return response
def size(self, path, request): '''Executes the FTP SIZE command on the given path.''' self.conn.voidcmd('TYPE I') # SIZE is not usually allowed in ASCII mode size = self.conn.size(path) if not str(size).isdigit(): self.conn.close() return None data = StringIO(size) # To ensure the StringIO gets cleaned up, we need to alias its close # method to the release_conn() method. This is a dirty hack, but there # you go. data.release_conn = data.close response = build_text_response(request, data, '213') self.conn.close() return response
def requests_adapters_process_request(request, **kwargs): with flask_current_app.app_context(): environ = {'SERVER_NAME':'localhost'} environ['wsgi.url_scheme'] = 'http' environ['SERVER_PORT'] = '80' environ['PATH_INFO'] = request.path_url environ['REQUEST_METHOD'] = request.method environ['CONTENT_LENGTH'] = request.headers.get('Content-Length') environ['CONTENT_TYPE'] = request.headers.get('Content-Type') body = StringIO() body.write(request.body) body.seek(0) environ['wsgi.input'] = body context_request = flask_current_app.request_context(environ) context_request.push() flask_current_app.dispatch_request()
def build_response(request, data, code, encoding): '''Builds a response object from the data returned by ftplib, using the specified encoding.''' response = Response() response.encoding = encoding # Fill in some useful fields. raw = StringIO() raw.write(data) raw.seek(0) response.raw = raw response.url = request.url response.request = request response.status_code = code # Run the response hook. response = dispatch_hook('response', request.hooks, response) return response
def test_iter_lines(self): lines = (0, 2, 10, 100) for i in lines: r = get(httpbin('stream', str(i)), prefetch=False) lines = list(r.iter_lines()) len_lines = len(lines) self.assertEqual(i, len_lines) # Test 'dangling' fragment in responses that do not terminate in # a newline. quote = ('''Why will he not upon our fair request\n''' '''Untent his person and share the air with us?''') # Make a request and monkey-patch its contents r = get(httpbin('get')) r.raw = StringIO(quote) # Make sure iter_lines doesn't chop the trailing bit lines = '\n'.join(r.iter_lines()) self.assertEqual(lines, quote)
def _encode_identity(iter): data = StringIO() for item in iter: data.write(item) data.write('\n') return data.getvalue()