test_app.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. from webtest import TestApp as Client
  2. import morepath
  3. import app
  4. from app import App
  5. def setup_module(module):
  6. morepath.scan(app)
  7. morepath.commit(App)
  8. def test_json():
  9. """/json"""
  10. app = App()
  11. c = Client(app)
  12. response = c.get('/json', status=200)
  13. assert response.headerlist == [
  14. ('Content-Type', 'application/json'),
  15. ('Content-Length', '27')
  16. ]
  17. assert response.json == {"message": "Hello, World!"}
  18. def test_db():
  19. """/db"""
  20. app = App()
  21. c = Client(app)
  22. response = c.get('/db', status=200)
  23. assert response.content_type == 'application/json'
  24. assert 'id' in response.json
  25. assert 'randomNumber' in response.json
  26. assert 1 <= response.json['id'] <= 10000
  27. assert 1 <= response.json['randomNumber'] <= 10000
  28. def test_queries():
  29. """/queries?queries="""
  30. app = App()
  31. c = Client(app)
  32. response = c.get('/queries?queries=', status=200)
  33. assert response.content_type == 'application/json'
  34. assert len(response.json) == 1
  35. def test_queries_foo():
  36. """/queries?queries=foo"""
  37. app = App()
  38. c = Client(app)
  39. response = c.get('/queries?queries=foo', status=200)
  40. assert response.content_type == 'application/json'
  41. assert len(response.json) == 1
  42. def test_queries_0():
  43. """/queries?queries=0"""
  44. app = App()
  45. c = Client(app)
  46. response = c.get('/queries?queries=0', status=200)
  47. assert response.content_type == 'application/json'
  48. assert len(response.json) == 1
  49. def test_queries_999():
  50. """/queries?queries=999"""
  51. app = App()
  52. c = Client(app)
  53. response = c.get('/queries?queries=999', status=200)
  54. assert response.content_type == 'application/json'
  55. assert len(response.json) == 500
  56. def test_queries_10():
  57. """/queries?queries=10"""
  58. app = App()
  59. c = Client(app)
  60. response = c.get('/queries?queries=10', status=200)
  61. assert response.content_type == 'application/json'
  62. assert len(response.json) == 10
  63. obj_list = response.json
  64. for obj in obj_list:
  65. assert 'id' in obj
  66. assert 'randomNumber' in obj
  67. assert 1 <= obj['id'] <= 10000
  68. assert 1 <= obj['randomNumber'] <= 10000
  69. def test_fortunes():
  70. """/fortunes"""
  71. app = App()
  72. c = Client(app)
  73. response = c.get('/fortunes', status=200)
  74. assert response.headerlist == [
  75. ('Content-Type', 'text/html; charset=UTF-8'),
  76. ('Content-Length', '1304')
  77. ]
  78. assert response.text == fortunes
  79. def test_updates():
  80. """/updates?queries="""
  81. app = App()
  82. c = Client(app)
  83. response = c.get('/updates?queries=', status=200)
  84. assert response.content_type == 'application/json'
  85. assert len(response.json) == 1
  86. def test_updates_foo():
  87. """/updates?queries=foo"""
  88. app = App()
  89. c = Client(app)
  90. response = c.get('/updates?queries=foo', status=200)
  91. assert response.content_type == 'application/json'
  92. assert len(response.json) == 1
  93. def test_updates_0():
  94. """/updates?queries=0"""
  95. app = App()
  96. c = Client(app)
  97. response = c.get('/updates?queries=0', status=200)
  98. assert response.content_type == 'application/json'
  99. assert len(response.json) == 1
  100. def test_updates_999():
  101. """/updates?queries=999"""
  102. app = App()
  103. c = Client(app)
  104. response = c.get('/updates?queries=999', status=200)
  105. assert response.content_type == 'application/json'
  106. assert len(response.json) == 500
  107. def test_updates_10():
  108. """/updates?queries=10"""
  109. app = App()
  110. c = Client(app)
  111. response = c.get('/updates?queries=10', status=200)
  112. assert response.content_type == 'application/json'
  113. assert len(response.json) == 10
  114. obj_list = response.json
  115. for obj in obj_list:
  116. assert 'id' in obj
  117. assert 'randomNumber' in obj
  118. assert 1 <= obj['id'] <= 10000
  119. assert 1 <= obj['randomNumber'] <= 10000
  120. def test_plaintext():
  121. """/plaintext"""
  122. app = App()
  123. c = Client(app)
  124. response = c.get('/plaintext', status=200)
  125. assert response.headerlist == [
  126. ('Content-Type', 'text/plain; charset=UTF-8'),
  127. ('Content-Length', '13')
  128. ]
  129. assert response.text == 'Hello, World!'
  130. fortunes = """<!DOCTYPE html>
  131. <html>
  132. <head>
  133. <title>Fortunes</title>
  134. </head>
  135. <body>
  136. <table>
  137. <tr>
  138. <th>id</th>
  139. <th>message</th>
  140. </tr>
  141. <tr>
  142. <td>11</td>
  143. <td>&lt;script&gt;alert(&#34;This should not be displayed in a browser alert box.&#34;);&lt;/script&gt;</td>
  144. </tr>
  145. <tr>
  146. <td>4</td>
  147. <td>A bad random number generator: 1, 1, 1, 1, 1, 4.33e+67, 1, 1, 1</td>
  148. </tr>
  149. <tr>
  150. <td>5</td>
  151. <td>A computer program does what you tell it to do, not what you want it to do.</td>
  152. </tr>
  153. <tr>
  154. <td>2</td>
  155. <td>A computer scientist is someone who fixes things that aren&#39;t broken.</td>
  156. </tr>
  157. <tr>
  158. <td>8</td>
  159. <td>A list is only as strong as its weakest link. — Donald Knuth</td>
  160. </tr>
  161. <tr>
  162. <td>0</td>
  163. <td>Additional fortune added at request time.</td>
  164. </tr>
  165. <tr>
  166. <td>3</td>
  167. <td>After enough decimal places, nobody gives a damn.</td>
  168. </tr>
  169. <tr>
  170. <td>7</td>
  171. <td>Any program that runs right is obsolete.</td>
  172. </tr>
  173. <tr>
  174. <td>10</td>
  175. <td>Computers make very fast, very accurate mistakes.</td>
  176. </tr>
  177. <tr>
  178. <td>6</td>
  179. <td>Emacs is a nice operating system, but I prefer UNIX. — Tom Christaensen</td>
  180. </tr>
  181. <tr>
  182. <td>9</td>
  183. <td>Feature: A bug with seniority.</td>
  184. </tr>
  185. <tr>
  186. <td>1</td>
  187. <td>fortune: No such file or directory</td>
  188. </tr>
  189. <tr>
  190. <td>12</td>
  191. <td>フレームワークのベンチマーク</td>
  192. </tr>
  193. </table>
  194. </body>
  195. </html>"""