/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
2929.3.10 by Vincent Ladeuil
Add a fake https server and test facilities.
1
# Copyright (C) 2007 Canonical Ltd
2
#
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
7
#
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
# GNU General Public License for more details.
12
#
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
2929.3.12 by Vincent Ladeuil
Implement an https server passing the same tests than http. Except
17
"""HTTPS test server, available when ssl python module is available"""
18
19
from bzrlib.tests import (
20
    http_server,
21
    ssl_certs,
22
    )
23
2929.3.10 by Vincent Ladeuil
Add a fake https server and test facilities.
24
25
class TestingHTTPSServer(http_server.TestingHTTPServer):
2929.3.12 by Vincent Ladeuil
Implement an https server passing the same tests than http. Except
26
27
    def __init__(self, server_address, request_handler_class,
28
                 test_case_server, key_file, cert_file):
29
        http_server.TestingHTTPServer.__init__(
30
            self, server_address, request_handler_class, test_case_server)
31
        self.key_file = key_file
32
        self.cert_file = cert_file
33
34
    def get_request (self):
35
        """Get the request and client address from the socket.
36
37
        This is called in response to a connection issued to the server, we
38
        wrap the socket with SSL.
39
        """
40
        import ssl
41
        sock, addr = self.socket.accept()
42
        sslconn = ssl.wrap_socket(sock, server_side=True,
43
                                  keyfile=self.key_file,
44
                                  certfile=self.cert_file)
45
        return sslconn, addr
2929.3.10 by Vincent Ladeuil
Add a fake https server and test facilities.
46
47
48
class HTTPSServer(http_server.HttpServer):
49
50
    _url_protocol = 'https'
51
2929.3.12 by Vincent Ladeuil
Implement an https server passing the same tests than http. Except
52
    # Provides usable defaults since an https server requires both a
53
    # private key and certificate to work.
54
    def __init__(self, request_handler=http_server.TestingHTTPRequestHandler,
55
                 key_file=ssl_certs.build_path('server_without_pass.key'),
56
                 cert_file=ssl_certs.build_path('server.crt')):
57
        http_server.HttpServer.__init__(self, request_handler)
58
        self.key_file = key_file
59
        self.cert_file = cert_file
60
        self.temp_files = []
61
62
    def create_httpd(self):
63
        return TestingHTTPSServer((self.host, self.port), self.request_handler,
64
                                  self, self.key_file, self.cert_file)
65
2929.3.10 by Vincent Ladeuil
Add a fake https server and test facilities.
66
67
class HTTPSServer_urllib(HTTPSServer):
68
    """Subclass of HTTPSServer that gives https+urllib urls.
69
70
    This is for use in testing: connections to this server will always go
71
    through urllib where possible.
72
    """
73
74
    # urls returned by this server should require the urllib client impl
75
    _url_protocol = 'https+urllib'
2929.3.12 by Vincent Ladeuil
Implement an https server passing the same tests than http. Except
76