test_web.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. # test_web.py -- Compatibility tests for the git web server.
  2. # Copyright (C) 2010 Google, Inc.
  3. #
  4. # This program is free software; you can redistribute it and/or
  5. # modify it under the terms of the GNU General Public License
  6. # as published by the Free Software Foundation; version 2
  7. # of the License or (at your option) any later version of
  8. # the License.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  18. # MA 02110-1301, USA.
  19. """Compatibility tests between Dulwich and the cgit HTTP server.
  20. warning: these tests should be fairly stable, but when writing/debugging new
  21. tests, deadlocks may freeze the test process such that it cannot be
  22. Ctrl-C'ed. On POSIX systems, you can kill the tests with Ctrl-Z, "kill %".
  23. """
  24. import threading
  25. from wsgiref import simple_server
  26. from dulwich.server import (
  27. DictBackend,
  28. )
  29. from dulwich.tests import (
  30. SkipTest,
  31. )
  32. from dulwich.web import (
  33. make_wsgi_chain,
  34. HTTPGitApplication,
  35. HTTPGitRequestHandler,
  36. )
  37. from dulwich.tests.compat.server_utils import (
  38. ServerTests,
  39. ShutdownServerMixIn,
  40. NoSideBand64kReceivePackHandler,
  41. )
  42. from dulwich.tests.compat.utils import (
  43. CompatTestCase,
  44. )
  45. if getattr(simple_server.WSGIServer, 'shutdown', None):
  46. WSGIServer = simple_server.WSGIServer
  47. else:
  48. class WSGIServer(ShutdownServerMixIn, simple_server.WSGIServer):
  49. """Subclass of WSGIServer that can be shut down."""
  50. def __init__(self, *args, **kwargs):
  51. # BaseServer is old-style so we have to call both __init__s
  52. ShutdownServerMixIn.__init__(self)
  53. simple_server.WSGIServer.__init__(self, *args, **kwargs)
  54. serve = ShutdownServerMixIn.serve_forever
  55. class WebTests(ServerTests):
  56. """Base tests for web server tests.
  57. Contains utility and setUp/tearDown methods, but does non inherit from
  58. TestCase so tests are not automatically run.
  59. """
  60. protocol = 'http'
  61. def _start_server(self, repo):
  62. backend = DictBackend({'/': repo})
  63. app = self._make_app(backend)
  64. dul_server = simple_server.make_server(
  65. 'localhost', 0, app, server_class=WSGIServer,
  66. handler_class=HTTPGitRequestHandler)
  67. self.addCleanup(dul_server.shutdown)
  68. threading.Thread(target=dul_server.serve_forever).start()
  69. self._server = dul_server
  70. _, port = dul_server.socket.getsockname()
  71. return port
  72. class SmartWebTestCase(WebTests, CompatTestCase):
  73. """Test cases for smart HTTP server.
  74. This server test case does not use side-band-64k in git-receive-pack.
  75. """
  76. min_git_version = (1, 6, 6)
  77. def _handlers(self):
  78. return {'git-receive-pack': NoSideBand64kReceivePackHandler}
  79. def _check_app(self, app):
  80. receive_pack_handler_cls = app.handlers['git-receive-pack']
  81. caps = receive_pack_handler_cls.capabilities()
  82. self.assertFalse('side-band-64k' in caps)
  83. def _make_app(self, backend):
  84. app = make_wsgi_chain(backend, handlers=self._handlers())
  85. to_check = app
  86. # peel back layers until we're at the base application
  87. while not issubclass(to_check.__class__, HTTPGitApplication):
  88. to_check = to_check.app
  89. self._check_app(to_check)
  90. return app
  91. class SmartWebSideBand64kTestCase(SmartWebTestCase):
  92. """Test cases for smart HTTP server with side-band-64k support."""
  93. # side-band-64k in git-receive-pack was introduced in git 1.7.0.2
  94. min_git_version = (1, 7, 0, 2)
  95. def _handlers(self):
  96. return None # default handlers include side-band-64k
  97. def _check_app(self, app):
  98. receive_pack_handler_cls = app.handlers['git-receive-pack']
  99. caps = receive_pack_handler_cls.capabilities()
  100. self.assertTrue('side-band-64k' in caps)
  101. class DumbWebTestCase(WebTests, CompatTestCase):
  102. """Test cases for dumb HTTP server."""
  103. def _make_app(self, backend):
  104. return make_wsgi_chain(backend, dumb=True)
  105. def test_push_to_dulwich(self):
  106. # Note: remove this if dumb pushing is supported
  107. raise SkipTest('Dumb web pushing not supported.')