Skip to content

Commit 8336362

Browse files
Rémy HUBSCHERdjc
authored andcommitted
Fix the basic_auth behavior
HTTP header should be in binary rather than strings.
1 parent 24e101e commit 8336362

File tree

3 files changed

+11
-2
lines changed

3 files changed

+11
-2
lines changed

couchdb/http.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -563,13 +563,13 @@ def extract_credentials(url):
563563
def basic_auth(credentials):
564564
"""Generates authorization header value for given credentials.
565565
>>> basic_auth(('root', 'relax'))
566-
u'Basic cm9vdDpyZWxheA=='
566+
b'Basic cm9vdDpyZWxheA=='
567567
>>> basic_auth(None)
568568
>>> basic_auth(())
569569
"""
570570
if credentials:
571571
token = b64encode(('%s:%s' % credentials).encode('latin1'))
572-
return 'Basic %s' % (token.strip().decode('latin1'))
572+
return ('Basic %s' % token.strip().decode('latin1')).encode('ascii')
573573

574574

575575
def quote(string, safe=''):

couchdb/tests/client.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,12 @@ def test_235_unicode_server(self):
130130
finally:
131131
server.delete(dbname)
132132

133+
def test_basic_auth(self):
134+
url = "http://root:password@localhost:5984/"
135+
server = client.Server(url)
136+
dbname = 'couchdb-python/test_basic_auth'
137+
self.assertRaises(http.Unauthorized, server.create, dbname)
138+
133139

134140
class DatabaseTestCase(testutil.TempDatabaseMixin, unittest.TestCase):
135141

couchdb/tests/testutil.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ def check_output(self, want, got, optionflags):
1818
if sys.version_info[0] > 2:
1919
want = re.sub("u'(.*?)'", "'\\1'", want)
2020
want = re.sub('u"(.*?)"', '"\\1"', want)
21+
else:
22+
want = re.sub("b'(.*?)'", "'\\1'", want)
23+
want = re.sub('b"(.*?)"', '"\\1"', want)
2124
return doctest.OutputChecker.check_output(self, want, got, optionflags)
2225

2326
def doctest_suite(mod):

0 commit comments

Comments
 (0)