IXWebSocket/test/IXSocketTest.cpp

99 lines
3.1 KiB
C++
Raw Permalink Normal View History

2019-01-05 20:42:25 +01:00
/*
* IXSocketTest.cpp
* Author: Benjamin Sergeant
* Copyright (c) 2019 Machine Zone. All rights reserved.
*/
#include "IXTest.h"
2019-01-05 20:42:25 +01:00
#include "catch.hpp"
2019-09-23 19:25:23 +02:00
#include <iostream>
#include <ixwebsocket/IXCancellationRequest.h>
#include <ixwebsocket/IXSocket.h>
#include <ixwebsocket/IXSocketFactory.h>
2019-03-11 00:07:48 +01:00
#include <string.h>
2019-01-05 20:42:25 +01:00
using namespace ix;
namespace ix
{
void testSocket(const std::string& host,
int port,
const std::string& request,
std::shared_ptr<Socket> socket,
int expectedStatus,
int timeoutSecs)
{
std::string errMsg;
static std::atomic<bool> requestInitCancellation(false);
auto isCancellationRequested =
makeCancellationRequestWithTimeout(timeoutSecs, requestInitCancellation);
bool success = socket->connect(host, port, errMsg, isCancellationRequested);
TLogger() << "errMsg: " << errMsg;
2019-01-05 20:42:25 +01:00
REQUIRE(success);
TLogger() << "Sending request: " << request << "to " << host << ":" << port;
2019-01-06 06:10:08 +01:00
REQUIRE(socket->writeBytes(request, isCancellationRequested));
2019-01-05 20:42:25 +01:00
auto lineResult = socket->readLine(isCancellationRequested);
auto lineValid = lineResult.first;
auto line = lineResult.second;
TLogger() << "read error: " << strerror(Socket::getErrno());
2019-01-06 06:16:13 +01:00
2019-01-05 20:42:25 +01:00
REQUIRE(lineValid);
int status = -1;
REQUIRE(sscanf(line.c_str(), "HTTP/1.1 %d", &status) == 1);
REQUIRE(status == expectedStatus);
}
2019-09-23 19:25:23 +02:00
} // namespace ix
2019-01-05 20:42:25 +01:00
TEST_CASE("socket", "[socket]")
{
2019-09-23 19:25:23 +02:00
SECTION("Connect to a local websocket server over a free port. Send GET request without "
"header. Should return 400")
2019-01-05 20:42:25 +01:00
{
// Start a server first which we'll hit with our socket code
int port = getFreePort();
ix::WebSocketServer server(port);
REQUIRE(startWebSocketEchoServer(server));
2019-03-15 02:37:38 +01:00
std::string errMsg;
bool tls = false;
SocketTLSOptions tlsOptions;
std::shared_ptr<Socket> socket = createSocket(tls, -1, errMsg, tlsOptions);
std::string host("127.0.0.1");
std::stringstream ss;
ss << "GET / HTTP/1.1\r\n";
ss << "Host: " << host << "\r\n";
ss << "\r\n";
std::string request(ss.str());
int expectedStatus = 400;
2019-01-06 05:38:43 +01:00
int timeoutSecs = 3;
2019-01-05 20:42:25 +01:00
testSocket(host, port, request, socket, expectedStatus, timeoutSecs);
}
#if defined(IXWEBSOCKET_USE_TLS)
2019-09-23 19:25:23 +02:00
SECTION("Connect to google HTTPS server over port 443. Send GET request without header. Should "
"return 200")
2019-01-05 20:42:25 +01:00
{
2019-03-15 02:37:38 +01:00
std::string errMsg;
bool tls = true;
2019-09-23 04:40:33 +02:00
SocketTLSOptions tlsOptions;
tlsOptions.caFile = "cacert.pem";
std::shared_ptr<Socket> socket = createSocket(tls, -1, errMsg, tlsOptions);
2019-01-05 20:42:25 +01:00
std::string host("www.google.com");
int port = 443;
std::string request("GET / HTTP/1.1\r\n\r\n");
int expectedStatus = 200;
2019-01-06 05:38:43 +01:00
int timeoutSecs = 3;
2019-01-05 20:42:25 +01:00
testSocket(host, port, request, socket, expectedStatus, timeoutSecs);
}
#endif
}