bakaid commented on a change in pull request #610: MINIFICPP-814 - Fixed 
ListenHTTP and HTTPClient bugs, created tests f…
URL: https://github.com/apache/nifi-minifi-cpp/pull/610#discussion_r303802090
 
 

 ##########
 File path: extensions/civetweb/tests/ListenHTTPTests.cpp
 ##########
 @@ -0,0 +1,514 @@
+/**
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <uuid/uuid.h>
+#include <fstream>
+#include <map>
+#include <memory>
+#include <set>
+#include <iostream>
+
+#include "TestBase.h"
+
+#include "utils/file/FileUtils.h"
+#include "processors/GetFile.h"
+#include "processors/UpdateAttribute.h"
+#include "processors/LogAttribute.h"
+#include "processors/ListenHTTP.h"
+#include "client/HTTPClient.h"
+#include "controllers/SSLContextService.h"
+#include "properties/Configure.h"
+
+class ListenHTTPTestsFixture {
+ public:
+  ListenHTTPTestsFixture()
+   : tmp_dir(strdup("/tmp/gt.XXXXXX")) {
+    LogTestController::getInstance().setTrace<TestPlan>();
+    LogTestController::getInstance().setDebug<minifi::FlowController>();
+    LogTestController::getInstance().setDebug<minifi::SchedulingAgent>();
+    LogTestController::getInstance().setDebug<minifi::core::ProcessGroup>();
+    LogTestController::getInstance().setDebug<minifi::core::Processor>();
+    LogTestController::getInstance().setTrace<minifi::core::ProcessSession>();
+    LogTestController::getInstance().setTrace<processors::ListenHTTP>();
+    
LogTestController::getInstance().setTrace<processors::ListenHTTP::Handler>();
+    LogTestController::getInstance().setDebug<processors::LogAttribute>();
+    LogTestController::getInstance().setDebug<utils::HTTPClient>();
+
+    // Create temporary directories
+    testController.createTempDirectory(tmp_dir);
+    REQUIRE(tmp_dir != nullptr);
+
+    // Define test input file
+    std::string test_input_file = utils::file::FileUtils::concat_path(tmp_dir, 
"test");
+    {
+      std::ofstream os(test_input_file);
+      os << "Hello response body" << std::endl;
+    }
+
+    // Build MiNiFi processing graph
+    plan = testController.createPlan();
+    get_file = plan->addProcessor(
+        "GetFile",
+        "GetFile");
+    update_attribute = plan->addProcessor(
+        "UpdateAttribute",
+        "UpdateAttribute",
+        core::Relationship("success", "description"),
+        true);
+    listen_http = plan->addProcessor(
+        "ListenHTTP",
+        "ListenHTTP",
+        core::Relationship("success", "description"),
+        true);
+    log_attribute = plan->addProcessor(
+        "LogAttribute",
+        "LogAttribute",
+        core::Relationship("success", "description"),
+        true);
+
+    // Configure GetFile processor
+    plan->setProperty(get_file, "Input Directory", tmp_dir);
+
+    // Configure UpdateAttribute processor
+    plan->setProperty(update_attribute, "http.type", "response_body", true);
+
+    // Configure ListenHTTP processor
+    plan->setProperty(listen_http, "Listening Port", "0");
+  }
+
+  virtual ~ListenHTTPTestsFixture() {
+    free(tmp_dir);
+    LogTestController::getInstance().reset();
+  }
+
+  void create_ssl_context_service(const char* ca, const char* client_cert) {
+    auto config = std::make_shared<minifi::Configure>();
+    if (ca != nullptr) {
+      config->set(minifi::Configure::nifi_security_client_ca_certificate, 
utils::file::FileUtils::get_executable_dir() + "/resources/" + ca);
+    }
+    if (client_cert != nullptr) {
+      config->set(minifi::Configure::nifi_security_client_certificate, 
utils::file::FileUtils::get_executable_dir() + "/resources/" + client_cert);
+      config->set(minifi::Configure::nifi_security_client_private_key, 
utils::file::FileUtils::get_executable_dir() + "/resources/" + client_cert);
+      config->set(minifi::Configure::nifi_security_client_pass_phrase, 
"Password12");
+    }
+    ssl_context_service = 
std::make_shared<minifi::controllers::SSLContextService>("SSLContextService", 
config);
+    ssl_context_service->onEnable();
+  }
+
+  void run_server() {
+    plan->runNextProcessor(); // GetFile
+    plan->runNextProcessor(); // UpdateAttribute
+    plan->runNextProcessor(); // ListenHTTP
+
+    auto raw_ptr = 
dynamic_cast<org::apache::nifi::minifi::processors::ListenHTTP*>(listen_http.get());
+    std::string protocol = std::string("http") + (raw_ptr->isSecure() ? "s" : 
"");
+    std::string portstr = raw_ptr->getPort();
+    REQUIRE(LogTestController::getInstance().contains("Listening on port " + 
portstr));
+
+    url = protocol + "://localhost:" + portstr + "/contentListener/" + 
endpoint;
+  }
+
+  void test_connect(bool should_succeed = true, int64_t response_code = 200) {
 
 Review comment:
   The naming might be misleading, but should_succeed means that we get an HTTP 
response (and not for example, die in the TLS connection phase because of a bad 
cert) and the response_code means the response code we get. Because our 
processor specification includes specific error codes for specific cases (403 
on AuthorizedDNPattern mismatch, 405 on unsupported HTTP methods), I want to 
specifically test for those.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to