Anonymous avatar Anonymous committed 18bbd17

Made testing framework more flexible (can now check response status and header)

Comments (0)

Files changed (4)

             os.execlp(infoMap['path'],infoMap['path'],'testsite.py')
     return pid
 
-def getPage(url, cookies, isSSL=0, extraRequestHeader = []):
+def getPage(url, cookies, extraRequestHeader = []):
     data=""
     i=0
     response = None
     while i<10:
         try:
-            if isSSL:
-                conn=httplib.HTTPSConnection('127.0.0.1:8000')
-            else:
-                conn=httplib.HTTPConnection('127.0.0.1:8000')
-
+            conn=httplib.HTTPConnection('127.0.0.1:8000')
             conn.putrequest("GET", url)
-
             conn.putheader("Host", "127.0.0.1")
             if cookies:
                 cookieList = []
 
             cookies=response.msg.getallmatchingheaders("Set-Cookie")
 
-            data=response.read()
+            class EmptyClass: pass
+            cpg = EmptyClass()
+            cpg.response = EmptyClass()
+            cpg.response.headerMap = {'Status': response.status}
+            for line in response.msg.headers:
+                line = line.strip()
+                i = line.find(':')
+                key, value = line[:i], line[i+1:].strip()
+                cpg.response.headerMap[key] = value
+
+            cpg.response.body = response.read()
 
             conn.close()
             break
         except socket.error:
             time.sleep(0.5)
         i+=1
-    return data, cookies, response
+    return cpg, cookies
 
-def getXmlrpc(url, func, isSSL=0):
-    import xmlrpclib
-    http="http"
-    if isSSL: http+="s"
-    if url: url='/'+url
-    data=""
-    i=0
-    try:
-        while i<10:
-            try:
-                testsvr=xmlrpclib.Server(http+"://127.0.0.1:8000"+url)
-                data=eval("testsvr.%s"%func)
-                break
-            except socket.error:
-                time.sleep(0.5)
-            i+=1
-    except xmlrpclib.Fault, msg:
-        return msg
-    return data
-
-
-def shutdownServer(pid, mode, isSSL=0):
-    if isSSL: h="https"
-    else: h="http"
+def shutdownServer(pid, mode):
     if mode=='t':
-        u=urllib.urlopen(h+"://127.0.0.1:8000/shutdown/thread")
-        if hasattr(socket, 'sslerror'): sslError = socket.sslerror
-        else: sslError = 'dummy'
-        try: u=urllib.urlopen(h+"://127.0.0.1:8000/shutdown/dummy")
+        u=urllib.urlopen("http://127.0.0.1:8000/shutdown/thread")
+        try: u=urllib.urlopen("http://127.0.0.1:8000/shutdown/dummy")
         except IOError: pass
-        except sslError: pass
         except AttributeError: pass # Happens on Mac OS X when run with Python-2.3
     elif mode=='tp':
-        try: sslError = socket.sslerror
-        except: sslError = 'dummy'
-        u=urllib.urlopen(h+"://127.0.0.1:8000/shutdown/thread")
-        try: u=urllib.urlopen(h+"://127.0.0.1:8000/shutdown/dummy")
+        u=urllib.urlopen("http://127.0.0.1:8000/shutdown/thread")
+        try: u=urllib.urlopen("http://127.0.0.1:8000/shutdown/dummy")
         except IOError: pass # Happens on Windows
-        except sslError: pass # Happens on Windows for https requests
     else:
         try:
-            u=urllib.urlopen(h+"://127.0.0.1:8000/shutdown/regular")
+            u=urllib.urlopen("http://127.0.0.1:8000/shutdown/regular")
         except IOError: pass
         except AttributeError: pass # For Python2.3
 
-def checkResult(testName, infoMap, serverMode, result, expectedResult, failedList, exactResult):
-    if result == expectedResult or ((not exactResult) and expectedResult in result):
+def checkResult(testName, infoMap, serverMode, cpg, rule, failedList):
+    if eval(rule):
         return True
     else:
-        failedList.append(testName+" for python%s"%infoMap['exactVersionShort']+" in "+serverMode+" mode failed: expected result was:\n%s, actual result was:\n%s"%(repr(expectedResult), repr(result)))
+        failedList.append(testName +
+            " for python%s" % infoMap['exactVersionShort'] + 
+            " in " + serverMode + " mode failed." + """
+* Rule:
+%s
+* cpg.response.headerMap:
+%s
+* cpg.response.body:
+%s""" % (rule, repr(cpg.response.headerMap), repr(cpg.response.body)))
         return False
 
 def prepareCode(code):
     f.write(code.replace('cpg.server.start', beforeStart + 'cpg.server.start'))
     f.close()
 
-def checkPageResult(testName, infoMap, code, config, urlList, expectedResultList, failedList, exactResult=True, isSSL=0, extraRequestHeader=[], expectedHeaderList=[]):
+def checkPageResult(testName, infoMap, code, testList, failedList, extraConfig = '', extraRequestHeader = []):
     response = None
     prepareCode(code)
