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

root/trunk/cherrypy/test/test_static.py

Revision 1958 (checked in by fumanchu, 2 days ago)

Fixed a test for HTTP/1.0.

  • 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 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     cherrypy.config.update({'environment': 'test_suite'})
30    
31     root = Root()
32     root.static = Static()
33    
34     rootconf = {
35         '/static': {
36             'tools.staticdir.on': True,
37             'tools.staticdir.dir': 'static',
38             'tools.staticdir.root': curdir,
39         },
40         '/style.css': {
41             'tools.staticfile.on': True,
42             'tools.staticfile.filename': os.path.join(curdir, 'style.css'),
43         },
44         '/docroot': {
45             'tools.staticdir.on': True,
46             'tools.staticdir.root': curdir,
47             'tools.staticdir.dir': 'static',
48             'tools.staticdir.index': 'index.html',
49         },
50         '/error': {
51             'tools.staticdir.on': True,
52             'request.show_tracebacks': True,
53         },
54         }
55     rootApp = cherrypy.Application(root)
56     rootApp.merge(rootconf)
57    
58     test_app_conf = {
59         '/test': {
60             'tools.staticdir.index': 'index.html',
61             'tools.staticdir.on': True,
62             'tools.staticdir.root': curdir,
63             'tools.staticdir.dir': 'static',
64             },
65         }
66     testApp = cherrypy.Application(Static())
67     testApp.merge(test_app_conf)
68    
69     vhost = cherrypy._cpwsgi.VirtualHost(rootApp, {'virt.net': testApp})
70     cherrypy.tree.graft(vhost)
71
72
73 def teardown_server():
74     if os.path.exists(has_space_filepath):
75         try:
76             os.unlink(has_space_filepath)
77         except:
78             pass
79        
80 from cherrypy.test import helper
81
82 class StaticTest(helper.CPWebCase):
83    
84     def testStatic(self):
85         self.getPage("/static/index.html")
86         self.assertStatus('200 OK')
87         self.assertHeader('Content-Type', 'text/html')
88         self.assertBody('Hello, world\r\n')
89        
90         # Using a staticdir.root value in a subdir...
91         self.getPage("/docroot/index.html")
92         self.assertStatus('200 OK')
93         self.assertHeader('Content-Type', 'text/html')
94         self.assertBody('Hello, world\r\n')
95        
96         # Check a filename with spaces in it
97         self.getPage("/static/has%20space.html")
98         self.assertStatus('200 OK')
99         self.assertHeader('Content-Type', 'text/html')
100         self.assertBody('Hello, world\r\n')
101        
102         self.getPage("/style.css")
103         self.assertStatus('200 OK')
104         self.assertHeader('Content-Type', 'text/css')
105         # Note: The body should be exactly 'Dummy stylesheet\n', but
106         #   unfortunately some tools such as WinZip sometimes turn \n
107         #   into \r\n on Windows when extracting the CherryPy tarball so
108         #   we just check the content
109         self.assertMatchesBody('^Dummy stylesheet')
110    
111     def test_fallthrough(self):
112         # Test that NotFound will then try dynamic handlers (see [878]).
113         self.getPage("/static/dynamic")
114         self.assertBody("This is a DYNAMIC page")
115        
116         # Check a directory via fall-through to dynamic handler.
117         self.getPage("/static/")
118         self.assertStatus('200 OK')
119         self.assertHeader('Content-Type', 'text/html')
120         self.assertBody('You want the Baron? You can have the Baron!')
121    
122     def test_index(self):
123         # Check a directory via "staticdir.index".
124         self.getPage("/docroot/")
125         self.assertStatus('200 OK')
126         self.assertHeader('Content-Type', 'text/html')
127         self.assertBody('Hello, world\r\n')
128         # The same page should be returned even if redirected.
129         self.getPage("/docroot")
130         self.assertStatus((302, 303))
131         self.assertHeader('Location', '%s/docroot/' % self.base())
132         self.assertMatchesBody("This resource .* at <a href='%s/docroot/'>"
133                                "%s/docroot/</a>." % (self.base(), self.base()))
134    
135     def test_config_errors(self):
136         # Check that we get an error if no .file or .dir
137         self.getPage("/error/thing.html")
138         self.assertErrorPage(500)
139         self.assertInBody("TypeError: staticdir() takes at least 2 "
140                           "arguments (0 given)")
141    
142     def test_security(self):
143         # Test up-level security
144         self.getPage("/static/../../test/style.css")
145         self.assertStatus((400, 403))
146    
147     def test_modif(self):
148         # Test modified-since on a reasonably-large file
149         self.getPage("/static/dirback.jpg")
150         self.assertStatus("200 OK")
151         lastmod = ""
152         for k, v in self.headers:
153             if k == 'Last-Modified':
154                 lastmod = v
155         ims = ("If-Modified-Since", lastmod)
156         self.getPage("/static/dirback.jpg", headers=[ims])
157         self.assertStatus(304)
158         self.assertNoHeader("Content-Type")
159         self.assertNoHeader("Content-Length")
160         self.assertNoHeader("Content-Disposition")
161         self.assertBody("")
162    
163     def test_755_vhost(self):
164         self.getPage("/test/", [('Host', 'virt.net')])
165         self.assertStatus(200)
166         self.getPage("/test", [('Host', 'virt.net')])
167         self.assertStatus((302, 303))
168         self.assertHeader('Location', self.scheme + '://virt.net/test/')
169
170
171 if __name__ == "__main__":
172     setup_server()
173     helper.testmain()
Note: See TracBrowser for help on using the browser.

Hosted by WebFaction

Log in as guest/cpguest to create tickets