def setUp(self): self.server = HTTPProxy() self.server.debug = True self.server.log = StringIO.StringIO() self.server.startServing()
class ProxyTestCase(unittest.TestCase): def setUp(self): self.server = HTTPProxy() self.server.debug = True self.server.log = StringIO.StringIO() self.server.startServing() def tearDown(self): self.server.stopServing() def test_getting_proxy(self): proxy = get_network_proxy() self.assertTrue(isinstance(proxy, QNetworkProxy)) self.assertEquals(str(proxy.hostName()), '127.0.0.1') self.assertEquals(proxy.port(), 8000) def test_access_without_credentials(self): loop = QEventLoop() proxy = get_network_proxy() manager = QNetworkAccessManager() manager.setProxy(proxy) manager.finished.connect(loop.exit) reply = manager.get(QNetworkRequest(QUrl('http://aws.amazon.com/'))) loop.exec_(flags=QEventLoop.ExcludeUserInputEvents) if reply.isFinished(): self.assertEquals(self.server.log.getvalue(), '407 Proxy Authentication Required\n\n') else: if reply.isRunning(): self.failUnless(False, msg='The request has timed out.') else: self.failUnless(False, msg='A Network error occurred.') def test_access_with_credentials(self): loop = QEventLoop() proxy = get_network_proxy() proxy.setUser(self.server.username) proxy.setPassword(self.server.password) manager = QNetworkAccessManager() manager.setProxy(proxy) manager.finished.connect(loop.exit) reply = manager.get(QNetworkRequest(QUrl('http://aws.amazon.com/'))) loop.exec_(flags=QEventLoop.ExcludeUserInputEvents) if reply.isFinished(): self.assertEquals(self.server.log.getvalue(), '407 Proxy Authentication Required\n\n'\ 'GET http://aws.amazon.com/ HTTP/1.1\n\n') else: if reply.isRunning(): self.failUnless(False, msg='The request has timed out.') else: self.failUnless(False, msg='A Network error occurred.') def test_access_to_remote_succeeded(self): loop = QEventLoop() proxy = get_network_proxy() proxy.setUser(self.server.username) proxy.setPassword(self.server.password) manager = QNetworkAccessManager() manager.setProxy(proxy) manager.finished.connect(loop.exit) reply = manager.get(QNetworkRequest(QUrl('http://aws.amazon.com/'))) loop.exec_(flags=QEventLoop.ExcludeUserInputEvents) if reply.isFinished(): response_code = reply.attribute( QNetworkRequest.HttpStatusCodeAttribute).toString() self.assertEquals(response_code, '200') self.assertEquals(reply.url(), QUrl('http://aws.amazon.com/')) else: if reply.isRunning(): self.failUnless(False, msg='The request has timed out.') else: self.failUnless(False, msg='A Network error occurred.')