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

Source Code for Module cherrypy.test.test_static

  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  has_space_filepath = os.path.join(curdir, 'static', 'has space.html') 
  7  import threading 
  8   
  9  import cherrypy 
 10   
11 -def setup_server():
12 if not os.path.exists(has_space_filepath): 13 file(has_space_filepath, 'wb').write('Hello, world\r\n') 14 15 class Root: 16 pass
17 18 class Static: 19 20 def index(self): 21 return 'You want the Baron? You can have the Baron!' 22 index.exposed = True 23 24 def dynamic(self): 25 return "This is a DYNAMIC page" 26 dynamic.exposed = True 27 28 29 root = Root() 30 root.static = Static() 31 32 conf = { 33 '/static': { 34 'tools.staticdir.on': True, 35 'tools.staticdir.dir': 'static', 36 'tools.staticdir.root': curdir, 37 }, 38 '/style.css': { 39 'tools.staticfile.on': True, 40 'tools.staticfile.filename': os.path.join(curdir, 'style.css'), 41 }, 42 '/docroot': { 43 'tools.staticdir.on': True, 44 'tools.staticdir.root': curdir, 45 'tools.staticdir.dir': 'static', 46 'tools.staticdir.index': 'index.html', 47 }, 48 '/error': { 49 'tools.staticdir.on': True, 50 'request.show_tracebacks': True, 51 }, 52 } 53 54 cherrypy.tree.mount(root, config=conf) 55 cherrypy.config.update({'environment': 'test_suite'}) 56
57 -def teardown_server():
58 if os.path.exists(has_space_filepath): 59 try: 60 os.unlink(has_space_filepath) 61 except: 62 pass
63 64 from cherrypy.test import helper 65
66 -class StaticTest(helper.CPWebCase):
67
68 - def testStatic(self):
69 self.getPage("/static/index.html") 70 self.assertStatus('200 OK') 71 self.assertHeader('Content-Type', 'text/html') 72 self.assertBody('Hello, world\r\n') 73 74 # Using a staticdir.root value in a subdir... 75 self.getPage("/docroot/index.html") 76 self.assertStatus('200 OK') 77 self.assertHeader('Content-Type', 'text/html') 78 self.assertBody('Hello, world\r\n') 79 80 # Check a filename with spaces in it 81 self.getPage("/static/has%20space.html") 82 self.assertStatus('200 OK') 83 self.assertHeader('Content-Type', 'text/html') 84 self.assertBody('Hello, world\r\n') 85 86 self.getPage("/style.css") 87 self.assertStatus('200 OK') 88 self.assertHeader('Content-Type', 'text/css') 89 # Note: The body should be exactly 'Dummy stylesheet\n', but 90 # unfortunately some tools such as WinZip sometimes turn \n 91 # into \r\n on Windows when extracting the CherryPy tarball so 92 # we just check the content 93 self.assertMatchesBody('^Dummy stylesheet') 94 95 # Test that NotFound will then try dynamic handlers (see [878]). 96 self.getPage("/static/dynamic") 97 self.assertBody("This is a DYNAMIC page") 98 99 # Check a directory via fall-through to dynamic handler. 100 self.getPage("/static/") 101 self.assertStatus('200 OK') 102 self.assertHeader('Content-Type', 'text/html') 103 self.assertBody('You want the Baron? You can have the Baron!') 104 105 # Check a directory via "staticdir.index". 106 self.getPage("/docroot/") 107 self.assertStatus('200 OK') 108 self.assertHeader('Content-Type', 'text/html') 109 self.assertBody('Hello, world\r\n') 110 # The same page should be returned even if redirected. 111 self.getPage("/docroot") 112 self.assertStatus('200 OK') 113 self.assertHeader('Content-Type', 'text/html') 114 self.assertBody('Hello, world\r\n') 115 116 # Check that we get an error if no .file or .dir 117 self.getPage("/error/thing.html") 118 self.assertErrorPage(500) 119 self.assertInBody("TypeError: staticdir() takes at least 2 " 120 "arguments (0 given)") 121 122 # Test up-level security 123 self.getPage("/static/../../test/style.css") 124 self.assertStatus((400, 403)) 125 126 # Test modified-since on a reasonably-large file 127 self.getPage("/static/dirback.jpg") 128 self.assertStatus("200 OK") 129 lastmod = "" 130 for k, v in self.headers: 131 if k == 'Last-Modified': 132 lastmod = v 133 ims = ("If-Modified-Since", lastmod) 134 self.getPage("/static/dirback.jpg", headers=[ims]) 135 self.assertStatus(304) 136 self.assertNoHeader("Content-Type") 137 self.assertNoHeader("Content-Length") 138 self.assertNoHeader("Content-Disposition") 139 self.assertBody("")
140 141 142 if __name__ == "__main__": 143 setup_server() 144 helper.testmain() 145