Clean up file headers.
[jelmer/dulwich-libgit2.git] / dulwich / tests / compat / test_web.py
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
20 """Compatibility 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     DictBackend,
32     )
33 from dulwich.tests import (
34     TestSkipped,
35     )
36 from dulwich.web import (
37     HTTPGitApplication,
38     )
39
40 from server_utils import (
41     ServerTests,
42     ShutdownServerMixIn,
43     )
44 from utils import (
45     CompatTestCase,
46     )
47
48
49 if getattr(simple_server.WSGIServer, 'shutdown', None):
50     WSGIServer = simple_server.WSGIServer
51 else:
52     class WSGIServer(ShutdownServerMixIn, simple_server.WSGIServer):
53         """Subclass of WSGIServer that can be shut down."""
54
55         def __init__(self, *args, **kwargs):
56             # BaseServer is old-style so we have to call both __init__s
57             ShutdownServerMixIn.__init__(self)
58             simple_server.WSGIServer.__init__(self, *args, **kwargs)
59
60         serve = ShutdownServerMixIn.serve_forever
61
62
63 class WebTests(ServerTests):
64     """Base tests for web server tests.
65
66     Contains utility and setUp/tearDown methods, but does non inherit from
67     TestCase so tests are not automatically run.
68     """
69
70     protocol = 'http'
71
72     def _start_server(self, repo):
73         backend = DictBackend({'/': repo})
74         app = self._make_app(backend)
75         dul_server = simple_server.make_server('localhost', 0, app,
76                                                server_class=WSGIServer)
77         threading.Thread(target=dul_server.serve_forever).start()
78         self._server = dul_server
79         _, port = dul_server.socket.getsockname()
80         return port
81
82
83 class SmartWebTestCase(WebTests, CompatTestCase):
84     """Test cases for smart HTTP server."""
85
86     min_git_version = (1, 6, 6)
87
88     def setUp(self):
89         WebTests.setUp(self)
90         CompatTestCase.setUp(self)
91
92     def tearDown(self):
93         WebTests.tearDown(self)
94         CompatTestCase.tearDown(self)
95
96     def _make_app(self, backend):
97         return HTTPGitApplication(backend)
98
99
100 class DumbWebTestCase(WebTests, CompatTestCase):
101     """Test cases for dumb HTTP server."""
102
103     def setUp(self):
104         WebTests.setUp(self)
105         CompatTestCase.setUp(self)
106
107     def tearDown(self):
108         WebTests.tearDown(self)
109         CompatTestCase.tearDown(self)
110
111     def _make_app(self, backend):
112         return HTTPGitApplication(backend, dumb=True)
113
114     def test_push_to_dulwich(self):
115         # Note: remove this if dumb pushing is supported
116         raise TestSkipped('Dumb web pushing not supported.')