UniEvent-WebSocket

 view release on metacpan or  search on metacpan

clib/tests/cpp_tests.cc  view on Meta::CPAN

#include "panda/unievent/http/Server.h"
#include <panda/log.h>
#include <panda/unievent/test/AsyncTest.h>
#include <panda/unievent/websocket/Client.h>
#include <panda/unievent/websocket/Server.h>
#include <thread>
#include <openssl/ssl.h>
#include <catch2/catch_test_macros.hpp>

#define panda_log_module panda::unievent::websocket::panda_log_module

using namespace panda::unievent::websocket;
using Location = panda::unievent::http::Server::Location;
using panda::unievent::test::AsyncTest;
using panda::unievent::LoopSP;
using panda::unievent::StreamSP;
using panda::string;

struct Pair {
    ServerSP server;
    ClientSP client;
};

using panda::unievent::SslContext;
static SslContext get_server_context(string ca_name) {
    auto ctx = SSL_CTX_new(SSLv23_server_method());

    auto r = SslContext::attach(ctx);

    string path("tests/cert");
    string cert = path + "/" + ca_name + ".pem";
    string key = path + "/" + ca_name + ".key";
    int err;

    err = SSL_CTX_use_certificate_file(ctx, cert.c_str(), SSL_FILETYPE_PEM);
    assert(err);

    err = SSL_CTX_use_PrivateKey_file(ctx, key.c_str(), SSL_FILETYPE_PEM);
    assert(err);

    err = SSL_CTX_check_private_key(ctx);
    assert(err);

    err = SSL_CTX_load_verify_locations(ctx, cert.c_str(), nullptr);
    assert(err);

    SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT, nullptr);
    SSL_CTX_set_verify_depth(ctx, 4);
    return r;
}

static ServerSP make_server (LoopSP loop, uint16_t& port, bool secure = false) {
    ServerSP server = new Server(loop);
    Server::Config conf;
    Location loc;
    loc.host = "127.0.0.1";
    if (secure) {
        loc.ssl_ctx = get_server_context("ca");
    }
    conf.locations.push_back(loc);
    server->configure(conf);
    server->run();
    port = server->sockaddr()->port();
    return server;
}

static ClientConnectRequestSP make_connect(uint16_t port) {
    ClientConnectRequestSP req = new ClientConnectRequest();
    req->uri = new URI();
    req->uri->host("127.0.0.1");
    req->uri->scheme("ws");
    req->uri->port(port);
    return req;
}

static Pair make_pair (LoopSP loop) {
    uint16_t port;
    ServerSP server = make_server(loop, port);
    ClientSP client = new Client(loop);
    client->connect(make_connect(port));
    return {server, client};
}

TEST_CASE("send ranges compiles", "[uews]") {
    if (false) {
        ServerConnectionSP conn;

        string msgs[] = {"1", "2", "3"};
        auto rr = msgs | ::ranges::view::transform([](string& s) -> string& {
            return s;
        });
        conn->send_message(begin(rr), end(rr));
    }
}

TEST_CASE("on_read after close", "[uews]") {
    AsyncTest test(1000, {"connect", "close"});
    {
        auto p = make_pair(test.loop);

        ServerConnectionSP sconn;

 view all matches for this distribution
 view release on metacpan -  search on metacpan

( run in 0.529 second using v1.00-cache-2.02-grep-82fe00e-cpan-f73e49a70403 )