def test_file_from_buffer_response(self): response = FileResponse(io.BytesIO(b'binary content')) self.assertEqual(response.headers['Content-Length'], '14') self.assertEqual(response.headers['Content-Type'], 'application/octet-stream') self.assertFalse(response.has_header('Content-Disposition')) self.assertEqual(list(response), [b'binary content'])
def test_file_from_named_pipe_response(self): with tempfile.TemporaryDirectory() as temp_dir: pipe_file = os.path.join(temp_dir, 'named_pipe') os.mkfifo(pipe_file) pipe_for_read = os.open(pipe_file, os.O_RDONLY | os.O_NONBLOCK) with open(pipe_file, 'wb') as pipe_for_write: pipe_for_write.write(b'binary content') response = FileResponse(os.fdopen(pipe_for_read, mode='rb')) self.assertEqual(list(response), [b'binary content']) response.close() self.assertFalse(response.has_header('Content-Length'))
def test_file_from_named_pipe_response(self): with tempfile.TemporaryDirectory() as temp_dir: pipe_file = os.path.join(temp_dir, 'named_pipe') os.mkfifo(pipe_file) pipe_for_read = os.open(pipe_file, os.O_RDONLY | os.O_NONBLOCK) with open(pipe_file, 'wb') as pipe_for_write: pipe_for_write.write(b'binary content') response = FileResponse(os.fdopen(pipe_for_read, mode='rb')) self.assertEqual(list(response), [b'binary content']) response.close() self.assertFalse(response.has_header('Ĉontent-Length'))
def test_file_from_named_pipe_response(self): with tempfile.TemporaryDirectory() as temp_dir: pipe_file = os.path.join(temp_dir, "named_pipe") os.mkfifo(pipe_file) pipe_for_read = os.open(pipe_file, os.O_RDONLY | os.O_NONBLOCK) with open(pipe_file, "wb") as pipe_for_write: pipe_for_write.write(b"binary content") response = FileResponse(os.fdopen(pipe_for_read, mode="rb")) response_content = list(response) response.close() self.assertEqual(response_content, [b"binary content"]) self.assertFalse(response.has_header("Content-Length"))
def test_compressed_response(self): """ If compressed responses are served with the uncompressed Content-Type and a compression Content-Encoding, browsers might automatically uncompress the file, which is most probably not wanted. """ test_tuples = ( (".tar.gz", "application/gzip"), (".tar.bz2", "application/x-bzip"), (".tar.xz", "application/x-xz"), ) for extension, mimetype in test_tuples: with self.subTest(ext=extension): with tempfile.NamedTemporaryFile(suffix=extension) as tmp: response = FileResponse(tmp) self.assertEqual(response.headers["Content-Type"], mimetype) self.assertFalse(response.has_header("Content-Encoding"))
def test_compressed_response(self): """ If compressed responses are served with the uncompressed Content-Type and a compression Content-Encoding, browsers might automatically uncompress the file, which is most probably not wanted. """ test_tuples = ( ('.tar.gz', 'application/gzip'), ('.tar.bz2', 'application/x-bzip'), ('.tar.xz', 'application/x-xz'), ) for extension, mimetype in test_tuples: with self.subTest(ext=extension): with tempfile.NamedTemporaryFile(suffix=extension) as tmp: response = FileResponse(tmp) self.assertEqual(response['Content-Type'], mimetype) self.assertFalse(response.has_header('Content-Encoding'))
def test_content_disposition_buffer(self): response = FileResponse(io.BytesIO(b"binary content")) self.assertFalse(response.has_header("Content-Disposition"))
def test_file_from_buffer_response(self): response = FileResponse(io.BytesIO(b'binary content')) self.assertEqual(response['Content-Length'], '14') self.assertEqual(response['Content-Type'], 'application/octet-stream') self.assertFalse(response.has_header('Content-Disposition')) self.assertEqual(list(response), [b'binary content'])