tests.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. # encoding: utf-8
  2. import unittest
  3. import json
  4. import sys
  5. class FunctionalTests(unittest.TestCase):
  6. def setUp(self):
  7. from frameworkbenchmarks import main
  8. app = main({})
  9. from webtest import TestApp
  10. self.testapp = TestApp(app)
  11. self.py3k = sys.version_info >= (3, 0)
  12. def _get(self, url, content_type='application/json'):
  13. res = self.testapp.get(url, status=200)
  14. self.assertTrue('Content-Length' in res.headers)
  15. # apparently these are all set by waitress, and so not
  16. # available for testing here...
  17. # self.assertTrue('Server' in res.headers)
  18. # self.assertTrue('Date' in res.headers)
  19. # self.assertTrue(content_type in res.headers['Content-Type'])
  20. return res
  21. def _str_compat(self, obj):
  22. if self.py3k:
  23. return obj.decode('utf-8')
  24. return obj
  25. def _test_obj(self, obj):
  26. self.assertTrue('id' in obj)
  27. self.assertTrue('randomNumber' in obj)
  28. self.assertTrue(1 <= obj['randomNumber'] <= 10000)
  29. def test_json(self):
  30. """
  31. /json
  32. """
  33. res = self._get('/json')
  34. self.assertEqual(self._str_compat(res.body), """{"message": "Hello, World!"}""")
  35. def test_db(self):
  36. """
  37. /db
  38. """
  39. res = self._get('/db')
  40. obj = json.loads(self._str_compat(res.body))
  41. self._test_obj(obj)
  42. def test_queries_0(self):
  43. """
  44. /queries?queries=0
  45. """
  46. res = self._get('/queries?queries=0')
  47. self.assertEqual(len(json.loads(self._str_compat(res.body))), 1)
  48. def test_queries_999(self):
  49. """
  50. /queries?queries=999
  51. """
  52. res = self._get('/queries?queries=999')
  53. self.assertEqual(len(json.loads(self._str_compat(res.body))), 500)
  54. def test_queries_999(self):
  55. """
  56. /queries?queries=10 objects
  57. """
  58. res = self._get('/queries?queries=10')
  59. objset = json.loads(self._str_compat(res.body))
  60. for obj in objset:
  61. self._test_obj(obj)
  62. def test_fortunes(self):
  63. """
  64. /fortunes
  65. """
  66. res = self._get('/fortunes')
  67. self.assertEqual(self._str_compat(res.body).strip(), fortunes.strip())
  68. def test_updates(self):
  69. """
  70. /updates?queries=10
  71. """
  72. res = self._get('/updates?queries=10')
  73. objset = json.loads(self._str_compat(res.body))
  74. # don't bother with more...
  75. for obj in objset:
  76. self._test_obj(obj)
  77. def test_plaintext(self):
  78. """
  79. /plaintext
  80. """
  81. res = self._get('/plaintext', content_type='text/plain')
  82. self.assertEqual(self._str_compat(res.body), "Hello, World!")
  83. fortunes = """
  84. <!DOCTYPE html>
  85. <html>
  86. <head>
  87. <title>Fortunes</title>
  88. </head>
  89. <body>
  90. <table>
  91. <tr>
  92. <th>id</th>
  93. <th>message</th>
  94. </tr>
  95. <tr>
  96. <td>11</td>
  97. <td>&lt;script&gt;alert("This should not be displayed in a browser alert box.");&lt;/script&gt;</td>
  98. </tr>
  99. <tr>
  100. <td>4</td>
  101. <td>A bad random number generator: 1, 1, 1, 1, 1, 4.33e+67, 1, 1, 1</td>
  102. </tr>
  103. <tr>
  104. <td>5</td>
  105. <td>A computer program does what you tell it to do, not what you want it to do.</td>
  106. </tr>
  107. <tr>
  108. <td>2</td>
  109. <td>A computer scientist is someone who fixes things that aren't broken.</td>
  110. </tr>
  111. <tr>
  112. <td>8</td>
  113. <td>A list is only as strong as its weakest link. — Donald Knuth</td>
  114. </tr>
  115. <tr>
  116. <td>0</td>
  117. <td>Additional fortune added at request time.</td>
  118. </tr>
  119. <tr>
  120. <td>3</td>
  121. <td>After enough decimal places, nobody gives a damn.</td>
  122. </tr>
  123. <tr>
  124. <td>7</td>
  125. <td>Any program that runs right is obsolete.</td>
  126. </tr>
  127. <tr>
  128. <td>10</td>
  129. <td>Computers make very fast, very accurate mistakes.</td>
  130. </tr>
  131. <tr>
  132. <td>6</td>
  133. <td>Emacs is a nice operating system, but I prefer UNIX. — Tom Christaensen</td>
  134. </tr>
  135. <tr>
  136. <td>9</td>
  137. <td>Feature: A bug with seniority.</td>
  138. </tr>
  139. <tr>
  140. <td>1</td>
  141. <td>fortune: No such file or directory</td>
  142. </tr>
  143. <tr>
  144. <td>12</td>
  145. <td>フレームワークのベンチマーク</td>
  146. </tr>
  147. </table>
  148. </body>
  149. </html>
  150. """