py3: Port more content server tests

This commit is contained in:
Kovid Goyal 2019-04-14 20:00:27 +05:30
parent 333dab88c2
commit c95e064e53
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
2 changed files with 21 additions and 19 deletions

View File

@ -100,7 +100,7 @@ class TestAuth(BaseTest):
conn.request('GET', '/closed') conn.request('GET', '/closed')
r = conn.getresponse() r = conn.getresponse()
self.ae(r.status, http_client.UNAUTHORIZED) self.ae(r.status, http_client.UNAUTHORIZED)
self.ae(r.getheader('WWW-Authenticate'), b'Basic realm="%s"' % bytes(REALM)) self.ae(r.getheader('WWW-Authenticate'), 'Basic realm="%s"' % REALM)
self.assertFalse(r.read()) self.assertFalse(r.read())
conn.request('GET', '/closed', headers={'Authorization': b'Basic ' + as_base64_bytes(b'testuser:testpw')}) conn.request('GET', '/closed', headers={'Authorization': b'Basic ' + as_base64_bytes(b'testuser:testpw')})
r = conn.getresponse() r = conn.getresponse()
@ -180,11 +180,13 @@ class TestAuth(BaseTest):
return {normalize_header_name(k):v for k, v in r.getheaders()} return {normalize_header_name(k):v for k, v in r.getheaders()}
conn = server.connect() conn = server.connect()
test(conn, '/open', body=b'open') test(conn, '/open', body=b'open')
auth = parse_http_dict(test(conn, '/closed', status=http_client.UNAUTHORIZED)['WWW-Authenticate'].partition(b' ')[2]) auth = parse_http_dict(test(conn, '/closed', status=http_client.UNAUTHORIZED)['WWW-Authenticate'].partition(' ')[2])
nonce = auth['nonce'] nonce = auth['nonce']
auth = parse_http_dict(test(conn, '/closed', status=http_client.UNAUTHORIZED)['WWW-Authenticate'].partition(b' ')[2]) auth = parse_http_dict(test(conn, '/closed', status=http_client.UNAUTHORIZED)['WWW-Authenticate'].partition(' ')[2])
self.assertNotEqual(nonce, auth['nonce'], 'nonce was re-used') self.assertNotEqual(nonce, auth['nonce'], 'nonce was re-used')
self.ae(auth[b'realm'], bytes(REALM)), self.ae(auth[b'algorithm'], b'MD5'), self.ae(auth[b'qop'], b'auth') self.ae(auth['realm'], REALM)
self.ae(auth['algorithm'], 'MD5')
self.ae(auth['qop'], 'auth')
self.assertNotIn('stale', auth) self.assertNotIn('stale', auth)
args = auth.copy() args = auth.copy()
args['un'], args['pw'], args['uri'] = 'testuser', 'testpw', '/closed' args['un'], args['pw'], args['uri'] = 'testuser', 'testpw', '/closed'
@ -202,7 +204,7 @@ class TestAuth(BaseTest):
# Check stale nonces # Check stale nonces
orig, r.auth_controller.max_age_seconds = r.auth_controller.max_age_seconds, -1 orig, r.auth_controller.max_age_seconds = r.auth_controller.max_age_seconds, -1
auth = parse_http_dict(test(conn, '/closed', headers={ auth = parse_http_dict(test(conn, '/closed', headers={
'Authorization':digest(**args)},status=http_client.UNAUTHORIZED)['WWW-Authenticate'].partition(b' ')[2]) 'Authorization':digest(**args)},status=http_client.UNAUTHORIZED)['WWW-Authenticate'].partition(' ')[2])
self.assertIn('stale', auth) self.assertIn('stale', auth)
r.auth_controller.max_age_seconds = orig r.auth_controller.max_age_seconds = orig
ok_test(conn, digest(**args)) ok_test(conn, digest(**args))

View File

