/** * Copy constructor * * @param other the options to copy */ public ClientOptionsBase(ClientOptionsBase other) { super(other); this.connectTimeout = other.getConnectTimeout(); this.trustAll = other.isTrustAll(); this.metricsName = other.metricsName; this.proxyOptions = other.proxyOptions != null ? new ProxyOptions(other.proxyOptions) : null; this.localAddress = other.localAddress; }
@Test public void testOptionsJson() { JsonObject json = new JsonObject(); json.put("type", randType.toString()) .put("host", randHost) .put("port", randPort) .put("username", randUsername) .put("password", randPassword); ProxyOptions options = new ProxyOptions(json); assertEquals(randType, options.getType()); assertEquals(randPort, options.getPort()); assertEquals(randHost, options.getHost()); assertEquals(randUsername, options.getUsername()); assertEquals(randPassword, options.getPassword()); } }
@Test public void testDefaultOptionsJson() { ProxyOptions def = new ProxyOptions(); ProxyOptions options = new ProxyOptions(new JsonObject()); assertEquals(def.getType(), options.getType()); assertEquals(def.getPort(), options.getPort()); assertEquals(def.getHost(), options.getHost()); assertEquals(def.getUsername(), options.getUsername()); assertEquals(def.getPassword(), options.getPassword()); }
@Test public void testProxyConnectError() { testConnectErrorNotifiesOnEventLoop(new NetClientOptions() .setProxyOptions(new ProxyOptions() .setPort(1234) .setType(ProxyType.SOCKS5) .setHost("localhost"))); }
private void proxyTest(int error, String username, String url, Handler<AsyncResult<HttpClientResponse>> assertResponse) throws Exception { startProxy(error, username); final HttpClientOptions options = new HttpClientOptions() .setSsl(url.startsWith("https")) .setProxyOptions(new ProxyOptions() .setType(ProxyType.HTTP) .setHost("localhost") .setPort(proxy.getPort())); HttpClient client = vertx.createHttpClient(options); client.getAbs(url, assertResponse).end(); await(); }
/** * test http connect proxy for accessing a arbitrary server port * note that this may not work with a "real" proxy since there are usually access rules defined * that limit the target host and ports (e.g. connecting to localhost or to port 25 may not be allowed) */ @Test public void testWithHttpConnectProxy() throws Exception { NetClientOptions clientOptions = new NetClientOptions() .setProxyOptions(new ProxyOptions().setType(ProxyType.HTTP).setPort(13128)); NetClient client = vertx.createNetClient(clientOptions); server.connectHandler(sock -> { }); proxy = new HttpProxy(null); proxy.start(vertx); server.listen(1234, "localhost", ar -> { assertTrue(ar.succeeded()); client.connect(1234, "localhost", ar2 -> { if (ar2.failed()) { log.warn("failed", ar2.cause()); } assertTrue(ar2.succeeded()); // make sure we have gone through the proxy assertEquals("localhost:1234", proxy.getLastUri()); testComplete(); }); }); await(); }
@Test public void testCopyProxyOptions() { ProxyOptions options = new ProxyOptions(); options.setType(randType); options.setHost(randHost); options.setPort(randPort); options.setUsername(randUsername); options.setPassword(randPassword); ProxyOptions copy = new ProxyOptions(options); assertEquals(randType, copy.getType()); assertEquals(randPort, copy.getPort()); assertEquals(randHost, copy.getHost()); assertEquals(randUsername, copy.getUsername()); assertEquals(randPassword, copy.getPassword()); }
/** * test socks4a proxy for accessing arbitrary server port. */ @Test public void testWithSocks4aProxy() throws Exception { NetClientOptions clientOptions = new NetClientOptions() .setProxyOptions(new ProxyOptions().setType(ProxyType.SOCKS4).setPort(11080)); NetClient client = vertx.createNetClient(clientOptions); server.connectHandler(sock -> { }); proxy = new Socks4Proxy(null); proxy.start(vertx); server.listen(1234, "localhost", ar -> { assertTrue(ar.succeeded()); client.connect(1234, "localhost", ar2 -> { if (ar2.failed()) { log.warn("failed", ar2.cause()); } assertTrue(ar2.succeeded()); // make sure we have gone through the proxy assertEquals("localhost:1234", proxy.getLastUri()); testComplete(); }); }); await(); }
/** * test socks4a proxy for accessing arbitrary server port using an already resolved address. */ @Test public void testWithSocks4LocalResolver() throws Exception { NetClientOptions clientOptions = new NetClientOptions() .setProxyOptions(new ProxyOptions().setType(ProxyType.SOCKS4).setPort(11080)); NetClient client = vertx.createNetClient(clientOptions); server.connectHandler(sock -> { }); proxy = new Socks4Proxy(null).start(vertx); server.listen(1234, "localhost", ar -> { assertTrue(ar.succeeded()); client.connect(1234, "127.0.0.1", ar2 -> { if (ar2.failed()) { log.warn("failed", ar2.cause()); } assertTrue(ar2.succeeded()); // make sure we have gone through the proxy assertEquals("127.0.0.1:1234", proxy.getLastUri()); testComplete(); }); }); await(); }
/** * test socks5 proxy for accessing arbitrary server port. */ @Test public void testWithSocks5Proxy() throws Exception { NetClientOptions clientOptions = new NetClientOptions() .setProxyOptions(new ProxyOptions().setType(ProxyType.SOCKS5).setPort(11080)); NetClient client = vertx.createNetClient(clientOptions); server.connectHandler(sock -> { }); proxy = new SocksProxy(null); proxy.start(vertx); server.listen(1234, "localhost", ar -> { assertTrue(ar.succeeded()); client.connect(1234, "localhost", ar2 -> { if (ar2.failed()) { log.warn("failed", ar2.cause()); } assertTrue(ar2.succeeded()); // make sure we have gone through the proxy assertEquals("localhost:1234", proxy.getLastUri()); testComplete(); }); }); await(); }
/** * test socks4a proxy for accessing arbitrary server port using username auth. */ @Test public void testWithSocks4aProxyAuth() throws Exception { NetClientOptions clientOptions = new NetClientOptions() .setProxyOptions(new ProxyOptions().setType(ProxyType.SOCKS4).setPort(11080) .setUsername("username")); NetClient client = vertx.createNetClient(clientOptions); server.connectHandler(sock -> { }); proxy = new Socks4Proxy("username"); proxy.start(vertx); server.listen(1234, "localhost", ar -> { assertTrue(ar.succeeded()); client.connect(1234, "localhost", ar2 -> { if (ar2.failed()) { log.warn("failed", ar2.cause()); } assertTrue(ar2.succeeded()); // make sure we have gone through the proxy assertEquals("localhost:1234", proxy.getLastUri()); testComplete(); }); }); await(); }
/** * test socks5 proxy for accessing arbitrary server port with authentication. */ @Test public void testWithSocks5ProxyAuth() throws Exception { NetClientOptions clientOptions = new NetClientOptions() .setProxyOptions(new ProxyOptions().setType(ProxyType.SOCKS5).setPort(11080) .setUsername("username").setPassword("username")); NetClient client = vertx.createNetClient(clientOptions); server.connectHandler(sock -> { }); proxy = new SocksProxy("username"); proxy.start(vertx); server.listen(1234, "localhost", ar -> { assertTrue(ar.succeeded()); client.connect(1234, "localhost", ar2 -> { assertTrue(ar2.succeeded()); testComplete(); }); }); await(); }
@Test public void testHttpSocksProxyRequest() throws Exception { startProxy(null, ProxyType.SOCKS5); client.close(); client = vertx.createHttpClient(new HttpClientOptions() .setProxyOptions(new ProxyOptions().setType(ProxyType.SOCKS5).setHost("localhost").setPort(proxy.getPort()))); server.requestHandler(req -> { req.response().end(); }); server.listen(onSuccess(s -> { client.get(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/", onSuccess(resp -> { assertEquals(200, resp.statusCode()); assertNotNull("request did not go through proxy", proxy.getLastUri()); testComplete(); })).exceptionHandler(th -> fail(th)).end(); })); await(); }
@Test public void testHttpProxyRequest() throws Exception { startProxy(null, ProxyType.HTTP); client.close(); client = vertx.createHttpClient(new HttpClientOptions() .setProxyOptions(new ProxyOptions().setType(ProxyType.HTTP).setHost("localhost").setPort(proxy.getPort()))); testHttpProxyRequest2(handler -> client.get(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/", handler)); }
@Test public void testPerPeerPoolingWithProxy() throws Exception { client.close(); client = vertx.createHttpClient(new HttpClientOptions() .setMaxPoolSize(1) .setKeepAlive(true) .setPipelining(false).setProxyOptions(new ProxyOptions() .setType(ProxyType.HTTP) .setHost(DEFAULT_HTTP_HOST) .setPort(DEFAULT_HTTP_PORT))); testPerXXXPooling((i, handler) -> client.get(80, "host" + i, "/somepath", handler), HttpServerRequest::host); }
@Test public void testHttpSocksProxyRequestAuth() throws Exception { startProxy("user", ProxyType.SOCKS5); client.close(); client = vertx.createHttpClient(new HttpClientOptions() .setProxyOptions(new ProxyOptions().setType(ProxyType.SOCKS5).setHost("localhost").setPort(proxy.getPort()) .setUsername("user").setPassword("user"))); server.requestHandler(req -> { req.response().end(); }); server.listen(onSuccess(s -> { client.get(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/", onSuccess(resp -> { assertEquals(200, resp.statusCode()); assertNotNull("request did not go through proxy", proxy.getLastUri()); testComplete(); })).exceptionHandler(th -> fail(th)).end(); })); await(); }
@Test public void testHttpProxyFtpRequest() throws Exception { startProxy(null, ProxyType.HTTP); client.close(); client = vertx.createHttpClient(new HttpClientOptions() .setProxyOptions(new ProxyOptions().setType(ProxyType.HTTP).setHost("localhost").setPort(proxy.getPort()))); final String url = "ftp://ftp.gnu.org/gnu/"; proxy.setForceUri("http://localhost:8080/"); server.requestHandler(req -> { req.response().end(); }); server.listen(onSuccess(s -> { HttpClientRequest clientReq = client.getAbs(url, onSuccess(resp -> { assertEquals(200, resp.statusCode()); assertEquals("request did sent the expected url", url, proxy.getLastUri()); testComplete(); })); clientReq.exceptionHandler(this::fail); clientReq.end(); })); await(); }
@Test public void testHttpProxyRequestAuth() throws Exception { startProxy("user", ProxyType.HTTP); client.close(); client = vertx.createHttpClient(new HttpClientOptions() .setProxyOptions(new ProxyOptions().setType(ProxyType.HTTP).setHost("localhost").setPort(proxy.getPort()) .setUsername("user").setPassword("user"))); server.requestHandler(req -> { req.response().end(); }); server.listen(onSuccess(s -> { client.get(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/", onSuccess(resp -> { assertEquals(200, resp.statusCode()); assertNotNull("request did not go through proxy", proxy.getLastUri()); assertEquals("Host header doesn't contain target host", "localhost:8080", proxy.getLastRequestHeaders().get("Host")); testComplete(); })).exceptionHandler(th -> fail(th)).end(); })); await(); }
@Test public void testHttpProxyRequestOverrideClientSsl() throws Exception { startProxy(null, ProxyType.HTTP); client.close(); client = vertx.createHttpClient(new HttpClientOptions() .setSsl(true).setProxyOptions(new ProxyOptions().setType(ProxyType.HTTP).setHost("localhost").setPort(proxy.getPort()))); testHttpProxyRequest2(handler -> client.get(new RequestOptions().setSsl(false).setHost("localhost").setPort(8080), handler)); }
@Test public void testProxyOptions() { ProxyOptions options = new ProxyOptions(); assertEquals(ProxyOptions.DEFAULT_TYPE, options.getType()); assertEquals(options, options.setType(randType)); assertEquals(randType, options.getType()); assertNullPointerException(() -> options.setType(null)); assertEquals(ProxyOptions.DEFAULT_HOST, options.getHost()); assertEquals(options, options.setHost(randHost)); assertEquals(randHost, options.getHost()); assertNullPointerException(() -> options.setHost(null)); assertEquals(ProxyOptions.DEFAULT_PORT, options.getPort()); assertEquals(options, options.setPort(randPort)); assertEquals(randPort, options.getPort()); assertIllegalArgumentException(() -> options.setPort(-1)); assertIllegalArgumentException(() -> options.setPort(65536)); assertEquals(null, options.getUsername()); assertEquals(options, options.setUsername(randUsername)); assertEquals(randUsername, options.getUsername()); assertEquals(null, options.getPassword()); assertEquals(options, options.setPassword(randPassword)); assertEquals(randPassword, options.getPassword()); }