2 curdir
= os
.path
.join(os
.getcwd(), os
.path
.dirname(__file__
))
6 from cherrypy
.test
import helper
9 class RoutesDispatchTest(helper
.CPWebCase
):
16 raise nose
.SkipTest('Install routes to test RoutesDispatcher code')
20 return "I said good day!"
24 def __init__(self
, 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']
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")
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")
65 self
.getPage("/surbiton")
66 self
.assertStatus("200 OK")
67 self
.assertHeader("Content-Language", "en-GB")
68 self
.assertBody("Welcome to Surbiton, pop. 1327")