Skip to content
Snippets Groups Projects
requests_tls.cpp 6.04 KiB
Newer Older
  • Learn to ignore specific revisions
  • #include <thread>
    
    
    #include "coeurl/client.hpp"
    #include "coeurl/request.hpp"
    
    #include "curl/curl.h"
    
    #if __has_include(<doctest.h>)
    #include <doctest.h>
    #else
    #include <doctest/doctest.h>
    #endif
    
    
    using namespace coeurl;
    
    
    TEST_CASE("Basic request") {
      Client g{};
      g.set_verify_peer(false);
    
    
      g.get("https://localhost:5443/", [&g](const Request &r) {
        CHECK(r.url() == "https://localhost:5443/");
    
        CHECK(r.response_code() == 200);
        CHECK(r.error_code() == CURLE_OK);
        CHECK(r.response() == "OK");
        CHECK(r.response_headers()["content-type"] == "text/plain; charset=utf-8");
        g.close();
      });
    }
    
    TEST_CASE("Basic manual request") {
      Client g{};
      g.set_verify_peer(false);
    
      auto r = std::make_shared<Request>(&g, Request::Method::GET,
    
                                         "https://localhost:5443/");
    
      r->on_complete([&g](const Request &r) {
    
        CHECK(r.url() == "https://localhost:5443/");
    
        CHECK(r.response_code() == 200);
        CHECK(r.response() == "OK");
        CHECK(r.response_headers()["content-type"] == "text/plain; charset=utf-8");
        g.close();
      });
    
      g.submit_request(r);
    }
    
    TEST_CASE("Basic redirect") {
      Client g{};
      g.set_verify_peer(false);
    
      g.get(
    
          "https://localhost:5443/redirect",
    
          [&g](const Request &r) {
    
            CHECK(r.url() == "https://localhost:5443/redirect");
    
            CHECK(r.response_code() == 200);
            CHECK(r.response() == "OK");
            g.close();
          },
          {}, 1);
    }
    
    TEST_CASE("No redirect") {
      Client g{};
      g.set_verify_peer(false);
    
      g.get(
    
          "https://localhost:5443/redirect",
    
          [&g](const Request &r) {
    
            CHECK(r.url() == "https://localhost:5443/redirect");
    
            CHECK(r.response_code() == 302);
            g.close();
          },
          {}, 0);
    }
    
    TEST_CASE("Max redirects") {
      Client g{};
      g.set_verify_peer(false);
    
      g.get(
    
          "https://localhost:5443/double_redirect",
    
          [&g](const Request &r) {
    
            CHECK(r.url() == "https://localhost:5443/double_redirect");
    
            CHECK(r.response_code() == 302);
            g.close();
          },
          {}, 1);
    }
    
    TEST_CASE("Basic manual POST request") {
      Client g{};
      g.set_verify_peer(false);
    
      auto r = std::make_shared<Request>(&g, Request::Method::POST,
    
                                         "https://localhost:5443/post");
    
      r->request("ABCD");
      r->on_complete([&g](const Request &r) {
    
        CHECK(r.url() == "https://localhost:5443/post");
    
        CHECK(r.response_code() == 200);
        CHECK(r.response() == "ABCD");
        g.close();
      });
    
      g.submit_request(r);
    }
    
    TEST_CASE("Basic manual PUT request") {
      Client g{};
      g.set_verify_peer(false);
    
      auto r = std::make_shared<Request>(&g, Request::Method::PUT,
    
                                         "https://localhost:5443/put");
    
      r->request("ABCD");
      r->on_complete([&g](const Request &r) {
    
        CHECK(r.url() == "https://localhost:5443/put");
    
        CHECK(r.response_code() == 200);
        CHECK(r.response() == "ABCD");
        g.close();
      });
    
      g.submit_request(r);
    }
    
    TEST_CASE("Basic manual HEAD request") {
      Client g{};
      g.set_verify_peer(false);
    
      auto r = std::make_shared<Request>(&g, Request::Method::HEAD,
    
                                         "https://localhost:5443/");
    
      r->on_complete([&g](const Request &r) {
    
        CHECK(r.url() == "https://localhost:5443/");
    
        CHECK(r.response_code() == 200);
        CHECK(r.response().empty());
        g.close();
      });
    
      g.submit_request(r);
    }
    
    TEST_CASE("Basic manual OPTIONS request") {
      Client g{};
      g.set_verify_peer(false);
    
      auto r = std::make_shared<Request>(&g, Request::Method::OPTIONS,
    
                                         "https://localhost:5443/");
    
      r->on_complete([&g](const Request &r) {
    
        CHECK(r.url() == "https://localhost:5443/");
    
        CHECK(r.response_code() == 200);
        CHECK(r.response_headers()["allow"].find("HEAD") != std::string::npos);
        CHECK(r.response_headers()["allow"].find("OPTIONS") != std::string::npos);
        CHECK(r.response_headers()["allow"].find("GET") != std::string::npos);
    
        g.close();
      });
    
      g.submit_request(r);
    }
    
    TEST_CASE("Basic manual DELETE request") {
      Client g{};
      g.set_verify_peer(false);
    
      auto r = std::make_shared<Request>(&g, Request::Method::DELETE,
    
                                         "https://localhost:5443/delete");
    
      r->on_complete([&g](const Request &r) {
    
        CHECK(r.url() == "https://localhost:5443/delete");
    
        CHECK(r.response_code() == 200);
    
        g.close();
      });
    
      g.submit_request(r);
    }
    
    TEST_CASE("Basic simple POST request") {
      Client g{};
      g.set_verify_peer(false);
    
    
      g.post("https://localhost:5443/post", "ABCD", "text/plain",
    
             [&g](const Request &r) {
    
               CHECK(r.url() == "https://localhost:5443/post");
    
               CHECK(r.response_code() == 200);
               CHECK(r.response() == "ABCD");
               g.close();
             });
    }
    
    TEST_CASE("Basic simple PUT request") {
      Client g{};
      g.set_verify_peer(false);
    
    
      g.put("https://localhost:5443/put", "ABCD", "text/plain",
    
            [&g](const Request &r) {
    
              CHECK(r.url() == "https://localhost:5443/put");
    
              CHECK(r.response_code() == 200);
              CHECK(r.response() == "ABCD");
              g.close();
            });
    }
    
    TEST_CASE("Basic simple HEAD request") {
      Client g{};
      g.set_verify_peer(false);
    
    
      g.head("https://localhost:5443/", [&g](const Request &r) {
        CHECK(r.url() == "https://localhost:5443/");
    
        CHECK(r.response_code() == 200);
        CHECK(r.response().empty());
        g.close();
      });
    }
    
    TEST_CASE("Basic simple OPTIONS request") {
      Client g{};
      g.set_verify_peer(false);
    
    
      g.options("https://localhost:5443/", [&g](const Request &r) {
        CHECK(r.url() == "https://localhost:5443/");
    
        CHECK(r.response_code() == 200);
        CHECK(r.response_headers()["allow"].find("HEAD") != std::string::npos);
        CHECK(r.response_headers()["allow"].find("OPTIONS") != std::string::npos);
        CHECK(r.response_headers()["allow"].find("GET") != std::string::npos);
    
        g.close();
      });
    }
    
    TEST_CASE("Basic simple DELETE request") {
      Client g{};
      g.set_verify_peer(false);
    
    
      g.delete_("https://localhost:5443/delete", [&g](const Request &r) {
        CHECK(r.url() == "https://localhost:5443/delete");
    
        CHECK(r.response_code() == 200);
    
        g.close();
      });
    }