Thanks to visit codestin.com
Credit goes to github.com

Skip to content

feat: implement http_system_test example #193

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 21, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 20 additions & 7 deletions examples/site/testing_http/http_integration_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <gmock/gmock.h>
#include <chrono>
#include <memory>
#include <optional>
#include <string>

namespace {
Expand All @@ -40,35 +41,47 @@ char const* argv0 = nullptr;
class HttpIntegrationTest : public ::testing::Test {
protected:
void SetUp() override {
auto const* base_url = std::getenv("BASE_URL");
if (base_url != nullptr) {
url_ = base_url;
return;
}
curl_global_init(CURL_GLOBAL_ALL);

auto const exe = bfs::path(argv0).parent_path() / "http_integration_server";
auto server = bp::child(exe, "--port=8080");
ASSERT_TRUE(WaitForServerReady("http://localhost:8080/"));
url_ = "http://localhost:8080";
ASSERT_TRUE(WaitForServerReady(url_));
process_ = std::move(server);
}

void TearDown() override {
process_.terminate();
process_.wait();
if (process_.has_value()) {
process_->terminate();
process_->wait();
}
curl_global_cleanup();
}

[[nodiscard]] std::string const& url() const { return url_; }

private:
bp::child process_;
std::optional<bp::child> process_;
std::string url_;
};

// [START functions_http_system_test]
TEST_F(HttpIntegrationTest, Basic) {
auto constexpr kOkay = 200;

auto actual = HttpGet("http://localhost:8080/", R"js({"name": "Foo"})js");
auto actual = HttpGet(url(), R"js({"name": "Foo"})js");
EXPECT_EQ(actual.code, kOkay);
EXPECT_EQ(actual.payload, "Hello Foo!");

actual = HttpGet("http://localhost:8080/", R"js({})js");
actual = HttpGet(url(), R"js({})js");
EXPECT_EQ(actual.code, kOkay);
EXPECT_EQ(actual.payload, "Hello World!");
}
// [END functions_http_system_test]

extern "C" size_t CurlOnWriteData(char* ptr, size_t size, size_t nmemb,
void* userdata) {
Expand Down