Skip to content

Commit cab4554

Browse files
authoredApr 2, 2024
Test packaging (#3)
Signed-off-by: Darshan Sen <raisinten@gmail.com>
1 parent 69a53a6 commit cab4554

File tree

7 files changed

+90
-2
lines changed

7 files changed

+90
-2
lines changed
 

‎CMakeLists.txt

+5-1
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,9 @@ endif()
2323

2424
if(BENONI_TESTS)
2525
enable_testing()
26-
add_subdirectory(test)
26+
add_subdirectory(test/unit)
27+
28+
if(PROJECT_IS_TOP_LEVEL)
29+
add_subdirectory(test/packaging)
30+
endif()
2731
endif()

‎Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ configure: .always
1414
$(CMAKE) -B build -DBENONI_TESTS:BOOL=ON -DBENONI_EXAMPLES:BOOL=ON
1515

1616
build: .always
17-
$(CLANG_FORMAT) --style=file -i include/benoni/http.h src/apple/http.mm src/win32/http.cc src/linux/http.cc examples/http_example.cc test/postman-echo-get.cc
17+
$(CLANG_FORMAT) --style=file -i include/benoni/http.h src/apple/http.mm src/win32/http.cc src/linux/http.cc examples/http_example.cc test/unit/postman-echo-get.cc test/packaging/project/project.cc
1818
$(CMAKE) --build build
1919

2020
example: .always

‎test/packaging/CMakeLists.txt

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
add_test(NAME packaging.project_configure COMMAND
2+
"${CMAKE_COMMAND}"
3+
-S "${CMAKE_CURRENT_SOURCE_DIR}/project"
4+
-B "${CMAKE_CURRENT_BINARY_DIR}/project"
5+
"-DCMAKE_BUILD_TYPE:STRING=${CMAKE_BUILD_TYPE}")
6+
7+
add_test(NAME packaging.project_build COMMAND
8+
"${CMAKE_COMMAND}"
9+
--build "${CMAKE_CURRENT_BINARY_DIR}/project")
10+
11+
set_tests_properties(packaging.project_build
12+
PROPERTIES DEPENDS packaging.project_configure)

‎test/packaging/project/CMakeLists.txt

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
cmake_minimum_required(VERSION 3.18)
2+
project(project VERSION 0.0.1 LANGUAGES CXX DESCRIPTION "Project")
3+
4+
set(CMAKE_CXX_STANDARD 20)
5+
6+
add_subdirectory(
7+
"${PROJECT_SOURCE_DIR}/../../.."
8+
"${CMAKE_CURRENT_BINARY_DIR}/benoni_build")
9+
10+
add_executable(project project.cc)
11+
target_link_libraries(project PRIVATE benoni)

‎test/packaging/project/project.cc

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#include <benoni/http.h>
2+
3+
#if defined(__APPLE__)
4+
#include <CoreFoundation/CoreFoundation.h>
5+
#elif defined(linux)
6+
#include <glib.h>
7+
#endif
8+
9+
#include <iostream>
10+
#include <variant>
11+
12+
using benoni::Method;
13+
using benoni::request;
14+
using benoni::RequestOptions;
15+
using benoni::RequestOptionsBuilder;
16+
using benoni::Response;
17+
18+
int main() {
19+
#if defined(linux)
20+
GMainLoop *loop = g_main_loop_new(nullptr, FALSE);
21+
#endif
22+
23+
std::string url{"https://postman-echo.com/get"};
24+
std::cout << "sending request to: \"" << url << "\"" << std::endl;
25+
26+
request(std::move(url), RequestOptionsBuilder{}.build(),
27+
[](std::variant<std::string, Response> result) {
28+
// This callback is run in a background thread on Windows and Apple
29+
// and on the main thread on Gtk Linux.
30+
if (std::holds_alternative<std::string>(result)) {
31+
std::cerr << "error: [" << std::get<std::string>(result) << "]"
32+
<< std::endl;
33+
exit(EXIT_FAILURE);
34+
}
35+
36+
Response response{std::get<Response>(result)};
37+
38+
std::cout << "response status: " << response.status << std::endl;
39+
std::cout << "response headers: [" << std::endl;
40+
for (const auto &[key, value] : response.headers) {
41+
std::cout << " \"" << key << "\": \"" << value << "\","
42+
<< std::endl;
43+
}
44+
std::cout << "]" << std::endl;
45+
std::cout << "response body: \"" << response.body << "\""
46+
<< std::endl;
47+
exit(EXIT_SUCCESS);
48+
});
49+
50+
#if defined(__APPLE__)
51+
CFRunLoopRun();
52+
#elif defined(linux)
53+
g_main_loop_run(loop);
54+
g_main_loop_unref(loop);
55+
#else
56+
while (true)
57+
;
58+
#endif
59+
60+
return 0;
61+
}
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)