http-client-branch: review comment - request builder copy should not require a URI http-client-branch
authorchegar
Mon, 16 Apr 2018 18:38:31 +0100
branchhttp-client-branch
changeset 56441 66018d90fc97
parent 56440 bb7ba987b170
child 56442 19534e49b523
http-client-branch: review comment - request builder copy should not require a URI
src/java.net.http/share/classes/jdk/internal/net/http/HttpRequestBuilderImpl.java
test/jdk/java/net/httpclient/RequestBuilderTest.java
--- a/src/java.net.http/share/classes/jdk/internal/net/http/HttpRequestBuilderImpl.java	Mon Apr 16 17:30:02 2018 +0100
+++ b/src/java.net.http/share/classes/jdk/internal/net/http/HttpRequestBuilderImpl.java	Mon Apr 16 18:38:31 2018 +0100
@@ -88,7 +88,8 @@
 
     @Override
     public HttpRequestBuilderImpl copy() {
-        HttpRequestBuilderImpl b = new HttpRequestBuilderImpl(this.uri);
+        HttpRequestBuilderImpl b = new HttpRequestBuilderImpl();
+        b.uri = this.uri;
         b.userHeaders = this.userHeaders.deepCopy();
         b.method = this.method;
         b.expectContinue = this.expectContinue;
--- a/test/jdk/java/net/httpclient/RequestBuilderTest.java	Mon Apr 16 17:30:02 2018 +0100
+++ b/test/jdk/java/net/httpclient/RequestBuilderTest.java	Mon Apr 16 18:38:31 2018 +0100
@@ -58,7 +58,9 @@
     @Test
     public void testDefaults() {
         List<HttpRequest.Builder> builders = List.of(newBuilder().uri(uri),
-                                                     newBuilder(uri));
+                                                     newBuilder(uri),
+                                                     newBuilder().copy().uri(uri),
+                                                     newBuilder(uri).copy());
         for (HttpRequest.Builder builder : builders) {
             assertFalse(builder.build().expectContinue());
             assertEquals(builder.build().method(), "GET");
@@ -376,6 +378,17 @@
         assertEquals(copyRequest.timeout().get(), ofSeconds(30));
         assertTrue(copyRequest.version().isPresent());
         assertEquals(copyRequest.version().get(), HTTP_1_1);
+
+        // lazy set URI ( maybe builder as a template )
+        copyRequest = newBuilder().copy().uri(uri).build();
+        assertEquals(copyRequest.uri(), uri);
+
+        builder = newBuilder().header("C", "D");
+        copy = builder.copy();
+        copy.uri(uri);
+        copyRequest = copy.build();
+        assertEquals(copyRequest.uri(), uri);
+        assertEquals(copyRequest.headers().firstValue("C").get(), "D");
     }
 
     @Test