mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Simplify test_ssl
This commit is contained in:
parent
86832d0e15
commit
388bceac2b
@ -6,7 +6,7 @@ from __future__ import (unicode_literals, division, absolute_import,
|
||||
__license__ = 'GPL v3'
|
||||
__copyright__ = '2015, Kovid Goyal <kovid at kovidgoyal.net>'
|
||||
|
||||
import httplib, ssl, os, socket, time, errno
|
||||
import httplib, ssl, os, socket, time
|
||||
from collections import namedtuple
|
||||
from unittest import skipIf
|
||||
from glob import glob
|
||||
@ -17,7 +17,6 @@ try:
|
||||
except ImportError:
|
||||
create_server_cert = None
|
||||
|
||||
from calibre.constants import isosx
|
||||
from calibre.srv.pre_activated import has_preactivated_support
|
||||
from calibre.srv.tests.base import BaseTest, TestServer
|
||||
from calibre.ptempfile import TemporaryDirectory
|
||||
@ -179,24 +178,13 @@ class LoopTest(BaseTest):
|
||||
@skipIf(create_server_cert is None, 'certgen module not available')
|
||||
def test_ssl(self):
|
||||
'Test serving over SSL'
|
||||
try:
|
||||
s = socket.socket(socket.AF_INET6, socket.SOCK_STREAM, 0)
|
||||
s.bind(('localhost', 0))
|
||||
except socket.error as err:
|
||||
if err.errno == errno.EADDRNOTAVAIL:
|
||||
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0)
|
||||
s.bind(('localhost', 0))
|
||||
else:
|
||||
raise
|
||||
address = s.getsockname()[0]
|
||||
if is_travis and isosx:
|
||||
address = '::1'
|
||||
address = '127.0.0.1'
|
||||
with TemporaryDirectory('srv-test-ssl') as tdir:
|
||||
cert_file, key_file, ca_file = map(lambda x:os.path.join(tdir, x), 'cka')
|
||||
create_server_cert(address, ca_file, cert_file, key_file, key_size=1024)
|
||||
ctx = ssl.create_default_context(cafile=ca_file)
|
||||
with TestServer(lambda data:(data.path[0] + data.read()), ssl_certfile=cert_file, ssl_keyfile=key_file) as server:
|
||||
conn = httplib.HTTPSConnection(server.address[0], server.address[1], strict=True, context=ctx)
|
||||
with TestServer(lambda data:(data.path[0] + data.read()), ssl_certfile=cert_file, ssl_keyfile=key_file, listen_on=address, port=0) as server:
|
||||
conn = httplib.HTTPSConnection(address, server.address[1], strict=True, context=ctx)
|
||||
conn.request('GET', '/test', 'body')
|
||||
r = conn.getresponse()
|
||||
self.ae(r.status, httplib.OK)
|
||||
|
Loading…
x
Reference in New Issue
Block a user