1 #include <gtest/gtest.h>
4 #include <boost/process.hpp>
6 #include "ostree_http_repo.h"
7 #include "ostree_ref.h"
8 #include "test_utils.h"
11 static size_t writeString(
void *contents,
size_t size,
size_t nmemb,
void *userp) {
14 (
static_cast<std::string *
>(userp))->append(
static_cast<char *
>(contents), size * nmemb);
21 TEST(treehub_server, token_auth) {
24 server.root_url(std::string(
"http://127.0.0.1:") + port);
25 server.SetToken(
"test_token");
26 server.InjectIntoCurl(
"/", curl_handle.get());
28 curlEasySetoptWrapper(curl_handle.get(), CURLOPT_WRITEFUNCTION, writeString);
29 curlEasySetoptWrapper(curl_handle.get(), CURLOPT_WRITEDATA,
static_cast<void *
>(&response));
30 curl_easy_perform(curl_handle.get());
33 curl_easy_getinfo(curl_handle.get(), CURLINFO_RESPONSE_CODE, &rescode);
35 auto response_json = Utils::parseJSON(response);
36 EXPECT_EQ(response_json[
"Authorization"],
"Bearer test_token");
38 FAIL() <<
"Failed to authenticate token with server";
43 TEST(treehub_server, basic_auth) {
46 server.root_url(std::string(
"http://127.0.0.1:") + port);
47 server.SetAuthBasic(
"login",
"password");
48 server.InjectIntoCurl(
"/", curl_handle.get());
50 curlEasySetoptWrapper(curl_handle.get(), CURLOPT_WRITEFUNCTION, writeString);
51 curlEasySetoptWrapper(curl_handle.get(), CURLOPT_WRITEDATA,
static_cast<void *
>(&response));
52 curl_easy_perform(curl_handle.get());
55 curl_easy_getinfo(curl_handle.get(), CURLINFO_RESPONSE_CODE, &rescode);
57 auto response_json = Utils::parseJSON(response);
58 EXPECT_EQ(response_json[
"Authorization"],
"Basic bG9naW46cGFzc3dvcmQ=");
60 FAIL() <<
"Failed to authenticate login/password with server";
65 int main(
int argc,
char **argv) {
66 ::testing::InitGoogleTest(&argc, argv);
67 std::string server =
"tests/sota_tools/headers_response_server.py";
68 port = TestUtils::getFreePort();
70 boost::process::child server_process(server, port);
71 TestUtils::waitForServer(
"http://127.0.0.1:" + port +
"/");
73 return RUN_ALL_TESTS();