Add simple HTTP and HTTPS client test ㊙️
This commit is contained in:
		@@ -74,7 +74,6 @@ namespace ix
 | 
				
			|||||||
        std::stringstream ss;
 | 
					        std::stringstream ss;
 | 
				
			||||||
        ss << verb << " " << path << " HTTP/1.1\r\n";
 | 
					        ss << verb << " " << path << " HTTP/1.1\r\n";
 | 
				
			||||||
        ss << "Host: " << host << "\r\n";
 | 
					        ss << "Host: " << host << "\r\n";
 | 
				
			||||||
        ss << "User-Agent: ixwebsocket/1.0.0" << "\r\n";
 | 
					 | 
				
			||||||
        ss << "Accept: */*" << "\r\n";
 | 
					        ss << "Accept: */*" << "\r\n";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if (args.compress)
 | 
					        if (args.compress)
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -37,6 +37,7 @@ set (SOURCES
 | 
				
			|||||||
  IXWebSocketTestConnectionDisconnection.cpp
 | 
					  IXWebSocketTestConnectionDisconnection.cpp
 | 
				
			||||||
  IXUrlParserTest.cpp
 | 
					  IXUrlParserTest.cpp
 | 
				
			||||||
  IXWebSocketServerTest.cpp
 | 
					  IXWebSocketServerTest.cpp
 | 
				
			||||||
 | 
					  IXHttpClientTest.cpp
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# Some unittest don't work on windows yet
 | 
					# Some unittest don't work on windows yet
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										117
									
								
								test/IXHttpClientTest.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										117
									
								
								test/IXHttpClientTest.cpp
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,117 @@
 | 
				
			|||||||
 | 
					/*
 | 
				
			||||||
 | 
					 *  IXSocketTest.cpp
 | 
				
			||||||
 | 
					 *  Author: Benjamin Sergeant
 | 
				
			||||||
 | 
					 *  Copyright (c) 2019 Machine Zone. All rights reserved.
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#include <iostream>
 | 
				
			||||||
 | 
					#include <ixwebsocket/IXHttpClient.h>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#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
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
		Reference in New Issue
	
	Block a user