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

Source Code for Module cherrypy.test.test_virtualhost

  1  import os 
  2  curdir = os.path.join(os.getcwd(), os.path.dirname(__file__)) 
  3   
  4  import cherrypy 
  5  from cherrypy.test import helper 
  6   
  7   
8 -class VirtualHostTest(helper.CPWebCase):
9
10 - def setup_server():
11 class Root: 12 13 def index(self): 14 return "Hello, world"
15 index.exposed = True 16 17 def dom4(self): 18 return "Under construction"
19 dom4.exposed = True 20 21 def method(self, value): 22 return "You sent %s" % value 23 method.exposed = True 24 25 class VHost: 26 27 def __init__(self, sitename): 28 self.sitename = sitename 29 30 def index(self): 31 return "Welcome to %s" % self.sitename 32 index.exposed = True 33 34 def vmethod(self, value): 35 return "You sent %s" % value 36 vmethod.exposed = True 37 38 def url(self): 39 return cherrypy.url("nextpage") 40 url.exposed = True 41 42 # Test static as a handler (section must NOT include vhost prefix) 43 static = cherrypy.tools.staticdir.handler( 44 section='/static', dir=curdir) 45 46 root = Root() 47 root.mydom2 = VHost("Domain 2") 48 root.mydom3 = VHost("Domain 3") 49 hostmap = {'www.mydom2.com': '/mydom2', 50 'www.mydom3.com': '/mydom3', 51 'www.mydom4.com': '/dom4', 52 } 53 cherrypy.tree.mount(root, config={ 54 '/': { 55 'request.dispatch': cherrypy.dispatch.VirtualHost(**hostmap) 56 }, 57 # Test static in config (section must include vhost prefix) 58 '/mydom2/static2': { 59 'tools.staticdir.on': True, 60 'tools.staticdir.root': curdir, 61 'tools.staticdir.dir': 'static', 62 'tools.staticdir.index': 'index.html', 63 }, 64 }) 65 setup_server = staticmethod(setup_server) 66
67 - def testVirtualHost(self):
68 self.getPage("/", [('Host', 'www.mydom1.com')]) 69 self.assertBody('Hello, world') 70 self.getPage("/mydom2/", [('Host', 'www.mydom1.com')]) 71 self.assertBody('Welcome to Domain 2') 72 73 self.getPage("/", [('Host', 'www.mydom2.com')]) 74 self.assertBody('Welcome to Domain 2') 75 self.getPage("/", [('Host', 'www.mydom3.com')]) 76 self.assertBody('Welcome to Domain 3') 77 self.getPage("/", [('Host', 'www.mydom4.com')]) 78 self.assertBody('Under construction') 79 80 # Test GET, POST, and positional params 81 self.getPage("/method?value=root") 82 self.assertBody("You sent root") 83 self.getPage("/vmethod?value=dom2+GET", [('Host', 'www.mydom2.com')]) 84 self.assertBody("You sent dom2 GET") 85 self.getPage("/vmethod", [('Host', 'www.mydom3.com')], method="POST", 86 body="value=dom3+POST") 87 self.assertBody("You sent dom3 POST") 88 self.getPage("/vmethod/pos", [('Host', 'www.mydom3.com')]) 89 self.assertBody("You sent pos") 90 91 # Test that cherrypy.url uses the browser url, not the virtual url 92 self.getPage("/url", [('Host', 'www.mydom2.com')]) 93 self.assertBody("%s://www.mydom2.com/nextpage" % self.scheme)
94
95 - def test_VHost_plus_Static(self):
96 # Test static as a handler 97 self.getPage("/static/style.css", [('Host', 'www.mydom2.com')]) 98 self.assertStatus('200 OK') 99 self.assertHeader('Content-Type', 'text/css;charset=utf-8') 100 101 # Test static in config 102 self.getPage("/static2/dirback.jpg", [('Host', 'www.mydom2.com')]) 103 self.assertStatus('200 OK') 104 self.assertHeaderIn('Content-Type', ['image/jpeg', 'image/pjpeg']) 105 106 # Test static config with "index" arg 107 self.getPage("/static2/", [('Host', 'www.mydom2.com')]) 108 self.assertStatus('200 OK') 109 self.assertBody('Hello, world\r\n') 110 # Since tools.trailing_slash is on by default, this should redirect 111 self.getPage("/static2", [('Host', 'www.mydom2.com')]) 112 self.assertStatus(301)
113