You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mesos.apache.org by be...@apache.org on 2014/05/22 02:21:59 UTC

git commit: Unauthorized HTTP Response + tests.

Repository: mesos
Updated Branches:
  refs/heads/master f9ff5f958 -> 4c15fd065


Unauthorized HTTP Response + tests.

Review: https://reviews.apache.org/r/21324


Project: http://git-wip-us.apache.org/repos/asf/mesos/repo
Commit: http://git-wip-us.apache.org/repos/asf/mesos/commit/4c15fd06
Tree: http://git-wip-us.apache.org/repos/asf/mesos/tree/4c15fd06
Diff: http://git-wip-us.apache.org/repos/asf/mesos/diff/4c15fd06

Branch: refs/heads/master
Commit: 4c15fd0650ba20dea0508a27acdc6aa9245ca6c5
Parents: f9ff5f9
Author: Isabel Jimenez <co...@isabeljimenez.com>
Authored: Wed May 21 17:19:58 2014 -0700
Committer: Benjamin Hindman <be...@gmail.com>
Committed: Wed May 21 17:21:30 2014 -0700

----------------------------------------------------------------------
 3rdparty/libprocess/include/process/http.hpp | 15 +++++++
 3rdparty/libprocess/src/tests/http_tests.cpp | 54 +++++++++++++++++++++++
 2 files changed, 69 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mesos/blob/4c15fd06/3rdparty/libprocess/include/process/http.hpp
----------------------------------------------------------------------
diff --git a/3rdparty/libprocess/include/process/http.hpp b/3rdparty/libprocess/include/process/http.hpp
index 06f2596..6358e33 100644
--- a/3rdparty/libprocess/include/process/http.hpp
+++ b/3rdparty/libprocess/include/process/http.hpp
@@ -232,6 +232,21 @@ struct TemporaryRedirect : Response
   }
 };
 
+struct Unauthorized : Response
+{
+  Unauthorized(const std::string& realm)
+  {
+    status = "401 Unauthorized";
+    headers["WWW-authenticate"] = "Basic realm=\"" + realm + "\"";
+  }
+
+  Unauthorized(const std::string& realm, const std::string& body)
+    : Response(body)
+  {
+    status = "401 Unauthorized";
+    headers["WWW-authenticate"] = "Basic realm=\"" + realm + "\"";
+  }
+};
 
 struct BadRequest : Response
 {

http://git-wip-us.apache.org/repos/asf/mesos/blob/4c15fd06/3rdparty/libprocess/src/tests/http_tests.cpp
----------------------------------------------------------------------
diff --git a/3rdparty/libprocess/src/tests/http_tests.cpp b/3rdparty/libprocess/src/tests/http_tests.cpp
index f58a129..a1c3685 100644
--- a/3rdparty/libprocess/src/tests/http_tests.cpp
+++ b/3rdparty/libprocess/src/tests/http_tests.cpp
@@ -13,6 +13,7 @@
 #include <process/http.hpp>
 #include <process/io.hpp>
 
+#include <stout/base64.hpp>
 #include <stout/gtest.hpp>
 #include <stout/none.hpp>
 #include <stout/nothing.hpp>
@@ -37,12 +38,23 @@ class HttpProcess : public Process<HttpProcess>
 public:
   HttpProcess()
   {
+    route("/auth", None(), &HttpProcess::auth);
     route("/body", None(), &HttpProcess::body);
     route("/pipe", None(), &HttpProcess::pipe);
     route("/get", None(), &HttpProcess::get);
     route("/post", None(), &HttpProcess::post);
   }
 
+  Future<http::Response> auth(const http::Request& request)
+  {
+    string encodedAuth = base64::encode("testuser:testpass");
+    Option<string> authHeader = request.headers.get("Authorization");
+    if (!authHeader.isSome() || (authHeader.get() != "Basic " + encodedAuth)) {
+      return http::Unauthorized("testrealm");
+    }
+    return http::OK();
+  }
+
   MOCK_METHOD1(body, Future<http::Response>(const http::Request&));
   MOCK_METHOD1(pipe, Future<http::Response>(const http::Request&));
   MOCK_METHOD1(get, Future<http::Response>(const http::Request&));
@@ -50,6 +62,48 @@ public:
 };
 
 
+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(HTTP, Endpoints)
 {
   ASSERT_TRUE(GTEST_IS_THREADSAFE);