test_auth.py: Update to Python 3.

This commit is contained in:
Emil Mikulic 2021-01-17 16:52:47 +11:00
parent 3f236fd71b
commit 7e60a9b731

View File

@ -1,19 +1,18 @@
#!/usr/bin/env python #!/usr/bin/env python3
# This is run by the "run-tests" script. # This is run by the "run-tests" script.
import unittest import unittest
import os import os
import random import random
import base64 import base64
from test import WWWROOT, TestHelper, Conn, parse from test import WWWROOT, TestHelper, Conn, parse, random_bytes
class TestAuth(TestHelper): class TestAuth(TestHelper):
def setUp(self): def setUp(self):
self.datalen = 2345 self.datalen = 2345
self.data = ''.join( self.data = random_bytes(self.datalen)
[chr(random.randint(0,255)) for _ in xrange(self.datalen)])
self.url = '/data.jpeg' self.url = '/data.jpeg'
self.fn = WWWROOT + self.url self.fn = WWWROOT + self.url
with open(self.fn, 'w') as f: with open(self.fn, 'wb') as f:
f.write(self.data) f.write(self.data)
def tearDown(self): def tearDown(self):
@ -23,20 +22,21 @@ class TestAuth(TestHelper):
resp = self.get(self.url) resp = self.get(self.url)
status, hdrs, body = parse(resp) status, hdrs, body = parse(resp)
self.assertContains(status, '401 Unauthorized') self.assertContains(status, '401 Unauthorized')
self.assertEquals(hdrs['WWW-Authenticate'], self.assertEqual(hdrs['WWW-Authenticate'],
'Basic realm="User Visible Realm"') 'Basic realm="User Visible Realm"')
def test_with_auth(self): def test_with_auth(self):
resp = self.get(self.url, req_hdrs={ resp = self.get(self.url, req_hdrs={
'Authorization': 'Basic '+base64.b64encode('myuser:mypass')}) 'Authorization':
'Basic ' + base64.b64encode(b'myuser:mypass').decode('utf-8')})
status, hdrs, body = parse(resp) status, hdrs, body = parse(resp)
self.assertContains(status, '200 OK') self.assertContains(status, '200 OK')
self.assertEquals(hdrs["Accept-Ranges"], "bytes") self.assertEqual(hdrs["Accept-Ranges"], "bytes")
self.assertEquals(hdrs["Content-Length"], str(self.datalen)) self.assertEqual(hdrs["Content-Length"], str(self.datalen))
self.assertEquals(hdrs["Content-Type"], "image/jpeg") self.assertEqual(hdrs["Content-Type"], "image/jpeg")
self.assertContains(hdrs["Server"], "darkhttpd/") self.assertContains(hdrs["Server"], "darkhttpd/")
assert body == self.data, [url, resp, status, hdrs, body] assert body == self.data, [url, resp, status, hdrs, body]
self.assertEquals(body, self.data) self.assertEqual(body, self.data)
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()