Feature/http async (#90)
* unittest working / uses shared_ptr for a bunch of things 🗿
* fix command line tools
* fix ws + add doc
* add more logging
This commit is contained in:
committed by
GitHub
parent
012193c74e
commit
78b3d7ff2d
@ -15,85 +15,219 @@ TEST_CASE("http client", "[http]")
|
||||
{
|
||||
SECTION("Connect to a remote HTTP server")
|
||||
{
|
||||
std::string url("http://httpbin.org/");
|
||||
|
||||
HttpClient httpClient;
|
||||
WebSocketHttpHeaders headers;
|
||||
headers["User-Agent"] = "ixwebsocket";
|
||||
|
||||
HttpRequestArgs args;
|
||||
args.extraHeaders = headers;
|
||||
args.connectTimeout = 60;
|
||||
args.transferTimeout = 60;
|
||||
args.followRedirects = true;
|
||||
args.maxRedirects = 10;
|
||||
args.verbose = true;
|
||||
args.compress = true;
|
||||
args.logger = [](const std::string& msg)
|
||||
std::string url("http://httpbin.org/");
|
||||
auto args = httpClient.createRequest(url);
|
||||
|
||||
args->extraHeaders = headers;
|
||||
args->connectTimeout = 60;
|
||||
args->transferTimeout = 60;
|
||||
args->followRedirects = true;
|
||||
args->maxRedirects = 10;
|
||||
args->verbose = true;
|
||||
args->compress = true;
|
||||
args->logger = [](const std::string& msg)
|
||||
{
|
||||
std::cout << msg;
|
||||
};
|
||||
args.onProgressCallback = [](int current, int total) -> bool
|
||||
args->onProgressCallback = [](int current, int total) -> bool
|
||||
{
|
||||
std::cerr << "\r" << "Downloaded "
|
||||
<< current << " bytes out of " << total;
|
||||
return true;
|
||||
};
|
||||
|
||||
HttpClient httpClient;
|
||||
HttpResponse response = httpClient.get(url, args);
|
||||
auto response = httpClient.get(url, args);
|
||||
|
||||
for (auto it : response.headers)
|
||||
for (auto it : response->headers)
|
||||
{
|
||||
std::cerr << it.first << ": " << it.second << std::endl;
|
||||
}
|
||||
|
||||
std::cerr << "Upload size: " << response.uploadSize << std::endl;
|
||||
std::cerr << "Download size: " << response.downloadSize << std::endl;
|
||||
std::cerr << "Status: " << response.statusCode << std::endl;
|
||||
std::cerr << "Upload size: " << response->uploadSize << std::endl;
|
||||
std::cerr << "Download size: " << response->downloadSize << std::endl;
|
||||
std::cerr << "Status: " << response->statusCode << std::endl;
|
||||
std::cerr << "Error message: " << response->errorMsg << std::endl;
|
||||
|
||||
REQUIRE(response.errorCode == HttpErrorCode::Ok);
|
||||
REQUIRE(response->errorCode == HttpErrorCode::Ok);
|
||||
REQUIRE(response->statusCode == 200);
|
||||
}
|
||||
|
||||
#if defined(IXWEBSOCKET_USE_TLS)
|
||||
SECTION("Connect to a remote HTTPS server")
|
||||
{
|
||||
std::string url("https://httpbin.org/");
|
||||
|
||||
HttpClient httpClient;
|
||||
WebSocketHttpHeaders headers;
|
||||
headers["User-Agent"] = "ixwebsocket";
|
||||
|
||||
HttpRequestArgs args;
|
||||
args.extraHeaders = headers;
|
||||
args.connectTimeout = 60;
|
||||
args.transferTimeout = 60;
|
||||
args.followRedirects = true;
|
||||
args.maxRedirects = 10;
|
||||
args.verbose = true;
|
||||
args.compress = true;
|
||||
args.logger = [](const std::string& msg)
|
||||
std::string url("https://httpbin.org/");
|
||||
auto args = httpClient.createRequest(url);
|
||||
|
||||
args->extraHeaders = headers;
|
||||
args->connectTimeout = 60;
|
||||
args->transferTimeout = 60;
|
||||
args->followRedirects = true;
|
||||
args->maxRedirects = 10;
|
||||
args->verbose = true;
|
||||
args->compress = true;
|
||||
args->logger = [](const std::string& msg)
|
||||
{
|
||||
std::cout << msg;
|
||||
};
|
||||
args.onProgressCallback = [](int current, int total) -> bool
|
||||
args->onProgressCallback = [](int current, int total) -> bool
|
||||
{
|
||||
std::cerr << "\r" << "Downloaded "
|
||||
<< current << " bytes out of " << total;
|
||||
return true;
|
||||
};
|
||||
|
||||
HttpClient httpClient;
|
||||
HttpResponse response = httpClient.get(url, args);
|
||||
auto response = httpClient.get(url, args);
|
||||
|
||||
for (auto it : response.headers)
|
||||
for (auto it : response->headers)
|
||||
{
|
||||
std::cerr << it.first << ": " << it.second << std::endl;
|
||||
}
|
||||
|
||||
std::cerr << "Upload size: " << response.uploadSize << std::endl;
|
||||
std::cerr << "Download size: " << response.downloadSize << std::endl;
|
||||
std::cerr << "Status: " << response.statusCode << std::endl;
|
||||
std::cerr << "Upload size: " << response->uploadSize << std::endl;
|
||||
std::cerr << "Download size: " << response->downloadSize << std::endl;
|
||||
std::cerr << "Status: " << response->statusCode << std::endl;
|
||||
std::cerr << "Error message: " << response->errorMsg << std::endl;
|
||||
|
||||
REQUIRE(response.errorCode == HttpErrorCode::Ok);
|
||||
REQUIRE(response->errorCode == HttpErrorCode::Ok);
|
||||
REQUIRE(response->statusCode == 200);
|
||||
}
|
||||
|
||||
SECTION("Async API, one call")
|
||||
{
|
||||
bool async = true;
|
||||
HttpClient httpClient(async);
|
||||
WebSocketHttpHeaders headers;
|
||||
|
||||
std::string url("https://httpbin.org/");
|
||||
auto args = httpClient.createRequest(url);
|
||||
|
||||
args->extraHeaders = headers;
|
||||
args->connectTimeout = 60;
|
||||
args->transferTimeout = 60;
|
||||
args->followRedirects = true;
|
||||
args->maxRedirects = 10;
|
||||
args->verbose = true;
|
||||
args->compress = true;
|
||||
args->logger = [](const std::string& msg)
|
||||
{
|
||||
std::cout << msg;
|
||||
};
|
||||
args->onProgressCallback = [](int current, int total) -> bool
|
||||
{
|
||||
std::cerr << "\r" << "Downloaded "
|
||||
<< current << " bytes out of " << total;
|
||||
return true;
|
||||
};
|
||||
|
||||
std::atomic<bool> requestCompleted(false);
|
||||
std::atomic<int> statusCode(0);
|
||||
|
||||
httpClient.performRequest(args, [&requestCompleted, &statusCode]
|
||||
(const HttpResponsePtr& response)
|
||||
{
|
||||
std::cerr << "Upload size: " << response->uploadSize << std::endl;
|
||||
std::cerr << "Download size: " << response->downloadSize << std::endl;
|
||||
std::cerr << "Status: " << response->statusCode << std::endl;
|
||||
std::cerr << "Error message: " << response->errorMsg << std::endl;
|
||||
|
||||
// In case of failure, print response->errorMsg
|
||||
statusCode = response->statusCode;
|
||||
requestCompleted = true;
|
||||
}
|
||||
);
|
||||
|
||||
int wait = 0;
|
||||
while (wait < 5000)
|
||||
{
|
||||
if (requestCompleted) break;
|
||||
|
||||
std::chrono::duration<double, std::milli> duration(10);
|
||||
std::this_thread::sleep_for(duration);
|
||||
wait += 10;
|
||||
}
|
||||
|
||||
std::cerr << "Done" << std::endl;
|
||||
REQUIRE(statusCode == 200);
|
||||
}
|
||||
|
||||
SECTION("Async API, multiple calls")
|
||||
{
|
||||
bool async = true;
|
||||
HttpClient httpClient(async);
|
||||
WebSocketHttpHeaders headers;
|
||||
|
||||
std::string url("http://httpbin.org/");
|
||||
auto args = httpClient.createRequest(url);
|
||||
|
||||
args->extraHeaders = headers;
|
||||
args->connectTimeout = 60;
|
||||
args->transferTimeout = 60;
|
||||
args->followRedirects = true;
|
||||
args->maxRedirects = 10;
|
||||
args->verbose = true;
|
||||
args->compress = true;
|
||||
args->logger = [](const std::string& msg)
|
||||
{
|
||||
std::cout << msg;
|
||||
};
|
||||
args->onProgressCallback = [](int current, int total) -> bool
|
||||
{
|
||||
std::cerr << "\r" << "Downloaded "
|
||||
<< current << " bytes out of " << total;
|
||||
return true;
|
||||
};
|
||||
|
||||
std::atomic<bool> requestCompleted(false);
|
||||
std::atomic<int> statusCode0(0);
|
||||
std::atomic<int> statusCode1(0);
|
||||
std::atomic<int> statusCode2(0);
|
||||
|
||||
for (int i = 0; i < 3; ++i)
|
||||
{
|
||||
httpClient.performRequest(args, [i, &requestCompleted, &statusCode0, &statusCode1, &statusCode2]
|
||||
(const HttpResponsePtr& response)
|
||||
{
|
||||
std::cerr << "Upload size: " << response->uploadSize << std::endl;
|
||||
std::cerr << "Download size: " << response->downloadSize << std::endl;
|
||||
std::cerr << "Status: " << response->statusCode << std::endl;
|
||||
std::cerr << "Error message: " << response->errorMsg << std::endl;
|
||||
|
||||
// In case of failure, print response->errorMsg
|
||||
if (i == 0)
|
||||
{
|
||||
statusCode0 = response->statusCode;
|
||||
}
|
||||
else if (i == 1)
|
||||
{
|
||||
statusCode1 = response->statusCode;
|
||||
}
|
||||
else if (i == 2)
|
||||
{
|
||||
statusCode2 = response->statusCode;
|
||||
requestCompleted = true;
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
int wait = 0;
|
||||
while (wait < 10000)
|
||||
{
|
||||
if (requestCompleted) break;
|
||||
|
||||
std::chrono::duration<double, std::milli> duration(10);
|
||||
std::this_thread::sleep_for(duration);
|
||||
wait += 10;
|
||||
}
|
||||
|
||||
std::cerr << "Done" << std::endl;
|
||||
REQUIRE(statusCode0 == 200);
|
||||
REQUIRE(statusCode1 == 200);
|
||||
REQUIRE(statusCode2 == 200);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
@ -210,7 +210,7 @@ namespace
|
||||
<< closeInfo.reason
|
||||
<< ")";
|
||||
log(ss.str());
|
||||
|
||||
|
||||
std::lock_guard<std::mutex> lck(mutexWrite);
|
||||
|
||||
receivedCloseCode = closeInfo.code;
|
||||
|
Reference in New Issue
Block a user