"""Tests for managing HTTP issues (malformed requests, etc).

Some of these tests check timeouts, etcetera, and therefore take a long
time to run. Therefore, this module should probably not be included in
the 'comprehensive' test suite (test.py). Note, however, that we default
to the builtin HTTP server (most tests default to 'serverless').
"""

import test
test.prefer_parent_path()

import httplib
import cherrypy


class Root:
    def index(self, *args, **kwargs):
        return "Hello world!"
    index.exposed = True
cherrypy.tree.mount(Root())

cherrypy.config.update({
    'global': {'server.log_to_screen': False,
               'server.environment': 'production',
               'server.show_tracebacks': True,
               },
})

import helper

class HTTPTests(helper.CPWebCase):
    
    def test_sockets(self):
        if cherrypy.server.httpserver:
            # By not including a Content-Length header, cgi.FieldStorage
            # will hang. Verify that CP times out the socket and responds
            # with 411 Length Required.
            c = httplib.HTTPConnection("localhost:%s" % self.PORT)
            c.request("POST", "/")
            self.assertEqual(c.getresponse().status, 411)


if __name__ == '__main__':
    helper.testmain(server="cherrypy._cpwsgi.WSGIServer")

@@ -380,16 +380,26 @@
 path()%0A%0A
+import gc%0A
 import h
@@ -416,17 +416,85 @@
 ort 
-cherrypy%0A
+threading%0Aimport cherrypy%0Afrom cherrypy import _cphttptools%0A%0A%0Adata = object()
 %0A%0Acl
@@ -541,16 +541,58 @@
 wargs):%0A
+        cherrypy.thread_data.thing = data%0A
         
@@ -1329,16 +1329,739 @@
  411)%0A%0A%0A
+def get_instances(cls):%0A    return %5Bx for x in gc.get_objects() if isinstance(x, cls)%5D%0A%0A%0Aclass ReferenceTests(helper.CPWebCase):%0A    %0A    def test_threadlocal_garbage(self):%0A        def getpage():%0A            self.getPage('/')%0A            self.assertBody(%22Hello world!%22)%0A        %0A        ts = %5B%5D%0A        for _ in range(100):%0A            t = threading.Thread(target=getpage)%0A            ts.append(t)%0A            t.start()%0A        %0A        for t in ts:%0A            t.join()%0A        %0A        self.assertEqual(gc.collect(), 0)%0A        self.assertEqual(len(get_instances(_cphttptools.Request)), 0)%0A        self.assertEqual(len(get_instances(_cphttptools.Response)), 0)%0A        self.assertEqual(len(gc.get_referrers(data)), 1)%0A%0A%0A
 if __nam

Test for #496 (memory leak in thread locals).