-    # Try it in all 3 modes (regular, threading, threadPooling) (we're missing forking and process pooling)
+    # Try it in all 2 modes (regular, threadPooling)
     modeList=[('r',''), ('tp', 'threadPool=3')]
-    # modeList=[('r','')] # TODO
     for mode,modeConfig in modeList:
         f=open("testsite.cfg", "w")
-        f.write(config)
+        f.write(extraConfig)
         f.write('''
 [session]
 storageType=ram
 [server]
 socketPort = 8000
 ''')
-        f.write(config+"\n"+modeConfig)
+        f.write(modeConfig)
         f.close()
 
         pid = startServer(infoMap)
         passed=True
         cookies=None
-        for i in range(len(urlList)):
-            url=urlList[i]
-            expectedResult=expectedResultList[i]
-            result, cookies, response=getPage(url, cookies, isSSL, extraRequestHeader)
-            if expectedHeaderList:
-                if response.status != expectedHeaderList[0]:
-                    failedList.append(testName+" for python%s"%infoMap['exactVersionShort']+" in "+mode+" mode failed: expected result status was %s, result status was %s"%(expectedHeaderList[0], response.status))
-                    passed=0
-                    print "*** FAILED ***"
-                    break
-            if not checkResult(testName, infoMap, mode, result, expectedResult, failedList, exactResult):
+        for url, rule in testList:
+            cpg, cookies = getPage(url, cookies, extraRequestHeader)
+            if not checkResult(testName, infoMap, mode, cpg, rule, failedList):
                 passed=0
                 print "*** FAILED ***"
                 break
