TEST(HTTP, Get) { ASSERT_TRUE(GTEST_IS_THREADSAFE); HttpProcess process; spawn(process); EXPECT_CALL(process, get(_)) .WillOnce(Invoke(validateGetWithoutQuery)); Future<http::Response> noQueryFuture = http::get(process.self(), "get"); AWAIT_READY(noQueryFuture); ASSERT_EQ(http::statuses[200], noQueryFuture.get().status); EXPECT_CALL(process, get(_)) .WillOnce(Invoke(validateGetWithQuery)); Future<http::Response> queryFuture = http::get(process.self(), "get", "foo=bar"); AWAIT_READY(queryFuture); ASSERT_EQ(http::statuses[200], queryFuture.get().status); terminate(process); wait(process); }
TEST(HTTP, Post) { ASSERT_TRUE(GTEST_IS_THREADSAFE); HttpProcess process; spawn(process); // Test the case where there is a content type but no body. Future<http::Response> future = http::post(process.self(), "post", None(), "text/plain"); AWAIT_EXPECT_FAILED(future); EXPECT_CALL(process, post(_)) .WillOnce(Invoke(validatePost)); future = http::post(process.self(), "post", "This is the payload.", "text/plain"); AWAIT_READY(future); ASSERT_EQ(http::statuses[200], future.get().status); terminate(process); wait(process); }
TEST(Process, http) { ASSERT_TRUE(GTEST_IS_THREADSAFE); HttpProcess process; EXPECT_CALL(process, handler(_)) .WillOnce(Return(http::OK())); spawn(process); int s = socket(AF_INET, SOCK_STREAM, IPPROTO_IP); ASSERT_LE(0, s); sockaddr_in addr; memset(&addr, 0, sizeof(addr)); addr.sin_family = PF_INET; addr.sin_port = htons(process.self().port); addr.sin_addr.s_addr = process.self().ip; ASSERT_EQ(0, connect(s, (sockaddr*) &addr, sizeof(addr))); std::ostringstream out; out << "GET /" << process.self().id << "/" << "handler" << " HTTP/1.0\r\n" << "Connection: Keep-Alive\r\n" << "\r\n"; const std::string& data = out.str(); ASSERT_EQ(data.size(), write(s, data.data(), data.size())); std::string response = "HTTP/1.1 200 OK"; char temp[response.size()]; ASSERT_LT(0, read(s, temp, response.size())); ASSERT_EQ(response, std::string(temp, response.size())); ASSERT_EQ(0, close(s)); terminate(process); wait(process); }
TEST(HTTP, auth) { ASSERT_TRUE(GTEST_IS_THREADSAFE); HttpProcess process; spawn(process); // Test the case where there is no auth. Future<http::Response> noAuthFuture = http::get(process.self(), "auth"); AWAIT_READY(noAuthFuture); EXPECT_EQ(http::statuses[401], noAuthFuture.get().status); ASSERT_SOME_EQ("Basic realm=\"testrealm\"", noAuthFuture.get().headers.get("WWW-authenticate")); // Now test passing wrong auth header. hashmap<string, string> headers; headers["Authorization"] = "Basic " + base64::encode("testuser:wrongpass"); Future<http::Response> wrongAuthFuture = http::get(process.self(), "auth", None(), headers); AWAIT_READY(wrongAuthFuture); EXPECT_EQ(http::statuses[401], wrongAuthFuture.get().status); ASSERT_SOME_EQ("Basic realm=\"testrealm\"", wrongAuthFuture.get().headers.get("WWW-authenticate")); // Now test passing right auth header. headers["Authorization"] = "Basic " + base64::encode("testuser:testpass"); Future<http::Response> rightAuthFuture = http::get(process.self(), "auth", None(), headers); AWAIT_READY(rightAuthFuture); EXPECT_EQ(http::statuses[200], rightAuthFuture.get().status); terminate(process); wait(process); }
TEST_F(FetcherTest, OSNetUriTest) { HttpProcess process; spawn(process); string url = "http://" + net::getHostname(process.self().node.ip).get() + ":" + stringify(process.self().node.port) + "/help"; string localFile = path::join(os::getcwd(), "help"); EXPECT_FALSE(os::exists(localFile)); slave::Flags flags; flags.frameworks_home = "/tmp/frameworks"; CommandInfo commandInfo; CommandInfo::URI* uri = commandInfo.add_uris(); uri->set_value(url); map<string, string> env = fetcher::environment(commandInfo, os::getcwd(), None(), flags); Try<Subprocess> fetcherProcess = process::subprocess( path::join(mesos::internal::tests::flags.build_dir, "src/mesos-fetcher"), env); ASSERT_SOME(fetcherProcess); Future<Option<int>> status = fetcherProcess.get().status(); AWAIT_READY(status); ASSERT_SOME(status.get()); EXPECT_EQ(0, status.get().get()); EXPECT_TRUE(os::exists(localFile)); }
TEST(HTTP, Endpoints) { ASSERT_TRUE(GTEST_IS_THREADSAFE); HttpProcess process; spawn(process); // First hit '/body' (using explicit sockets and HTTP/1.0). int s = socket(AF_INET, SOCK_STREAM, IPPROTO_IP); ASSERT_LE(0, s); sockaddr_in addr; memset(&addr, 0, sizeof(addr)); addr.sin_family = PF_INET; addr.sin_port = htons(process.self().port); addr.sin_addr.s_addr = process.self().ip; ASSERT_EQ(0, connect(s, (sockaddr*) &addr, sizeof(addr))); std::ostringstream out; out << "GET /" << process.self().id << "/body" << " HTTP/1.0\r\n" << "Connection: Keep-Alive\r\n" << "\r\n"; const std::string& data = out.str(); EXPECT_CALL(process, body(_)) .WillOnce(Return(http::OK())); ASSERT_SOME(os::write(s, data)); std::string response = "HTTP/1.1 200 OK"; char temp[response.size()]; ASSERT_LT(0, ::read(s, temp, response.size())); ASSERT_EQ(response, std::string(temp, response.size())); ASSERT_EQ(0, close(s)); // Now hit '/pipe' (by using http::get). int pipes[2]; ASSERT_NE(-1, ::pipe(pipes)); http::OK ok; ok.type = http::Response::PIPE; ok.pipe = pipes[0]; Future<Nothing> pipe; EXPECT_CALL(process, pipe(_)) .WillOnce(DoAll(FutureSatisfy(&pipe), Return(ok))); Future<http::Response> future = http::get(process.self(), "pipe"); AWAIT_READY(pipe); ASSERT_SOME(os::write(pipes[1], "Hello World\n")); ASSERT_SOME(os::close(pipes[1])); AWAIT_READY(future); ASSERT_EQ(http::statuses[200], future.get().status); ASSERT_EQ("chunked", future.get().headers["Transfer-Encoding"]); ASSERT_EQ("Hello World\n", future.get().body); terminate(process); wait(process); }
TEST(HTTP, Endpoints) { ASSERT_TRUE(GTEST_IS_THREADSAFE); HttpProcess process; spawn(process); // First hit '/body' (using explicit sockets and HTTP/1.0). Try<Socket> create = Socket::create(); ASSERT_SOME(create); Socket socket = create.get(); AWAIT_READY(socket.connect(process.self().address)); std::ostringstream out; out << "GET /" << process.self().id << "/body" << " HTTP/1.0\r\n" << "Connection: Keep-Alive\r\n" << "\r\n"; const string& data = out.str(); EXPECT_CALL(process, body(_)) .WillOnce(Return(http::OK())); AWAIT_READY(socket.send(data)); string response = "HTTP/1.1 200 OK"; AWAIT_EXPECT_EQ(response, socket.recv(response.size())); // Now hit '/pipe' (by using http::get). int pipes[2]; ASSERT_NE(-1, ::pipe(pipes)); http::OK ok; ok.type = http::Response::PIPE; ok.pipe = pipes[0]; Future<Nothing> pipe; EXPECT_CALL(process, pipe(_)) .WillOnce(DoAll(FutureSatisfy(&pipe), Return(ok))); Future<http::Response> future = http::get(process.self(), "pipe"); AWAIT_READY(pipe); ASSERT_SOME(os::write(pipes[1], "Hello World\n")); ASSERT_SOME(os::close(pipes[1])); AWAIT_READY(future); EXPECT_EQ(http::statuses[200], future.get().status); EXPECT_SOME_EQ("chunked", future.get().headers.get("Transfer-Encoding")); EXPECT_EQ("Hello World\n", future.get().body); terminate(process); wait(process); }