1 import os
2 curdir = os.path.join(os.getcwd(), os.path.dirname(__file__))
3
4 import cherrypy
5
6 from cherrypy.test import helper
7 import nose
8
9
11
13
14 try:
15 import routes
16 except ImportError:
17 raise nose.SkipTest('Install routes to test RoutesDispatcher code')
18
19 class Dummy:
20
21 def index(self):
22 return "I said good day!"
23
24 class City:
25
26 def __init__(self, name):
27 self.name = name
28 self.population = 10000
29
30 def index(self, **kwargs):
31 return "Welcome to %s, pop. %s" % (self.name, self.population)
32 index._cp_config = {
33 'tools.response_headers.on': True,
34 'tools.response_headers.headers': [
35 ('Content-Language', 'en-GB')
36 ]
37 }
38
39 def update(self, **kwargs):
40 self.population = kwargs['pop']
41 return "OK"
42
43 d = cherrypy.dispatch.RoutesDispatcher()
44 d.connect(action='index', name='hounslow', route='/hounslow',
45 controller=City('Hounslow'))
46 d.connect(
47 name='surbiton', route='/surbiton', controller=City('Surbiton'),
48 action='index', conditions=dict(method=['GET']))
49 d.mapper.connect('/surbiton', controller='surbiton',
50 action='update', conditions=dict(method=['POST']))
51 d.connect('main', ':action', controller=Dummy())
52
53 conf = {'/': {'request.dispatch': d}}
54 cherrypy.tree.mount(root=None, config=conf)
55 setup_server = staticmethod(setup_server)
56
76