Skip to content
Snippets Groups Projects
requests_tls.cpp 6.62 KiB
Newer Older
#include <thread>

#include "cocurl/client.hpp"
#include "cocurl/request.hpp"
#include "curl/curl.h"

#if __has_include(<doctest.h>)
#include <doctest.h>
#else
#include <doctest/doctest.h>
#endif

using namespace cocurl;

TEST_CASE("Basic request") {
  Client g{};
  g.set_verify_peer(false);

  g.get("https://localhost:5000/", [&g](const Request &r) {
    CHECK(r.url() == "https://localhost:5000/");
    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();
  });

  g.run();
}

TEST_CASE("Basic request on bg thread") {
  Client g{};
  g.set_verify_peer(false);

  std::thread t([&g]() { g.run(); });

  g.get("https://localhost:5000/", [&g](const Request &r) {
    CHECK(r.url() == "https://localhost:5000/");
    CHECK(r.response_code() == 200);
    CHECK(r.response() == "OK");
    CHECK(r.response_headers()["content-type"] == "text/plain; charset=utf-8");
    g.close();
  });

  t.join();
}

TEST_CASE("Basic manual request") {
  Client g{};
  g.set_verify_peer(false);

  auto r = std::make_shared<Request>(&g, Request::Method::GET,
                                     "https://localhost:5000/");
  r->on_complete([&g](const Request &r) {
    CHECK(r.url() == "https://localhost:5000/");
    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);

  g.run();
}

TEST_CASE("Basic redirect") {
  Client g{};
  g.set_verify_peer(false);

  g.get(
      "https://localhost:5000/redirect",
      [&g](const Request &r) {
        CHECK(r.url() == "https://localhost:5000/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:5000/redirect",
      [&g](const Request &r) {
        CHECK(r.url() == "https://localhost:5000/redirect");
        CHECK(r.response_code() == 302);
        g.close();
      },
      {}, 0);

  g.run();
}

TEST_CASE("Max redirects") {
  Client g{};
  g.set_verify_peer(false);

  g.get(
      "https://localhost:5000/double_redirect",
      [&g](const Request &r) {
        CHECK(r.url() == "https://localhost:5000/double_redirect");
        CHECK(r.response_code() == 302);
        g.close();
      },
      {}, 1);

  g.run();
}

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:5000/post");
  r->request("ABCD");
  r->on_complete([&g](const Request &r) {
    CHECK(r.url() == "https://localhost:5000/post");
    CHECK(r.response_code() == 200);
    CHECK(r.response() == "ABCD");
    g.close();
  });

  g.submit_request(r);

  g.run();
}

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:5000/put");
  r->request("ABCD");
  r->on_complete([&g](const Request &r) {
    CHECK(r.url() == "https://localhost:5000/put");
    CHECK(r.response_code() == 200);
    CHECK(r.response() == "ABCD");
    g.close();
  });

  g.submit_request(r);

  g.run();
}

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:5000/");
  r->on_complete([&g](const Request &r) {
    CHECK(r.url() == "https://localhost:5000/");
    CHECK(r.response_code() == 200);
    CHECK(r.response().empty());
    g.close();
  });

  g.submit_request(r);

  g.run();
}

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:5000/");
  r->on_complete([&g](const Request &r) {
    CHECK(r.url() == "https://localhost:5000/");
    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);

  g.run();
}

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:5000/delete");
  r->on_complete([&g](const Request &r) {
    CHECK(r.url() == "https://localhost:5000/delete");
    CHECK(r.response_code() == 200);

    g.close();
  });

  g.submit_request(r);

  g.run();
}

TEST_CASE("Basic simple POST request") {
  Client g{};
  g.set_verify_peer(false);

  g.post("https://localhost:5000/post", "ABCD", "text/plain",
         [&g](const Request &r) {
           CHECK(r.url() == "https://localhost:5000/post");
           CHECK(r.response_code() == 200);
           CHECK(r.response() == "ABCD");
           g.close();
         });

  g.run();
}

TEST_CASE("Basic simple PUT request") {
  Client g{};
  g.set_verify_peer(false);

  g.put("https://localhost:5000/put", "ABCD", "text/plain",
        [&g](const Request &r) {
          CHECK(r.url() == "https://localhost:5000/put");
          CHECK(r.response_code() == 200);
          CHECK(r.response() == "ABCD");
          g.close();
        });

  g.run();
}

TEST_CASE("Basic simple HEAD request") {
  Client g{};
  g.set_verify_peer(false);

  g.head("https://localhost:5000/", [&g](const Request &r) {
    CHECK(r.url() == "https://localhost:5000/");
    CHECK(r.response_code() == 200);
    CHECK(r.response().empty());
    g.close();
  });

  g.run();
}

TEST_CASE("Basic simple OPTIONS request") {
  Client g{};
  g.set_verify_peer(false);

  g.options("https://localhost:5000/", [&g](const Request &r) {
    CHECK(r.url() == "https://localhost:5000/");
    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.run();
}

TEST_CASE("Basic simple DELETE request") {
  Client g{};
  g.set_verify_peer(false);

  g.delete_("https://localhost:5000/delete", [&g](const Request &r) {
    CHECK(r.url() == "https://localhost:5000/delete");
    CHECK(r.response_code() == 200);

    g.close();
  });

  g.run();
}