Download Install Tutorial Docs FAQ Tools WikiLicense Team IRC Planet Involvement Shop Book

Changeset 16

Show
Ignore:
Timestamp:
11/23/04 16:58:03
Author:
rdelon
Message:

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

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/cherrypy/test/helper.py

    r8 r16  
    2323    return pid 
    2424 
    25 def getPage(url, cookies, isSSL=0, extraRequestHeader = []): 
     25def getPage(url, cookies, extraRequestHeader = []): 
    2626    data="" 
    2727    i=0 
     
    2929    while i<10: 
    3030        try: 
    31             if isSSL: 
    32                 conn=httplib.HTTPSConnection('127.0.0.1:8000') 
    33             else: 
    34                 conn=httplib.HTTPConnection('127.0.0.1:8000') 
    35  
     31            conn=httplib.HTTPConnection('127.0.0.1:8000') 
    3632            conn.putrequest("GET", url) 
    37  
    3833            conn.putheader("Host", "127.0.0.1") 
    3934            if cookies: 
     
    5550            cookies=response.msg.getallmatchingheaders("Set-Cookie") 
    5651 
    57             data=response.read() 
     52            class EmptyClass: pass 
     53            cpg = EmptyClass() 
     54            cpg.response = EmptyClass() 
     55            cpg.response.headerMap = {'Status': response.status} 
     56            for line in response.msg.headers: 
     57                line = line.strip() 
     58                i = line.find(':') 
     59                key, value = line[:i], line[i+1:].strip() 
     60                cpg.response.headerMap[key] = value 
     61 
     62            cpg.response.body = response.read() 
    5863 
    5964            conn.close() 
     
    6267            time.sleep(0.5) 
    6368        i+=1 
    64     return data, cookies, response 
     69    return cpg, cookies 
    6570 
    66 def getXmlrpc(url, func, isSSL=0): 
    67     import xmlrpclib 
    68     http="http" 
    69     if isSSL: http+="s" 
    70     if url: url='/'+url 
    71     data="" 
    72     i=0 
    73     try: 
    74         while i<10: 
    75             try: 
    76                 testsvr=xmlrpclib.Server(http+"://127.0.0.1:8000"+url) 
    77                 data=eval("testsvr.%s"%func) 
    78                 break 
    79             except socket.error: 
    80                 time.sleep(0.5) 
    81             i+=1 
    82     except xmlrpclib.Fault, msg: 
    83         return msg 
    84     return data 
    85  
    86  
    87 def shutdownServer(pid, mode, isSSL=0): 
    88     if isSSL: h="https" 
    89     else: h="http" 
     71def shutdownServer(pid, mode): 
    9072    if mode=='t': 
    91         u=urllib.urlopen(h+"://127.0.0.1:8000/shutdown/thread") 
    92         if hasattr(socket, 'sslerror'): sslError = socket.sslerror 
    93         else: sslError = 'dummy' 
    94         try: u=urllib.urlopen(h+"://127.0.0.1:8000/shutdown/dummy") 
     73        u=urllib.urlopen("http://127.0.0.1:8000/shutdown/thread") 
     74        try: u=urllib.urlopen("http://127.0.0.1:8000/shutdown/dummy") 
    9575        except IOError: pass 
    96         except sslError: pass 
    9776        except AttributeError: pass # Happens on Mac OS X when run with Python-2.3 
    9877    elif mode=='tp': 
    99         try: sslError = socket.sslerror 
    100         except: sslError = 'dummy' 
    101         u=urllib.urlopen(h+"://127.0.0.1:8000/shutdown/thread") 
    102         try: u=urllib.urlopen(h+"://127.0.0.1:8000/shutdown/dummy") 
     78        u=urllib.urlopen("http://127.0.0.1:8000/shutdown/thread") 
     79        try: u=urllib.urlopen("http://127.0.0.1:8000/shutdown/dummy") 
    10380        except IOError: pass # Happens on Windows 
    104         except sslError: pass # Happens on Windows for https requests 
    10581    else: 
    10682        try: 
    107             u=urllib.urlopen(h+"://127.0.0.1:8000/shutdown/regular") 
     83            u=urllib.urlopen("http://127.0.0.1:8000/shutdown/regular") 
    10884        except IOError: pass 
    10985        except AttributeError: pass # For Python2.3 
    11086 
    111 def checkResult(testName, infoMap, serverMode, result, expectedResult, failedList, exactResult): 
    112     if result == expectedResult or ((not exactResult) and expectedResult in result): 
     87def checkResult(testName, infoMap, serverMode, cpg, rule, failedList): 
     88    if eval(rule): 
    11389        return True 
    11490    else: 
    115         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))) 
     91        failedList.append(testName + 
     92            " for python%s" % infoMap['exactVersionShort'] +  
     93            " in " + serverMode + " mode failed." + """ 
     94* Rule: 
     95%s 
     96* cpg.response.headerMap: 
     97%s 
     98* cpg.response.body: 
     99%s""" % (rule, repr(cpg.response.headerMap), repr(cpg.response.body))) 
    116100        return False 
    117101 
     
    139123    f.close() 
    140124 
    141 def checkPageResult(testName, infoMap, code, config, urlList, expectedResultList, failedList, exactResult=True, isSSL=0, extraRequestHeader=[], expectedHeaderList=[]): 
     125def checkPageResult(testName, infoMap, code, testList, failedList, extraConfig = '', extraRequestHeader = []): 
    142126    response = None 
    143127    prepareCode(code) 
    144     # Try it in all 3 modes (regular, threading, threadPooling) (we're missing forking and process pooling) 
     128    # Try it in all 2 modes (regular, threadPooling) 
    145129    modeList=[('r',''), ('tp', 'threadPool=3')] 
    146     # modeList=[('r','')] # TODO 
    147130    for mode,modeConfig in modeList: 
    148131        f=open("testsite.cfg", "w") 
    149         f.write(config) 
     132        f.write(extraConfig) 
    150133        f.write(''' 
    151134[session] 
     
    154137socketPort = 8000 
    155138''') 
    156         f.write(config+"\n"+modeConfig) 
     139        f.write(modeConfig) 
    157140        f.close() 
    158141 
     
    160143        passed=True 
    161144        cookies=None 
    162         for i in range(len(urlList)): 
    163             url=urlList[i] 
    164             expectedResult=expectedResultList[i] 
    165             result, cookies, response=getPage(url, cookies, isSSL, extraRequestHeader) 
    166             if expectedHeaderList: 
    167                 if response.status != expectedHeaderList[0]: 
    168                     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)) 
    169                     passed=0 
    170                     print "*** FAILED ***" 
    171                     break 
    172             if not checkResult(testName, infoMap, mode, result, expectedResult, failedList, exactResult): 
     145        for url, rule in testList: 
     146            cpg, cookies = getPage(url, cookies, extraRequestHeader) 
     147            if not checkResult(testName, infoMap, mode, cpg, rule, failedList): 
    173148                passed=0 
    174149                print "*** FAILED ***" 
    175150                break 
    176         shutdownServer(pid, mode, isSSL
     151        shutdownServer(pid, mode
    177152        if passed: 
    178153            print mode+"...", 
  • trunk/cherrypy/test/test.py

    r15 r16  
    6868 
    6969tutorialTestList = [ 
    70     ('01', ['/'], ['Hello world!']), 
    71     ('02', ['/showMessage'], ['Hello world!']), 
    72     ('03', ['/greetUser?name=Bob'], ["Hey Bob, what's up?"]), 
    73     ('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        ']), 
    74     ('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        ']), 
    75     ('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        ']),  
    76     ('07', ['/hendrik'], ['Hendrik Mans, CherryPy co-developer & crazy German (<a href="./">back</a>)']),  
    77     ('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        "]),  
    78     ('09', ['/'], ['<html><body><h2>Generators rule!</h2><h3>List of users:</h3>Remi<br/>Carlos<br/>Hendrik<br/>Lorenzo Lamas<br/></body></html>']),  
     70    ('01', [('/', "cpg.response.body == 'Hello world!'")]), 
     71    ('02', [('/showMessage', "cpg.response.body == 'Hello world!'")]), 
     72    ('03', [('/greetUser?name=Bob', 
     73            '''cpg.response.body == "Hey Bob, what's up?"''')]), 
     74    ('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        '""")]), 
     75    ('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        '""")]), 
     76    ('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        '""")]), 
     77    ('07', [('/hendrik', r"""cpg.response.body == 'Hendrik Mans, CherryPy co-developer & crazy German (<a href="./">back</a>)'""")]), 
     78    ('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        "''')]),  
     79    ('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>'""")]), 
    7980] 
    8081 
     
    8990    newTutorialTestList = [] 
    9091    newTestList = [] 
    91     for number, urlList, resultList in tutorialTestList: 
     92    for number, myTestList in tutorialTestList: 
    9293        if "tutorial%s" % number in sys.argv[1:]: 
    93             newTutorialTestList.append([number, urlList, resultList]
     94            newTutorialTestList.append((number, myTestList)
    9495    for t in testList: 
    9596        if t in sys.argv[1:]: 
     
    105106 
    106107    # Run tests based on tutorials 
    107     for number, urlList, resultList in tutorialTestList: 
     108    for number, myTestList in tutorialTestList: 
    108109        code = open('../tutorial/tutorial%s.py' % number, 'r').read() 
    109110        code = code.replace('tutorial.conf', 'testsite.cfg') 
     
    115116        #    continue 
    116117            
    117         helper.checkPageResult('Tutorial %s' % number, infoMap, code, "", urlList, resultList, failedList) 
     118        helper.checkPageResult('Tutorial %s' % number, infoMap, code, myTestList, failedList) 
    118119 
    119120    # Running actual unittests 
  • trunk/cherrypy/test/testFilter1.py

    r8 r16  
    3636europoundUnicode = u'\x80\xa3' 
    3737expectedResult = (u"Hello," + u"world" + europoundUnicode).encode('utf-8') 
     38zbuf = StringIO.StringIO() 
     39zfile = gzip.GzipFile(mode='wb', fileobj = zbuf, compresslevel = 9) 
     40zfile.write(expectedResult) 
     41zfile.close() 
     42 
     43testList = [ 
     44    ('/', '%s in cpg.response.body' % repr(zbuf.getvalue()[:3])), 
     45] 
    3846 
    3947def test(infoMap, failedList, skippedList): 
    4048    print "    Testing Filters (1) ...", 
    41     zbuf = StringIO.StringIO() 
    42     zfile = gzip.GzipFile(mode='wb', fileobj = zbuf, compresslevel = 9) 
    43     zfile.write(expectedResult) 
    44     zfile.close() 
    4549    # Gzip compression doesn't always return the same exact result ! 
    4650    # So we just check that the first few bytes are the same 
    47     helper.checkPageResult('Object mapping', infoMap, code, config, [""], [zbuf.getvalue()[:3]], failedList, exactResult = False, extraRequestHeader = [("Accept-Encoding", "gzip")]) 
     51    helper.checkPageResult('Object mapping', infoMap, code, testList, failedList, extraRequestHeader = [("Accept-Encoding", "gzip")]) 
  • trunk/cherrypy/test/testObjectMapping.py

    r11 r16  
    5050cpg.server.start(configFile = 'testsite.cfg') 
    5151""" 
    52 config = "" 
     52 
    5353testList = [ 
    54     ("", "world"), 
    55     ("/this/method/does/not/exist", "default:('this', 'method', 'does', 'not', 'exist')"), 
    56     ("/other", "other"), 
    57     ("/notExposed", "default:('notExposed',)"), 
    58     ("/dir1/dir2/", "index for dir2, path is:/dir1/dir2/"), 
     54    ("/", "cpg.response.body == 'world'"), 
     55    ("/this/method/does/not/exist", '''cpg.response.body == "default:('this', 'method', 'does', 'not', 'exist')"'''), 
     56    ("/other", "cpg.response.body == 'other'"), 
     57    ("/notExposed", '''cpg.response.body == "default:('notExposed',)"'''), 
     58    ("/dir1/dir2/", "cpg.response.body == 'index for dir2, path is:/dir1/dir2/'"), 
     59    ("/dir1/dir2", "cpg.response.headerMap['Status'] == 302" + 
     60        " and cpg.response.headerMap['Location'] == 'http://127.0.0.1/dir1/dir2/'"), 
    5961] 
    60 urlList = [test[0] for test in testList] 
    61 expectedResultList = [test[1] for test in testList] 
    6262 
    6363def test(infoMap, failedList, skippedList): 
    6464    print "    Testing object mapping...", 
    65     helper.checkPageResult('Object mapping', infoMap, code, config, urlList, expectedResultList, failedList) 
     65    helper.checkPageResult('Object mapping', infoMap, code, testList, failedList) 

Hosted by WebFaction

Log in as guest/cpguest to create tickets