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