Пример #1
0
 def test_file_from_disk_response(self):
     response = FileResponse(open(__file__, 'rb'))
     self.assertEqual(response['Content-Length'],
                      str(os.path.getsize(__file__)))
     self.assertIn(response['Content-Type'],
                   ['text/x-python', 'text/plain'])
     response.close()
Пример #2
0
 def test_content_length_nonzero_starting_position_file(self):
     file = open(__file__, "rb")
     file.seek(10)
     response = FileResponse(file)
     response.close()
     self.assertEqual(response.headers["Content-Length"],
                      str(os.path.getsize(__file__) - 10))
Пример #3
0
 def test_file_from_disk_as_attachment(self):
     response = FileResponse(open(__file__, 'rb'), as_attachment=True)
     self.assertEqual(response['Content-Length'],
                      str(os.path.getsize(__file__)))
     self.assertIn(response['Content-Type'],
                   ['text/x-python', 'text/plain'])
     self.assertEqual(response['Content-Disposition'],
                      'attachment; filename="test_fileresponse.py"')
     response.close()
Пример #4
0
 def test_file_from_disk_response(self):
     response = FileResponse(open(__file__, 'rb'))
     self.assertEqual(response.headers['Content-Length'],
                      str(os.path.getsize(__file__)))
     self.assertIn(response.headers['Content-Type'],
                   ['text/x-python', 'text/plain'])
     self.assertEqual(
         response.headers['Content-Disposition'],
         'inline; filename="test_fileresponse.py"',
     )
     response.close()
Пример #5
0
    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'))
Пример #6
0
    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"))
Пример #7
0
    def test_unclosable_filelike_object(self):
        Channel("test").send({
            "reply_channel": "test",
            "http_version": "1.1",
            "method": "GET",
            "path": b"/test/",
        })

        # This is a readable object that cannot be closed.
        class Unclosable:
            def read(self, n=-1):
                # Nothing to see here
                return b""

        response = FileResponse(Unclosable())
        handler = FakeAsgiHandler(response)
        reply_messages = list(
            islice(handler(self.get_next_message("test", require=True)), 5))
        self.assertEqual(len(reply_messages), 1)
        response.close()
Пример #8
0
    def test_content_length_nonzero_starting_position_file_seekable_no_tell(
            self):
        class TestFile:
            def __init__(self, path, *args, **kwargs):
                self._file = open(path, *args, **kwargs)

            def read(self, n_bytes=-1):
                return self._file.read(n_bytes)

            def seek(self, offset, whence=io.SEEK_SET):
                return self._file.seek(offset, whence)

            def seekable(self):
                return True

            @property
            def name(self):
                return self._file.name

            def close(self):
                if self._file:
                    self._file.close()
                    self._file = None

            def __enter__(self):
                return self

            def __exit__(self, e_type, e_val, e_tb):
                self.close()

        file = TestFile(__file__, "rb")
        file.seek(10)
        response = FileResponse(file)
        response.close()
        self.assertEqual(response.headers["Content-Length"],
                         str(os.path.getsize(__file__) - 10))
Пример #9
0
 def test_content_disposition_file(self):
     filenames = (
         ("", "test_fileresponse.py"),
         ("custom_name.py", "custom_name.py"),
     )
     dispositions = (
         (False, "inline"),
         (True, "attachment"),
     )
     for (filename, header_filename), (
             as_attachment,
             header_disposition,
     ) in itertools.product(filenames, dispositions):
         with self.subTest(filename=filename,
                           disposition=header_disposition):
             response = FileResponse(open(__file__, "rb"),
                                     filename=filename,
                                     as_attachment=as_attachment)
             response.close()
             self.assertEqual(
                 response.headers["Content-Disposition"],
                 '%s; filename="%s"' %
                 (header_disposition, header_filename),
             )
Пример #10
0
 def test_content_disposition_file(self):
     filenames = (
         ('', 'test_fileresponse.py'),
         ('custom_name.py', 'custom_name.py'),
     )
     dispositions = (
         (False, 'inline'),
         (True, 'attachment'),
     )
     for (filename,
          header_filename), (as_attachment,
                             header_disposition) in itertools.product(
                                 filenames, dispositions):
         with self.subTest(filename=filename,
                           disposition=header_disposition):
             response = FileResponse(open(__file__, 'rb'),
                                     filename=filename,
                                     as_attachment=as_attachment)
             response.close()
             self.assertEqual(
                 response.headers['Content-Disposition'],
                 '%s; filename="%s"' %
                 (header_disposition, header_filename),
             )
Пример #11
0
 def test_content_type_file(self):
     response = FileResponse(open(__file__, "rb"))
     response.close()
     self.assertIn(response.headers["Content-Type"],
                   ["text/x-python", "text/plain"])
Пример #12
0
 def test_content_length_file(self):
     response = FileResponse(open(__file__, "rb"))
     response.close()
     self.assertEqual(response.headers["Content-Length"],
                      str(os.path.getsize(__file__)))
Пример #13
0
 def test_content_type_file(self):
     response = FileResponse(open(__file__, 'rb'))
     response.close()
     self.assertIn(response.headers['Content-Type'],
                   ['text/x-python', 'text/plain'])