def test_connect_error(self, conn_mock): response = Mock(status=404) conn_mock.return_value.getresponse.return_value = response conn = nw.HTTPThread(Mock(), 0) info = conn.connect() self.assertIsInstance(info, ti.TaskHeadError) self.assertEqual(info.status, 404)
def test_connect_redirect(self, conn_mock): response = Mock(status=301) response.getheader.return_value = 'http://server.com' conn_mock.return_value.getresponse.return_value = response conn = nw.HTTPThread(Mock(), 0) info = conn.connect() self.assertIsInstance(info, ti.TaskRedirect) self.assertEqual(info.location.url, 'http://server.com')
def test_connect_ok(self, conn_mock): response = Mock(status=200) response.getheader.return_value = '100' conn_mock.return_value.getresponse.return_value = response conn = nw.HTTPThread(Mock(), 0) info = conn.connect() self.assertIsInstance(info, ti.TaskHeadData) self.assertEqual(info.file_size, 100)
def test_redirect_absolute(self): url = Mock() url.protocol = 'http' url.host = 'server.com' conn = nw.HTTPThread(url, 0) info = conn.redirect('/path/to/file', 301) self.assertIsInstance(info, ti.TaskRedirect) self.assertEqual(info.location.url, 'http://server.com/path/to/file')
def test_run_ok(self): task_info = Mock() conn = nw.HTTPThread(Mock(), 0) conn.data_queue = Mock() conn.connect = Mock(return_value=task_info) self.assertFalse(conn.ready.is_set()) conn.run() conn.data_queue.put.assert_called_with(task_info) self.assertTrue(conn.ready.is_set())
def test_run_error(self): conn = nw.HTTPThread(Mock(), 0) conn.data_queue = Mock() conn.connect = Mock(side_effect=Exception) self.assertFalse(conn.ready.is_set()) conn.run() args = conn.data_queue.put.call_args self.assertIsInstance(args[0][0], ti.TaskHeadError) self.assertTrue(conn.ready.is_set())
def test_redirect_with_host(self): location = 'http://server.com/path/to/file' conn = nw.HTTPThread(Mock(), 0) info = conn.redirect(location, 301) self.assertIsInstance(info, ti.TaskRedirect) self.assertEqual(info.location.url, location)