test_routes.py (2361B)
1 #Embedded file name: C:\BuildAgent\work\20d73f1b24eed06e\eve\release\V24.01\carbon\common\lib\cherrypy\test\test_routes.py 2 import os 3 curdir = os.path.join(os.getcwd(), os.path.dirname(__file__)) 4 import cherrypy 5 from cherrypy.test import helper 6 import nose 7 8 class RoutesDispatchTest(helper.CPWebCase): 9 10 def setup_server(): 11 try: 12 import routes 13 except ImportError: 14 raise nose.SkipTest('Install routes to test RoutesDispatcher code') 15 16 class Dummy: 17 18 def index(self): 19 return 'I said good day!' 20 21 class City: 22 23 def __init__(self, name): 24 self.name = name 25 self.population = 10000 26 27 def index(self, **kwargs): 28 return 'Welcome to %s, pop. %s' % (self.name, self.population) 29 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', controller=City('Hounslow')) 39 d.connect(name='surbiton', route='/surbiton', controller=City('Surbiton'), action='index', conditions=dict(method=['GET'])) 40 d.mapper.connect('/surbiton', controller='surbiton', action='update', conditions=dict(method=['POST'])) 41 d.connect('main', ':action', controller=Dummy()) 42 conf = {'/': {'request.dispatch': d}} 43 cherrypy.tree.mount(root=None, config=conf) 44 45 setup_server = staticmethod(setup_server) 46 47 def test_Routes_Dispatch(self): 48 self.getPage('/hounslow') 49 self.assertStatus('200 OK') 50 self.assertBody('Welcome to Hounslow, pop. 10000') 51 self.getPage('/foo') 52 self.assertStatus('404 Not Found') 53 self.getPage('/surbiton') 54 self.assertStatus('200 OK') 55 self.assertBody('Welcome to Surbiton, pop. 10000') 56 self.getPage('/surbiton', method='POST', body='pop=1327') 57 self.assertStatus('200 OK') 58 self.assertBody('OK') 59 self.getPage('/surbiton') 60 self.assertStatus('200 OK') 61 self.assertHeader('Content-Language', 'en-GB') 62 self.assertBody('Welcome to Surbiton, pop. 1327')