'If a custom L{WebClientConnectionCreator}-like object is passed to
L{Agent.__init__} it will be used to determine the SSL parameters for
HTTPS requests.  When an HTTPS request is made, the hostname and port
number of the request URL will be passed to the connection creator\'s
C{creatorForNetloc} method.  The resulting context object will be used
to establish the SSL connection.'

def test_connectHTTPSCustomConnectionCreator(self):

    expectedHost = 'example.org'
    expectedPort = 20443
    class JustEnoughConnection(object, ):
        handshakeStarted = False
        connectState = False
        def do_handshake(self):
            '\n                                                                The    handshake    started.        Record    that    fact.\n                                                                '
            self.handshakeStarted = True
        def set_connect_state(self):
            '\n                                                                The    connection    started.        Record    that    fact.\n                                                                '
            self.connectState = True
    contextArgs = []
    @implementer(IOpenSSLClientConnectionCreator)
    class JustEnoughCreator(object, ):
        def __init__(self, hostname, port):
            self.hostname = hostname
            self.port = port
        def clientConnectionForTLS(self, tlsProtocol):
            '\n                                                                Implement    L{IOpenSSLClientConnectionCreator}.\n\n                                                                @param    tlsProtocol:    The    TLS    protocol.\n                                                                @type    tlsProtocol:    L{TLSMemoryBIOProtocol}\n\n                                                                @return:    C{expectedConnection}\n                                                                '
            contextArgs.append((tlsProtocol, self.hostname, self.port))
            return expectedConnection
    expectedConnection = JustEnoughConnection()
    @implementer(IPolicyForHTTPS)
    class StubBrowserLikePolicyForHTTPS(object, ):
        def creatorForNetloc(self, hostname, port):
            '\n                                                                Emulate    L{BrowserLikePolicyForHTTPS}.\n\n                                                                @param    hostname:    The    hostname    to    verify.\n                                                                @type    hostname:    L{bytes}\n\n                                                                @param    port:    The    port    number.\n                                                                @type    port:    L{int}\n\n                                                                @return:    a    stub    L{IOpenSSLClientConnectionCreator}\n                                                                @rtype:    L{JustEnoughCreator}\n                                                                '
            return JustEnoughCreator(hostname, port)
    expectedCreatorCreator = StubBrowserLikePolicyForHTTPS()
    reactor = self.createReactor()
    agent = client.Agent(reactor, expectedCreatorCreator)
    endpoint = agent._getEndpoint(URI.fromBytes(((('https://' + expectedHost) + ':') + intToBytes(expectedPort))))
    endpoint.connect(Factory.forProtocol(Protocol))
    tlsFactory = reactor.tcpClients[(-1)][2]
    tlsProtocol = tlsFactory.buildProtocol(None)
    tlsProtocol.makeConnection(StringTransport())
    tls = contextArgs[0][0]
    self.assertIsInstance(tls, TLSMemoryBIOProtocol)
    self.assertEqual(contextArgs[0][1:], (expectedHost, expectedPort))
    self.assertTrue(expectedConnection.handshakeStarted)
    self.assertTrue(expectedConnection.connectState)
