| 1 |
from cherrypy.test import test |
|---|
| 2 |
test.prefer_parent_path() |
|---|
| 3 |
|
|---|
| 4 |
import sys |
|---|
| 5 |
|
|---|
| 6 |
import cherrypy |
|---|
| 7 |
from cherrypy.test import helper |
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
def setup_server(): |
|---|
| 11 |
|
|---|
| 12 |
conf = cherrypy.config.copy() |
|---|
| 13 |
|
|---|
| 14 |
def load_tut_module(name): |
|---|
| 15 |
"""Import or reload tutorial module as needed.""" |
|---|
| 16 |
cherrypy.config.reset() |
|---|
| 17 |
cherrypy.config.update(conf) |
|---|
| 18 |
|
|---|
| 19 |
target = "cherrypy.tutorial." + name |
|---|
| 20 |
if target in sys.modules: |
|---|
| 21 |
module = reload(sys.modules[target]) |
|---|
| 22 |
else: |
|---|
| 23 |
module = __import__(target, globals(), locals(), ['']) |
|---|
| 24 |
|
|---|
| 25 |
app = cherrypy.tree.apps[""] |
|---|
| 26 |
|
|---|
| 27 |
app.root.load_tut_module = load_tut_module |
|---|
| 28 |
app.root.sessions = sessions |
|---|
| 29 |
app.root.traceback_setting = traceback_setting |
|---|
| 30 |
|
|---|
| 31 |
helper.sync_apps() |
|---|
| 32 |
load_tut_module.exposed = True |
|---|
| 33 |
|
|---|
| 34 |
def sessions(): |
|---|
| 35 |
cherrypy.config.update({"tools.sessions.on": True}) |
|---|
| 36 |
sessions.exposed = True |
|---|
| 37 |
|
|---|
| 38 |
def traceback_setting(): |
|---|
| 39 |
return repr(cherrypy.request.show_tracebacks) |
|---|
| 40 |
traceback_setting.exposed = True |
|---|
| 41 |
|
|---|
| 42 |
class Dummy: |
|---|
| 43 |
pass |
|---|
| 44 |
root = Dummy() |
|---|
| 45 |
root.load_tut_module = load_tut_module |
|---|
| 46 |
cherrypy.tree.mount(root) |
|---|
| 47 |
|
|---|
| 48 |
|
|---|
| 49 |
class TutorialTest(helper.CPWebCase): |
|---|
| 50 |
|
|---|
| 51 |
def test01HelloWorld(self): |
|---|
| 52 |
self.getPage("/load_tut_module/tut01_helloworld") |
|---|
| 53 |
self.getPage("/") |
|---|
| 54 |
self.assertBody('Hello world!') |
|---|
| 55 |
|
|---|
| 56 |
def test02ExposeMethods(self): |
|---|
| 57 |
self.getPage("/load_tut_module/tut02_expose_methods") |
|---|
| 58 |
self.getPage("/showMessage") |
|---|
| 59 |
self.assertBody('Hello world!') |
|---|
| 60 |
|
|---|
| 61 |
def test03GetAndPost(self): |
|---|
| 62 |
self.getPage("/load_tut_module/tut03_get_and_post") |
|---|
| 63 |
|
|---|
| 64 |
|
|---|
| 65 |
self.getPage("/greetUser?name=Bob") |
|---|
| 66 |
self.assertBody("Hey Bob, what's up?") |
|---|
| 67 |
|
|---|
| 68 |
self.getPage("/greetUser") |
|---|
| 69 |
self.assertBody('Please enter your name <a href="./">here</a>.') |
|---|
| 70 |
|
|---|
| 71 |
self.getPage("/greetUser?name=") |
|---|
| 72 |
self.assertBody('No, really, enter your name <a href="./">here</a>.') |
|---|
| 73 |
|
|---|
| 74 |
|
|---|
| 75 |
self.getPage("/greetUser", method="POST", body="name=Bob") |
|---|
| 76 |
self.assertBody("Hey Bob, what's up?") |
|---|
| 77 |
|
|---|
| 78 |
self.getPage("/greetUser", method="POST", body="name=") |
|---|
| 79 |
self.assertBody('No, really, enter your name <a href="./">here</a>.') |
|---|
| 80 |
|
|---|
| 81 |
def test04ComplexSite(self): |
|---|
| 82 |
self.getPage("/load_tut_module/tut04_complex_site") |
|---|
| 83 |
msg = ''' |
|---|
| 84 |
<p>Here are some extra useful links:</p> |
|---|
| 85 |
|
|---|
| 86 |
<ul> |
|---|
| 87 |
<li><a href="del.icio.ushttp://del.icio.us">del.icio.us</a></li> |
|---|
| 88 |
<li><a href="Hendrik's">http://www.mornography.de">Hendrik's weblog</a></li> |
|---|
| 89 |
</ul> |
|---|
| 90 |
|
|---|
| 91 |
<p>[<a href="../">Return to links page</a>]</p>''' |
|---|
| 92 |
self.getPage("/links/extra/") |
|---|
| 93 |
self.assertBody(msg) |
|---|
| 94 |
|
|---|
| 95 |
def test05DerivedObjects(self): |
|---|
| 96 |
self.getPage("/load_tut_module/tut05_derived_objects") |
|---|
| 97 |
msg = ''' |
|---|
| 98 |
<html> |
|---|
| 99 |
<head> |
|---|
| 100 |
<title>Another Page</title> |
|---|
| 101 |
<head> |
|---|
| 102 |
<body> |
|---|
| 103 |
<h2>Another Page</h2> |
|---|
| 104 |
|
|---|
| 105 |
<p> |
|---|
| 106 |
And this is the amazing second page! |
|---|
| 107 |
</p> |
|---|
| 108 |
|
|---|
| 109 |
</body> |
|---|
| 110 |
</html> |
|---|
| 111 |
''' |
|---|
| 112 |
self.getPage("/another/") |
|---|
| 113 |
self.assertBody(msg) |
|---|
| 114 |
|
|---|
| 115 |
def test06DefaultMethod(self): |
|---|
| 116 |
self.getPage("/load_tut_module/tut06_default_method") |
|---|
| 117 |
self.getPage('/hendrik') |
|---|
| 118 |
self.assertBody('Hendrik Mans, CherryPy co-developer & crazy German ' |
|---|
| 119 |
'(<a href="./">back</a>)') |
|---|
| 120 |
def test07Sessions(self): |
|---|
| 121 |
self.getPage("/load_tut_module/tut07_sessions") |
|---|
| 122 |
self.getPage("/sessions") |
|---|
| 123 |
|
|---|
| 124 |
self.getPage('/') |
|---|
| 125 |
self.assertBody("\n During your current session, you've viewed this" |
|---|
| 126 |
"\n page 1 times! Your life is a patio of fun!" |
|---|
| 127 |
"\n ") |
|---|
| 128 |
|
|---|
| 129 |
self.getPage('/', self.cookies) |
|---|
| 130 |
self.assertBody("\n During your current session, you've viewed this" |
|---|
| 131 |
"\n page 2 times! Your life is a patio of fun!" |
|---|
| 132 |
"\n ") |
|---|
| 133 |
|
|---|
| 134 |
def test08GeneratorsAndYield(self): |
|---|
| 135 |
self.getPage("/load_tut_module/tut08_generators_and_yield") |
|---|
| 136 |
self.getPage('/') |
|---|
| 137 |
self.assertBody('<html><body><h2>Generators rule!</h2>' |
|---|
| 138 |
'<h3>List of users:</h3>' |
|---|
| 139 |
'Remi<br/>Carlos<br/>Hendrik<br/>Lorenzo Lamas<br/>' |
|---|
| 140 |
'</body></html>') |
|---|
| 141 |
|
|---|
| 142 |
def test09Files(self): |
|---|
| 143 |
self.getPage("/load_tut_module/tut09_files") |
|---|
| 144 |
|
|---|
| 145 |
|
|---|
| 146 |
h = [("Content-type", "multipart/form-data; boundary=x"), |
|---|
| 147 |
("Content-Length", "110")] |
|---|
| 148 |
b = """--x |
|---|
| 149 |
Content-Disposition: form-data; name="myFile"; filename="hello.txt" |
|---|
| 150 |
Content-Type: text/plain |
|---|
| 151 |
|
|---|
| 152 |
hello |
|---|
| 153 |
--x-- |
|---|
| 154 |
""" |
|---|
| 155 |
self.getPage('/upload', h, "POST", b) |
|---|
| 156 |
self.assertBody('''<html> |
|---|
| 157 |
<body> |
|---|
| 158 |
myFile length: 5<br /> |
|---|
| 159 |
myFile filename: hello.txt<br /> |
|---|
| 160 |
myFile mime-type: text/plain |
|---|
| 161 |
</body> |
|---|
| 162 |
</html>''') |
|---|
| 163 |
|
|---|
| 164 |
|
|---|
| 165 |
self.getPage('/download') |
|---|
| 166 |
self.assertStatus("200 OK") |
|---|
| 167 |
self.assertHeader("Content-Type", "application/x-download") |
|---|
| 168 |
self.assertHeader("Content-Disposition", |
|---|
| 169 |
|
|---|
| 170 |
'attachment; filename="pdf_file.pdf"') |
|---|
| 171 |
self.assertEqual(len(self.body), 85698) |
|---|
| 172 |
|
|---|
| 173 |
def test10HTTPErrors(self): |
|---|
| 174 |
self.getPage("/load_tut_module/tut10_http_errors") |
|---|
| 175 |
|
|---|
| 176 |
self.getPage("/") |
|---|
| 177 |
self.assertInBody("""<a href="toggleTracebacks">""") |
|---|
| 178 |
self.assertInBody("""<a href="/doesNotExist">""") |
|---|
| 179 |
self.assertInBody("""<a href="/error?code=403">""") |
|---|
| 180 |
self.assertInBody("""<a href="/error?code=500">""") |
|---|
| 181 |
self.assertInBody("""<a href="/messageArg">""") |
|---|
| 182 |
|
|---|
| 183 |
self.getPage("/traceback_setting") |
|---|
| 184 |
setting = self.body |
|---|
| 185 |
self.getPage("/toggleTracebacks") |
|---|
| 186 |
self.assertStatus((302, 303)) |
|---|
| 187 |
self.getPage("/traceback_setting") |
|---|
| 188 |
self.assertBody(str(not eval(setting))) |
|---|
| 189 |
|
|---|
| 190 |
self.getPage("/error?code=500") |
|---|
| 191 |
self.assertStatus(500) |
|---|
| 192 |
self.assertInBody("The server encountered an unexpected condition " |
|---|
| 193 |
"which prevented it from fulfilling the request.") |
|---|
| 194 |
|
|---|
| 195 |
self.getPage("/error?code=403") |
|---|
| 196 |
self.assertStatus(403) |
|---|
| 197 |
self.assertInBody("<h2>You can't do that!</h2>") |
|---|
| 198 |
|
|---|
| 199 |
self.getPage("/messageArg") |
|---|
| 200 |
self.assertStatus(500) |
|---|
| 201 |
self.assertInBody("If you construct an HTTPError with a 'message'") |
|---|
| 202 |
|
|---|
| 203 |
|
|---|
| 204 |
if __name__ == "__main__": |
|---|
| 205 |
conf = {'server.socket_host': '127.0.0.1', |
|---|
| 206 |
'server.socket_port': 8080, |
|---|
| 207 |
'server.thread_pool': 10, |
|---|
| 208 |
'environment': "test_suite", |
|---|
| 209 |
} |
|---|
| 210 |
cherrypy.config.update(conf) |
|---|
| 211 |
setup_server() |
|---|
| 212 |
helper.testmain() |
|---|