From 91c2c7bfc70186c82950a8db2536f8d7383da20e Mon Sep 17 00:00:00 2001 From: Richard Thai Date: Thu, 21 Aug 2014 14:33:20 -0700 Subject: [PATCH] Request URI's have a dependency on the request URL (if the URI is uninitialized). However, URL's can also be null if not initialized with the urlString. This fix initializes the URL properly to avoid NPE on Request.uri() calls. --- .../src/test/java/com/squareup/okhttp/RequestTest.java | 9 +++++++++ okhttp/src/main/java/com/squareup/okhttp/Request.java | 2 +- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/okhttp-tests/src/test/java/com/squareup/okhttp/RequestTest.java b/okhttp-tests/src/test/java/com/squareup/okhttp/RequestTest.java index 4dbbad2eb..00f1d0542 100644 --- a/okhttp-tests/src/test/java/com/squareup/okhttp/RequestTest.java +++ b/okhttp-tests/src/test/java/com/squareup/okhttp/RequestTest.java @@ -19,6 +19,9 @@ import com.squareup.okhttp.internal.Util; import java.io.File; import java.io.FileWriter; import java.io.IOException; +import java.net.URI; +import java.net.URL; + import okio.Buffer; import org.junit.Test; @@ -104,6 +107,12 @@ public final class RequestTest { assertEquals(body, patch.body()); } + @Test public void uninitializedURI() throws Exception { + Request request = new Request.Builder().url("http://localhost/api").build(); + assertEquals(new URI("http://localhost/api"), request.uri()); + assertEquals(new URL("http://localhost/api"), request.url()); + } + private String bodyToHex(RequestBody body) throws IOException { Buffer buffer = new Buffer(); body.writeTo(buffer); diff --git a/okhttp/src/main/java/com/squareup/okhttp/Request.java b/okhttp/src/main/java/com/squareup/okhttp/Request.java index 81cc5d1f5..b8f417ebe 100644 --- a/okhttp/src/main/java/com/squareup/okhttp/Request.java +++ b/okhttp/src/main/java/com/squareup/okhttp/Request.java @@ -60,7 +60,7 @@ public final class Request { public URI uri() throws IOException { try { URI result = uri; - return result != null ? result : (uri = Platform.get().toUriLenient(url)); + return result != null ? result : (uri = Platform.get().toUriLenient(url())); } catch (URISyntaxException e) { throw new IOException(e.getMessage()); }