diff --git a/ixwebsocket/IXHttpClient.cpp b/ixwebsocket/IXHttpClient.cpp index cb39f4ed..5a6b380b 100644 --- a/ixwebsocket/IXHttpClient.cpp +++ b/ixwebsocket/IXHttpClient.cpp @@ -74,7 +74,6 @@ namespace ix std::stringstream ss; ss << verb << " " << path << " HTTP/1.1\r\n"; ss << "Host: " << host << "\r\n"; - ss << "User-Agent: ixwebsocket/1.0.0" << "\r\n"; ss << "Accept: */*" << "\r\n"; if (args.compress) diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 8c5d5cb2..31a9867d 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -37,6 +37,7 @@ set (SOURCES IXWebSocketTestConnectionDisconnection.cpp IXUrlParserTest.cpp IXWebSocketServerTest.cpp + IXHttpClientTest.cpp ) # Some unittest don't work on windows yet diff --git a/test/IXHttpClientTest.cpp b/test/IXHttpClientTest.cpp new file mode 100644 index 00000000..d3567677 --- /dev/null +++ b/test/IXHttpClientTest.cpp @@ -0,0 +1,117 @@ +/* + * IXSocketTest.cpp + * Author: Benjamin Sergeant + * Copyright (c) 2019 Machine Zone. All rights reserved. + */ + +#include +#include + +#include "catch.hpp" + +using namespace ix; + +TEST_CASE("http client", "[http]") +{ + SECTION("Connect to a remote HTTP server") + { + std::string url("http://httpbin.org/"); + + 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::cout << msg; + }; + args.onProgressCallback = [](int current, int total) -> bool + { + std::cerr << "\r" << "Downloaded " + << current << " bytes out of " << total; + return true; + }; + + HttpClient httpClient; + HttpResponse out; + out = httpClient.get(url, args); + + auto statusCode = std::get<0>(out); + auto errorCode = std::get<1>(out); + auto responseHeaders = std::get<2>(out); + auto payload = std::get<3>(out); + auto errorMsg = std::get<4>(out); + auto uploadSize = std::get<5>(out); + auto downloadSize = std::get<6>(out); + + for (auto it : responseHeaders) + { + std::cerr << it.first << ": " << it.second << std::endl; + } + + std::cerr << "Upload size: " << uploadSize << std::endl; + std::cerr << "Download size: " << downloadSize << std::endl; + std::cerr << "Status: " << statusCode << std::endl; + + REQUIRE(errorCode == HttpErrorCode::Ok); + } + +#if defined(IXWEBSOCKET_USE_TLS) + SECTION("Connect to a remote HTTP server") + { + std::string url("https://httpbin.org/"); + + 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::cout << msg; + }; + args.onProgressCallback = [](int current, int total) -> bool + { + std::cerr << "\r" << "Downloaded " + << current << " bytes out of " << total; + return true; + }; + + HttpClient httpClient; + HttpResponse out; + out = httpClient.get(url, args); + + auto statusCode = std::get<0>(out); + auto errorCode = std::get<1>(out); + auto responseHeaders = std::get<2>(out); + auto payload = std::get<3>(out); + auto errorMsg = std::get<4>(out); + auto uploadSize = std::get<5>(out); + auto downloadSize = std::get<6>(out); + + for (auto it : responseHeaders) + { + std::cerr << it.first << ": " << it.second << std::endl; + } + + std::cerr << "Upload size: " << uploadSize << std::endl; + std::cerr << "Download size: " << downloadSize << std::endl; + std::cerr << "Status: " << statusCode << std::endl; + + REQUIRE(errorCode == HttpErrorCode::Ok); + } +#endif +}