@ -34,20 +34,20 @@ class TestHTTP(BaseTest):
self.assertSetEqual(set(p.hdict.items()), {(k.replace('_', '-').title(), v) for k, v in iteritems(kwargs)}, name + ' failed') self.assertSetEqual(set(p.hdict.items()), {(k.replace('_', '-').title(), v) for k, v in iteritems(kwargs)}, name + ' failed')
test('Continuation line parsing', test('Continuation line parsing',
'a: one', b'a: one',
'b: two', b'b: two',
' 2', b' 2',
'\t3', b'\t3',
'c:three', b'c:three',
'\r\n', a='one', b='two 2 3', c='three') b'\r\n', a='one', b='two 2 3', c='three')
test('Non-ascii headers parsing', test('Non-ascii headers parsing',
'a:mūs\r'.encode('utf-8'), '\r\n', a='mūs') 'a:mūs\r'.encode('utf-8'), b'\r\n', a='mūs')
test('Comma-separated parsing', test('Comma-separated parsing',
'Accept-Encoding: one', b'Accept-Encoding: one',
'accept-Encoding: two', b'accept-Encoding: two',
'\r\n', accept_encoding='one, two') b'\r\n', accept_encoding='one, two')
def parse(*lines): def parse(*lines):
lines = list(lines) lines = list(lines)
@ -98,7 +98,7 @@ class TestHTTP(BaseTest):
r = conn.getresponse() r = conn.getresponse()
self.ae(r.status, http_client.OK) self.ae(r.status, http_client.OK)
q += getattr(get_translator(q)[-1], 'gettext' if ispy3 else 'ugettext')('Unknown') q += getattr(get_translator(q)[-1], 'gettext' if ispy3 else 'ugettext')('Unknown')
self.ae(r.read(), q) self.ae(r.read(), q.encode('utf-8'))
test('en', 'en') test('en', 'en')
test('eng', 'en') test('eng', 'en')
@ -317,7 +317,7 @@ class TestHTTP(BaseTest):
return conn.generate_static_output('test', lambda : ''.join(conn.path)) return conn.generate_static_output('test', lambda : ''.join(conn.path))
with NamedTemporaryFile(suffix='test.epub') as f, open(P('localization/locales.zip'), 'rb') as lf, \ with NamedTemporaryFile(suffix='test.epub') as f, open(P('localization/locales.zip'), 'rb') as lf, \
TestServer(handler, timeout=1, compress_min_size=0) as server: TestServer(handler, timeout=1, compress_min_size=0) as server:
fdata = string.ascii_letters * 100 fdata = (string.ascii_letters * 100).encode('ascii')
f.write(fdata), f.seek(0) f.write(fdata), f.seek(0)
# Test ETag # Test ETag
@ -326,7 +326,7 @@ class TestHTTP(BaseTest):
r = conn.getresponse() r = conn.getresponse()
self.ae(r.status, http_client.OK), self.ae(r.read(), b'an_etagged_path') self.ae(r.status, http_client.OK), self.ae(r.read(), b'an_etagged_path')
etag = r.getheader('ETag') etag = r.getheader('ETag')
self.ae(etag, '"%s"' % hashlib.sha1('an_etagged_path').hexdigest()) self.ae(etag, '"%s"' % hashlib.sha1(b'an_etagged_path').hexdigest())
conn.request('GET', '/an_etagged_path', headers={'If-None-Match':etag}) conn.request('GET', '/an_etagged_path', headers={'If-None-Match':etag})
r = conn.getresponse() r = conn.getresponse()
self.ae(r.status, http_client.NOT_MODIFIED) self.ae(r.status, http_client.NOT_MODIFIED)
@ -353,7 +353,7 @@ class TestHTTP(BaseTest):
r = conn.getresponse() r = conn.getresponse()
self.ae(r.status, http_client.OK), self.ae(r.read(), b'data') self.ae(r.status, http_client.OK), self.ae(r.read(), b'data')
etag = r.getheader('ETag') etag = r.getheader('ETag')
self.ae(etag, b'"xxx"') self.ae(etag, '"xxx"')
self.ae(r.getheader('Content-Length'), '4') self.ae(r.getheader('Content-Length'), '4')
conn.request('GET', '/an_etagged_path', headers={'If-None-Match':etag}) conn.request('GET', '/an_etagged_path', headers={'If-None-Match':etag})
r = conn.getresponse() r = conn.getresponse()