empiredan commented on code in PR #1583:
URL: 
https://github.com/apache/incubator-pegasus/pull/1583#discussion_r1339488621


##########
src/http/http_client.cpp:
##########
@@ -0,0 +1,336 @@
+// 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 "http/http_client.h"
+
+#include <fmt/core.h>
+#include <limits>
+#include <utility>
+
+#include "curl/curl.h"
+#include "utils/error_code.h"
+#include "utils/flags.h"
+#include "utils/fmt_logging.h"
+
+namespace dsn {
+
+DSN_DEFINE_uint32(http,
+                  curl_timeout_ms,
+                  10000,
+                  "The maximum time in milliseconds that you allow the libcurl 
transfer operation "
+                  "to complete");
+
+http_client::http_client()
+    : _curl(nullptr),
+      _method(http_method::GET),
+      _recv_callback(nullptr),
+      _header_changed(true),
+      _header_list(nullptr)
+{
+    // Since `kErrorBufferBytes` is private, `static_assert` have to be put in 
constructor.
+    static_assert(http_client::kErrorBufferBytes >= CURL_ERROR_SIZE,
+                  "The error buffer used by libcurl must be at least 
CURL_ERROR_SIZE bytes big");
+
+    clear_error_buf();
+}
+
+http_client::~http_client()
+{
+    if (_curl != nullptr) {
+        curl_easy_cleanup(_curl);
+        _curl = nullptr;
+    }
+
+    free_header_list();
+}
+
+namespace {
+
+inline dsn::error_code to_error_code(CURLcode code)
+{
+    if (code == CURLE_OK) {

Review Comment:
   OK.



##########
src/http/test/http_client_test.cpp:
##########
@@ -0,0 +1,206 @@
+// 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 <fmt/core.h>
+#include <gtest/gtest.h>
+// IWYU pragma: no_include <gtest/gtest-message.h>
+// IWYU pragma: no_include <gtest/gtest-param-test.h>
+// IWYU pragma: no_include <gtest/gtest-test-part.h>
+#include <cstring>
+#include <iostream>
+#include <string>
+#include <vector>
+
+#include "http/http_client.h"
+#include "http/http_method.h"
+#include "utils/error_code.h"
+#include "utils/errors.h"
+#include "utils/fmt_logging.h"
+
+namespace dsn {
+
+void check_expected_description_prefix(const std::string 
&expected_description_prefix,
+                                       const dsn::error_s &err)
+{
+    const std::string actual_description(err.description());
+    std::cout << actual_description << std::endl;
+
+    ASSERT_LT(expected_description_prefix.size(), actual_description.size());
+    EXPECT_EQ(expected_description_prefix,
+              actual_description.substr(0, 
expected_description_prefix.size()));
+}
+
+TEST(HttpClientTest, Connect)
+{
+    http_client client;
+    ASSERT_TRUE(client.init());
+
+    // No one has listened on port 20000, thus this would lead to "Connection 
refused".
+    ASSERT_TRUE(client.set_url("http://127.0.0.1:20000/test/get";));
+
+    const auto &err = client.exec_method();
+    ASSERT_EQ(dsn::ERR_CURL_CONNECT_FAILED, err.code());
+
+    std::cout << "failed to connect: ";
+
+    // We just check the prefix of description, including `method`, `url`, 
`code` and `desc`.
+    // The `msg` differ in various systems, such as:
+    // * msg="Failed to connect to 127.0.0.1 port 20000: Connection refused"
+    // * msg="Failed to connect to 127.0.0.1 port 20000 after 0 ms: Connection 
refused"
+    // Thus we don't check if `msg` fields are consistent.
+    const std::string expected_description_prefix(
+        "ERR_CURL_CONNECT_FAILED: failed to perform http request("
+        "method=GET, url=http://127.0.0.1:20000/test/get): code=7, "
+        "desc=\"Couldn't connect to server\"");
+    check_expected_description_prefix(expected_description_prefix, err);

Review Comment:
   OK.



-- 
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.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to