Skip to content

Commit

Permalink
Add unit tests for https
Browse files Browse the repository at this point in the history
  • Loading branch information
spinorx committed Dec 4, 2017
1 parent 1e1867b commit 2ff4aa0
Show file tree
Hide file tree
Showing 9 changed files with 101 additions and 6 deletions.
9 changes: 9 additions & 0 deletions CMakeLists.txt
Expand Up @@ -52,6 +52,15 @@ else (UNIX)
SET(DEPENDENT_LIBRARIES event glog)
endif (UNIX)

# Set to true if https support is needed.
# Note that this needs openssl
# SET(HTTPS True)
if (HTTPS)
list(APPEND DEPENDENT_LIBRARIES ssl crypto event_openssl)
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DEVPP_HTTP_CLIENT_SUPPORTS_SSL")
endif (HTTPS)


if (CMAKE_BENCHMARK_TESTING)
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DH_BENCHMARK_TESTING=1")
endif (CMAKE_BENCHMARK_TESTING)
Expand Down
6 changes: 6 additions & 0 deletions evpp/CMakeLists.txt
Expand Up @@ -16,6 +16,12 @@ include_directories(${PROJECT_SOURCE_DIR})
add_library(evpp_static STATIC ${evpp_SRCS})
target_link_libraries(evpp_static ${DEPENDENT_LIBRARIES})

if (HTTPS)
add_library(evpp_https_static STATIC ${evpp_SRCS})
target_compile_definitions(evpp_https_static PRIVATE -DEVPP_HTTP_CLIENT_SUPPORTS_SSL)
target_link_libraries(evpp_https_static ${DEPENDENT_LIBRARIES})
endif (HTTPS)

add_library(evpp_lite_static STATIC ${evpp_lite_SRCS})
target_link_libraries(evpp_lite_static ${DEPENDENT_LIBRARIES})

Expand Down
1 change: 1 addition & 0 deletions evpp/httpc/conn.cc
Expand Up @@ -4,6 +4,7 @@
#if defined(EVPP_HTTP_CLIENT_SUPPORTS_SSL)
#include "evpp/httpc/ssl.h"
#include <openssl/x509v3.h>
#include <openssl/err.h>
#endif

#include "evpp/libevent.h"
Expand Down
7 changes: 4 additions & 3 deletions evpp/httpc/request.cc
@@ -1,10 +1,13 @@

#include "evpp/libevent.h"
#include "evpp/httpc/conn_pool.h"
#include "evpp/httpc/response.h"
#include "evpp/httpc/request.h"
#include "evpp/httpc/url_parser.h"

#if defined(EVPP_HTTP_CLIENT_SUPPORTS_SSL)
#include <openssl/err.h>
#endif

