1
0
mirror of https://github.com/square/okhttp.git synced 2026-01-18 20:40:58 +03:00

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.

This commit is contained in:
Richard Thai
2014-08-21 14:33:20 -07:00
committed by Jake Wharton
parent dfb3fc6a18
commit 91c2c7bfc7
2 changed files with 10 additions and 1 deletions

View File

@@ -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);

View File

@@ -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());
}