Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ public HttpRequest.Builder GET() {

@Override
public HttpRequest.Builder POST(BodyPublisher body) {
return method0("POST", requireNonNull(body));
return method0("POST", requireNonNull(body, "BodyPublisher must be non-null"));
}

@Override
Expand All @@ -218,12 +218,12 @@ public HttpRequest.Builder HEAD() {

@Override
public HttpRequest.Builder PUT(BodyPublisher body) {
return method0("PUT", requireNonNull(body));
return method0("PUT", requireNonNull(body, "BodyPublisher must be non-null"));
}

@Override
public HttpRequest.Builder method(String method, BodyPublisher body) {
requireNonNull(method);
requireNonNull(method, "HTTP method must be non-null");
if (method.isEmpty())
throw newIAE("illegal method <empty string>");
if (method.equals("CONNECT"))
Expand All @@ -234,7 +234,7 @@ public HttpRequest.Builder method(String method, BodyPublisher body) {
.replace("\r", "\\r")
.replace("\t", "\\t")
+ "\"");
return method0(method, requireNonNull(body));
return method0(method, requireNonNull(body, "BodyPublisher must be non-null"));
}

private HttpRequest.Builder method0(String method, BodyPublisher body) {
Expand Down
37 changes: 37 additions & 0 deletions test/jdk/java/net/httpclient/RequestBuilderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -486,4 +486,41 @@ public void testEquals() {
assertNotEquals(builder.build(), newBuilder(uri).header("x", "Z").build());
assertNotEquals(builder.build(), newBuilder(uri).header("z", "y").build());
}

@Test
public void testNullMessages() {
HttpRequest.Builder builder = HttpRequest.newBuilder();

try {
builder.method(null, null);
fail("builder.method(null, null) should have thrown NullPointerException");
} catch (NullPointerException e) {
assertTrue(e.getMessage().contains("HTTP method"),
"Expected exception message to contain 'HTTP method', but got: " + e.getMessage());
}

try {
builder.POST(null);
fail("builder.POST(null) should have thrown NullPointerException");
} catch (NullPointerException e) {
assertTrue(e.getMessage().contains("BodyPublisher"),
"Expected exception message to contain 'BodyPublisher', but got: " + e.getMessage());
}

try {
builder.PUT(null);
fail("builder.PUT(null) should have thrown NullPointerException");
} catch (NullPointerException e) {
assertTrue(e.getMessage().contains("BodyPublisher"),
"Expected exception message to contain 'BodyPublisher', but got: " + e.getMessage());
}

try {
builder.method("PATCH", null);
fail("builder.method(\"PATCH\", null) should have thrown NullPointerException");
} catch (NullPointerException e) {
assertTrue(e.getMessage().contains("BodyPublisher"),
"Expected exception message to contain 'BodyPublisher', but got: " + e.getMessage());
}
}
}