1
# Copyright (C) 2005-2013, 2016 Canonical Ltd
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.
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.
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
import SocketServer as socketserver
22
from ..mergeable import read_mergeable_from_url
23
from ..directory_service import directories
34
class TestReadMergeableFromUrl(tests.TestCaseWithTransport):
36
def test_read_mergeable_skips_local(self):
37
"""A local bundle named like the URL should not be read.
39
out, wt = test_read_bundle.create_bundle_file(self)
41
class FooService(object):
42
"""A directory service that always returns source"""
44
def look_up(self, name, url):
46
directories.register('foo:', FooService, 'Testing directory service')
47
self.addCleanup(directories.remove, 'foo:')
48
self.build_tree_contents([('./foo:bar', out.getvalue())])
49
self.assertRaises(errors.NotABundle, read_mergeable_from_url,
52
def test_infinite_redirects_are_not_a_bundle(self):
53
"""If a URL causes TooManyRedirections then NotABundle is raised.
55
from .blackbox.test_push import RedirectingMemoryServer
56
server = RedirectingMemoryServer()
57
self.start_server(server)
58
url = server.get_url() + 'infinite-loop'
59
self.assertRaises(errors.NotABundle, read_mergeable_from_url, url)
61
def test_smart_server_connection_reset(self):
62
"""If a smart server connection fails during the attempt to read a
63
bundle, then the ConnectionReset error should be propagated.
65
# Instantiate a server that will provoke a ConnectionReset
66
sock_server = DisconnectingServer()
67
self.start_server(sock_server)
68
# We don't really care what the url is since the server will close the
69
# connection without interpreting it
70
url = sock_server.get_url()
71
self.assertRaises(errors.ConnectionReset, read_mergeable_from_url, url)
74
class DisconnectingHandler(socketserver.BaseRequestHandler):
75
"""A request handler that immediately closes any connection made to it."""
81
class DisconnectingServer(test_server.TestingTCPServerInAThread):
84
super(DisconnectingServer, self).__init__(
86
test_server.TestingTCPServer,
90
"""Return the url of the server"""
91
return "bzr://%s:%d/" % self.server.server_address