test_web.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. HTTPGitApplication,
  34. HTTPGitRequestHandler,
  35. )
  36. from dulwich.tests.compat.server_utils import (
  37. ServerTests,
  38. ShutdownServerMixIn,
  39. NoSideBand64kReceivePackHandler,
  40. )
  41. from dulwich.tests.compat.utils import (
  42. CompatTestCase,
  43. )
  44. if getattr(simple_server.WSGIServer, 'shutdown', None):
  45. WSGIServer = simple_server.WSGIServer
  46. else:
  47. class WSGIServer(ShutdownServerMixIn, simple_server.WSGIServer):
  48. """Subclass of WSGIServer that can be shut down."""
  49. def __init__(self, *args, **kwargs):
  50. # BaseServer is old-style so we have to call both __init__s
  51. ShutdownServerMixIn.__init__(self)
  52. simple_server.WSGIServer.__init__(self, *args, **kwargs)
  53. serve = ShutdownServerMixIn.serve_forever
  54. class WebTests(ServerTests):
  55. """Base tests for web server tests.
  56. Contains utility and setUp/tearDown methods, but does non inherit from
  57. TestCase so tests are not automatically run.
  58. """
  59. protocol = 'http'
  60. def _start_server(self, repo):
  61. backend = DictBackend({'/': repo})
  62. app = self._make_app(backend)
  63. dul_server = simple_server.make_server(
  64. 'localhost', 0, app, server_class=WSGIServer,
  65. handler_class=HTTPGitRequestHandler)
  66. threading.Thread(target=dul_server.serve_forever).start()
  67. self._server = dul_server
  68. _, port = dul_server.socket.getsockname()
  69. return port
  70. class SmartWebTestCase(WebTests, CompatTestCase):
  71. """Test cases for smart HTTP server.
  72. This server test case does not use side-band-64k in git-receive-pack.
  73. """
  74. min_git_version = (1, 6, 6)
  75. def setUp(self):
  76. WebTests.setUp(self)
  77. CompatTestCase.setUp(self)
  78. def tearDown(self):
  79. WebTests.tearDown(self)
  80. CompatTestCase.tearDown(self)
  81. def _handlers(self):
  82. return {'git-receive-pack': NoSideBand64kReceivePackHandler}
  83. def _check_app(self, app):
  84. receive_pack_handler_cls = app.handlers['git-receive-pack']
  85. caps = receive_pack_handler_cls.capabilities()
  86. self.assertFalse('side-band-64k' in caps)
  87. def _make_app(self, backend):
  88. app = HTTPGitApplication(backend, handlers=self._handlers())
  89. self._check_app(app)
  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 setUp(self):
  104. WebTests.setUp(self)
  105. CompatTestCase.setUp(self)
  106. def tearDown(self):
  107. WebTests.tearDown(self)
  108. CompatTestCase.tearDown(self)
  109. def _make_app(self, backend):
  110. return HTTPGitApplication(backend, dumb=True)
  111. def test_push_to_dulwich(self):
  112. # Note: remove this if dumb pushing is supported
  113. raise SkipTest('Dumb web pushing not supported.')