4 from cherrypy
.test
import helper
7 class TutorialTest(helper
.CPWebCase
):
11 conf
= cherrypy
.config
.copy()
13 def load_tut_module(name
):
14 """Import or reload tutorial module as needed."""
15 cherrypy
.config
.reset()
16 cherrypy
.config
.update(conf
)
18 target
= "cherrypy.tutorial." + name
19 if target
in sys
.modules
:
20 module
= reload(sys
.modules
[target
])
22 module
= __import__(target
, globals(), locals(), [''])
23 # The above import will probably mount a new app at "".
24 app
= cherrypy
.tree
.apps
[""]
26 app
.root
.load_tut_module
= load_tut_module
27 app
.root
.sessions
= sessions
28 app
.root
.traceback_setting
= traceback_setting
30 cls
.supervisor
.sync_apps()
31 load_tut_module
.exposed
= True
34 cherrypy
.config
.update({"tools.sessions.on": True})
35 sessions
.exposed
= True
37 def traceback_setting():
38 return repr(cherrypy
.request
.show_tracebacks
)
39 traceback_setting
.exposed
= True
44 root
.load_tut_module
= load_tut_module
45 cherrypy
.tree
.mount(root
)
46 setup_server
= classmethod(setup_server
)
49 def test01HelloWorld(self
):
50 self
.getPage("/load_tut_module/tut01_helloworld")
52 self
.assertBody('Hello world!')
54 def test02ExposeMethods(self
):
55 self
.getPage("/load_tut_module/tut02_expose_methods")
56 self
.getPage("/showMessage")
57 self
.assertBody('Hello world!')
59 def test03GetAndPost(self
):
60 self
.getPage("/load_tut_module/tut03_get_and_post")
62 # Try different GET queries
63 self
.getPage("/greetUser?name=Bob")
64 self
.assertBody("Hey Bob, what's up?")
66 self
.getPage("/greetUser")
67 self
.assertBody('Please enter your name <a href="./">here</a>.')
69 self
.getPage("/greetUser?name=")
70 self
.assertBody('No, really, enter your name <a href="./">here</a>.')
72 # Try the same with POST
73 self
.getPage("/greetUser", method
="POST", body
="name=Bob")
74 self
.assertBody("Hey Bob, what's up?")
76 self
.getPage("/greetUser", method
="POST", body
="name=")
77 self
.assertBody('No, really, enter your name <a href="./">here</a>.')
79 def test04ComplexSite(self
):
80 self
.getPage("/load_tut_module/tut04_complex_site")
82 <p>Here are some extra useful links:</p>
85 <li><a href="http://del.icio.us">del.icio.us</a></li>
86 <li><a href="http://www.mornography.de">Hendrik's weblog</a></li>
89 <p>[<a href="../">Return to links page</a>]</p>'''
90 self
.getPage("/links/extra/")
93 def test05DerivedObjects(self
):
94 self
.getPage("/load_tut_module/tut05_derived_objects")
98 <title>Another Page</title>
101 <h2>Another Page</h2>
104 And this is the amazing second page!
110 self
.getPage("/another/")
113 def test06DefaultMethod(self
):
114 self
.getPage("/load_tut_module/tut06_default_method")
115 self
.getPage('/hendrik')
116 self
.assertBody('Hendrik Mans, CherryPy co-developer & crazy German '
117 '(<a href="./">back</a>)')
119 def test07Sessions(self
):
120 self
.getPage("/load_tut_module/tut07_sessions")
121 self
.getPage("/sessions")
124 self
.assertBody("\n During your current session, you've viewed this"
125 "\n page 1 times! Your life is a patio of fun!"
128 self
.getPage('/', self
.cookies
)
129 self
.assertBody("\n During your current session, you've viewed this"
130 "\n page 2 times! Your life is a patio of fun!"
133 def test08GeneratorsAndYield(self
):
134 self
.getPage("/load_tut_module/tut08_generators_and_yield")
136 self
.assertBody('<html><body><h2>Generators rule!</h2>'
137 '<h3>List of users:</h3>'
138 'Remi<br/>Carlos<br/>Hendrik<br/>Lorenzo Lamas<br/>'
141 def test09Files(self
):
142 self
.getPage("/load_tut_module/tut09_files")
146 h
= [("Content-type", "multipart/form-data; boundary=x"),
147 ("Content-Length", str(105 + filesize
))]
149 'Content-Disposition: form-data; name="myFile"; filename="hello.txt"\r\n' + \
150 'Content-Type: text/plain\r\n' + \
152 'a' * filesize
+ '\n' + \
154 self
.getPage('/upload', h
, "POST", b
)
155 self
.assertBody('''<html>
157 myFile length: %d<br />
158 myFile filename: hello.txt<br />
159 myFile mime-type: text/plain
161 </html>''' % filesize
)
164 self
.getPage('/download')
165 self
.assertStatus("200 OK")
166 self
.assertHeader("Content-Type", "application/x-download")
167 self
.assertHeader("Content-Disposition",
168 # Make sure the filename is quoted.
169 'attachment; filename="pdf_file.pdf"')
170 self
.assertEqual(len(self
.body
), 85698)
172 def test10HTTPErrors(self
):
173 self
.getPage("/load_tut_module/tut10_http_errors")
176 self
.assertInBody("""<a href="toggleTracebacks">""")
177 self
.assertInBody("""<a href="/doesNotExist">""")
178 self
.assertInBody("""<a href="/error?code=403">""")
179 self
.assertInBody("""<a href="/error?code=500">""")
180 self
.assertInBody("""<a href="/messageArg">""")
182 self
.getPage("/traceback_setting")
184 self
.getPage("/toggleTracebacks")
185 self
.assertStatus((302, 303))
186 self
.getPage("/traceback_setting")
187 self
.assertBody(str(not eval(setting
)))
189 self
.getPage("/error?code=500")
190 self
.assertStatus(500)
191 self
.assertInBody("The server encountered an unexpected condition "
192 "which prevented it from fulfilling the request.")
194 self
.getPage("/error?code=403")
195 self
.assertStatus(403)
196 self
.assertInBody("<h2>You can't do that!</h2>")
198 self
.getPage("/messageArg")
199 self
.assertStatus(500)
200 self
.assertInBody("If you construct an HTTPError with a 'message'")