/usr/share/pyshared/cherrypy/test/test_static_filter.py is in python-cherrypy 2.3.0-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 | import test
test.prefer_parent_path()
import os
curdir = os.path.join(os.getcwd(), os.path.dirname(__file__))
import threading
import cherrypy
from cherrypy.lib import cptools
def setup_server():
class Root:
pass
class Static:
def index(self):
return 'You want the Baron? You can have the Baron!'
index.exposed = True
def dynamic(self):
return "This is a DYNAMIC page"
dynamic.exposed = True
cherrypy.root = Root()
cherrypy.root.static = Static()
cherrypy.config.update({
'global': {
'static_filter.on': False,
'server.log_to_screen': False,
'server.environment': 'production',
},
'/static': {
'static_filter.on': True,
'static_filter.dir': 'static',
'static_filter.root': curdir,
},
'/style.css': {
'static_filter.on': True,
'static_filter.file': 'style.css',
'static_filter.root': curdir,
},
'/docroot': {
'static_filter.on': True,
'static_filter.root': curdir,
'static_filter.dir': 'static',
'static_filter.index': 'index.html',
},
'/error': {
'static_filter.on': True,
'server.show_tracebacks': True,
},
})
import helper
class StaticFilterTest(helper.CPWebCase):
def testStaticFilter(self):
self.getPage("/static/index.html")
self.assertStatus('200 OK')
self.assertHeader('Content-Type', 'text/html')
self.assertBody('Hello, world\r\n')
# Using a static_filter.root value in a subdir...
self.getPage("/docroot/index.html")
self.assertStatus('200 OK')
self.assertHeader('Content-Type', 'text/html')
self.assertBody('Hello, world\r\n')
# Check a filename with spaces in it
self.getPage("/static/has%20space.html")
self.assertStatus('200 OK')
self.assertHeader('Content-Type', 'text/html')
self.assertBody('Hello, world\r\n')
self.getPage("/style.css")
self.assertStatus('200 OK')
self.assertHeader('Content-Type', 'text/css')
# Note: The body should be exactly 'Dummy stylesheet\n', but
# unfortunately some tools such as WinZip sometimes turn \n
# into \r\n on Windows when extracting the CherryPy tarball so
# we just check the content
self.assertMatchesBody('^Dummy stylesheet')
# Test that NotFound will then try dynamic handlers (see [878]).
self.getPage("/static/dynamic")
self.assertBody("This is a DYNAMIC page")
# Check a directory via fall-through to dynamic handler.
self.getPage("/static/")
self.assertStatus('200 OK')
self.assertHeader('Content-Type', 'text/html')
self.assertBody('You want the Baron? You can have the Baron!')
# Check a directory via "static_filter.index".
self.getPage("/docroot/")
self.assertStatus('200 OK')
self.assertHeader('Content-Type', 'text/html')
self.assertBody('Hello, world\r\n')
# The same page should be returned even if redirected.
self.getPage("/docroot")
self.assertStatus('200 OK')
self.assertHeader('Content-Type', 'text/html')
self.assertBody('Hello, world\r\n')
# Check that we get a WrongConfigValue error if no .file or .dir
self.getPage("/error/thing.html")
self.assertErrorPage(500)
self.assertInBody("WrongConfigValue: StaticFilter requires either "
"static_filter.file or static_filter.dir "
"(/error/thing.html)")
# Test up-level security
self.getPage("/static/../../test/style.css")
self.assertStatus((400, 403))
# Test modified-since on a reasonably-large file
self.getPage("/static/dirback.jpg")
self.assertStatus("200 OK")
lastmod = ""
for k, v in self.headers:
if k == 'Last-Modified':
lastmod = v
ims = ("If-Modified-Since", lastmod)
self.getPage("/static/dirback.jpg", headers=[ims])
self.assertStatus(304)
self.assertNoHeader("Content-Type")
self.assertNoHeader("Content-Length")
self.assertNoHeader("Content-Disposition")
self.assertBody("")
##
## # Test lots of requests for the same file (no If-Mod).
## ts = []
## for x in xrange(500):
## t = threading.Thread(target=self.getPage,
## args=("/static/dirback.jpg",))
## ts.append(t)
## t.start()
## for t in ts:
## t.join()
##
if __name__ == "__main__":
setup_server()
helper.testmain()
|