-        shutdownServer(pid, mode, isSSL)
+        shutdownServer(pid, mode)
         if passed:
             print mode+"...",
             sys.stdout.flush()
 skippedList=[]
 
 tutorialTestList = [
-    ('01', ['/'], ['Hello world!']),
-    ('02', ['/showMessage'], ['Hello world!']),
-    ('03', ['/greetUser?name=Bob'], ["Hey Bob, what's up?"]),
-    ('04', ['/links/extra/'], ['\n            <p>Here are some extra useful links:</p>\n\n            <ul>\n                <li><a href="http://del.icio.us">del.icio.us</a></li>\n                <li><a href="http://www.mornography.de">Hendrik\'s weblog</a></li>\n            </ul>\n\n            <p>[<a href="../">Return to links page</a>]</p>\n        ']),
-    ('05', ['/another/'], ['\n            <html>\n            <head>\n                <title>Another Page</title>\n            <head>\n            <body>\n            <h2>Another Page</h2>\n        \n            <p>\n            And this is the amazing second page!\n            </p>\n        \n            </body>\n            </html>\n        ']),
-    ('06', ['/'], ['\n            <html>\n            <head>\n                <title>Tutorial 6 -- Aspect Powered!</title>\n            <head>\n            <body>\n            <h2>Tutorial 6 -- Aspect Powered!</h2>\n        \n            <p>\n            Isn\'t this exciting? There\'s\n            <a href="./another/">another page</a>, too!\n            </p>\n        \n            </body>\n            </html>\n        ']), 
-    ('07', ['/hendrik'], ['Hendrik Mans, CherryPy co-developer & crazy German (<a href="./">back</a>)']), 
-    ('08', ['/', '/'], ["\n            During your current session, you've viewed this\n            page 1 times! Your life is a patio of fun!\n        ", "\n            During your current session, you've viewed this\n            page 2 times! Your life is a patio of fun!\n        "]), 
-    ('09', ['/'], ['<html><body><h2>Generators rule!</h2><h3>List of users:</h3>Remi<br/>Carlos<br/>Hendrik<br/>Lorenzo Lamas<br/></body></html>']), 
+    ('01', [('/', "cpg.response.body == 'Hello world!'")]),
+    ('02', [('/showMessage', "cpg.response.body == 'Hello world!'")]),
+    ('03', [('/greetUser?name=Bob',
+            '''cpg.response.body == "Hey Bob, what's up?"''')]),
+    ('04', [('/links/extra/', r"""cpg.response.body == '\n            <p>Here are some extra useful links:</p>\n\n            <ul>\n                <li><a href="http://del.icio.us">del.icio.us</a></li>\n                <li><a href="http://www.mornography.de">Hendrik\'s weblog</a></li>\n            </ul>\n\n            <p>[<a href="../">Return to links page</a>]</p>\n        '""")]),
+    ('05', [('/another/', r"""cpg.response.body == '\n            <html>\n            <head>\n                <title>Another Page</title>\n            <head>\n            <body>\n            <h2>Another Page</h2>\n        \n            <p>\n            And this is the amazing second page!\n            </p>\n        \n            </body>\n            </html>\n        '""")]),
+    ('06', [('/', r"""cpg.response.body == '\n            <html>\n            <head>\n                <title>Tutorial 6 -- Aspect Powered!</title>\n            <head>\n            <body>\n            <h2>Tutorial 6 -- Aspect Powered!</h2>\n        \n            <p>\n            Isn\'t this exciting? There\'s\n            <a href="./another/">another page</a>, too!\n            </p>\n        \n            </body>\n            </html>\n        '""")]),
+    ('07', [('/hendrik', r"""cpg.response.body == 'Hendrik Mans, CherryPy co-developer & crazy German (<a href="./">back</a>)'""")]),
+    ('08', [('/', r'''cpg.response.body == "\n            During your current session, you've viewed this\n            page 1 times! Your life is a patio of fun!\n        "'''), ('/', r'''cpg.response.body == "\n            During your current session, you've viewed this\n            page 2 times! Your life is a patio of fun!\n        "''')]), 
+    ('09', [('/', r"""cpg.response.body == '<html><body><h2>Generators rule!</h2><h3>List of users:</h3>Remi<br/>Carlos<br/>Hendrik<br/>Lorenzo Lamas<br/></body></html>'""")]),
 ]
 
 testList = [
     # Limit the tests to these ones
     newTutorialTestList = []
     newTestList = []
-    for number, urlList, resultList in tutorialTestList:
+    for number, myTestList in tutorialTestList:
         if "tutorial%s" % number in sys.argv[1:]:
-            newTutorialTestList.append([number, urlList, resultList])
+            newTutorialTestList.append((number, myTestList))
     for t in testList:
         if t in sys.argv[1:]:
             newTestList.append(t)
     print "Running tests for python %s..."%infoMap['exactVersionShort']
 
     # Run tests based on tutorials
-    for number, urlList, resultList in tutorialTestList:
+    for number, myTestList in tutorialTestList:
         code = open('../tutorial/tutorial%s.py' % number, 'r').read()
         code = code.replace('tutorial.conf', 'testsite.cfg')
         print "    Testing tutorial %s..." % number,
         #    skippedList.append("Tutorial %s for python2.%s" % (number, version))
         #    continue
            
-        helper.checkPageResult('Tutorial %s' % number, infoMap, code, "", urlList, resultList, failedList)
+        helper.checkPageResult('Tutorial %s' % number, infoMap, code, myTestList, failedList)
 
     # Running actual unittests
     for test in testList:

test/testFilter1.py

 config = ""
 europoundUnicode = u'\x80\xa3'
 expectedResult = (u"Hello," + u"world" + europoundUnicode).encode('utf-8')
+zbuf = StringIO.StringIO()
+zfile = gzip.GzipFile(mode='wb', fileobj = zbuf, compresslevel = 9)
+zfile.write(expectedResult)
+zfile.close()
+
+testList = [
+    ('/', '%s in cpg.response.body' % repr(zbuf.getvalue()[:3])),
+]
 
 def test(infoMap, failedList, skippedList):
     print "    Testing Filters (1) ...",
-    zbuf = StringIO.StringIO()
-    zfile = gzip.GzipFile(mode='wb', fileobj = zbuf, compresslevel = 9)
-    zfile.write(expectedResult)
-    zfile.close()
     # Gzip compression doesn't always return the same exact result !
     # So we just check that the first few bytes are the same
-    helper.checkPageResult('Object mapping', infoMap, code, config, [""], [zbuf.getvalue()[:3]], failedList, exactResult = False, extraRequestHeader = [("Accept-Encoding", "gzip")])
+    helper.checkPageResult('Object mapping', infoMap, code, testList, failedList, extraRequestHeader = [("Accept-Encoding", "gzip")])

test/testObjectMapping.py

 cpg.root.dir1.dir2 = Dir2()
 cpg.server.start(configFile = 'testsite.cfg')
 """
-config = ""
+
 testList = [
-    ("", "world"),
-    ("/this/method/does/not/exist", "default:('this', 'method', 'does', 'not', 'exist')"),
-    ("/other", "other"),
-    ("/notExposed", "default:('notExposed',)"),
-    ("/dir1/dir2/", "index for dir2, path is:/dir1/dir2/"),
+    ("/", "cpg.response.body == 'world'"),
+    ("/this/method/does/not/exist", '''cpg.response.body == "default:('this', 'method', 'does', 'not', 'exist')"'''),
+    ("/other", "cpg.response.body == 'other'"),
+    ("/notExposed", '''cpg.response.body == "default:('notExposed',)"'''),
+    ("/dir1/dir2/", "cpg.response.body == 'index for dir2, path is:/dir1/dir2/'"),
+    ("/dir1/dir2", "cpg.response.headerMap['Status'] == 302" +
+        " and cpg.response.headerMap['Location'] == 'http://127.0.0.1/dir1/dir2/'"),
 ]
-urlList = [test[0] for test in testList]
-expectedResultList = [test[1] for test in testList]
 
 def test(infoMap, failedList, skippedList):
     print "    Testing object mapping...",
-    helper.checkPageResult('Object mapping', infoMap, code, config, urlList, expectedResultList, failedList)
+    helper.checkPageResult('Object mapping', infoMap, code, testList, failedList)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.