public static Builder builder(final String localBase) { return new Builder(localBase); }
@Override protected Optional<String> doRemoteUrl(final HttpRequest request) { String uri = request.getUri(); if (!proxyConfig.canAccessedBy(uri)) { return absent(); } return of(proxyConfig.remoteUrl(uri)); }
public static ProxyConfig.Builder from(final String localBase) { return ProxyConfig.builder(checkNotNullOrEmpty(localBase, "Local base should not be null")); }
public final ProxyConfig getProxyConfig() { return from(from).to(to); }
@Override @SuppressWarnings("unchecked") public ResponseHandler apply(final MocoConfig config) { if (config.isFor(MocoConfig.URI_ID)) { String newLocalBase = (String) config.apply(proxyConfig.localBase()); return new ProxyBatchResponseHandler(from(newLocalBase).to(proxyConfig.remoteBase()), failover()); } return this; } }
public ProxyConfig to(final String remoteBase) { return new ProxyConfig(toBase(localBase), toBase(checkNotNullOrEmpty(remoteBase, "Remote base should not be null"))); } }
@Override public final HttpResponseSetting proxy(final ProxyConfig proxyConfig, final Failover failover) { ProxyConfig config = checkNotNull(proxyConfig, "Proxy config should not be null"); this.request(InternalApis.context(config.localBase())) .response(Moco.proxy(config, checkNotNull(failover, "Failover should not be null"))); return this; }
@Test(expected = HttpResponseException.class) public void should_not_proxy_url_for_unmatching_url_for_batch_proxy_from_server() throws Exception { server.proxy(from("/proxy").to(remoteUrl("/target"))); running(server, new Runnable() { @Override public void run() throws Exception { helper.get(remoteUrl("/proxy1/1")); } }); }
@Test public void should_proxy_a_batch_of_urls_with_failover_from_server() throws Exception { server.proxy(from("/proxy").to(remoteUrl("/target")), failover("src/test/resources/failover.response")); running(server, new Runnable() { @Override public void run() throws Exception { assertThat(helper.postContent(remoteUrl("/proxy/1"), "proxy"), is("proxy")); } }); }
@Test public void should_proxy_a_batch_of_urls_with_failover() throws Exception { server.request(match(uri("/proxy/.*"))).response(proxy(from("/proxy").to(remoteUrl("/target")), failover("src/test/resources/failover.response"))); running(server, new Runnable() { @Override public void run() throws Exception { assertThat(helper.postContent(remoteUrl("/proxy/1"), "proxy"), is("proxy")); } }); }
@Test public void should_batch_proxy_from_server() throws Exception { server.get(by(uri("/target/1"))).response("target_1"); server.get(by(uri("/target/2"))).response("target_2"); server.proxy(from("/proxy").to(remoteUrl("/target"))); running(server, new Runnable() { @Override public void run() throws Exception { assertThat(helper.get(remoteUrl("/proxy/1")), is("target_1")); assertThat(helper.get(remoteUrl("/proxy/2")), is("target_2")); } }); }
@Test public void should_batch_proxy_from_server_with_context_server() throws Exception { server = httpServer(port(), context("/proxy")); server.get(by(uri("/target/1"))).response("target_1"); server.get(by(uri("/target/2"))).response("target_2"); server.proxy(from("/proxy").to(remoteUrl("/proxy/target"))); running(server, new Runnable() { @Override public void run() throws Exception { assertThat(helper.get(remoteUrl("/proxy/proxy/1")), is("target_1")); assertThat(helper.get(remoteUrl("/proxy/proxy/2")), is("target_2")); } }); }
@Test public void should_proxy_a_batch_of_urls() throws Exception { server.get(by(uri("/target/1"))).response("target_1"); server.get(by(uri("/target/2"))).response("target_2"); server.get(match(uri("/proxy/.*"))).response(proxy(from("/proxy").to(remoteUrl("/target")))); running(server, new Runnable() { @Override public void run() throws Exception { assertThat(helper.get(remoteUrl("/proxy/1")), is("target_1")); assertThat(helper.get(remoteUrl("/proxy/2")), is("target_2")); } }); }
public ProxyConfig getProxyConfig() { return from(from).to(to); }