]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[#1764] Shared http and tls
authorFrancis Dupont <fdupont@isc.org>
Sat, 3 Aug 2024 11:07:34 +0000 (13:07 +0200)
committerFrancis Dupont <fdupont@isc.org>
Thu, 22 Aug 2024 08:23:03 +0000 (10:23 +0200)
src/lib/http/tests/Makefile.am
src/lib/http/tests/http_client_unittests.cc
src/lib/http/tests/http_response_creator_test.h [new file with mode: 0644]
src/lib/http/tests/http_server_unittests.cc
src/lib/http/tests/http_tests.h
src/lib/http/tests/tls_client_unittests.cc
src/lib/http/tests/tls_response_creator_test.h [new file with mode: 0644]
src/lib/http/tests/tls_server_unittests.cc

index cb07e41bfc2871dc2eeafee71855807bf18ab21a..04683e1ba07b4eddcf3f6ba473d301c658955024 100644 (file)
@@ -44,6 +44,7 @@ libhttp_unittests_SOURCES += response_unittests.cc
 libhttp_unittests_SOURCES += response_json_unittests.cc
 libhttp_unittests_SOURCES += run_unittests.cc
 libhttp_unittests_SOURCES += http_tests.h
+libhttp_unittests_SOURCES += http_response_creator_test.h
 libhttp_unittests_SOURCES += http_client_unittests.cc
 libhttp_unittests_SOURCES += http_server_unittests.cc
 if HAVE_OPENSSL
@@ -51,6 +52,7 @@ libhttp_unittests_SOURCES += tls_server_unittests.cc
 libhttp_unittests_SOURCES += tls_client_unittests.cc
 endif
 if HAVE_BOTAN_BOOST
+libhttp_unittests_SOURCES += tls_response_creator_test.h
 libhttp_unittests_SOURCES += tls_server_unittests.cc
 libhttp_unittests_SOURCES += tls_client_unittests.cc
 endif
index 355f6f0b841171df79e8d540e251da0ac1cdf49f..dac63a8e1a15aae9083e6991ba1c8c7d758fb786 100644 (file)
@@ -21,7 +21,6 @@
 #include <http/tests/response_test.h>
 #include <http/url.h>
 #include <util/multi_threading_mgr.h>
-#include <http/tests/http_tests.h>
 
 #include <boost/asio/buffer.hpp>
 #include <boost/asio/ip/tcp.hpp>
 #include <sstream>
 #include <string>
 
+// Keep order of these headers.
+#include <http/tests/http_tests.h>
+#include <http/tests/http_response_creator_test.h>
+
 using namespace boost::asio::ip;
 using namespace isc::asiolink;
 using namespace isc::data;
@@ -43,127 +46,6 @@ namespace ph = std::placeholders;
 
 namespace {
 
-/// @brief Implementation of the @ref HttpResponseCreator.
-class TestHttpResponseCreator : public HttpResponseCreator {
-public:
-
-    /// @brief Create a new request.
-    ///
-    /// @return Pointer to the new instance of the @ref HttpRequest.
-    virtual HttpRequestPtr
-    createNewHttpRequest() const {
-        return (HttpRequestPtr(new PostHttpRequestJson()));
-    }
-
-private:
-
-    /// @brief Creates HTTP response.
-    ///
-    /// @param request Pointer to the HTTP request.
-    /// @return Pointer to the generated HTTP response.
-    virtual HttpResponsePtr
-    createStockHttpResponse(const HttpRequestPtr& request,
-                            const HttpStatusCode& status_code) const {
-        // The request hasn't been finalized so the request object
-        // doesn't contain any information about the HTTP version number
-        // used. But, the context should have this data (assuming the
-        // HTTP version is parsed ok).
-        HttpVersion http_version(request->context()->http_version_major_,
-                                 request->context()->http_version_minor_);
-        // This will generate the response holding JSON content.
-        ResponsePtr response(new Response(http_version, status_code));
-        response->finalize();
-        return (response);
-    }
-
-    /// @brief Creates HTTP response.
-    ///
-    /// This method generates 3 types of responses:
-    /// - response with a requested content type,
-    /// - partial response with incomplete JSON body,
-    /// - response with JSON body copied from the request.
-    ///
-    /// The first one is useful to test situations when received response can't
-    /// be parsed because of the content type mismatch. The second one is useful
-    /// to test request timeouts. The third type is used by most of the unit tests
-    /// to test successful transactions.
-    ///
-    /// @param request Pointer to the HTTP request.
-    /// @return Pointer to the generated HTTP OK response with no content.
-    virtual HttpResponsePtr
-    createDynamicHttpResponse(HttpRequestPtr request) {
-        // Request must always be JSON.
-        PostHttpRequestJsonPtr request_json =
-            boost::dynamic_pointer_cast<PostHttpRequestJson>(request);
-        ConstElementPtr body;
-        if (request_json) {
-            body = request_json->getBodyAsJson();
-            if (body) {
-                // Check if the client requested one of the two first response
-                // types.
-                GenericResponsePtr response;
-                ConstElementPtr content_type = body->get("requested-content-type");
-                ConstElementPtr partial_response = body->get("partial-response");
-                if (content_type || partial_response) {
-                    // The first two response types can only be generated using the
-                    // generic response as we have to explicitly modify some of the
-                    // values.
-                    response.reset(new GenericResponse(request->getHttpVersion(),
-                                                       HttpStatusCode::OK));
-                    HttpResponseContextPtr ctx = response->context();
-
-                    if (content_type) {
-                        // Provide requested content type.
-                        ctx->headers_.push_back(HttpHeaderContext("Content-Type",
-                                                                  content_type->stringValue()));
-                        // It doesn't matter what body is there.
-                        ctx->body_ = "abcd";
-                        response->finalize();
-
-                    } else {
-                        // Generate JSON response.
-                        ctx->headers_.push_back(HttpHeaderContext("Content-Type",
-                                                                  "application/json"));
-                        // The body lacks '}' so the client will be waiting for it and
-                        // eventually should time out.
-                        ctx->body_ = "{";
-                        response->finalize();
-                        // The auto generated Content-Length header would be based on the
-                        // body size (so set to 1 byte). We have to override it to
-                        // account for the missing '}' character.
-                        response->setContentLength(2);
-                    }
-                    return (response);
-                }
-            }
-        }
-
-        // Third type of response is requested.
-        ResponsePtr response(new Response(request->getHttpVersion(),
-                                          HttpStatusCode::OK));
-        // If body was included in the request. Let's copy it.
-        if (body) {
-            response->setBodyAsJson(body);
-        }
-
-        response->finalize();
-        return (response);
-    }
-};
-
-/// @brief Implementation of the test @ref HttpResponseCreatorFactory.
-///
-/// This factory class creates @ref TestHttpResponseCreator instances.
-class TestHttpResponseCreatorFactory : public HttpResponseCreatorFactory {
-public:
-
-    /// @brief Creates @ref TestHttpResponseCreator instance.
-    virtual HttpResponseCreatorPtr create() const {
-        HttpResponseCreatorPtr response_creator(new TestHttpResponseCreator());
-        return (response_creator);
-    }
-};
-
 /// @brief Test fixture class for @ref HttpListener.
 class HttpListenerTest : public ::testing::Test {
 public:
diff --git a/src/lib/http/tests/http_response_creator_test.h b/src/lib/http/tests/http_response_creator_test.h
new file mode 100644 (file)
index 0000000..8a984f1
--- /dev/null
@@ -0,0 +1,140 @@
+// Copyright (C) 2017-2024 Internet Systems Consortium, Inc. ("ISC")
+//
+// This Source Code Form is subject to the terms of the Mozilla Public
+// License, v. 2.0. If a copy of the MPL was not distributed with this
+// file, You can obtain one at http://mozilla.org/MPL/2.0/.
+
+#ifndef HTTP_RESPONSE_CREATOR_TEST_H
+#define HTTP_RESPONSE_CREATOR_TEST_H
+
+namespace isc {
+namespace http {
+namespace test {
+
+/// @brief Implementation of the @ref HttpResponseCreator.
+class TestHttpResponseCreator : public HttpResponseCreator {
+public:
+
+    /// @brief Create a new request.
+    ///
+    /// @return Pointer to the new instance of the @ref HttpRequest.
+    virtual HttpRequestPtr
+    createNewHttpRequest() const {
+        return (HttpRequestPtr(new PostHttpRequestJson()));
+    }
+
+private:
+
+    /// @brief Creates HTTP response.
+    ///
+    /// @param request Pointer to the HTTP request.
+    /// @return Pointer to the generated HTTP response.
+    virtual HttpResponsePtr
+    createStockHttpResponse(const HttpRequestPtr& request,
+                            const HttpStatusCode& status_code) const {
+        // The request hasn't been finalized so the request object
+        // doesn't contain any information about the HTTP version number
+        // used. But, the context should have this data (assuming the
+        // HTTP version is parsed ok).
+        HttpVersion http_version(request->context()->http_version_major_,
+                                 request->context()->http_version_minor_);
+        // This will generate the response holding JSON content.
+        ResponsePtr response(new Response(http_version, status_code));
+        response->finalize();
+        return (response);
+    }
+
+    /// @brief Creates HTTP response.
+    ///
+    /// This method generates 3 types of responses:
+    /// - response with a requested content type,
+    /// - partial response with incomplete JSON body,
+    /// - response with JSON body copied from the request.
+    ///
+    /// The first one is useful to test situations when received response can't
+    /// be parsed because of the content type mismatch. The second one is useful
+    /// to test request timeouts. The third type is used by most of the unit tests
+    /// to test successful transactions.
+    ///
+    /// @param request Pointer to the HTTP request.
+    /// @return Pointer to the generated HTTP OK response with no content.
+    virtual HttpResponsePtr
+    createDynamicHttpResponse(HttpRequestPtr request) {
+        // Request must always be JSON.
+        PostHttpRequestJsonPtr request_json =
+            boost::dynamic_pointer_cast<PostHttpRequestJson>(request);
+        data::ConstElementPtr body;
+        if (request_json) {
+            body = request_json->getBodyAsJson();
+            if (body) {
+                // Check if the client requested one of the two first response
+                // types.
+                GenericResponsePtr response;
+                data::ConstElementPtr content_type =
+                    body->get("requested-content-type");
+                data::ConstElementPtr partial_response =
+                    body->get("partial-response");
+                if (content_type || partial_response) {
+                    // The first two response types can only be generated using the
+                    // generic response as we have to explicitly modify some of the
+                    // values.
+                    response.reset(new GenericResponse(request->getHttpVersion(),
+                                                       HttpStatusCode::OK));
+                    HttpResponseContextPtr ctx = response->context();
+
+                    if (content_type) {
+                        // Provide requested content type.
+                        ctx->headers_.push_back(HttpHeaderContext("Content-Type",
+                                                                  content_type->stringValue()));
+                        // It doesn't matter what body is there.
+                        ctx->body_ = "abcd";
+                        response->finalize();
+
+                    } else {
+                        // Generate JSON response.
+                        ctx->headers_.push_back(HttpHeaderContext("Content-Type",
+                                                                  "application/json"));
+                        // The body lacks '}' so the client will be waiting for it and
+                        // eventually should time out.
+                        ctx->body_ = "{";
+                        response->finalize();
+                        // The auto generated Content-Length header would be based on the
+                        // body size (so set to 1 byte). We have to override it to
+                        // account for the missing '}' character.
+                        response->setContentLength(2);
+                    }
+                    return (response);
+                }
+            }
+        }
+
+        // Third type of response is requested.
+        ResponsePtr response(new Response(request->getHttpVersion(),
+                                          HttpStatusCode::OK));
+        // If body was included in the request. Let's copy it.
+        if (body) {
+            response->setBodyAsJson(body);
+        }
+
+        response->finalize();
+        return (response);
+    }
+};
+
+/// @brief Implementation of the test @ref HttpResponseCreatorFactory.
+///
+/// This factory class creates @ref TestHttpResponseCreator instances.
+class TestHttpResponseCreatorFactory : public HttpResponseCreatorFactory {
+public:
+
+    /// @brief Creates @ref TestHttpResponseCreator instance.
+    virtual HttpResponseCreatorPtr create() const {
+        HttpResponseCreatorPtr response_creator(new TestHttpResponseCreator());
+        return (response_creator);
+    }
+};
+
+}
+}
+}
+#endif // HTTP_RESPONSE_CREATOR_TEST_H
index 60e7fc66373644bd078fedc3d00ff60ed2578e37..708eb50791572281edf528f045d1a230568cdd2d 100644 (file)
@@ -21,7 +21,6 @@
 #include <http/tests/response_test.h>
 #include <http/url.h>
 #include <util/multi_threading_mgr.h>
-#include <http/tests/http_tests.h>
 
 #include <boost/asio/buffer.hpp>
 #include <boost/asio/ip/tcp.hpp>
 #include <sstream>
 #include <string>
 
+// Keep order of these headers.
+#include <http/tests/http_tests.h>
+#include <http/tests/http_response_creator_test.h>
+
 using namespace boost::asio::ip;
 using namespace isc::asiolink;
 using namespace isc::data;
@@ -43,127 +46,6 @@ namespace ph = std::placeholders;
 
 namespace {
 
-/// @brief Implementation of the @ref HttpResponseCreator.
-class TestHttpResponseCreator : public HttpResponseCreator {
-public:
-
-    /// @brief Create a new request.
-    ///
-    /// @return Pointer to the new instance of the @ref HttpRequest.
-    virtual HttpRequestPtr
-    createNewHttpRequest() const {
-        return (HttpRequestPtr(new PostHttpRequestJson()));
-    }
-
-private:
-
-    /// @brief Creates HTTP response.
-    ///
-    /// @param request Pointer to the HTTP request.
-    /// @return Pointer to the generated HTTP response.
-    virtual HttpResponsePtr
-    createStockHttpResponse(const HttpRequestPtr& request,
-                            const HttpStatusCode& status_code) const {
-        // The request hasn't been finalized so the request object
-        // doesn't contain any information about the HTTP version number
-        // used. But, the context should have this data (assuming the
-        // HTTP version is parsed ok).
-        HttpVersion http_version(request->context()->http_version_major_,
-                                 request->context()->http_version_minor_);
-        // This will generate the response holding JSON content.
-        ResponsePtr response(new Response(http_version, status_code));
-        response->finalize();
-        return (response);
-    }
-
-    /// @brief Creates HTTP response.
-    ///
-    /// This method generates 3 types of responses:
-    /// - response with a requested content type,
-    /// - partial response with incomplete JSON body,
-    /// - response with JSON body copied from the request.
-    ///
-    /// The first one is useful to test situations when received response can't
-    /// be parsed because of the content type mismatch. The second one is useful
-    /// to test request timeouts. The third type is used by most of the unit tests
-    /// to test successful transactions.
-    ///
-    /// @param request Pointer to the HTTP request.
-    /// @return Pointer to the generated HTTP OK response with no content.
-    virtual HttpResponsePtr
-    createDynamicHttpResponse(HttpRequestPtr request) {
-        // Request must always be JSON.
-        PostHttpRequestJsonPtr request_json =
-            boost::dynamic_pointer_cast<PostHttpRequestJson>(request);
-        ConstElementPtr body;
-        if (request_json) {
-            body = request_json->getBodyAsJson();
-            if (body) {
-                // Check if the client requested one of the two first response
-                // types.
-                GenericResponsePtr response;
-                ConstElementPtr content_type = body->get("requested-content-type");
-                ConstElementPtr partial_response = body->get("partial-response");
-                if (content_type || partial_response) {
-                    // The first two response types can only be generated using the
-                    // generic response as we have to explicitly modify some of the
-                    // values.
-                    response.reset(new GenericResponse(request->getHttpVersion(),
-                                                       HttpStatusCode::OK));
-                    HttpResponseContextPtr ctx = response->context();
-
-                    if (content_type) {
-                        // Provide requested content type.
-                        ctx->headers_.push_back(HttpHeaderContext("Content-Type",
-                                                                  content_type->stringValue()));
-                        // It doesn't matter what body is there.
-                        ctx->body_ = "abcd";
-                        response->finalize();
-
-                    } else {
-                        // Generate JSON response.
-                        ctx->headers_.push_back(HttpHeaderContext("Content-Type",
-                                                                  "application/json"));
-                        // The body lacks '}' so the client will be waiting for it and
-                        // eventually should time out.
-                        ctx->body_ = "{";
-                        response->finalize();
-                        // The auto generated Content-Length header would be based on the
-                        // body size (so set to 1 byte). We have to override it to
-                        // account for the missing '}' character.
-                        response->setContentLength(2);
-                    }
-                    return (response);
-                }
-            }
-        }
-
-        // Third type of response is requested.
-        ResponsePtr response(new Response(request->getHttpVersion(),
-                                          HttpStatusCode::OK));
-        // If body was included in the request. Let's copy it.
-        if (body) {
-            response->setBodyAsJson(body);
-        }
-
-        response->finalize();
-        return (response);
-    }
-};
-
-/// @brief Implementation of the test @ref HttpResponseCreatorFactory.
-///
-/// This factory class creates @ref TestHttpResponseCreator instances.
-class TestHttpResponseCreatorFactory : public HttpResponseCreatorFactory {
-public:
-
-    /// @brief Creates @ref TestHttpResponseCreator instance.
-    virtual HttpResponseCreatorPtr create() const {
-        HttpResponseCreatorPtr response_creator(new TestHttpResponseCreator());
-        return (response_creator);
-    }
-};
-
 /// @brief Implementation of the HTTP listener used in tests.
 ///
 /// This implementation replaces the @c HttpConnection type with a custom
index 08219a96d98d6f9c65fcab76bfadf112c8129225..d1c23d7f3cb6dbf7ca52b4b8f09525b861988112 100644 (file)
@@ -4,8 +4,12 @@
 // License, v. 2.0. If a copy of the MPL was not distributed with this
 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
 
+#ifndef HTTP_TEST_H
+#define HTTP_TEST_H
+
 namespace isc {
 namespace http {
+namespace test {
 
 /// @brief IP address to which HTTP service is bound.
 const std::string SERVER_ADDRESS = "127.0.0.1";
@@ -30,16 +34,18 @@ const long SHORT_IDLE_TIMEOUT = 200;
 const long TEST_TIMEOUT = 10000;
 
 /// @brief Test HTTP response.
-typedef test::TestHttpResponseBase<HttpResponseJson> Response;
+typedef TestHttpResponseBase<HttpResponseJson> Response;
 
 /// @brief Pointer to test HTTP response.
 typedef boost::shared_ptr<Response> ResponsePtr;
 
 /// @brief Generic test HTTP response.
-typedef test::TestHttpResponseBase<HttpResponse> GenericResponse;
+typedef TestHttpResponseBase<HttpResponse> GenericResponse;
 
 /// @brief Pointer to generic test HTTP response.
 typedef boost::shared_ptr<GenericResponse> GenericResponsePtr;
 
 }
 }
+}
+#endif // HTTP_TEST_H
index 66638ace76a61a28c8fd967f6b741ed0180ef042..6ee8e93ddf7fe30a279a012cae523ee59cdfa314 100644 (file)
@@ -21,7 +21,6 @@
 #include <http/tests/response_test.h>
 #include <http/url.h>
 #include <util/multi_threading_mgr.h>
-#include <http/tests/http_tests.h>
 
 #include <boost/asio/buffer.hpp>
 #include <boost/asio/ip/tcp.hpp>
 #include <sstream>
 #include <string>
 
+// Keep order of these headers.
+#include <http/tests/http_tests.h>
+#include <http/tests/tls_response_creator_test.h>
+
 #ifdef WITH_BOTAN
 #define DISABLE_SOME_TESTS
 #endif
@@ -54,136 +57,6 @@ namespace ph = std::placeholders;
 
 namespace {
 
-/// @brief Implementation of the @ref HttpResponseCreator.
-class TestHttpResponseCreator : public HttpResponseCreator {
-public:
-
-    /// @brief Create a new request.
-    ///
-    /// @return Pointer to the new instance of the @ref HttpRequest.
-    virtual HttpRequestPtr
-    createNewHttpRequest() const {
-        return (HttpRequestPtr(new PostHttpRequestJson()));
-    }
-
-private:
-
-    /// @brief Creates HTTP response.
-    ///
-    /// @param request Pointer to the HTTP request.
-    /// @return Pointer to the generated HTTP response.
-    virtual HttpResponsePtr
-    createStockHttpResponse(const HttpRequestPtr& request,
-                            const HttpStatusCode& status_code) const {
-        // The request hasn't been finalized so the request object
-        // doesn't contain any information about the HTTP version number
-        // used. But, the context should have this data (assuming the
-        // HTTP version is parsed ok).
-        HttpVersion http_version(request->context()->http_version_major_,
-                                 request->context()->http_version_minor_);
-        // This will generate the response holding JSON content.
-        ResponsePtr response(new Response(http_version, status_code));
-        response->finalize();
-        return (response);
-    }
-
-    /// @brief Creates HTTP response.
-    ///
-    /// This method generates 3 types of responses:
-    /// - response with a requested content type,
-    /// - partial response with incomplete JSON body,
-    /// - response with JSON body copied from the request.
-    ///
-    /// The first one is useful to test situations when received response can't
-    /// be parsed because of the content type mismatch. The second one is useful
-    /// to test request timeouts. The third type is used by most of the unit tests
-    /// to test successful transactions.
-    ///
-    /// @param request Pointer to the HTTP request.
-    /// @return Pointer to the generated HTTP OK response with no content.
-    virtual HttpResponsePtr
-    createDynamicHttpResponse(HttpRequestPtr request) {
-        // Check access parameters.
-        if (HttpRequest::recordSubject_) {
-            EXPECT_TRUE(request->getTls());
-            EXPECT_EQ("kea-client", request->getSubject());
-        }
-        if (HttpRequest::recordIssuer_) {
-            EXPECT_TRUE(request->getTls());
-            EXPECT_EQ("kea-ca", request->getIssuer());
-        }
-        // Request must always be JSON.
-        PostHttpRequestJsonPtr request_json =
-            boost::dynamic_pointer_cast<PostHttpRequestJson>(request);
-        ConstElementPtr body;
-        if (request_json) {
-            body = request_json->getBodyAsJson();
-            if (body) {
-                // Check if the client requested one of the two first response
-                // types.
-                GenericResponsePtr response;
-                ConstElementPtr content_type = body->get("requested-content-type");
-                ConstElementPtr partial_response = body->get("partial-response");
-                if (content_type || partial_response) {
-                    // The first two response types can only be generated using the
-                    // generic response as we have to explicitly modify some of the
-                    // values.
-                    response.reset(new GenericResponse(request->getHttpVersion(),
-                                                       HttpStatusCode::OK));
-                    HttpResponseContextPtr ctx = response->context();
-
-                    if (content_type) {
-                        // Provide requested content type.
-                        ctx->headers_.push_back(HttpHeaderContext("Content-Type",
-                                                                  content_type->stringValue()));
-                        // It doesn't matter what body is there.
-                        ctx->body_ = "abcd";
-                        response->finalize();
-
-                    } else {
-                        // Generate JSON response.
-                        ctx->headers_.push_back(HttpHeaderContext("Content-Type",
-                                                                  "application/json"));
-                        // The body lacks '}' so the client will be waiting for it and
-                        // eventually should time out.
-                        ctx->body_ = "{";
-                        response->finalize();
-                        // The auto generated Content-Length header would be based on the
-                        // body size (so set to 1 byte). We have to override it to
-                        // account for the missing '}' character.
-                        response->setContentLength(2);
-                    }
-                    return (response);
-                }
-            }
-        }
-
-        // Third type of response is requested.
-        ResponsePtr response(new Response(request->getHttpVersion(),
-                                          HttpStatusCode::OK));
-        // If body was included in the request. Let's copy it.
-        if (body) {
-            response->setBodyAsJson(body);
-        }
-
-        response->finalize();
-        return (response);
-    }
-};
-
-/// @brief Implementation of the test @ref HttpResponseCreatorFactory.
-///
-/// This factory class creates @ref TestHttpResponseCreator instances.
-class TestHttpResponseCreatorFactory : public HttpResponseCreatorFactory {
-public:
-
-    /// @brief Creates @ref TestHttpResponseCreator instance.
-    virtual HttpResponseCreatorPtr create() const {
-        HttpResponseCreatorPtr response_creator(new TestHttpResponseCreator());
-        return (response_creator);
-    }
-};
-
 /// @brief Test fixture class for @ref HttpListener.
 class HttpListenerTest : public ::testing::Test {
 public:
diff --git a/src/lib/http/tests/tls_response_creator_test.h b/src/lib/http/tests/tls_response_creator_test.h
new file mode 100644 (file)
index 0000000..55f3f84
--- /dev/null
@@ -0,0 +1,149 @@
+// Copyright (C) 2017-2024 Internet Systems Consortium, Inc. ("ISC")
+//
+// This Source Code Form is subject to the terms of the Mozilla Public
+// License, v. 2.0. If a copy of the MPL was not distributed with this
+// file, You can obtain one at http://mozilla.org/MPL/2.0/.
+
+#ifndef TLS_RESPONSE_CREATOR_TEST_H
+#define TLS_RESPONSE_CREATOR_TEST_H
+
+namespace isc {
+namespace http {
+namespace test {
+
+/// @brief Implementation of the @ref HttpResponseCreator.
+class TestHttpResponseCreator : public HttpResponseCreator {
+public:
+
+    /// @brief Create a new request.
+    ///
+    /// @return Pointer to the new instance of the @ref HttpRequest.
+    virtual HttpRequestPtr
+    createNewHttpRequest() const {
+        return (HttpRequestPtr(new PostHttpRequestJson()));
+    }
+
+private:
+
+    /// @brief Creates HTTP response.
+    ///
+    /// @param request Pointer to the HTTP request.
+    /// @return Pointer to the generated HTTP response.
+    virtual HttpResponsePtr
+    createStockHttpResponse(const HttpRequestPtr& request,
+                            const HttpStatusCode& status_code) const {
+        // The request hasn't been finalized so the request object
+        // doesn't contain any information about the HTTP version number
+        // used. But, the context should have this data (assuming the
+        // HTTP version is parsed ok).
+        HttpVersion http_version(request->context()->http_version_major_,
+                                 request->context()->http_version_minor_);
+        // This will generate the response holding JSON content.
+        ResponsePtr response(new Response(http_version, status_code));
+        response->finalize();
+        return (response);
+    }
+
+    /// @brief Creates HTTP response.
+    ///
+    /// This method generates 3 types of responses:
+    /// - response with a requested content type,
+    /// - partial response with incomplete JSON body,
+    /// - response with JSON body copied from the request.
+    ///
+    /// The first one is useful to test situations when received response can't
+    /// be parsed because of the content type mismatch. The second one is useful
+    /// to test request timeouts. The third type is used by most of the unit tests
+    /// to test successful transactions.
+    ///
+    /// @param request Pointer to the HTTP request.
+    /// @return Pointer to the generated HTTP OK response with no content.
+    virtual HttpResponsePtr
+    createDynamicHttpResponse(HttpRequestPtr request) {
+        // Check access parameters.
+        if (HttpRequest::recordSubject_) {
+            EXPECT_TRUE(request->getTls());
+            EXPECT_EQ("kea-client", request->getSubject());
+        }
+        if (HttpRequest::recordIssuer_) {
+            EXPECT_TRUE(request->getTls());
+            EXPECT_EQ("kea-ca", request->getIssuer());
+        }
+        // Request must always be JSON.
+        PostHttpRequestJsonPtr request_json =
+            boost::dynamic_pointer_cast<PostHttpRequestJson>(request);
+        data::ConstElementPtr body;
+        if (request_json) {
+            body = request_json->getBodyAsJson();
+            if (body) {
+                // Check if the client requested one of the two first response
+                // types.
+                GenericResponsePtr response;
+                data::ConstElementPtr content_type =
+                    body->get("requested-content-type");
+                data::ConstElementPtr partial_response =
+                    body->get("partial-response");
+                if (content_type || partial_response) {
+                    // The first two response types can only be generated using the
+                    // generic response as we have to explicitly modify some of the
+                    // values.
+                    response.reset(new GenericResponse(request->getHttpVersion(),
+                                                       HttpStatusCode::OK));
+                    HttpResponseContextPtr ctx = response->context();
+
+                    if (content_type) {
+                        // Provide requested content type.
+                        ctx->headers_.push_back(HttpHeaderContext("Content-Type",
+                                                                  content_type->stringValue()));
+                        // It doesn't matter what body is there.
+                        ctx->body_ = "abcd";
+                        response->finalize();
+
+                    } else {
+                        // Generate JSON response.
+                        ctx->headers_.push_back(HttpHeaderContext("Content-Type",
+                                                                  "application/json"));
+                        // The body lacks '}' so the client will be waiting for it and
+                        // eventually should time out.
+                        ctx->body_ = "{";
+                        response->finalize();
+                        // The auto generated Content-Length header would be based on the
+                        // body size (so set to 1 byte). We have to override it to
+                        // account for the missing '}' character.
+                        response->setContentLength(2);
+                    }
+                    return (response);
+                }
+            }
+        }
+
+        // Third type of response is requested.
+        ResponsePtr response(new Response(request->getHttpVersion(),
+                                          HttpStatusCode::OK));
+        // If body was included in the request. Let's copy it.
+        if (body) {
+            response->setBodyAsJson(body);
+        }
+
+        response->finalize();
+        return (response);
+    }
+};
+
+/// @brief Implementation of the test @ref HttpResponseCreatorFactory.
+///
+/// This factory class creates @ref TestHttpResponseCreator instances.
+class TestHttpResponseCreatorFactory : public HttpResponseCreatorFactory {
+public:
+
+    /// @brief Creates @ref TestHttpResponseCreator instance.
+    virtual HttpResponseCreatorPtr create() const {
+        HttpResponseCreatorPtr response_creator(new TestHttpResponseCreator());
+        return (response_creator);
+    }
+};
+
+}
+}
+}
+#endif // TLS_RESPONSE_CREATOR_TEST_H
index 82a77abc3644c9c0b51af86fff48bd98eb0bc7fe..b11e0d9f7150cd0cf30c5ca3fcaa95a215a019c1 100644 (file)
@@ -22,7 +22,6 @@
 #include <http/testutils/test_http_client.h>
 #include <http/url.h>
 #include <util/multi_threading_mgr.h>
-#include <http/tests/http_tests.h>
 
 #include <boost/asio/buffer.hpp>
 #include <boost/asio/ip/tcp.hpp>
 #include <sstream>
 #include <string>
 
+// Keep order of these headers.
+#include <http/tests/http_tests.h>
+#include <http/tests/tls_response_creator_test.h>
+
 using namespace boost::asio;
 using namespace boost::asio::ip;
 using namespace isc::asiolink;
@@ -45,127 +48,6 @@ using namespace isc::util;
 
 namespace {
 
-/// @brief Implementation of the @ref HttpResponseCreator.
-class TestHttpResponseCreator : public HttpResponseCreator {
-public:
-
-    /// @brief Create a new request.
-    ///
-    /// @return Pointer to the new instance of the @ref HttpRequest.
-    virtual HttpRequestPtr
-    createNewHttpRequest() const {
-        return (HttpRequestPtr(new PostHttpRequestJson()));
-    }
-
-private:
-
-    /// @brief Creates HTTP response.
-    ///
-    /// @param request Pointer to the HTTP request.
-    /// @return Pointer to the generated HTTP response.
-    virtual HttpResponsePtr
-    createStockHttpResponse(const HttpRequestPtr& request,
-                            const HttpStatusCode& status_code) const {
-        // The request hasn't been finalized so the request object
-        // doesn't contain any information about the HTTP version number
-        // used. But, the context should have this data (assuming the
-        // HTTP version is parsed ok).
-        HttpVersion http_version(request->context()->http_version_major_,
-                                 request->context()->http_version_minor_);
-        // This will generate the response holding JSON content.
-        ResponsePtr response(new Response(http_version, status_code));
-        response->finalize();
-        return (response);
-    }
-
-    /// @brief Creates HTTP response.
-    ///
-    /// This method generates 3 types of responses:
-    /// - response with a requested content type,
-    /// - partial response with incomplete JSON body,
-    /// - response with JSON body copied from the request.
-    ///
-    /// The first one is useful to test situations when received response can't
-    /// be parsed because of the content type mismatch. The second one is useful
-    /// to test request timeouts. The third type is used by most of the unit tests
-    /// to test successful transactions.
-    ///
-    /// @param request Pointer to the HTTP request.
-    /// @return Pointer to the generated HTTP OK response with no content.
-    virtual HttpResponsePtr
-    createDynamicHttpResponse(HttpRequestPtr request) {
-        // Request must always be JSON.
-        PostHttpRequestJsonPtr request_json =
-            boost::dynamic_pointer_cast<PostHttpRequestJson>(request);
-        ConstElementPtr body;
-        if (request_json) {
-            body = request_json->getBodyAsJson();
-            if (body) {
-                // Check if the client requested one of the two first response
-                // types.
-                GenericResponsePtr response;
-                ConstElementPtr content_type = body->get("requested-content-type");
-                ConstElementPtr partial_response = body->get("partial-response");
-                if (content_type || partial_response) {
-                    // The first two response types can only be generated using the
-                    // generic response as we have to explicitly modify some of the
-                    // values.
-                    response.reset(new GenericResponse(request->getHttpVersion(),
-                                                       HttpStatusCode::OK));
-                    HttpResponseContextPtr ctx = response->context();
-
-                    if (content_type) {
-                        // Provide requested content type.
-                        ctx->headers_.push_back(HttpHeaderContext("Content-Type",
-                                                                  content_type->stringValue()));
-                        // It doesn't matter what body is there.
-                        ctx->body_ = "abcd";
-                        response->finalize();
-
-                    } else {
-                        // Generate JSON response.
-                        ctx->headers_.push_back(HttpHeaderContext("Content-Type",
-                                                                  "application/json"));
-                        // The body lacks '}' so the client will be waiting for it and
-                        // eventually should time out.
-                        ctx->body_ = "{";
-                        response->finalize();
-                        // The auto generated Content-Length header would be based on the
-                        // body size (so set to 1 byte). We have to override it to
-                        // account for the missing '}' character.
-                        response->setContentLength(2);
-                    }
-                    return (response);
-                }
-            }
-        }
-
-        // Third type of response is requested.
-        ResponsePtr response(new Response(request->getHttpVersion(),
-                                          HttpStatusCode::OK));
-        // If body was included in the request. Let's copy it.
-        if (body) {
-            response->setBodyAsJson(body);
-        }
-
-        response->finalize();
-        return (response);
-    }
-};
-
-/// @brief Implementation of the test @ref HttpResponseCreatorFactory.
-///
-/// This factory class creates @ref TestHttpResponseCreator instances.
-class TestHttpResponseCreatorFactory : public HttpResponseCreatorFactory {
-public:
-
-    /// @brief Creates @ref TestHttpResponseCreator instance.
-    virtual HttpResponseCreatorPtr create() const {
-        HttpResponseCreatorPtr response_creator(new TestHttpResponseCreator());
-        return (response_creator);
-    }
-};
-
 /// @brief Implementation of the HTTP listener used in tests.
 ///
 /// This implementation replaces the @c HttpConnection type with a custom