Skip to content

Commit

Permalink
Expose request URL as HttpUrl
Browse files Browse the repository at this point in the history
  • Loading branch information
aahlenst committed Mar 7, 2017
1 parent 8eeebc1 commit 62cab6d
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.util.List;
import javax.net.ssl.SSLSocket;
import okhttp3.Headers;
import okhttp3.HttpUrl;
import okhttp3.TlsVersion;
import okio.Buffer;

Expand All @@ -34,6 +35,7 @@ public final class RecordedRequest {
private final Buffer body;
private final int sequenceNumber;
private final TlsVersion tlsVersion;
private final HttpUrl requestUrl;

public RecordedRequest(String requestLine, Headers headers, List<Integer> chunkSizes,
long bodySize, Buffer body, int sequenceNumber, Socket socket) {
Expand All @@ -52,12 +54,22 @@ public RecordedRequest(String requestLine, Headers headers, List<Integer> chunkS
int pathEnd = requestLine.indexOf(' ', methodEnd + 1);
this.method = requestLine.substring(0, methodEnd);
this.path = requestLine.substring(methodEnd + 1, pathEnd);

String scheme = socket instanceof SSLSocket ? "https" : "http";
String hostname = socket.getInetAddress().getHostName();
int port = socket.getLocalPort();
this.requestUrl = HttpUrl.parse(String.format("%s://%s:%s%s", scheme, hostname, port, path));
} else {
this.requestUrl = null;
this.method = null;
this.path = null;
}
}

public HttpUrl getRequestUrl() {
return requestUrl;
}

public String getRequestLine() {
return requestLine;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import okhttp3.Headers;
import okhttp3.HttpUrl;
import okhttp3.internal.Util;
import org.junit.After;
import org.junit.Rule;
Expand Down Expand Up @@ -405,4 +406,25 @@ private List<String> headersToList(MockResponse response) {
// Shutting down the server should unblock the dispatcher.
server.shutdown();
}

@Test public void requestUrlReconstructed() throws Exception {
server.enqueue(new MockResponse().setBody("hello world"));

URL url = server.url("/a/deep/path?key=foo%20bar").url();
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
InputStream in = connection.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
assertEquals(HttpURLConnection.HTTP_OK, connection.getResponseCode());
assertEquals("hello world", reader.readLine());

RecordedRequest request = server.takeRequest();
assertEquals("GET /a/deep/path?key=foo%20bar HTTP/1.1", request.getRequestLine());

HttpUrl requestUrl = request.getRequestUrl();
assertEquals("http", requestUrl.scheme());
assertEquals(server.getHostName(), requestUrl.host());
assertEquals(server.getPort(), requestUrl.port());
assertEquals("/a/deep/path", requestUrl.encodedPath());
assertEquals("foo bar", requestUrl.queryParameter("key"));
}
}

0 comments on commit 62cab6d

Please sign in to comment.