Skip to content
This repository has been archived by the owner on May 28, 2022. It is now read-only.

Commit

Permalink
Get tests passing again for SSL, and partially passing for URL-relate…
Browse files Browse the repository at this point in the history
…d changes in Twisted trunk (pre-15.5).
  • Loading branch information
posita committed Oct 27, 2015
1 parent 1f18caa commit 8cf0290
Show file tree
Hide file tree
Showing 3 changed files with 141 additions and 87 deletions.
34 changes: 16 additions & 18 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,38 +20,36 @@ install:
# without a fix
env:
- TOX_ENV=py27 TWISTED='Twisted == 15.3'
- TOX_ENV=py27 TWISTED='Twisted == 15.3' OPENSSL=pyOpenSSL CRYPTOGRAPHY=cryptography
- TOX_ENV=py27 TWISTED='Twisted == 15.4'
- TOX_ENV=py27 TWISTED=svn+svn://svn.twistedmatrix.com/svn/Twisted/trunk

- TOX_ENV=py27 OPENSSL=pyOpenSSL CRYPTOGRAPHY=cryptography TWISTED='Twisted == 15.3'
- TOX_ENV=py27 OPENSSL=pyOpenSSL CRYPTOGRAPHY=cryptography TWISTED='Twisted == 15.4'
- TOX_ENV=py27 OPENSSL=pyOpenSSL CRYPTOGRAPHY=cryptography TWISTED=svn+svn://svn.twistedmatrix.com/svn/Twisted/trunk
- TOX_ENV=py27 TWISTED='Twisted == 15.4' OPENSSL=pyOpenSSL CRYPTOGRAPHY=cryptography

- TOX_ENV=pypy TWISTED='Twisted == 15.3'
- TOX_ENV=pypy TWISTED='Twisted == 15.3' OPENSSL=pyOpenSSL CRYPTOGRAPHY='cryptography < 1.0'
- TOX_ENV=pypy TWISTED='Twisted == 15.4'
- TOX_ENV=pypy TWISTED=svn+svn://svn.twistedmatrix.com/svn/Twisted/trunk

- TOX_ENV=pypy OPENSSL=pyOpenSSL CRYPTOGRAPHY='cryptography < 1.0' TWISTED='Twisted == 15.3'
- TOX_ENV=pypy OPENSSL=pyOpenSSL CRYPTOGRAPHY='cryptography < 1.0' TWISTED='Twisted == 15.4'
- TOX_ENV=pypy OPENSSL=pyOpenSSL CRYPTOGRAPHY='cryptography < 1.0' TWISTED=svn+svn://svn.twistedmatrix.com/svn/Twisted/trunk
- TOX_ENV=pypy TWISTED='Twisted == 15.4' OPENSSL=pyOpenSSL CRYPTOGRAPHY='cryptography < 1.0'

matrix:
allow_failures:
- env: TOX_ENV=py27 TWISTED=svn+svn://svn.twistedmatrix.com/svn/Twisted/trunk
- env: TOX_ENV=py27 TWISTED=svn+svn://svn.twistedmatrix.com/svn/Twisted/trunk OPENSSL=pyOpenSSL CRYPTOGRAPHY=cryptography

- env: TOX_ENV=pypy TWISTED=svn+svn://svn.twistedmatrix.com/svn/Twisted/trunk
- env: TOX_ENV=pypy TWISTED=svn+svn://svn.twistedmatrix.com/svn/Twisted/trunk OPENSSL=pyOpenSSL CRYPTOGRAPHY='cryptography < 1.0'

- env: TOX_ENV=py33 TWISTED='Twisted == 15.3'
- env: TOX_ENV=py33 TWISTED='Twisted == 15.3' OPENSSL=pyOpenSSL CRYPTOGRAPHY=cryptography
- env: TOX_ENV=py33 TWISTED='Twisted == 15.4'
- env: TOX_ENV=py33 TWISTED='Twisted == 15.4' OPENSSL=pyOpenSSL CRYPTOGRAPHY=cryptography
- env: TOX_ENV=py33 TWISTED=svn+svn://svn.twistedmatrix.com/svn/Twisted/trunk

