Skip to content

Commit

Permalink
Avoid deprecation warning with base64.b64encode. (#4)
Browse files Browse the repository at this point in the history
Projects which depend on the `zope.app.wsgi.testlayer` will have a
`DeprecationWarning` issued when used with Python 3. This is in particular
troublesome when it happens in doctests. We have this method in a variety of
places in the packages of Zopefoundation, which makes it harder to update them
all at once.
  • Loading branch information
sallner committed Oct 25, 2016
1 parent bcb7a10 commit 4b02de7
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
2 changes: 1 addition & 1 deletion CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ CHANGES
4.1.0 (unreleased)
------------------

- Nothing changed yet.
- Use `base64.b64encode` to avoid deprecation warning with Python 3.


4.0.0 (2016-08-08)
Expand Down
2 changes: 1 addition & 1 deletion src/zope/app/wsgi/testlayer.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def auth_header(header):
if p is None:
p = ''
plain = '%s:%s' % (u, p)
auth = base64.encodestring(plain.encode('utf-8'))
auth = base64.b64encode(plain.encode('utf-8'))
return 'Basic %s' % str(auth.rstrip().decode('latin1'))
return header

Expand Down
30 changes: 30 additions & 0 deletions src/zope/app/wsgi/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,35 @@ def test_WSGIPublisherApplication___call___1(self):
self.assertEqual('-', environ['wsgi.logging_info'])


class AuthHeaderTestCase(unittest.TestCase):

def test_auth_encoded(self):
from zope.app.wsgi.testlayer import auth_header
header = 'Basic Z2xvYmFsbWdyOmdsb2JhbG1ncnB3'
self.assertEquals(auth_header(header), header)

def test_auth_non_encoded(self):
from zope.app.wsgi.testlayer import auth_header
header = 'Basic globalmgr:globalmgrpw'
expected = 'Basic Z2xvYmFsbWdyOmdsb2JhbG1ncnB3'
self.assertEquals(auth_header(header), expected)

def test_auth_non_encoded_empty(self):
from zope.app.wsgi.testlayer import auth_header
header = 'Basic globalmgr:'
expected = 'Basic Z2xvYmFsbWdyOg=='
self.assertEquals(auth_header(header), expected)
header = 'Basic :pass'
expected = 'Basic OnBhc3M='
self.assertEquals(auth_header(header), expected)

def test_auth_non_encoded_colon(self):
from zope.app.wsgi.testlayer import auth_header
header = 'Basic globalmgr:pass:pass'
expected = 'Basic Z2xvYmFsbWdyOnBhc3M6cGFzcw=='
self.assertEquals(auth_header(header), expected)


def test_suite():
suites = []
checker = renormalizing.RENormalizing([
Expand All @@ -131,6 +160,7 @@ def test_suite():
suites.append(dt_suite)

suites.append(unittest.makeSuite(WSGIPublisherApplicationTests))
suites.append(unittest.makeSuite(AuthHeaderTestCase))

readme_test = doctest.DocFileSuite(
'README.txt',
Expand Down

0 comments on commit 4b02de7

Please sign in to comment.