Provide reusable code to create a safe twisted tcp server for tests

Bug #944125 reported by Manuel de la Peña on 2012-03-01
This bug affects 1 person
Affects Status Importance Assigned to Milestone
Status tracked in Trunk
Manuel de la Peña

Bug Description

During the development of the proxy code we have been writing several times the same server code to fake a connection. The common code looks like the following:

SAMPLE_CONTENT = "hello world!"
SIMPLERESOURCE = "simpleresource"
DUMMY_KEY_FILENAME = "dummy.key"
DUMMY_CERT_FILENAME = "dummy.cert"

class SaveHTTPChannel(http.HTTPChannel):
    """A save protocol to be used in tests."""

    protocolInstance = None

    # pylint: disable=C0103
    def connectionMade(self):
        """Keep track of the given protocol."""
        SaveHTTPChannel.protocolInstance = self

class SaveSite(server.Site):
    """A site that let us know when it's closed."""

    protocol = SaveHTTPChannel

    def __init__(self, *args, **kwargs):
        """Create a new instance."""
        server.Site.__init__(self, *args, **kwargs)
        # we disable the timeout in the tests, we will deal with it manually.
        # pylint: disable=C0103
        self.timeOut = None

class BaseMockWebServer(object):
    """A mock webserver for testing"""

    def __init__(self):
        """Start up this instance."""
        self.root = self.get_root_resource() = SaveSite(self.root)
        application = service.Application('web')
        self.service_collection = service.IServiceCollection(application)
        #pylint: disable=E1101
        self.tcpserver = internet.TCPServer(0,
        self.sslserver = internet.SSLServer(0,, self.get_context())

    def get_dummy_path(self, filename):
        """Path pointing at the dummy certificate files."""
        base_path = path.dirname(__file__)
        return path.join(base_path, "ssl", filename)

    def get_context(self):
        """Return an ssl context."""
        key_path = self.get_dummy_path(DUMMY_KEY_FILENAME)
        cert_path = self.get_dummy_path(DUMMY_CERT_FILENAME)
        return ssl.DefaultOpenSSLContextFactory(key_path, cert_path)

    def get_root_resource(self):
        """Get the root resource with all the children."""
        raise NotImplementedError

    def get_iri(self):
        """Build the iri for this mock server."""
        #pylint: disable=W0212
        port_num = self.tcpserver._port.getHost().port
        return u"" % port_num

    def get_ssl_iri(self):
        """Build the iri for the ssl mock server."""
        #pylint: disable=W0212
        port_num = self.sslserver._port.getHost().port
        return u"" % port_num

   def stop(self):
        """Shut it down."""
        #pylint: disable=E1101
        return self.service_collection.stopService()

class SimpleResource(resource.Resource):
    """A simple web resource."""

    def __init__(self):
        """Initialize this mock resource."""
        self.rendered = defer.Deferred()

    def render_GET(self, request):
        """Make a bit of html out of the resource's content."""
        if not self.rendered.called:
        return SAMPLE_CONTENT

class MockWebServer(BaseMockWebServer):
    """A mock webserver."""

    def __init__(self):
        """Initialize this mock server."""
        self.simple_resource = SimpleResource()
        super(MockWebServer, self).__init__()

    def get_root_resource(self):
        """Get the root resource with all the children."""
        root = resource.Resource()
        root.putChild(SIMPLERESOURCE, self.simple_resource)
        return root

It makes sense that we move the code to this more common project.

Changed in ubuntuone-dev-tools:
assignee: nobody → Ubuntu One Desktop+ team (ubuntuone-desktop+)
importance: Undecided → Low
status: New → Triaged
Rodney Dawes (dobey) on 2012-04-18
summary: - Provide reusable code to create a twisted save server for tests
+ Provide reusable code to create a safe twisted tcp server for tests
To post a comment you must log in.
This report contains Public information  Edit
Everyone can see this information.

Other bug subscribers