Bundled cherrypy.
[smonitor.git] / monitor / cherrypy / test / test_routes.py
bloba8062f8fd0a37803d3333b30dccd298b199932bc
1 import os
2 curdir = os.path.join(os.getcwd(), os.path.dirname(__file__))
4 import cherrypy
6 from cherrypy.test import helper
7 import nose
9 class RoutesDispatchTest(helper.CPWebCase):
11 def setup_server():
13 try:
14 import routes
15 except ImportError:
16 raise nose.SkipTest('Install routes to test RoutesDispatcher code')
18 class Dummy:
19 def index(self):
20 return "I said good day!"
22 class City:
24 def __init__(self, name):
25 self.name = name
26 self.population = 10000
28 def index(self, **kwargs):
29 return "Welcome to %s, pop. %s" % (self.name, self.population)
30 index._cp_config = {'tools.response_headers.on': True,
31 'tools.response_headers.headers': [('Content-Language', 'en-GB')]}
33 def update(self, **kwargs):
34 self.population = kwargs['pop']
35 return "OK"
37 d = cherrypy.dispatch.RoutesDispatcher()
38 d.connect(action='index', name='hounslow', route='/hounslow',
39 controller=City('Hounslow'))
40 d.connect(name='surbiton', route='/surbiton', controller=City('Surbiton'),
41 action='index', conditions=dict(method=['GET']))
42 d.mapper.connect('/surbiton', controller='surbiton',
43 action='update', conditions=dict(method=['POST']))
44 d.connect('main', ':action', controller=Dummy())
46 conf = {'/': {'request.dispatch': d}}
47 cherrypy.tree.mount(root=None, config=conf)
48 setup_server = staticmethod(setup_server)
50 def test_Routes_Dispatch(self):
51 self.getPage("/hounslow")
52 self.assertStatus("200 OK")
53 self.assertBody("Welcome to Hounslow, pop. 10000")
55 self.getPage("/foo")
56 self.assertStatus("404 Not Found")
58 self.getPage("/surbiton")
59 self.assertStatus("200 OK")
60 self.assertBody("Welcome to Surbiton, pop. 10000")
62 self.getPage("/surbiton", method="POST", body="pop=1327")
63 self.assertStatus("200 OK")
64 self.assertBody("OK")
65 self.getPage("/surbiton")
66 self.assertStatus("200 OK")
67 self.assertHeader("Content-Language", "en-GB")
68 self.assertBody("Welcome to Surbiton, pop. 1327")