Skip to content

Commit

Permalink
Validate xContentType in PutWatchRequest. (#31088)
Browse files Browse the repository at this point in the history
Trying to post a new watch without any body currently results in a 
NullPointerException. This change fixes that by validating that 
Post and Put requests always have a body.

Closes #30057
  • Loading branch information
adhulipa authored and Christoph Büscher committed Jun 12, 2018
1 parent 0c38c94 commit b9944aa
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,9 @@ public ActionRequestValidationException validate() {
if (source == null) {
validationException = ValidateActions.addValidationError("watch source is missing", validationException);
}
if (xContentType == null) {
validationException = ValidateActions.addValidationError("request body is missing", validationException);
}
return validationException;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
},
"body": {
"description" : "The watch",
"required" : true
"required" : false
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,16 @@
}
}
- match: { _id: "my_watch" }

---
"Test empty body is rejected by put watch":
- do:
cluster.health:
wait_for_status: yellow

- do:
catch: bad_request
xpack.watcher.put_watch:
id: "my_watch"
- match: { error.root_cause.0.type: "action_request_validation_exception" }
- match: { error.root_cause.0.reason: "Validation Failed: 1: request body is missing;" }
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,12 @@ public void testPutWatchSourceNull() {
assertThat(e.validationErrors(), hasItem("watch source is missing"));
}

public void testPutWatchContentNull() {
ActionRequestValidationException e = new PutWatchRequest("foo", BytesArray.EMPTY, null).validate();
assertThat(e, is(notNullValue()));
assertThat(e.validationErrors(), hasItem("request body is missing"));
}

public void testGetWatchInvalidWatchId() {
ActionRequestValidationException e = new GetWatchRequest("id with whitespaces").validate();
assertThat(e, is(notNullValue()));
Expand Down

0 comments on commit b9944aa

Please sign in to comment.