Skip to content
Permalink
Browse files

Fixing tests.

- Prefer urllib fetcher to pycurl, as the latter refuses to work
  (and we definitely don't want to fix that, too)
- Don't decode received payload into a string, as an xml parser
  wants bytes.
- Fix str/bytes discrepancies everywhere.
  • Loading branch information...
isagalaev committed Jul 15, 2014
1 parent 00fb482 commit 020c1e137253a8a9b4dde947af3a635e2e5e16f5
@@ -49,15 +49,8 @@ def fetch(url, body=None, headers=None):


def createHTTPFetcher():
"""Create a default HTTP fetcher instance
prefers Curl to urllib2."""
if pycurl is None:
fetcher = Urllib2Fetcher()
else:
fetcher = CurlHTTPFetcher()

return fetcher
"""Create a default HTTP fetcher instance."""
return Urllib2Fetcher()

# Contains the currently set HTTP fetcher. If it is set to None, the
# library will call createHTTPFetcher() to set it. Do not access this
@@ -256,17 +249,6 @@ def _makeResponse(self, urllib2_response):
else:
resp.status = 200

_, extra_dict = self._parseHeaderValue(
resp.headers.get("content-type", ""))
# Try to decode the response body to a string, if there's a
# charset known; fall back to ISO-8859-1 otherwise, since that's
# what's suggested in HTTP/1.1
charset = extra_dict.get('charset', 'latin1')
try:
resp.body = resp.body.decode(charset)
except Exception:
pass

return resp

def _lowerCaseKeys(self, headers_dict):
@@ -32,7 +32,7 @@ def simpleOpenIDTransformer(endpoint):

class TestServiceParser(unittest.TestCase):
def setUp(self):
with open(XRD_FILE) as f:
with open(XRD_FILE, 'rb') as f:
self.xmldoc = f.read()
self.yadis_url = 'http://unittest.url/'

@@ -104,7 +104,7 @@ def testGetSeveralForOne(self):
def testNoXRDS(self):
"""Make sure that we get an exception when an XRDS element is
not present"""
with open(NOXRDS_FILE) as f:
with open(NOXRDS_FILE, 'rb') as f:
self.xmldoc = f.read()
self.assertRaises(
etxrd.XRDSError,
@@ -121,7 +121,7 @@ def testEmpty(self):
def testNoXRD(self):
"""Make sure that we get an exception when there is no XRD
element present."""
with open(NOXRD_FILE) as f:
with open(NOXRD_FILE, 'rb') as f:
self.xmldoc = f.read()
self.assertRaises(
etxrd.XRDSError,
@@ -137,7 +137,7 @@ def mkTest(iname, filename, expectedID):
filename = datapath(filename)

def test(self):
with open(filename) as f:
with open(filename, 'rb') as f:
xrds = etxrd.parseXRDS(f.read())
self._getCanonicalID(iname, xrds, expectedID)
return test
@@ -40,11 +40,11 @@ def geturl(path):
def plain(path, code):
path = '/' + path
expected = fetchers.HTTPResponse(
geturl(path), code, expected_headers, path)
geturl(path), code, expected_headers, path.encode('utf-8'))
return (path, expected)

expect_success = fetchers.HTTPResponse(
geturl('/success'), 200, expected_headers, '/success')
geturl('/success'), 200, expected_headers, b'/success')
cases = [
('/success', expect_success),
('/301redirect', expect_success),
@@ -97,7 +97,6 @@ def run_fetcher_tests(server):
exc_fetchers = []
for klass, library_name in [
(fetchers.Urllib2Fetcher, 'urllib2'),
(fetchers.CurlHTTPFetcher, 'pycurl'),
(fetchers.HTTPLib2Fetcher, 'httplib2'),
]:
try:
@@ -6,7 +6,6 @@


XRDS_BOILERPLATE = '''\
<?xml version="1.0" encoding="UTF-8"?>
<xrds:XRDS xmlns:xrds="xri://$xrds"
xmlns="xri://$xrd*($v*2.0)"
xmlns:openid="http://openid.net/xmlns/1.0">
@@ -77,7 +77,6 @@ def test_badXML(self):

def test_noEntries(self):
self.failUnlessXRDSHasReturnURLs('''\
<?xml version="1.0" encoding="UTF-8"?>
<xrds:XRDS xmlns:xrds="xri://$xrds"
xmlns="xri://$xrd*($v*2.0)"
>
@@ -88,7 +87,6 @@ def test_noEntries(self):

def test_noReturnToEntries(self):
self.failUnlessXRDSHasReturnURLs('''\
<?xml version="1.0" encoding="UTF-8"?>
<xrds:XRDS xmlns:xrds="xri://$xrds"
xmlns="xri://$xrd*($v*2.0)"
>
@@ -103,7 +101,6 @@ def test_noReturnToEntries(self):

def test_oneEntry(self):
self.failUnlessXRDSHasReturnURLs('''\
<?xml version="1.0" encoding="UTF-8"?>
<xrds:XRDS xmlns:xrds="xri://$xrds"
xmlns="xri://$xrd*($v*2.0)"
>
@@ -118,7 +115,6 @@ def test_oneEntry(self):

def test_twoEntries(self):
self.failUnlessXRDSHasReturnURLs('''\
<?xml version="1.0" encoding="UTF-8"?>
<xrds:XRDS xmlns:xrds="xri://$xrds"
xmlns="xri://$xrd*($v*2.0)"
>
@@ -138,7 +134,6 @@ def test_twoEntries(self):

def test_twoEntries_withOther(self):
self.failUnlessXRDSHasReturnURLs('''\
<?xml version="1.0" encoding="UTF-8"?>
<xrds:XRDS xmlns:xrds="xri://$xrds"
xmlns="xri://$xrd*($v*2.0)"
>
@@ -202,7 +197,7 @@ def setUp(self):

def tearDown(self):
CatchLogs.tearDown(self)

def test_bogusRealm(self):
self.assertFalse(trustroot.verifyReturnTo('', 'http://example.com/'))

0 comments on commit 020c1e1

Please sign in to comment.
You can’t perform that action at this time.