Skip to content

Commit

Permalink
Added ability to return an unresolved address from a proxyToServerRes…
Browse files Browse the repository at this point in the history
…olutionStarted() filter method
  • Loading branch information
jekh committed Dec 21, 2014
1 parent 91db6fb commit 39963c1
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -709,6 +709,10 @@ private void setupConnectionParameters() throws UnknownHostException {
.proxyToServerResolutionStarted(serverHostAndPort);
if (this.remoteAddress == null) {
this.remoteAddress = addressFor(serverHostAndPort, proxyServer);
} else if (this.remoteAddress.isUnresolved()) {
// filter returned an unresolved address, so resolve it using the proxy server's resolver
this.remoteAddress = proxyServer.getServerResolver().resolve(this.remoteAddress.getHostName(),
this.remoteAddress.getPort());
}
this.currentFilters.proxyToServerResolutionSucceeded(
serverHostAndPort, this.remoteAddress);
Expand Down
40 changes: 40 additions & 0 deletions src/test/java/org/littleshoot/proxy/HttpFilterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@
import java.util.Date;
import java.util.LinkedList;
import java.util.Queue;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

public class HttpFilterTest {
Expand Down Expand Up @@ -330,6 +332,44 @@ public int getMaximumResponseBufferSizeInBytes() {
server.stop();
}

@Test
public void testResolutionStartedFilterReturnsUnresolvedAddress() throws Exception {
final AtomicBoolean resolutionSucceeded = new AtomicBoolean(false);

HttpFiltersSource filtersSource = new HttpFiltersSourceAdapter() {
@Override
public HttpFilters filterRequest(HttpRequest originalRequest) {
return new HttpFiltersAdapter(originalRequest) {
@Override
public InetSocketAddress proxyToServerResolutionStarted(String resolvingServerHostAndPort) {
return InetSocketAddress.createUnresolved("localhost", WEB_SERVER_PORT);
}

@Override
public void proxyToServerResolutionSucceeded(String serverHostAndPort, InetSocketAddress resolvedRemoteAddress) {
assertFalse("expected to receive a resolved InetSocketAddress", resolvedRemoteAddress.isUnresolved());
resolutionSucceeded.set(true);
}
};
}
};

final HttpProxyServer server = DefaultHttpProxyServer.bootstrap()
.withPort(PROXY_PORT)
.withFiltersSource(filtersSource)
.start();

final Server webServer = new Server(WEB_SERVER_PORT);
webServer.start();

org.apache.http.HttpResponse response1 = getResponse("http://localhost:" + WEB_SERVER_PORT + "/");

assertTrue("proxyToServerResolutionSucceeded method was not called", resolutionSucceeded.get());

webServer.stop();
server.stop();
}

private org.apache.http.HttpResponse getResponse(final String url)
throws Exception {
final DefaultHttpClient http = new DefaultHttpClient();
Expand Down

0 comments on commit 39963c1

Please sign in to comment.