Implemented a dummy http server to run unit-test on a offline computer.
Reviewed: Marcelo Lira <marcelo.lira@openbossa.org>
Luciano Wolf <luciano.wolf@openbossa.org>
This commit is contained in:
parent
5ce7c94536
commit
88146cf500
4 changed files with 118 additions and 32 deletions
|
|
@ -1,3 +1,5 @@
|
||||||
|
|
||||||
|
PYSIDE_TEST(basic_auth_test.py)
|
||||||
PYSIDE_TEST(accessManager_test.py)
|
PYSIDE_TEST(accessManager_test.py)
|
||||||
PYSIDE_TEST(http_test.py)
|
PYSIDE_TEST(http_test.py)
|
||||||
PYSIDE_TEST(tcpserver_test.py)
|
PYSIDE_TEST(tcpserver_test.py)
|
||||||
|
|
|
||||||
38
tests/QtNetwork/basic_auth_test.py
Normal file
38
tests/QtNetwork/basic_auth_test.py
Normal file
|
|
@ -0,0 +1,38 @@
|
||||||
|
import unittest
|
||||||
|
|
||||||
|
from PySide.QtCore import *
|
||||||
|
from PySide.QtNetwork import *
|
||||||
|
|
||||||
|
from helper import UsesQApplication
|
||||||
|
from httpd import TestServer
|
||||||
|
|
||||||
|
class testAuthenticationSignal(UsesQApplication):
|
||||||
|
|
||||||
|
def setUp(self):
|
||||||
|
super(testAuthenticationSignal, self).setUp()
|
||||||
|
self.httpd = TestServer(secure=True)
|
||||||
|
self.httpd.start()
|
||||||
|
self._resultOk = False
|
||||||
|
|
||||||
|
def tearDown(self):
|
||||||
|
self.httpd.shutdown()
|
||||||
|
del self.httpd
|
||||||
|
super(testAuthenticationSignal, self).tearDown()
|
||||||
|
|
||||||
|
def onAuthRequest(self, hostname, port, auth):
|
||||||
|
self.assert_(isinstance(auth, QAuthenticator))
|
||||||
|
self._resultOk = True
|
||||||
|
self.app.quit()
|
||||||
|
|
||||||
|
|
||||||
|
def testwaitSignal(self):
|
||||||
|
http = QHttp()
|
||||||
|
http.setHost("localhost", self.httpd.port())
|
||||||
|
http.connect(SIGNAL("authenticationRequired(const QString&, quint16, QAuthenticator*)"), self.onAuthRequest)
|
||||||
|
path = QUrl.toPercentEncoding("/index.html", "!$&'()*+,;=:@/")
|
||||||
|
data = http.get(path)
|
||||||
|
self.app.exec_()
|
||||||
|
self.assert_(self._resultOk)
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
unittest.main()
|
||||||
|
|
@ -7,24 +7,27 @@ from PySide.QtCore import *
|
||||||
from PySide.QtNetwork import *
|
from PySide.QtNetwork import *
|
||||||
|
|
||||||
from helper import UsesQApplication
|
from helper import UsesQApplication
|
||||||
|
from httpd import TestServer
|
||||||
|
|
||||||
"""
|
|
||||||
class HttpSignalsCase(UsesQApplication):
|
class HttpSignalsCase(UsesQApplication):
|
||||||
'''Test case for launching QHttp signals'''
|
'''Test case for launching QHttp signals'''
|
||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
super(HttpSignalsCase, self).setUp()
|
super(HttpSignalsCase, self).setUp()
|
||||||
|
|
||||||
self.http = QHttp()
|
self.http = QHttp()
|
||||||
self.url = QUrl('http://www.google.com')
|
self.httpd = TestServer()
|
||||||
self.timer = QTimer.singleShot(250, self.app.quit)
|
self.httpd.start()
|
||||||
|
self.url = QUrl('localhost:' + str(self.httpd.port()))
|
||||||
|
self.called = False
|
||||||
|
|
||||||
def tearDown(self):
|
def tearDown(self):
|
||||||
|
self.httpd.shutdown()
|
||||||
del self.http
|
del self.http
|
||||||
super(HttpSignalsCase, self).tearDown()
|
super(HttpSignalsCase, self).tearDown()
|
||||||
|
|
||||||
def callback(self, ident):
|
def callback(self, ident):
|
||||||
self.called = True
|
self.called = True
|
||||||
|
self.app.quit()
|
||||||
|
|
||||||
def testDefaultArgs(self):
|
def testDefaultArgs(self):
|
||||||
#QHttp signal requestStarted signal
|
#QHttp signal requestStarted signal
|
||||||
|
|
@ -35,33 +38,5 @@ class HttpSignalsCase(UsesQApplication):
|
||||||
self.app.exec_()
|
self.app.exec_()
|
||||||
self.assert_(self.called)
|
self.assert_(self.called)
|
||||||
|
|
||||||
class testHttp(UsesQApplication):
|
|
||||||
def testRead(self):
|
|
||||||
header = QHttpRequestHeader("GET", QString(QUrl.toPercentEncoding("/index.html")))
|
|
||||||
header.setValue("Host", "qtsoftware.com");
|
|
||||||
http = QHttp()
|
|
||||||
http.setHost("qtsoftware.com")
|
|
||||||
http.request(header)
|
|
||||||
data = http.read(100)
|
|
||||||
"""
|
|
||||||
|
|
||||||
class testAuthenticationSignal(UsesQApplication):
|
|
||||||
def onAuthRequest(self, hostname, port, auth):
|
|
||||||
self.assert_(isinstance(auth, QAuthenticator))
|
|
||||||
print auth.realm()
|
|
||||||
self._resultOk = True
|
|
||||||
self.app.exit()
|
|
||||||
|
|
||||||
def testwaitSignal(self):
|
|
||||||
self._resultOk = False
|
|
||||||
http = QHttp()
|
|
||||||
http.setHost("projects.maemo.org", QHttp.ConnectionModeHttps, 0)
|
|
||||||
http.connect(SIGNAL("authenticationRequired(const QString&, quint16, QAuthenticator*)"), self.onAuthRequest)
|
|
||||||
path = QUrl.toPercentEncoding("/index.html", "!$&'()*+,;=:@/")
|
|
||||||
print http.get(path)
|
|
||||||
self.app.exec_()
|
|
||||||
self.assert_(self._resultOk)
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
|
|
||||||
71
tests/util/httpd.py
Normal file
71
tests/util/httpd.py
Normal file
|
|
@ -0,0 +1,71 @@
|
||||||
|
import SocketServer
|
||||||
|
import BaseHTTPServer
|
||||||
|
import random
|
||||||
|
|
||||||
|
from threading import Thread
|
||||||
|
|
||||||
|
|
||||||
|
class TestHandler(BaseHTTPServer.BaseHTTPRequestHandler):
|
||||||
|
DATA = "PySide Server"
|
||||||
|
|
||||||
|
def do_GET(self):
|
||||||
|
self.send_head()
|
||||||
|
self.wfile.write(TestHandler.DATA)
|
||||||
|
|
||||||
|
def do_HEAD(self):
|
||||||
|
self.send_head()
|
||||||
|
|
||||||
|
def send_head(self):
|
||||||
|
self.send_response(200)
|
||||||
|
self.send_header("Content-type", "text/plain")
|
||||||
|
self.send_header("Content-Length", str(len(TestHandler.DATA)))
|
||||||
|
self.end_headers()
|
||||||
|
|
||||||
|
class TestSecureHandler(BaseHTTPServer.BaseHTTPRequestHandler):
|
||||||
|
DATA = "PySide"
|
||||||
|
|
||||||
|
def do_GET(self):
|
||||||
|
self.send_head()
|
||||||
|
self.wfile.write(TestHandler.DATA)
|
||||||
|
|
||||||
|
def do_HEAD(self):
|
||||||
|
self.send_head()
|
||||||
|
|
||||||
|
def send_head(self):
|
||||||
|
try:
|
||||||
|
handler = self.marshall_handler()
|
||||||
|
handler.do_request(self)
|
||||||
|
except:
|
||||||
|
self.send_response(401)
|
||||||
|
self.send_header("WWW-Authenticate", "Basic realm='Secure Area'")
|
||||||
|
self.send_header("Content-type", "text/plain")
|
||||||
|
self.send_header("Content-Length", str(len(TestHandler.DATA)))
|
||||||
|
self.end_headers()
|
||||||
|
|
||||||
|
|
||||||
|
class TestServer(Thread):
|
||||||
|
|
||||||
|
def __init__(self, secure=False):
|
||||||
|
Thread.__init__(self)
|
||||||
|
|
||||||
|
self._port = 8000 + random.randint(0, 100)
|
||||||
|
self.keep_running = True
|
||||||
|
server = SocketServer.TCPServer
|
||||||
|
|
||||||
|
if secure:
|
||||||
|
handle = TestSecureHandler
|
||||||
|
else:
|
||||||
|
handle = TestHandler
|
||||||
|
|
||||||
|
self.httpd = SocketServer.TCPServer(('' , self._port), handle)
|
||||||
|
|
||||||
|
def port(self):
|
||||||
|
return self._port
|
||||||
|
|
||||||
|
def run(self):
|
||||||
|
self.httpd.serve_forever()
|
||||||
|
|
||||||
|
def shutdown(self):
|
||||||
|
self.httpd.shutdown()
|
||||||
|
self.join()
|
||||||
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue