Package cherrypy :: Package test :: Module test_http
[hide private]
[frames] | no frames]

Source Code for Module cherrypy.test.test_http

 1  """Tests for managing HTTP issues (malformed requests, etc). 
 2   
 3  Some of these tests check timeouts, etcetera, and therefore take a long 
 4  time to run. Therefore, this module should probably not be included in 
 5  the 'comprehensive' test suite (test.py). 
 6  """ 
 7   
 8  from cherrypy.test import test 
 9  test.prefer_parent_path() 
10   
11  import gc 
12  import httplib 
13  import threading 
14  import cherrypy 
15  from cherrypy import _cprequest 
16   
17   
18  data = object() 
19   
20 -def get_instances(cls):
21 return [x for x in gc.get_objects() if isinstance(x, cls)]
22
23 -def setup_server():
24 25 class Root: 26 def index(self, *args, **kwargs): 27 cherrypy.request.thing = data 28 return "Hello world!"
29 index.exposed = True 30 31 def gc_stats(self): 32 return "%s %s %s %s" % (gc.collect(), 33 len(get_instances(_cprequest.Request)), 34 len(get_instances(_cprequest.Response)), 35 len(gc.get_referrers(data))) 36 gc_stats.exposed = True 37 cherrypy.tree.mount(Root()) 38 cherrypy.config.update({'environment': 'test_suite'}) 39 40 41 from cherrypy.test import helper 42
43 -class HTTPTests(helper.CPWebCase):
44
45 - def test_sockets(self):
46 # By not including a Content-Length header, cgi.FieldStorage 47 # will hang. Verify that CP times out the socket and responds 48 # with 411 Length Required. 49 c = httplib.HTTPConnection("localhost:%s" % self.PORT) 50 c.request("POST", "/") 51 self.assertEqual(c.getresponse().status, 411)
52 53
54 -class ReferenceTests(helper.CPWebCase):
55
57 def getpage(): 58 self.getPage('/') 59 self.assertBody("Hello world!")
60 61 ts = [] 62 for _ in range(25): 63 t = threading.Thread(target=getpage) 64 ts.append(t) 65 t.start() 66 67 for t in ts: 68 t.join() 69 70 self.getPage("/gc_stats") 71 self.assertBody("0 1 1 1")
72 73 74 if __name__ == '__main__': 75 setup_server() 76 helper.testmain() 77