forked from Qihoo360/evpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttps_client_test.cc
More file actions
68 lines (62 loc) · 2.23 KB
/
Copy pathhttps_client_test.cc
File metadata and controls
68 lines (62 loc) · 2.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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);
}