| 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 |
|
|---|
| 21 |
def update(self, **kwargs): |
|---|
| 22 |
self.population = kwargs['pop'] |
|---|
| 23 |
return "OK" |
|---|
| 24 |
|
|---|
| 25 |
d = cherrypy.dispatch.RoutesDispatcher() |
|---|
| 26 |
d.connect(name='hounslow', route='hounslow', controller=City('Hounslow')) |
|---|
| 27 |
d.connect(name='surbiton', route='surbiton', controller=City('Surbiton'), |
|---|
| 28 |
action='index', conditions=dict(method=['GET'])) |
|---|
| 29 |
d.mapper.connect('surbiton', controller='surbiton', |
|---|
| 30 |
action='update', conditions=dict(method=['POST'])) |
|---|
| 31 |
|
|---|
| 32 |
conf = {'/': {'request.dispatch': d}} |
|---|
| 33 |
cherrypy.tree.mount(root=None, config=conf) |
|---|
| 34 |
cherrypy.config.update({'environment': 'test_suite'}) |
|---|
| 35 |
|
|---|
| 36 |
|
|---|
| 37 |
from cherrypy.test import helper |
|---|
| 38 |
|
|---|
| 39 |
class RoutesDispatchTest(helper.CPWebCase): |
|---|
| 40 |
|
|---|
| 41 |
def test_Routes_Dispatch(self): |
|---|
| 42 |
self.getPage("/hounslow") |
|---|
| 43 |
self.assertStatus("200 OK") |
|---|
| 44 |
self.assertBody("Welcome to Hounslow, pop. 10000") |
|---|
| 45 |
|
|---|
| 46 |
self.getPage("/surbiton") |
|---|
| 47 |
self.assertStatus("200 OK") |
|---|
| 48 |
self.assertBody("Welcome to Surbiton, pop. 10000") |
|---|
| 49 |
|
|---|
| 50 |
self.getPage("/surbiton", method="POST", body="pop=1327") |
|---|
| 51 |
self.assertStatus("200 OK") |
|---|
| 52 |
self.assertBody("OK") |
|---|
| 53 |
self.getPage("/surbiton") |
|---|
| 54 |
self.assertStatus("200 OK") |
|---|
| 55 |
self.assertBody("Welcome to Surbiton, pop. 1327") |
|---|
| 56 |
|
|---|
| 57 |
|
|---|
| 58 |
if __name__ == '__main__': |
|---|
| 59 |
setup_server() |
|---|
| 60 |
helper.testmain() |
|---|
| 61 |
|
|---|