예제 #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')
예제 #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')
예제 #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())
예제 #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)
예제 #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)
예제 #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)
예제 #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)
예제 #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)
예제 #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)
예제 #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)
예제 #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)