- env: TOX_ENV=py33 OPENSSL=pyOpenSSL CRYPTOGRAPHY=cryptography TWISTED='Twisted == 15.3'
- env: TOX_ENV=py33 OPENSSL=pyOpenSSL CRYPTOGRAPHY=cryptography TWISTED='Twisted == 15.4'
- env: TOX_ENV=py33 OPENSSL=pyOpenSSL CRYPTOGRAPHY=cryptography TWISTED=svn+svn://svn.twistedmatrix.com/svn/Twisted/trunk
- env: TOX_ENV=py33 TWISTED=svn+svn://svn.twistedmatrix.com/svn/Twisted/trunk OPENSSL=pyOpenSSL CRYPTOGRAPHY=cryptography

- env: TOX_ENV=py34 TWISTED='Twisted == 15.3'
- env: TOX_ENV=py34 TWISTED='Twisted == 15.3' OPENSSL=pyOpenSSL CRYPTOGRAPHY=cryptography
- env: TOX_ENV=py34 TWISTED='Twisted == 15.4'
- env: TOX_ENV=py34 TWISTED='Twisted == 15.4' OPENSSL=pyOpenSSL CRYPTOGRAPHY=cryptography
- env: TOX_ENV=py34 TWISTED=svn+svn://svn.twistedmatrix.com/svn/Twisted/trunk

- env: TOX_ENV=py34 OPENSSL=pyOpenSSL CRYPTOGRAPHY=cryptography TWISTED='Twisted == 15.3'
- env: TOX_ENV=py34 OPENSSL=pyOpenSSL CRYPTOGRAPHY=cryptography TWISTED='Twisted == 15.4'
- env: TOX_ENV=py34 OPENSSL=pyOpenSSL CRYPTOGRAPHY=cryptography TWISTED=svn+svn://svn.twistedmatrix.com/svn/Twisted/trunk
- env: TOX_ENV=py34 TWISTED=svn+svn://svn.twistedmatrix.com/svn/Twisted/trunk OPENSSL=pyOpenSSL CRYPTOGRAPHY=cryptography

