Changeset 1551
- Timestamp:
- 12/21/06 17:43:44
- Files:
-
- trunk/cherrypy/test/test_tools.py (modified) (9 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/cherrypy/test/test_tools.py
r1542 r1551 3 3 import gzip, StringIO 4 4 import time 5 timeout = 0.2 6 5 7 import types 6 8 from cherrypy.test import test … … 51 53 52 54 def __init__(self): 53 self.counter = 054 55 self.ended = {} 55 56 self._name = "nadsat" 56 57 57 58 def nadsat(self): 58 59 def nadsat_it_up(body): … … 67 68 # This runs after the request has been completely written out. 68 69 cherrypy.response.body = "razdrez" 69 self.ended[cherrypy.request.counter] = True 70 id = cherrypy.request.params.get("id") 71 if id: 72 self.ended[id] = True 70 73 cleanup.failsafe = True 71 74 72 75 def _setup(self): 73 cherrypy.request.counter = self.counter = self.counter + 174 self.ended[cherrypy.request.counter] = False75 76 cherrypy.request.hooks.attach('before_finalize', self.nadsat) 76 77 cherrypy.request.hooks.attach('on_end_request', self.cleanup) … … 136 137 _cp_config = {"tools.nadsat.on": True} 137 138 138 def index(self ):139 def index(self, id=None): 139 140 return "A good piece of cherry pie" 140 141 141 142 def ended(self, id): 142 return repr(tools.nadsat.ended[i nt(id)])143 144 def err(self ):143 return repr(tools.nadsat.ended[id]) 144 145 def err(self, id=None): 145 146 raise ValueError() 146 147 147 def errinstream(self ):148 def errinstream(self, id=None): 148 149 raise ValueError() 149 150 yield "confidential" … … 160 161 return "success!" 161 162 162 def stream(self ):163 def stream(self, id=None): 163 164 for x in xrange(100000000): 164 165 yield str(x) … … 209 210 class ToolTests(helper.CPWebCase): 210 211 211 def test Demo(self):212 self.getPage("/demo/ ")212 def testHookErrors(self): 213 self.getPage("/demo/?id=1") 213 214 # If body is "razdrez", then on_end_request is being called too early. 214 215 self.assertBody("A horrorshow lomtick of cherry 3.14159") … … 219 220 220 221 valerr = '\n raise ValueError()\nValueError' 221 self.getPage("/demo/err ")222 self.getPage("/demo/err?id=3") 222 223 # If body is "razdrez", then on_end_request is being called too early. 223 224 self.assertErrorPage(502, pattern=valerr) … … 228 229 229 230 # If body is "razdrez", then on_end_request is being called too early. 230 self.getPage("/demo/errinstream ")231 self.getPage("/demo/errinstream?id=5") 231 232 # Because this error is raised after the response body has 232 233 # started, the status should not change to an error status. … … 245 246 self.getPage("/demo/userid") 246 247 self.assertBody("Welcome!") 247 248 # Test that on_end_request is called even if the client drops.249 self.persistent = True248 249 def testEndRequestOnDrop(self): 250 old_timeout = None 250 251 try: 251 conn = self.HTTP_CONN 252 conn.putrequest("GET", "/demo/stream", skip_host=True) 253 conn.putheader("Host", self.HOST) 254 conn.endheaders() 255 # Skip the rest of the request and close the conn. This will 256 # cause the server's active socket to error, which *should* 257 # result in the request being aborted, and request.close being 258 # called all the way up the stack (including WSGI middleware), 259 # eventually calling our on_end_request hook. 252 httpserver = cherrypy.server.httpservers.keys()[0] 253 old_timeout = httpserver.timeout 254 except (AttributeError, IndexError): 255 print "skipped ", 256 return 257 258 try: 259 httpserver.timeout = timeout 260 261 # Test that on_end_request is called even if the client drops. 262 self.persistent = True 263 try: 264 conn = self.HTTP_CONN 265 conn.putrequest("GET", "/demo/stream?id=9", skip_host=True) 266 conn.putheader("Host", self.HOST) 267 conn.endheaders() 268 # Skip the rest of the request and close the conn. This will 269 # cause the server's active socket to error, which *should* 270 # result in the request being aborted, and request.close being 271 # called all the way up the stack (including WSGI middleware), 272 # eventually calling our on_end_request hook. 273 finally: 274 self.persistent = False 275 time.sleep(timeout * 2) 276 # Test that the on_end_request hook was called. 277 self.getPage("/demo/ended/9") 278 self.assertBody("True") 260 279 finally: 261 self.persistent = False 262 time.sleep(0.1) 263 # Test that the on_end_request hooks was called. 264 self.getPage("/demo/ended/9") 265 self.assertBody("True") 280 if old_timeout is not None: 281 httpserver.timeout = old_timeout 266 282 267 283 def testGuaranteedHooks(self):

