Changeset 16
- Timestamp:
- 11/23/04 16:58:03
- Files:
-
- trunk/cherrypy/test/helper.py (modified) (7 diffs)
- trunk/cherrypy/test/test.py (modified) (4 diffs)
- trunk/cherrypy/test/testFilter1.py (modified) (1 diff)
- trunk/cherrypy/test/testObjectMapping.py (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/cherrypy/test/helper.py
r8 r16 23 23 return pid 24 24 25 def getPage(url, cookies, isSSL=0,extraRequestHeader = []):25 def getPage(url, cookies, extraRequestHeader = []): 26 26 data="" 27 27 i=0 … … 29 29 while i<10: 30 30 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') 36 32 conn.putrequest("GET", url) 37 38 33 conn.putheader("Host", "127.0.0.1") 39 34 if cookies: … … 55 50 cookies=response.msg.getallmatchingheaders("Set-Cookie") 56 51 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() 58 63 59 64 conn.close() … … 62 67 time.sleep(0.5) 63 68 i+=1 64 return data, cookies, response69 return cpg, cookies 65 70 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" 71 def shutdownServer(pid, mode): 90 72 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") 95 75 except IOError: pass 96 except sslError: pass97 76 except AttributeError: pass # Happens on Mac OS X when run with Python-2.3 98 77 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") 103 80 except IOError: pass # Happens on Windows 104 except sslError: pass # Happens on Windows for https requests105 81 else: 106 82 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") 108 84 except IOError: pass 109 85 except AttributeError: pass # For Python2.3 110 86 111 def checkResult(testName, infoMap, serverMode, result, expectedResult, failedList, exactResult):112 if result == expectedResult or ((not exactResult) and expectedResult in result):87 def checkResult(testName, infoMap, serverMode, cpg, rule, failedList): 88 if eval(rule): 113 89 return True 114 90 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))) 116 100 return False 117 101 … … 139 123 f.close() 140 124 141 def checkPageResult(testName, infoMap, code, config, urlList, expectedResultList, failedList, exactResult=True, isSSL=0, extraRequestHeader=[], expectedHeaderList=[]):125 def checkPageResult(testName, infoMap, code, testList, failedList, extraConfig = '', extraRequestHeader = []): 142 126 response = None 143 127 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) 145 129 modeList=[('r',''), ('tp', 'threadPool=3')] 146 # modeList=[('r','')] # TODO147 130 for mode,modeConfig in modeList: 148 131 f=open("testsite.cfg", "w") 149 f.write( config)132 f.write(extraConfig) 150 133 f.write(''' 151 134 [session] … … 154 137 socketPort = 8000 155 138 ''') 156 f.write( config+"\n"+modeConfig)139 f.write(modeConfig) 157 140 f.close() 158 141 … … 160 143 passed=True 161 144 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): 173 148 passed=0 174 149 print "*** FAILED ***" 175 150 break 176 shutdownServer(pid, mode , isSSL)151 shutdownServer(pid, mode) 177 152 if passed: 178 153 print mode+"...", trunk/cherrypy/test/test.py
r15 r16 68 68 69 69 tutorialTestList = [ 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>'""")]), 79 80 ] 80 81 … … 89 90 newTutorialTestList = [] 90 91 newTestList = [] 91 for number, urlList, resultList in tutorialTestList:92 for number, myTestList in tutorialTestList: 92 93 if "tutorial%s" % number in sys.argv[1:]: 93 newTutorialTestList.append( [number, urlList, resultList])94 newTutorialTestList.append((number, myTestList)) 94 95 for t in testList: 95 96 if t in sys.argv[1:]: … … 105 106 106 107 # Run tests based on tutorials 107 for number, urlList, resultList in tutorialTestList:108 for number, myTestList in tutorialTestList: 108 109 code = open('../tutorial/tutorial%s.py' % number, 'r').read() 109 110 code = code.replace('tutorial.conf', 'testsite.cfg') … … 115 116 # continue 116 117 117 helper.checkPageResult('Tutorial %s' % number, infoMap, code, "", urlList, resultList, failedList)118 helper.checkPageResult('Tutorial %s' % number, infoMap, code, myTestList, failedList) 118 119 119 120 # Running actual unittests trunk/cherrypy/test/testFilter1.py
r8 r16 36 36 europoundUnicode = u'\x80\xa3' 37 37 expectedResult = (u"Hello," + u"world" + europoundUnicode).encode('utf-8') 38 zbuf = StringIO.StringIO() 39 zfile = gzip.GzipFile(mode='wb', fileobj = zbuf, compresslevel = 9) 40 zfile.write(expectedResult) 41 zfile.close() 42 43 testList = [ 44 ('/', '%s in cpg.response.body' % repr(zbuf.getvalue()[:3])), 45 ] 38 46 39 47 def test(infoMap, failedList, skippedList): 40 48 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()45 49 # Gzip compression doesn't always return the same exact result ! 46 50 # 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 50 50 cpg.server.start(configFile = 'testsite.cfg') 51 51 """ 52 config = "" 52 53 53 testList = [ 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/'"), 59 61 ] 60 urlList = [test[0] for test in testList]61 expectedResultList = [test[1] for test in testList]62 62 63 63 def test(infoMap, failedList, skippedList): 64 64 print " Testing object mapping...", 65 helper.checkPageResult('Object mapping', infoMap, code, config, urlList, expectedResultList, failedList)65 helper.checkPageResult('Object mapping', infoMap, code, testList, failedList)