script:
- tox -e "${TOX_ENV}"
Expand Down
127 changes: 79 additions & 48 deletions tests/test_endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@
import logging
import sys
import unittest
import twisted
from twisted.internet import endpoints as t_endpoints
from twisted.python import versions as t_versions
from twisted.trial import unittest as t_unittest
from twisted.web import (
client as t_client,
Expand All @@ -48,6 +50,8 @@

_LOGGER = logging.getLogger(__name__)

_TWISTED_15_5 = t_versions.Version('twisted', 15, 5, 0)

#---- Classes ------------------------------------------------------------

#=========================================================================
Expand All @@ -63,12 +67,16 @@ def setUp(self):
def tearDown(self):
super().tearDown()

@unittest.skipIf(sys.version_info >= ( 3, 0 ), 'URLPath is not bytes friendly; see <https://twistedmatrix.com/trac/ticket/7994>')
@unittest.skipIf(sys.version_info >= ( 3, 0 ) and twisted.version < _TWISTED_15_5, 'URLPath is not bytes friendly; see <https://twistedmatrix.com/trac/ticket/7994>')
def test_child(self):
base = BaseUrl.fromString(b'http://localhost/~xyz?foo=bar')
base = BaseUrl.fromBytes(b'http://localhost/~xyz?foo=bar')
sub = base.child(b'')
self.assertIs(type(sub), BaseUrl)
self.assertEqual(sub.path, b'~xyz/')

if twisted.version < _TWISTED_15_5:
self.assertEqual(sub.path, b'~xyz/')
else:
self.assertEqual(sub.path, b'%7Exyz/')

sub = base.child(b'/')
self.assertEqual(sub.path, b'/')
Expand All @@ -77,15 +85,24 @@ def test_child(self):
self.assertEqual(sub.path, b'/abc')

def test_join(self):
base = BaseUrl.fromString(b'http://localhost/~xyz?foo=bar')
self.assertEqual(base.path, b'/~xyz')
base = BaseUrl.fromBytes(b'http://localhost/~xyz?foo=bar')

if twisted.version < _TWISTED_15_5:
self.assertEqual(base.path, b'/~xyz')
else:
self.assertEqual(base.path, b'/%7Exyz')

self.assertEqual(base.query, b'foo=bar')

p = ( b'', )

sub = base.join(*p)
self.assertIs(type(sub), BaseUrl)
self.assertEqual(sub.path, b'/~xyz/')

if twisted.version < _TWISTED_15_5:
self.assertEqual(sub.path, b'/~xyz/')
else:
self.assertEqual(sub.path, b'/%7Exyz/')

self.assertEqual(sub.query, b'')

subquery = base.joinquery(*p)
Expand All @@ -94,52 +111,66 @@ def test_join(self):
self.assertEqual(subquery.query, base.query)

p = ( b'', b'', b'' )

sub = base.join(*p)
self.assertEqual(sub.path, b'/~xyz/')

if twisted.version < _TWISTED_15_5:
self.assertEqual(sub.path, b'/~xyz/')
else:
self.assertEqual(sub.path, b'/%7Exyz/')

subquery = base.joinquery(*p)
self.assertEqual(subquery.path, sub.path)
self.assertEqual(subquery.query, base.query)

p = ( b'abc', )

sub = base.join(*p)
self.assertEqual(sub.path, b'/~xyz/abc')

if twisted.version < _TWISTED_15_5:
self.assertEqual(sub.path, b'/~xyz/abc')
else:
self.assertEqual(sub.path, b'/%7Exyz/abc')

subquery = base.joinquery(*p)
self.assertEqual(subquery.path, sub.path)
self.assertEqual(subquery.query, base.query)

p = ( b'abc', b'' )

sub = base.join(*p)
self.assertEqual(sub.path, b'/~xyz/abc/')

if twisted.version < _TWISTED_15_5:
self.assertEqual(sub.path, b'/~xyz/abc/')
else:
self.assertEqual(sub.path, b'/%7Exyz/abc/')

subquery = base.joinquery(*p)
self.assertEqual(subquery.path, sub.path)
self.assertEqual(subquery.query, base.query)

p = ( b'abc', b'', b'', b'' )

sub = base.join(*p)
self.assertEqual(sub.path, b'/~xyz/abc/')

if twisted.version < _TWISTED_15_5:
self.assertEqual(sub.path, b'/~xyz/abc/')
else:
self.assertEqual(sub.path, b'/%7Exyz/abc/')

subquery = base.joinquery(*p)
self.assertEqual(subquery.path, sub.path)
self.assertEqual(subquery.query, base.query)

p = ( b'abc', b'', b'', b'', b'def' )

sub = base.join(*p)
self.assertEqual(sub.path, b'/~xyz/abc/def')

if twisted.version < _TWISTED_15_5:
self.assertEqual(sub.path, b'/~xyz/abc/def')
else:
self.assertEqual(sub.path, b'/%7Exyz/abc/def')

subquery = base.joinquery(*p)
self.assertEqual(subquery.path, sub.path)
self.assertEqual(subquery.query, base.query)

p = ( b'/abc', )

sub = base.join(*p)
self.assertEqual(sub.path, b'/abc')

Expand Down Expand Up @@ -198,7 +229,7 @@ def test_parsenetloc(self):
self.assertRaises(NetLocParseError, parsenetloc, netloc)

def test_scheme(self):
base = BaseUrl.fromString(b'http://localhost/~xyz')
base = BaseUrl.fromBytes(b'http://localhost/~xyz')
self.assertEqual(base.scheme, b'http')

sub = base.asscheme(b'https')
Expand All @@ -207,10 +238,10 @@ def test_scheme(self):
self.assertEqual(sub.__str__(), b'https://localhost/~xyz')

def test_uri(self):
urlbytes = b'http://localhost/cgi-bin/test?key=val#name'
uri = t_client.URI.fromBytes(urlbytes)
url_bytes = b'http://localhost/cgi-bin/test?key=val#name'
uri = t_client.URI.fromBytes(url_bytes)
base_url = BaseUrl.fromURI(uri)
self.assertEqual(base_url.unsplit(), urlbytes)
self.assertEqual(base_url.unsplit(), url_bytes)
self.assertEqual(base_url.toURI().toBytes(), uri.toBytes())

#=========================================================================
Expand All @@ -230,23 +261,23 @@ def test_endpointbadscheme(self):
from twisted.internet import reactor
factory = ClientEndpointFactory(reactor)

urlbytes = b'homingpigeon://tweets.socket.io/engine.io/?EIO=3&transport=polling'
url = BaseUrl.fromString(urlbytes)
url_bytes = b'homingpigeon://tweets.socket.io/engine.io/?EIO=3&transport=polling'
url = BaseUrl.fromBytes(url_bytes)
self.assertRaises(t_error.SchemeNotSupported, factory.endpointForURI, url)

def test_endpointforhttp(self):
from twisted.internet import reactor
factory = ClientEndpointFactory(reactor)

urlbytes = b'http://tweets.socket.io/engine.io/?EIO=3&transport=polling'
url = BaseUrl.fromString(urlbytes)
url_bytes = b'http://tweets.socket.io/engine.io/?EIO=3&transport=polling'
url = BaseUrl.fromBytes(url_bytes)
endpoint = factory.endpointForURI(url)
self.assertIsInstance(endpoint, t_endpoints.HostnameEndpoint)
self.assertEqual(endpoint._host, b'tweets.socket.io') # pylint: disable=protected-access
self.assertEqual(endpoint._port, 80) # pylint: disable=protected-access

urlbytes = b'http://tweets.socket.io:54321/engine.io/?EIO=3&transport=polling'
url = BaseUrl.fromString(urlbytes)
url_bytes = b'http://tweets.socket.io:54321/engine.io/?EIO=3&transport=polling'
url = BaseUrl.fromBytes(url_bytes)
endpoint = factory.endpointForURI(url)
self.assertIsInstance(endpoint, t_endpoints.HostnameEndpoint)
self.assertEqual(endpoint._host, b'tweets.socket.io') # pylint: disable=protected-access
Expand All @@ -257,19 +288,19 @@ def test_endpointforhttps(self):
from twisted.internet import reactor
factory = ClientEndpointFactory(reactor)

urlbytes = b'https://tweets.socket.io/engine.io/?EIO=3&transport=polling'
url = BaseUrl.fromString(urlbytes)
url_bytes = b'https://tweets.socket.io/engine.io/?EIO=3&transport=polling'
url = BaseUrl.fromBytes(url_bytes)
endpoint = factory.endpointForURI(url)
self.assertIsInstance(endpoint, t_endpoints.TLSWrapperClientEndpoint)
self.assertIsInstance(endpoint.wrappedEndpoint, t_client.HostnameEndpoint)
self.assertIsInstance(endpoint.wrappedEndpoint, t_endpoints.HostnameEndpoint)
self.assertEqual(endpoint.wrappedEndpoint._host, b'tweets.socket.io') # pylint: disable=protected-access
self.assertEqual(endpoint.wrappedEndpoint._port, 443) # pylint: disable=protected-access

urlbytes = b'https://tweets.socket.io:54321/engine.io/?EIO=3&transport=polling'
url = BaseUrl.fromString(urlbytes)
url_bytes = b'https://tweets.socket.io:54321/engine.io/?EIO=3&transport=polling'
url = BaseUrl.fromBytes(url_bytes)
endpoint = factory.endpointForURI(url)
self.assertIsInstance(endpoint, t_endpoints.TLSWrapperClientEndpoint)
self.assertIsInstance(endpoint.wrappedEndpoint, t_client.HostnameEndpoint)
self.assertIsInstance(endpoint.wrappedEndpoint, t_endpoints.HostnameEndpoint)
self.assertEqual(endpoint.wrappedEndpoint._host, b'tweets.socket.io') # pylint: disable=protected-access
self.assertEqual(endpoint.wrappedEndpoint._port, 54321) # pylint: disable=protected-access

Expand All @@ -278,25 +309,25 @@ def test_endpointfornohttps(self):
from twisted.internet import reactor
factory = ClientEndpointFactory(reactor)

urlbytes = b'https://tweets.socket.io/engine.io/?EIO=3&transport=polling'
url = BaseUrl.fromString(urlbytes)
url_bytes = b'https://tweets.socket.io/engine.io/?EIO=3&transport=polling'
url = BaseUrl.fromBytes(url_bytes)
self.assertRaises(t_error.SchemeNotSupported, factory.endpointForURI, url)

@unittest.skipIf(hasattr(t_endpoints, 'TLSWrapperClientEndpoint'), 'OpenSSL is installed')
def test_endpointfornowss(self):
from twisted.internet import reactor
factory = ClientEndpointFactory(reactor)

urlbytes = b'wss://tweets.socket.io/engine.io/?EIO=3&transport=polling'
url = BaseUrl.fromString(urlbytes)
url_bytes = b'wss://tweets.socket.io/engine.io/?EIO=3&transport=polling'
url = BaseUrl.fromBytes(url_bytes)
self.assertRaises(t_error.SchemeNotSupported, factory.endpointForURI, url)

def test_endpointforunix(self):
from twisted.internet import reactor
factory = ClientEndpointFactory(reactor)

urlbytes = b'unix://.%2Ftests%2Fnode%2Fhttp.sock/engine.io/?EIO=3&transport=polling'
url = BaseUrl.fromString(urlbytes)
url_bytes = b'unix://.%2Ftests%2Fnode%2Fhttp.sock/engine.io/?EIO=3&transport=polling'
url = BaseUrl.fromBytes(url_bytes)
endpoint = factory.endpointForURI(url)
self.assertIsInstance(endpoint, t_endpoints.UNIXClientEndpoint)
self.assertEqual(endpoint._path.encode('utf_8'), b'./tests/node/http.sock') # pylint: disable=protected-access
Expand All @@ -305,15 +336,15 @@ def test_endpointforws(self):
from twisted.internet import reactor
factory = ClientEndpointFactory(reactor)

urlbytes = b'ws://tweets.socket.io/engine.io/?EIO=3&transport=polling'
url = BaseUrl.fromString(urlbytes)
url_bytes = b'ws://tweets.socket.io/engine.io/?EIO=3&transport=polling'
url = BaseUrl.fromBytes(url_bytes)
endpoint = factory.endpointForURI(url)
self.assertIsInstance(endpoint, t_endpoints.HostnameEndpoint)
self.assertEqual(endpoint._host, b'tweets.socket.io') # pylint: disable=protected-access
self.assertEqual(endpoint._port, 80) # pylint: disable=protected-access

urlbytes = b'ws://tweets.socket.io:12345/engine.io/?EIO=3&transport=polling'
url = BaseUrl.fromString(urlbytes)
url_bytes = b'ws://tweets.socket.io:12345/engine.io/?EIO=3&transport=polling'
url = BaseUrl.fromBytes(url_bytes)
endpoint = factory.endpointForURI(url)
self.assertIsInstance(endpoint, t_endpoints.HostnameEndpoint)
self.assertEqual(endpoint._host, b'tweets.socket.io') # pylint: disable=protected-access
Expand All @@ -324,16 +355,16 @@ def test_endpointforwss(self):
from twisted.internet import reactor
factory = ClientEndpointFactory(reactor)

urlbytes = b'wss://tweets.socket.io/engine.io/?EIO=3&transport=polling'
url = BaseUrl.fromString(urlbytes)
url_bytes = b'wss://tweets.socket.io/engine.io/?EIO=3&transport=polling'
url = BaseUrl.fromBytes(url_bytes)
endpoint = factory.endpointForURI(url)
self.assertIsInstance(endpoint, t_endpoints.TLSWrapperClientEndpoint)
self.assertIsInstance(endpoint.wrappedEndpoint, t_endpoints.HostnameEndpoint)
self.assertEqual(endpoint.wrappedEndpoint._host, b'tweets.socket.io') # pylint: disable=protected-access
self.assertEqual(endpoint.wrappedEndpoint._port, 443) # pylint: disable=protected-access

urlbytes = b'wss://tweets.socket.io:54321/engine.io/?EIO=3&transport=polling'
url = BaseUrl.fromString(urlbytes)
url_bytes = b'wss://tweets.socket.io:54321/engine.io/?EIO=3&transport=polling'
url = BaseUrl.fromBytes(url_bytes)
endpoint = factory.endpointForURI(url)
self.assertIsInstance(endpoint, t_endpoints.TLSWrapperClientEndpoint)
self.assertIsInstance(endpoint.wrappedEndpoint, t_endpoints.HostnameEndpoint)
Expand Down
Loading

0 comments on commit 8cf0290

Please sign in to comment.