c30b40045e715a3ca00c0408811baf1c4ea96e19
[jelmer/dulwich-libgit2.git] / dulwich / tests / compat / test_web.py
1 # test_web.py -- Compatibilty 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
20 """Compatibilty tests between Dulwich and the cgit HTTP server.
21
22 Warning: these tests should be fairly stable, but when writing/debugging new
23 tests, deadlocks may freeze the test process such that it cannot be Ctrl-C'ed.
24 On *nix, you can kill the tests with Ctrl-Z, "kill %".
25 """
26
27 import threading
28 from wsgiref import simple_server
29
30 from dulwich.server import (
31     GitBackend,
32     )
33 from dulwich.web import (
34     HTTPGitApplication,
35     )
36
37 from server_utils import (
38     ServerTests,
39     ShutdownServerMixIn,
40     )
41 from utils import (
42     CompatTestCase,
43     SkipTest,
44     )
45
46
47 if getattr(simple_server.WSGIServer, 'shutdown', None):
48     WSGIServer = simple_server.WSGIServer
49 else:
50     class WSGIServer(ShutdownServerMixIn, simple_server.WSGIServer):
51         """Subclass of WSGIServer that can be shut down."""
52
53         def __init__(self, *args, **kwargs):
54             # BaseServer is old-style so we have to call both __init__s
55             ShutdownServerMixIn.__init__(self)
56             simple_server.WSGIServer.__init__(self, *args, **kwargs)
57
58         serve = ShutdownServerMixIn.serve_forever
59
60
61 class WebTests(ServerTests):
62     """Base tests for web server tests.
63
64     Contains utility and setUp/tearDown methods, but does non inherit from
65     TestCase so tests are not automatically run.
66     """
67
68     protocol = 'http'
69
70     def _start_server(self, repo):
71         app = self._make_app(GitBackend(repo))
72         dul_server = simple_server.make_server('localhost', 0, app,
73                                                server_class=WSGIServer)
74         threading.Thread(target=dul_server.serve_forever).start()
75         self._server = dul_server
76         _, port = dul_server.socket.getsockname()
77         return port
78
79
80 class SmartWebTestCase(WebTests, CompatTestCase):
81     """Test cases for smart HTTP server."""
82
83     min_git_version = (1, 6, 6)
84
85     def setUp(self):
86         WebTests.setUp(self)
87         CompatTestCase.setUp(self)
88
89     def tearDown(self):
90         WebTests.tearDown(self)
91         CompatTestCase.tearDown(self)
92
93     def _make_app(self, backend):
94         return HTTPGitApplication(backend)
95
96     def test_push_to_dulwich(self):
97         # TODO(dborowitz): enable after merging thin pack fixes.
98         raise SkipTest('Skipping push test due to known pack bug.')
99
100
101 class DumbWebTestCase(WebTests, CompatTestCase):
102     """Test cases for dumb HTTP server."""
103
104     def setUp(self):
105         WebTests.setUp(self)
106         CompatTestCase.setUp(self)
107
108     def tearDown(self):
109         WebTests.tearDown(self)
110         CompatTestCase.tearDown(self)
111
112     def _make_app(self, backend):
113         return HTTPGitApplication(backend, dumb=True)
114
115     def test_push_to_dulwich(self):
116         # Note: remove this if dumb pushing is supported
117         raise SkipTest('Dumb web pushing not supported.')