Example #1
0
    def test_no_accept_range(self, fake_urlopen):
        """WebHandle - ``__init__`` raises RuntimeError if the HTTP server doesn't support Range header"""
        fake_resp = MagicMock()
        fake_resp.code = 200
        fake_urlopen.return_value = fake_resp

        with self.assertRaises(RuntimeError):
            ova.WebHandle('http://localhost/nothing')
Example #2
0
    def test_not_found(self, fake_urlopen):
        """WebHandle - ``__init__`` raises FileNotFoundError if the HTTP response isn't OK"""
        fake_resp = MagicMock()
        fake_resp.code = 404
        fake_urlopen.return_value = fake_resp

        with self.assertRaises(FileNotFoundError):
            ova.WebHandle('http://localhost/nothing')
Example #3
0
    def test_seekable(self, fake_urlopen):
        """WebHandle - ``seekable`` returns True"""
        fake_resp = MagicMock()
        fake_resp.code = 200
        fake_resp.getheaders.return_value = [('content-length', '10'),
                                             ('accept-ranges', 'bytes')]
        fake_urlopen.return_value = fake_resp

        handle = ova.WebHandle('http://localhost/nothing')

        self.assertTrue(handle.seekable())
Example #4
0
    def test_read_closes(self, fake_urlopen):
        """WebHandle - ``read`` closes the socket automatically"""
        fake_resp = MagicMock()
        fake_resp.code = 200
        fake_resp.getheaders.return_value = [('content-length', '10'),
                                             ('accept-ranges', 'bytes')]
        fake_urlopen.return_value = fake_resp

        handle = ova.WebHandle('http://localhost/nothing')
        handle.read(100)

        self.assertTrue(fake_resp.close.called)
Example #5
0
    def test_tell(self, fake_urlopen):
        """WebHandle - ``tell`` returns the offset value"""
        fake_resp = MagicMock()
        fake_resp.code = 200
        fake_resp.getheaders.return_value = [('content-length', '10'),
                                             ('accept-ranges', 'bytes')]
        fake_urlopen.return_value = fake_resp

        handle = ova.WebHandle('http://localhost/nothing')
        expected = 0

        self.assertEqual(handle.tell(), expected)
Example #6
0
    def test_init_ok(self, fake_urlopen):
        """WebHandle - ``__init__`` happy path test"""
        fake_resp = MagicMock()
        fake_resp.code = 200
        fake_resp.getheaders.return_value = [('content-length', '10'),
                                             ('accept-ranges', 'bytes')]
        fake_urlopen.return_value = fake_resp

        handle = ova.WebHandle('http://localhost/nothing')

        self.assertTrue(isinstance(handle, ova.WebHandle))
        self.assertEqual(handle.st_size, 10)
Example #7
0
    def test_close(self, fake_urlopen):
        """WebHandle - ``close`` method is callable"""
        fake_resp = MagicMock()
        fake_resp.code = 200
        fake_resp.getheaders.return_value = [('content-length', '10'),
                                             ('accept-ranges', 'bytes')]
        fake_urlopen.return_value = fake_resp

        handle = ova.WebHandle('http://localhost/nothing')
        result = handle.close()
        expected = None

        self.assertEqual(result, expected)
Example #8
0
    def test_read_math(self, fake_urlopen):
        """WebHandle - ``read`` adjusts the offset correctly"""
        fake_resp = MagicMock()
        fake_resp.code = 200
        fake_resp.getheaders.return_value = [('content-length', '10'),
                                             ('accept-ranges', 'bytes')]
        fake_urlopen.return_value = fake_resp

        handle = ova.WebHandle('http://localhost/nothing')
        handle.read(100)
        expected = 100

        self.assertEqual(handle.offset, expected)
Example #9
0
    def test_seek_whence_2(self, fake_urlopen):
        """WebHandle - ``seek`` returns the st_size minus the offset when whence is 2"""
        fake_resp = MagicMock()
        fake_resp.code = 200
        fake_resp.getheaders.return_value = [('content-length', '10'),
                                             ('accept-ranges', 'bytes')]
        fake_urlopen.return_value = fake_resp

        handle = ova.WebHandle('http://localhost/nothing')
        result = handle.seek(5, whence=2)
        expected = 5

        self.assertEqual(result, expected)
Example #10
0
    def test_progress(self, fake_urlopen):
        """WebHandle - ``progress`` returns how much of the file has been read"""
        fake_resp = MagicMock()
        fake_resp.code = 200
        fake_resp.getheaders.return_value = [('content-length', '10'),
                                             ('accept-ranges', 'bytes')]
        fake_urlopen.return_value = fake_resp

        handle = ova.WebHandle('http://localhost/nothing')
        handle.offset = 5
        result = handle.progress()
        expected = 50

        self.assertEqual(result, expected)
Example #11
0
    def test_seek_whence_1(self, fake_urlopen):
        """WebHandle - ``seek`` returns the summed offset when param whence is 1"""
        fake_resp = MagicMock()
        fake_resp.code = 200
        fake_resp.getheaders.return_value = [('content-length', '10'),
                                             ('accept-ranges', 'bytes')]
        fake_urlopen.return_value = fake_resp

        handle = ova.WebHandle('http://localhost/nothing')
        handle.offset = 5
        result = handle.seek(10, whence=1)
        expected = 15

        self.assertEqual(result, expected)