| 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 |
import test |
|---|
| 9 |
test.prefer_parent_path() |
|---|
| 10 |
|
|---|
| 11 |
import gc |
|---|
| 12 |
import httplib |
|---|
| 13 |
import threading |
|---|
| 14 |
import cherrypy |
|---|
| 15 |
from cherrypy import _cphttptools |
|---|
| 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(_cphttptools.Request)), |
|---|
| 34 |
len(get_instances(_cphttptools.Response)), |
|---|
| 35 |
len(gc.get_referrers(data))) |
|---|
| 36 |
gc_stats.exposed = True |
|---|
| 37 |
cherrypy.tree.mount(Root()) |
|---|
| 38 |
|
|---|
| 39 |
cherrypy.config.update({ |
|---|
| 40 |
'global': {'server.log_to_screen': False, |
|---|
| 41 |
'server.environment': 'production', |
|---|
| 42 |
'server.show_tracebacks': True, |
|---|
| 43 |
}, |
|---|
| 44 |
}) |
|---|
| 45 |
|
|---|
| 46 |
|
|---|
| 47 |
import helper |
|---|
| 48 |
|
|---|
| 49 |
class HTTPTests(helper.CPWebCase): |
|---|
| 50 |
|
|---|
| 51 |
def test_sockets(self): |
|---|
| 52 |
|
|---|
| 53 |
|
|---|
| 54 |
|
|---|
| 55 |
c = httplib.HTTPConnection("localhost:%s" % self.PORT) |
|---|
| 56 |
c.request("POST", "/") |
|---|
| 57 |
self.assertEqual(c.getresponse().status, 411) |
|---|
| 58 |
|
|---|
| 59 |
|
|---|
| 60 |
class ReferenceTests(helper.CPWebCase): |
|---|
| 61 |
|
|---|
| 62 |
def test_threadlocal_garbage(self): |
|---|
| 63 |
def getpage(): |
|---|
| 64 |
self.getPage('/') |
|---|
| 65 |
self.assertBody("Hello world!") |
|---|
| 66 |
|
|---|
| 67 |
ts = [] |
|---|
| 68 |
for _ in range(25): |
|---|
| 69 |
t = threading.Thread(target=getpage) |
|---|
| 70 |
ts.append(t) |
|---|
| 71 |
t.start() |
|---|
| 72 |
|
|---|
| 73 |
for t in ts: |
|---|
| 74 |
t.join() |
|---|
| 75 |
|
|---|
| 76 |
self.getPage("/gc_stats") |
|---|
| 77 |
self.assertBody("0 1 1 1") |
|---|
| 78 |
|
|---|
| 79 |
|
|---|
| 80 |
if __name__ == '__main__': |
|---|
| 81 |
setup_server() |
|---|
| 82 |
helper.testmain() |
|---|