public static MockWebServer getSslMockWebServer(KeyManager[] keyManagers) throws Exception { MockWebServer server = new MockWebServer(); server.useHttps( OkHttpUnsafe.getUnsafeSSLSocketFactory(keyManagers, unsafeTrustManager()), false); return server; } }
@Before public void setUp() throws Exception { TlsConfig tlsConfig = TlsConfig.builderFrom(ConfigFactory.load().getConfig("xio.testServer.settings.tls")) .build(); client = OkHttpUnsafe.getUnsafeClient(); server = OkHttpUnsafe.getSslMockWebServer(getKeyManagers(tlsConfig)); server.start(); }
OkHttpUnsafe.getSslMockWebServer( getKeyManagers(tlsConfig.getPrivateKey(), tlsConfig.getCertificateAndChain())); server.setProtocols(protocols); server.setDispatcher(
@Before public void setUp() throws Exception { reportedSpans = new ArrayList<>(); TlsConfig tlsConfig = TlsConfig.builderFrom(ConfigFactory.load().getConfig("xio.h2BackendServer.settings.tls")) .build(); server = OkHttpUnsafe.getSslMockWebServer(getKeyManagers(tlsConfig)); server.setProtocols(Collections.singletonList(Protocol.HTTP_1_1)); server.enqueue(buildResponse()); server.start(); }
private void setupClient(int count, boolean h2) throws Exception { clients = IntStream.range(0, count) .mapToObj( index -> { try { if (h2) { return OkHttpUnsafe.getUnsafeClient() .newBuilder() .protocols(Arrays.asList(HTTP_2, HTTP_1_1)) .build(); } else { return OkHttpUnsafe.getUnsafeClient() .newBuilder() .protocols(Collections.singletonList(HTTP_1_1)) .build(); } } catch (Exception e) { return null; } }) .collect(Collectors.toList()); }
@Before public void setUp() throws Exception { log.debug("Test: " + testName.getMethodName()); client = OkHttpUnsafe.getUnsafeClient() .newBuilder() .protocols(Arrays.asList(Protocol.HTTP_2, Protocol.HTTP_1_1)) .build(); TlsConfig tlsConfig = TlsConfig.builderFrom(ConfigFactory.load().getConfig("xio.h2BackendServer.settings.tls")) .build(); server = OkHttpUnsafe.getSslMockWebServer(getKeyManagers(tlsConfig)); server.setProtocols(Arrays.asList(Protocol.HTTP_2, Protocol.HTTP_1_1)); server.start(); }
@Test public void testConnectingSuccessfulConnection() throws Exception { // set up fake origin backend server so we can connect to it, we connect to port 8888 // the outbound 8888 is specified in the ClientConnectionManagerIntegrationTest.conf TlsConfig tlsConfig = TlsConfig.builderFrom(ConfigFactory.load().getConfig("xio.h2BackendServer.settings.tls")) .build(); server = OkHttpUnsafe.getSslMockWebServer(TlsHelper.getKeyManagers(tlsConfig)); server.setProtocols(Arrays.asList(Protocol.HTTP_2, Protocol.HTTP_1_1)); // tell the server to bind to 8888 server.start(8888); subject = subjectFactory(true); Future<Void> connectionResult = subject.connect(); assertEquals(ClientConnectionState.CONNECTING, subject.connectionState()); Thread.sleep(100); // todo: (WK) do something smarter try { connectionResult.get(5, TimeUnit.SECONDS); } catch (Exception e) { System.out.println("Connection exception = " + e.toString()); } finally { assertEquals(ClientConnectionState.CONNECTED, subject.connectionState()); server.close(); } }
/** * Make a synchronous https request using an unsafe http client. * * @param address the request address. * @param protocols the protocol(s) to support. * @return a response. */ public static Response https(InetSocketAddress address, Protocol... protocols) throws Exception { String url = buildUrl("https://", address); return request(url, OkHttpUnsafe.getUnsafeClient(protocols)); } }
public static OkHttpClient getUnsafeClient(Protocol... protocols) throws Exception { X509TrustManager trustManager = unsafeTrustManager(); final SSLSocketFactory sslSocketFactory = getUnsafeSSLSocketFactory(null, trustManager); final List<Protocol> protocolList; if (protocols.length == 0) { protocolList = Collections.singletonList(Protocol.HTTP_1_1); } else { protocolList = Arrays.asList(protocols); } return new OkHttpClient.Builder() .sslSocketFactory(sslSocketFactory, trustManager) .hostnameVerifier( new HostnameVerifier() { public boolean verify(String hostname, SSLSession session) { return true; } }) .protocols(protocolList) .build(); }