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/b4cf9a93 Tree: http://git-wip-us.apache.org/repos/asf/mesos/tree/b4cf9a93 Diff: http://git-wip-us.apache.org/repos/asf/mesos/diff/b4cf9a93 Branch: refs/heads/master Commit: b4cf9a931983ff0b129d49d1b39339dbcac21adb Parents: bb801c5 Author: Anand Mazumdar <[email protected]> Authored: Tue Aug 11 19:54:35 2015 -0700 Committer: Vinod Kone <[email protected]> Committed: Tue Aug 11 19:54:35 2015 -0700 ---------------------------------------------------------------------- src/tests/http_api_tests.cpp | 79 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/mesos/blob/b4cf9a93/src/tests/http_api_tests.cpp ---------------------------------------------------------------------- diff --git a/src/tests/http_api_tests.cpp b/src/tests/http_api_tests.cpp index 0c356e5..a176f9a 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 recordio::Decoder; @@ -143,6 +144,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(), + "api/v1/scheduler", + 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(), + "api/v1/scheduler", + 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(), + "api/v1/scheduler", + 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)
