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

root/branches/cherrypy-2.1/cherrypy/test/test_static_filter.py

Revision 759 (checked in by fumanchu, 3 years ago)

New test for directory in staticfilter.

  • Property svn:eol-style set to LF
Line 
1 """
2 Copyright (c) 2004, CherryPy Team (team@cherrypy.org)
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without modification,
6 are permitted provided that the following conditions are met:
7
8     * Redistributions of source code must retain the above copyright notice,
9       this list of conditions and the following disclaimer.
10     * Redistributions in binary form must reproduce the above copyright notice,
11       this list of conditions and the following disclaimer in the documentation
12       and/or other materials provided with the distribution.
13     * Neither the name of the CherryPy Team nor the names of its contributors
14       may be used to endorse or promote products derived from this software
15       without specific prior written permission.
16
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
21 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
24 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 """
28
29 import cherrypy
30 import os
31
32
33 class Root: pass
34
35 cherrypy.root = Root()
36 cherrypy.config.update({
37     'global': {
38         'staticFilter.on': False,
39         'server.logToScreen': False,
40         'server.environment': 'production',
41     },
42     '/static': {
43         'staticFilter.on': True,
44         'staticFilter.dir': 'static',
45     },
46     '/style.css': {
47         'staticFilter.on': True,
48         'staticFilter.file': 'style.css',
49     },
50     '/docroot': {
51         'staticFilter.on': True,
52         'staticFilter.root': os.path.join(os.getcwd(), os.path.dirname(__file__)),
53         'staticFilter.dir': 'static',
54     },
55 })
56
57 import helper
58
59 class StaticFilterTest(helper.CPWebCase):
60    
61     def testStaticFilter(self):
62         # This should resolve relative to cherrypy.root.__module__.
63         self.getPage("/static/index.html")
64         self.assertStatus('200 OK')
65         self.assertHeader('Content-Type', 'text/html')
66         self.assertBody('Hello, world\r\n')
67        
68         # Using a staticFilter.root value...
69         self.getPage("/docroot/index.html")
70         self.assertStatus('200 OK')
71         self.assertHeader('Content-Type', 'text/html')
72         self.assertBody('Hello, world\r\n')
73        
74         # Check a filename with spaces in it
75         self.getPage("/static/has%20space.html")
76         self.assertStatus('200 OK')
77         self.assertHeader('Content-Type', 'text/html')
78         self.assertBody('Hello, world\r\n')
79        
80         self.getPage("/style.css")
81         self.assertStatus('200 OK')
82         self.assertHeader('Content-Type', 'text/css')
83         # Note: The body should be exactly 'Dummy stylesheet\n', but
84         #   unfortunately some tools such as WinZip sometimes turn \n
85         #   into \r\n on Windows when extracting the CherryPy tarball so
86         #   we just check the content
87         self.assertMatchesBody('^Dummy stylesheet')
88        
89         # Check a directory (should currently fail--no provision for it)
90         ignore = helper.webtest.ignored_exceptions
91         ignore.append(IOError)
92         try:
93             self.getPage("/static/")
94             self.assertErrorPage(500)
95         finally:
96             ignore.pop()
97
98 if __name__ == "__main__":
99     helper.testmain()
Note: See TracBrowser for help on using the browser.

Hosted by WebFaction

Log in as guest/cpguest to create tickets