Provide reusable code to create a safe twisted tcp server for tests
Affects | Status | Importance | Assigned to | Milestone | ||
---|---|---|---|---|---|---|
ubuntuone-dev-tools | Status tracked in Trunk | |||||
Trunk |
Fix Committed
|
Low
|
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
"""A save protocol to be used in tests."""
protocolIns
# pylint: disable=C0103
def connectionMade(
"""Keep track of the given protocol."""
class SaveSite(
"""A site that let us know when it's closed."""
protocol = SaveHTTPChannel
def __init__(self, *args, **kwargs):
"""Create a new instance."""
# we disable the timeout in the tests, we will deal with it manually.
# pylint: disable=C0103
class BaseMockWebServ
"""A mock webserver for testing"""
def __init__(self):
"""Start up this instance."""
self.root = self.get_
self.site = SaveSite(self.root)
application = service.
#pylint: disable=E1101
def get_dummy_
"""Path pointing at the dummy certificate files."""
base_path = path.dirname(
return path.join(
def get_context(self):
"""Return an ssl context."""
key_path = self.get_
cert_path = self.get_
return ssl.DefaultOpen
def get_root_
"""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.
return u"http://
def get_ssl_iri(self):
"""Build the iri for the ssl mock server."""
#pylint: disable=W0212
port_num = self.sslserver.
return u"https:/
def stop(self):
"""Shut it down."""
#pylint: disable=E1101
if self.site.
return self.service_
class SimpleResource(
"""A simple web resource."""
def __init__(self):
def render_GET(self, request):
"""Make a bit of html out of the resource's content."""
if not self.rendered.
return SAMPLE_CONTENT
class MockWebServer(
"""A mock webserver."""
def __init__(self):
def get_root_
"""Get the root resource with all the children."""
root = resource.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 |
summary: |
- Provide reusable code to create a twisted save server for tests + Provide reusable code to create a safe twisted tcp server for tests |