More basic call validation tests for HTTP API. Added more basic call validation tests around malformed body, invalid media types etc for the HTTP API.
Review: https://reviews.apache.org/r/37192 Project: http://git-wip-us.apache.org/repos/asf/mesos/repo Commit: http://git-wip-us.apache.org/repos/asf/mesos/commit/077b29a2 Tree: http://git-wip-us.apache.org/repos/asf/mesos/tree/077b29a2 Diff: http://git-wip-us.apache.org/repos/asf/mesos/diff/077b29a2 Branch: refs/heads/master Commit: 077b29a2197bc0376843bd62b03f6bdde4d3970d Parents: 67a85c6 Author: Anand Mazumdar <[email protected]> Authored: Fri Aug 7 10:39:30 2015 -0700 Committer: Vinod Kone <[email protected]> Committed: Fri Aug 7 10:39:30 2015 -0700 ---------------------------------------------------------------------- src/tests/http_api_tests.cpp | 79 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/mesos/blob/077b29a2/src/tests/http_api_tests.cpp ---------------------------------------------------------------------- diff --git a/src/tests/http_api_tests.cpp b/src/tests/http_api_tests.cpp index 57d7e74..e63a29d 100644 --- a/src/tests/http_api_tests.cpp +++ b/src/tests/http_api_tests.cpp @@ -52,6 +52,7 @@ using process::http::BadRequest; using process::http::OK; using process::http::Pipe; using process::http::Response; +using process::http::UnsupportedMediaType; using std::string; @@ -132,6 +133,84 @@ TEST_F(HttpApiTest, NoContentType) } +// This test sends a valid JSON blob that cannot be deserialized +// into a valid protobuf resulting in a BadRequest. +TEST_F(HttpApiTest, ValidJsonButInvalidProtobuf) +{ + Try<PID<Master>> master = StartMaster(); + ASSERT_SOME(master); + + JSON::Object object; + object.values["string"] = "valid_json"; + + hashmap<string, string> headers; + headers["Accept"] = APPLICATION_JSON; + + Future<Response> response = process::http::post( + master.get(), + "call", + headers, + stringify(object), + APPLICATION_JSON); + + AWAIT_EXPECT_RESPONSE_STATUS_EQ(BadRequest().status, response); +} + + +// This test sends a malformed body that cannot be deserialized +// into a valid protobuf resulting in a BadRequest. +TEST_P(HttpApiTest, MalformedContent) +{ + Try<PID<Master>> master = StartMaster(); + ASSERT_SOME(master); + + const std::string body = "MALFORMED_CONTENT"; + + const std::string contentType = GetParam(); + hashmap<string, string> headers; + headers["Accept"] = contentType; + + Future<Response> response = process::http::post( + master.get(), + "call", + headers, + body, + contentType); + + AWAIT_EXPECT_RESPONSE_STATUS_EQ(BadRequest().status, response); +} + + +// This test sets an unsupported media type as Content-Type. This +// should result in a 415 (UnsupportedMediaType) response. +TEST_P(HttpApiTest, UnsupportedContentMediaType) +{ + Try<PID<Master>> master = StartMaster(); + ASSERT_SOME(master); + + const std::string contentType = GetParam(); + hashmap<string, string> headers; + headers["Accept"] = contentType; + + Call call; + call.set_type(Call::SUBSCRIBE); + + Call::Subscribe* subscribe = call.mutable_subscribe(); + subscribe->mutable_framework_info()->CopyFrom(DEFAULT_FRAMEWORK_INFO); + + const std::string unknownMediaType = "application/unknown-media-type"; + + Future<Response> response = process::http::post( + master.get(), + "call", + headers, + serialize(call, contentType), + unknownMediaType); + + AWAIT_EXPECT_RESPONSE_STATUS_EQ(UnsupportedMediaType().status, response); +} + + // This test verifies if the scheduler is able to receive a Subscribed // event on the stream in response to a Subscribe call request. TEST_P(HttpApiTest, Subscribe)
