~ne02ptzero/pistache

898a303154dd7b333f038e537f9b17606be4be1a — Louis Solofrizzo 2 years ago eaa6b65 + c85bfbc work/louis/test_ssl_sendfile
Merge branch 'kip-ssl-servefile-test' of https://github.com/kiplingw/pistache into work/louis/test_ssl_sendfile
1 files changed, 57 insertions(+), 0 deletions(-)

M tests/https_server_test.cc
M tests/https_server_test.cc => tests/https_server_test.cc +57 -0
@@ 1,3 1,5 @@
#include <array>

#include <pistache/http.h>
#include <pistache/client.h>
#include <pistache/endpoint.h>


@@ 26,6 28,16 @@ struct HelloHandler : public Http::Handler {
    }
};

struct ServeFileHandler : public Http::Handler {
    HTTP_PROTOTYPE(ServeFileHandler)

    void onRequest(const Http::Request&, Http::ResponseWriter writer) override {
        Http::serveFile(writer, "./certs/rootCA.crt").then([](ssize_t bytes) {
            std::cout << "Sent " << bytes << " bytes" << std::endl;
        }, Async::NoExcept);
    }
};

TEST(http_client_test, basic_tls_request) {
    Http::Endpoint server(ADDRESS "1");
    auto           flags = Tcp::Options::ReuseAddr;


@@ 236,3 248,48 @@ TEST(http_client_test, basic_tls_request_with_auth_with_cb) {
    server.shutdown();
}

TEST(http_client_test, basic_tls_request_with_servefile) {
    Http::Endpoint server(ADDRESS "6");
    auto           flags = Tcp::Options::ReuseAddr;
    auto           server_opts = Http::Endpoint::options().flags(flags);

    server.init(server_opts);
    server.setHandler(Http::make_handler<ServeFileHandler>());
    server.useSSL("./certs/server.crt", "./certs/server.key");
    server.serveThreaded();

    CURL        *curl;
    CURLcode    res;
    std::string buffer;

    curl_global_init(CURL_GLOBAL_DEFAULT);
    curl = curl_easy_init();
    ASSERT_NE(curl, nullptr);

    curl_easy_setopt(curl, CURLOPT_URL, "https://" ADDRESS "6");
    curl_easy_setopt(curl, CURLOPT_CAINFO, "./certs/rootCA.crt");
    curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 1);
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &write_cb);
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buffer);

    std::array<char, CURL_ERROR_SIZE> errorstring;
    curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errorstring.data());
    //curl_easy_setopt(curl, CURLOPT_VERBOSE, true);

    /* Skip hostname check */
    curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);

    res = curl_easy_perform(curl);

    if(res != CURLE_OK)
        std::cerr << errorstring.data() << std::endl;

    ASSERT_EQ(res, CURLE_OK);
    ASSERT_EQ(buffer.rfind("-----BEGIN CERTIFICATE-----", 0), 0);

    curl_easy_cleanup(curl);
    curl_global_cleanup();

    server.shutdown();
}