Package cherrypy :: Package test :: Module test_routes
[hide private]
[frames] | no frames]

Source Code for Module cherrypy.test.test_routes

 1  from cherrypy.test import test 
 2  test.prefer_parent_path() 
 3   
 4  import os 
 5  curdir = os.path.join(os.getcwd(), os.path.dirname(__file__)) 
 6   
 7  import cherrypy 
 8   
 9   
10 -def setup_server():
11 12 class City: 13 14 def __init__(self, name): 15 self.name = name 16 self.population = 10000
17 18 def index(self, **kwargs): 19 return "Welcome to %s, pop. %s" % (self.name, self.population) 20 index._cp_config = {'tools.response_headers.on': True, 21 'tools.response_headers.headers': [('Content-Language', 'en-GB')]} 22 23 def update(self, **kwargs): 24 self.population = kwargs['pop'] 25 return "OK" 26 27 d = cherrypy.dispatch.RoutesDispatcher() 28 d.connect(name='hounslow', route='hounslow', controller=City('Hounslow')) 29 d.connect(name='surbiton', route='surbiton', controller=City('Surbiton'), 30 action='index', conditions=dict(method=['GET'])) 31 d.mapper.connect('surbiton', controller='surbiton', 32 action='update', conditions=dict(method=['POST'])) 33 34 conf = {'/': {'request.dispatch': d}} 35 cherrypy.tree.mount(root=None, config=conf) 36 cherrypy.config.update({'environment': 'test_suite'}) 37 38 39 from cherrypy.test import helper 40
41 -class RoutesDispatchTest(helper.CPWebCase):
42
43 - def test_Routes_Dispatch(self):
44 self.getPage("/hounslow") 45 self.assertStatus("200 OK") 46 self.assertBody("Welcome to Hounslow, pop. 10000") 47 48 self.getPage("/surbiton") 49 self.assertStatus("200 OK") 50 self.assertBody("Welcome to Surbiton, pop. 10000") 51 52 self.getPage("/surbiton", method="POST", body="pop=1327") 53 self.assertStatus("200 OK") 54 self.assertBody("OK") 55 self.getPage("/surbiton") 56 self.assertStatus("200 OK") 57 self.assertHeader("Content-Language", "en-GB") 58 self.assertBody("Welcome to Surbiton, pop. 1327")
59 60 if __name__ == '__main__': 61 setup_server() 62 helper.testmain() 63