namespace evpp {
namespace httpc {
const std::string Request::empty_ = "";
Expand Down Expand Up @@ -195,9 +198,7 @@ void Request::HandleResponse(struct evhttp_request* r) {
}

#if defined(EVPP_HTTP_CLIENT_SUPPORTS_SSL)
bool had_ssl_error = false;
if (!r) {
had_ssl_error = true;
int errcode = EVUTIL_SOCKET_ERROR();
unsigned long oslerr;
bool printed_some_error = false;
Expand Down
4 changes: 2 additions & 2 deletions evpp/httpc/request.h
Expand Up @@ -45,7 +45,7 @@ class EVPP_EXPORT Request {
const std::string& host() const {
return host_;
}
const int port() const {
int port() const {
return port_;
}
void set_retry_number(int v) {
Expand Down Expand Up @@ -75,7 +75,7 @@ class EVPP_EXPORT Request {

// The retried times
int retried_ = 0;

// The max retry times. Set to 0 if you don't want to retry when failed.
// The total execution times is retry_number_+1
int retry_number_ = 2;
Expand Down
1 change: 1 addition & 0 deletions evpp/httpc/ssl.cc
Expand Up @@ -4,6 +4,7 @@
#include "evpp/logging.h"

#include <openssl/rand.h>
#include <openssl/err.h>

namespace evpp {
namespace httpc {
Expand Down
9 changes: 9 additions & 0 deletions test/CMakeLists.txt
@@ -1,6 +1,10 @@
file(GLOB evpp_unittest_SRCS *.cc
${PROJECT_SOURCE_DIR}/3rdparty/gtest/src/gtest-all.cc
${PROJECT_SOURCE_DIR}/3rdparty/gtest/src/gtest_main.cc)
list(REMOVE_ITEM evpp_unittest_SRCS ${PROJECT_SOURCE_DIR}/test/https_client_test.cc)
file(GLOB evpp_https_unittest_SRCS https_client_test.cc
${PROJECT_SOURCE_DIR}/3rdparty/gtest/src/gtest-all.cc
${PROJECT_SOURCE_DIR}/3rdparty/gtest/src/gtest_main.cc)
include_directories(${PROJECT_SOURCE_DIR}/3rdparty ${PROJECT_SOURCE_DIR}/3rdparty/gtest)

if (MSVC)
Expand All @@ -10,6 +14,11 @@ endif (MSVC)
add_executable(evpp_unittest ${evpp_unittest_SRCS})
target_link_libraries(evpp_unittest evpp_static ${DEPENDENT_LIBRARIES})

if (HTTPS)
add_executable(evpp_https_unittest ${evpp_https_unittest_SRCS})
target_link_libraries(evpp_https_unittest evpp_https_static ${DEPENDENT_LIBRARIES})
endif (HTTPS)

if (UNIX)
add_executable(evpp_unittest_boost_lockfree ${evpp_unittest_SRCS})
target_link_libraries(evpp_unittest_boost_lockfree evpp_boost ${DEPENDENT_LIBRARIES})
Expand Down
2 changes: 1 addition & 1 deletion test/evhttp_client_test.cc
Expand Up @@ -162,4 +162,4 @@ TEST_UNIT(testHTTPRequest5) {
H_TEST_ASSERT(hc::retried == 3);
t.Stop(true);
LOG_INFO << "EventLoopThread stopped.";
}
}
68 changes: 68 additions & 0 deletions test/https_client_test.cc
@@ -0,0 +1,68 @@
#include <iostream>
#include <chrono>
#include <thread>

#include "test_common.h"

#include <evpp/event_loop_thread.h>
#include <evpp/httpc/request.h>
#include <evpp/httpc/conn.h>
#include <evpp/httpc/response.h>
#include <evpp/httpc/ssl.h>

namespace {
void InitSSLOnce() {
static std::once_flag flag;
std::call_once(flag, [](){ evpp::httpc::InitSSL(); });
}

std::string HttpFetch(const std::string& url) {
InitSSLOnce();
evpp::EventLoopThread t;
t.Start(true);
evpp::httpc::GetRequest* req =
new evpp::httpc::GetRequest(t.loop(), url, evpp::Duration(1.0));
volatile bool responsed = false;
std::string ret;
req->Execute([req, &ret, &responsed](
const std::shared_ptr<evpp::httpc::Response>& response) mutable {
std::stringstream oss;
oss << "http_code="
<< response->http_code()
<< std::endl
<< "body ["
<< std::endl
<< response->body().ToString()
<< "]"
<< std::endl;
responsed = true;
delete req;
ret = oss.str();
});
while (!responsed) {
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}
t.Stop(true);
return ret;
}
}

TEST_UNIT(testHTTPResponse) {
std::string response = HttpFetch("http://httpbin.org/headers?show_env=1");
H_TEST_ASSERT(!response.empty());
H_TEST_ASSERT(response.find("http_code=200") != std::string::npos);
H_TEST_ASSERT(response.find("\"Host\": \"httpbin.org\",") != std::string::npos);
H_TEST_ASSERT(response.find("\"X-Forwarded-Port\": \"80\",") != std::string::npos);
H_TEST_ASSERT(response.find("\"X-Forwarded-Proto\": \"http\",") != std::string::npos);
H_TEST_ASSERT(response.find("\"Connection\": \"close\",") != std::string::npos);
}

TEST_UNIT(testHTTPSResponse) {
std::string response = HttpFetch("https://httpbin.org/headers?show_env=1");
H_TEST_ASSERT(!response.empty());
H_TEST_ASSERT(response.find("http_code=200") != std::string::npos);
H_TEST_ASSERT(response.find("\"Host\": \"httpbin.org\",") != std::string::npos);
H_TEST_ASSERT(response.find("\"X-Forwarded-Port\": \"443\",") != std::string::npos);
H_TEST_ASSERT(response.find("\"X-Forwarded-Proto\": \"https\",") != std::string::npos);
H_TEST_ASSERT(response.find("\"Connection\": \"close\",") != std::string::npos);
}

0 comments on commit 2ff4aa0

Please sign in to comment.