2019-06-23 23:54:21 +02:00
|
|
|
/*
|
|
|
|
* IXSocketTest.cpp
|
|
|
|
* Author: Benjamin Sergeant
|
|
|
|
* Copyright (c) 2019 Machine Zone. All rights reserved.
|
|
|
|
*/
|
|
|
|
|
2019-09-23 19:25:23 +02:00
|
|
|
#include "IXGetFreePort.h"
|
|
|
|
#include "catch.hpp"
|
2019-06-23 23:54:21 +02:00
|
|
|
#include <iostream>
|
|
|
|
#include <ixwebsocket/IXHttpClient.h>
|
|
|
|
#include <ixwebsocket/IXHttpServer.h>
|
|
|
|
|
|
|
|
using namespace ix;
|
|
|
|
|
|
|
|
TEST_CASE("http server", "[httpd]")
|
|
|
|
{
|
|
|
|
SECTION("Connect to a local HTTP server")
|
|
|
|
{
|
|
|
|
int port = getFreePort();
|
|
|
|
ix::HttpServer server(port, "127.0.0.1");
|
|
|
|
|
|
|
|
auto res = server.listen();
|
|
|
|
REQUIRE(res.first);
|
|
|
|
server.start();
|
|
|
|
|
|
|
|
HttpClient httpClient;
|
|
|
|
WebSocketHttpHeaders headers;
|
|
|
|
|
|
|
|
std::string url("http://127.0.0.1:");
|
|
|
|
url += std::to_string(port);
|
|
|
|
url += "/data/foo.txt";
|
|
|
|
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;
|
2019-09-23 19:25:23 +02:00
|
|
|
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;
|
2019-06-23 23:54:21 +02:00
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
auto response = httpClient.get(url, args);
|
|
|
|
|
|
|
|
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 << "Error message: " << response->errorMsg << std::endl;
|
|
|
|
|
|
|
|
REQUIRE(response->errorCode == HttpErrorCode::Ok);
|
|
|
|
REQUIRE(response->statusCode == 200);
|
2020-02-21 21:22:37 +01:00
|
|
|
REQUIRE(response->headers["Accept-Encoding"] == "gzip");
|
2019-06-23 23:54:21 +02:00
|
|
|
|
|
|
|
server.stop();
|
|
|
|
}
|
2020-02-21 21:24:22 +01:00
|
|
|
}
|
2019-09-26 18:45:59 +02:00
|
|
|
|
2020-02-21 21:24:22 +01:00
|
|
|
TEST_CASE("http server redirection", "[httpd_redirect]")
|
|
|
|
{
|
2019-09-26 18:45:59 +02:00
|
|
|
SECTION("Connect to a local HTTP server, with redirection enabled")
|
|
|
|
{
|
|
|
|
int port = getFreePort();
|
|
|
|
ix::HttpServer server(port, "127.0.0.1");
|
|
|
|
server.makeRedirectServer("http://example.com");
|
|
|
|
|
|
|
|
auto res = server.listen();
|
|
|
|
REQUIRE(res.first);
|
|
|
|
server.start();
|
|
|
|
|
|
|
|
HttpClient httpClient;
|
|
|
|
WebSocketHttpHeaders headers;
|
|
|
|
|
|
|
|
std::string url("http://127.0.0.1:");
|
|
|
|
url += std::to_string(port);
|
|
|
|
url += "/data/foo.txt";
|
|
|
|
auto args = httpClient.createRequest(url);
|
|
|
|
|
|
|
|
args->extraHeaders = headers;
|
|
|
|
args->connectTimeout = 60;
|
|
|
|
args->transferTimeout = 60;
|
|
|
|
args->followRedirects = false; // we dont want to follow redirect during testing
|
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
|
|
|
auto response = httpClient.get(url, args);
|
|
|
|
|
|
|
|
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 << "Error message: " << response->errorMsg << std::endl;
|
|
|
|
|
|
|
|
REQUIRE(response->errorCode == HttpErrorCode::Ok);
|
|
|
|
REQUIRE(response->statusCode == 301);
|
|
|
|
REQUIRE(response->headers["Location"] == "http://example.com");
|
|
|
|
|
|
|
|
server.stop();
|
|
|
|
}
|
2019-06-23 23:54:21 +02:00
|
|
|
}
|