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

Source Code for Module cherrypy.test.test_routes

 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   
10 -class RoutesDispatchTest(helper.CPWebCase):
11
12 - def setup_server():
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
57 - def test_Routes_Dispatch(self):
58 self.getPage("/hounslow") 59 self.assertStatus("200 OK") 60 self.assertBody("Welcome to Hounslow, pop. 10000") 61 62 self.getPage("/foo") 63 self.assertStatus("404 Not Found") 64 65 self.getPage("/surbiton") 66 self.assertStatus("200 OK") 67 self.assertBody("Welcome to Surbiton, pop. 10000") 68 69 self.getPage("/surbiton", method="POST", body="pop=1327") 70 self.assertStatus("200 OK") 71 self.assertBody("OK") 72 self.getPage("/surbiton") 73 self.assertStatus("200 OK") 74 self.assertHeader("Content-Language", "en-GB") 75 self.assertBody("Welcome to Surbiton, pop. 1327")
76