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 -class RoutesDispatchTest(helper.CPWebCase):
10
11 - def setup_server():
12 13 try: 14 import routes 15 except ImportError: 16 raise nose.SkipTest('Install routes to test RoutesDispatcher code') 17 18 class Dummy: 19 def index(self): 20 return "I said good day!"
21 22 class City: 23 24 def __init__(self, name): 25 self.name = name 26 self.population = 10000 27 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')]} 32 33 def update(self, **kwargs): 34 self.population = kwargs['pop'] 35 return "OK" 36 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()) 45 46 conf = {'/': {'request.dispatch': d}} 47 cherrypy.tree.mount(root=None, config=conf) 48 setup_server = staticmethod(setup_server) 49
50 - def test_Routes_Dispatch(self):
51 self.getPage("/hounslow") 52 self.assertStatus("200 OK") 53 self.assertBody("Welcome to Hounslow, pop. 10000") 54 55 self.getPage("/foo") 56 self.assertStatus("404 Not Found") 57 58 self.getPage("/surbiton") 59 self.assertStatus("200 OK") 60 self.assertBody("Welcome to Surbiton, pop. 10000") 61 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")
69