Download Install Tutorial Docs FAQ Tools WikiLicense Team IRC Planet Involvement Shop Book

root/tags/cherrypy-3.0.0/cherrypy/test/test_virtualhost.py

Revision 1470 (checked in by fumanchu, 2 years ago)

Fix for #614 (VirtualHost? and staticdir tool still don't play well together).

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

Hosted by WebFaction

Log in as guest/cpguest to create tickets