Skip to content

Commit

Permalink
Add two dummy unit tests, to validate the minimal requirements : lib …
Browse files Browse the repository at this point in the history
…linking and server start
  • Loading branch information
benlabbe committed Oct 6, 2023
1 parent efd7bd5 commit da92b77
Show file tree
Hide file tree
Showing 15 changed files with 222 additions and 21 deletions.
41 changes: 24 additions & 17 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ if (NOT (${CMAKE_SYSTEM_NAME} STREQUAL "Windows"))
endif()
endif()

#enable_testing()
enable_testing()

include(SystemSpecificInformations.cmake)

Expand All @@ -29,12 +29,12 @@ if (NOT (${CMAKE_SYSTEM_NAME} STREQUAL "Windows"))


if (HAVE_STDCPP17)
message("C++17 supported ")
message("C++17 supported")
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
elseif (HAVE_STDCPP14)
message("C++14 supported ")
message("C++14 supported")
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
Expand Down Expand Up @@ -98,7 +98,6 @@ set(CMAKE_PREFIX_PATH
)



add_definitions(-DQT_NO_KEYWORDS)
add_definitions(-DQT_DISABLE_DEPRECATED_BEFORE=0)
if (NOT (${CMAKE_SYSTEM_NAME} STREQUAL "Windows"))
Expand All @@ -123,10 +122,17 @@ if (${CMAKE_SYSTEM_NAME} STREQUAL "Windows")
DESTINATION ${LIB_INSTALL_DIR})
endif ()

add_subdirectory(src)
#add_subdirectory(test)

if(NOT DEFINED VERSION_RELEASE)
set(VERSION_RELEASE "1")
endif()
configure_file(QHttpServerConfig-src.cmake QHttpServerConfig.cmake @ONLY)
INCLUDE(${CMAKE_CURRENT_BINARY_DIR}/QHttpServerConfig.cmake)


add_subdirectory(src)
add_subdirectory(examples)

########### documentation ###############

# add a target to generate API documentation with Doxygen
Expand All @@ -152,9 +158,6 @@ set(CMAKE_INSTALL_DEBUG_LIBRARIES TRUE)
set(MAKE_INSTALL_UCRT_LIBRARIES TRUE)
INCLUDE(InstallRequiredSystemLibraries)

if(NOT DEFINED VERSION_RELEASE)
set(VERSION_RELEASE "1")
endif()

set(CPACK_MONOLITHIC_INSTALL 1)
SET(CPACK_PACKAGE_NAME "qhttpserver")
Expand All @@ -163,16 +166,20 @@ SET(CPACK_PACKAGE_VENDOR "Nikhil Marathe <nsm.nikhil@gmail.com>")
SET(CPACK_PACKAGE_CONTACT "Gaël de Chalendar <kleag@free.fr>")
SET(CPACK_PACKAGE_DESCRIPTION_FILE "${CMAKE_CURRENT_SOURCE_DIR}/README.md")
SET(CPACK_RESOURCE_FILE_LICENSE "${CMAKE_CURRENT_SOURCE_DIR}/LICENSE")
SET(CPACK_PACKAGE_VERSION_MAJOR "0")
SET(CPACK_PACKAGE_VERSION_MINOR "0")
SET(CPACK_PACKAGE_VERSION_PATCH "${VERSION_RELEASE}")
SET(CPACK_PACKAGE_INSTALL_DIRECTORY "qhttpserver-0.0.${VERSION_RELEASE}")
SET(CPACK_NSIS_INSTALL_ROOT "$PROGRAMFILES64")
SET(CPACK_NSIS_MODIFY_PATH "ON")
SET(CPACK_PACKAGE_VERSION_MAJOR "${QHTTPSERVER_VERSION_MAJOR}")
SET(CPACK_PACKAGE_VERSION_MINOR "${QHTTPSERVER_VERSION_MINOR}")
SET(CPACK_PACKAGE_VERSION_PATCH "${QHTTPSERVER_VERSION_RELEASE}")
SET(CPACK_PACKAGE_VERSION "${QHTTPSERVER_VERSION}")
SET(CPACK_PACKAGE_INSTALL_DIRECTORY "qhttpserver-${QHTTPSERVER_VERSION}")
#SET(CPACK_NSIS_INSTALL_ROOT "$PROGRAMFILES64")
#SET(CPACK_NSIS_MODIFY_PATH "ON")
SET(CPACK_STRIP_FILES "")
SET(CPACK_SOURCE_STRIP_FILES "")
# set(CPACK_DEBIAN_PACKAGE_SHLIBDEPS ON)
set(CPACK_GENERATOR ${SPECIFIC_SYSTEM_PREFERED_CPACK_GENERATOR})
SET(CPACK_DEBIAN_PACKAGE_SHLIBDEPS ON)
SET(CPACK_GENERATOR ${SPECIFIC_SYSTEM_PREFERED_CPACK_GENERATOR})
# set(CPACK_RPM_PACKAGE_REQUIRES "qt5 >= 5.2")
# SET(CPACK_DEBIAN_PACKAGE_DEPENDS "")

SET(CPACK_PACKAGE_FILE_NAME "${CPACK_PACKAGE_NAME}-${CPACK_PACKAGE_VERSION}")

INCLUDE(CPack)
15 changes: 15 additions & 0 deletions QHttpServerConfig-src.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@

SET(QHTTPSERVER_INCLUDE_DIR @CMAKE_INSTALL_PREFIX@/include)
SET(QHTTPSERVER_LIBRARY_DIRS @CMAKE_INSTALL_PREFIX@/${LIB_INSTALL_DIR})

set(QHTTPSERVER_VERSION_MAJOR "0")
set(QHTTPSERVER_VERSION_MINOR "2")
set(QHTTPSERVER_VERSION_RELEASE "@VERSION_RELEASE@")

set(QHTTPSERVER_VERSION "${QHTTPSERVER_VERSION_MAJOR}.${QHTTPSERVER_VERSION_MINOR}.${QHTTPSERVER_VERSION_RELEASE}")

set(QHTTPSERVER_GENERIC_LIB_VERSION ${QHTTPSERVER_VERSION})
set(QHTTPSERVER_GENERIC_LIB_SOVERSION ${QHTTPSERVER_VERSION_MINOR})


message(STATUS "QHttpServer version ${QHTTPSERVER_VERSION} - QHTTPSERVER_GENERIC_LIB_VERSION ${QHTTPSERVER_GENERIC_LIB_VERSION} ")
3 changes: 3 additions & 0 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
add_subdirectory(helloworld)
add_subdirectory(greeting)
#add_subdirectory(bodydata)
16 changes: 16 additions & 0 deletions examples/bodydata/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
include_directories(BEFORE ${CMAKE_SOURCE_DIR}/src)

add_definitions( -DQHTTPSERVER_EXPORT )

SET(œ_EXE_SRCS
bodydata.cpp
)

add_executable(bodydata_server ${bodydata_EXE_SRCS})

target_link_libraries(bodydata_server qhttpserver ${Qt5_LIBRARIES})

add_test(
NAME bodydata_Test
COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/test/bodydata.sh ${CMAKE_CURRENT_BINARY_DIR}
)
6 changes: 3 additions & 3 deletions examples/bodydata/bodydata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ BodyData::BodyData()
QHttpServer *server = new QHttpServer(this);
connect(server, SIGNAL(newRequest(QHttpRequest*, QHttpResponse*)),
this, SLOT(handleRequest(QHttpRequest*, QHttpResponse*)));

server->listen(QHostAddress::Any, 8080);
}

void BodyData::handleRequest(QHttpRequest *req, QHttpResponse *resp)
{
new Responder(req, resp);
Responder* resp = new Responder(req, resp);
}

/// Responder
Expand All @@ -42,7 +42,7 @@ Responder::Responder(QHttpRequest *req, QHttpResponse *resp)

resp->setHeader("Content-Type", "text/html");
resp->writeHead(200);

QString name = exp.capturedTexts()[1];
QString bodyStart = tr("<html><head><title>BodyData App</title></head><body><h1>Hello %1!</h1><p>").arg(name);
resp->write(bodyStart.toUtf8());
Expand Down
38 changes: 38 additions & 0 deletions examples/bodydata/test/bodydata.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#!/bin/bash
set -o errexit
set -o nounset
set -o pipefail

#-------------------------------------------------------------------------------
# to ensure the script actually runs inside its own folder
MY_PATH="`dirname \"$0\"`" # relative
MY_PATH="`( cd \"$MY_PATH\" && pwd )`" # absolutized and normalized
if [ -z "$MY_PATH" ] ; then
# Error. for some reason, the path is not accessible
# to the script (e.g. permissions re-evaled after suid)
exit 1 # Fail
fi

BINARY_DIR=$1

coproc $BINARY_DIR/bodydata_server
SERVER_PID=$!

expected="$MY_PATH/expected.html"
observed="output.html"
\rm -f $observed

curl --request GET \
--url http://localhost:8080/user/abc \
-o $observed
if cmp -s "$expected" "$observed"; then
echo "All good"
else
echo "Unexpected response from server"
exit 1
fi
\rm -f $observed

kill $SERVER_PID

exit 0
1 change: 1 addition & 0 deletions examples/bodydata/test/expected.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<html><head><title>BodyData App</title></head><body><h1>Hello abc!</h1><p></p></body></html>
16 changes: 16 additions & 0 deletions examples/greeting/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
include_directories(BEFORE ${CMAKE_SOURCE_DIR}/src)

add_definitions( -DQHTTPSERVER_EXPORT )

SET(greeting_EXE_SRCS
greeting.cpp
)

add_executable(greeting_server ${greeting_EXE_SRCS})

target_link_libraries(greeting_server qhttpserver ${Qt5_LIBRARIES})

add_test(
NAME greeting_Test
COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/test/greeting.sh ${CMAKE_CURRENT_BINARY_DIR}
)
1 change: 1 addition & 0 deletions examples/greeting/test/bad_user_expected.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
You aren't allowed here!
1 change: 1 addition & 0 deletions examples/greeting/test/expected.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<html><head><title>Greeting App</title></head><body><h1>Hello abc!</h1></body></html>
50 changes: 50 additions & 0 deletions examples/greeting/test/greeting.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#!/bin/bash
set -o errexit
set -o nounset
set -o pipefail

#-------------------------------------------------------------------------------
# to ensure the script actually runs inside its own folder
MY_PATH="`dirname \"$0\"`" # relative
MY_PATH="`( cd \"$MY_PATH\" && pwd )`" # absolutized and normalized
if [ -z "$MY_PATH" ] ; then
# Error. for some reason, the path is not accessible
# to the script (e.g. permissions re-evaled after suid)
exit 1 # Fail
fi

BINARY_DIR=$1
coproc $BINARY_DIR/greeting_server
SERVER_PID=$!

expected="$MY_PATH/bad_user_expected.txt"
observed="output.txt"
\rm -f $observed

curl --request GET \
--url http://localhost:8080 \
-o $observed
if cmp -s "$expected" "$observed"; then
echo "All good"
else
echo "Unexpected response from server"
exit 1
fi
\rm -f $observed


expected="$MY_PATH/expected.html"
curl --request GET \
--url http://localhost:8080/user/abc \
-o $observed
if cmp -s "$expected" "$observed"; then
echo "All good"
else
echo "Unexpected response from server"
exit 1
fi
\rm -f $observed

kill $SERVER_PID

exit 0
16 changes: 16 additions & 0 deletions examples/helloworld/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
include_directories(BEFORE ${CMAKE_SOURCE_DIR}/src)

add_definitions( -DQHTTPSERVER_EXPORT )

SET(helloworld_EXE_SRCS
helloworld.cpp
)

add_executable(helloworld_server ${helloworld_EXE_SRCS})

target_link_libraries(helloworld_server qhttpserver ${Qt5_LIBRARIES})

add_test(
NAME helloworld_Test
COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/test/helloworld.sh ${CMAKE_CURRENT_BINARY_DIR}
)
1 change: 0 additions & 1 deletion examples/helloworld/helloworld.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ HelloWorld::HelloWorld()
void HelloWorld::handleRequest(QHttpRequest *req, QHttpResponse *resp)
{
Q_UNUSED(req);

QByteArray body = "Hello World";
resp->setHeader("Content-Length", QString::number(body.size()));
resp->writeHead(200);
Expand Down
1 change: 1 addition & 0 deletions examples/helloworld/test/expected.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello World
37 changes: 37 additions & 0 deletions examples/helloworld/test/helloworld.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#!/bin/bash
set -o errexit
set -o nounset
set -o pipefail

#-------------------------------------------------------------------------------
# to ensure the script actually runs inside its own folder
MY_PATH="`dirname \"$0\"`" # relative
MY_PATH="`( cd \"$MY_PATH\" && pwd )`" # absolutized and normalized
if [ -z "$MY_PATH" ] ; then
# Error. for some reason, the path is not accessible
# to the script (e.g. permissions re-evaled after suid)
exit 1 # Fail
fi

BINARY_DIR=$1
coproc $BINARY_DIR/helloworld_server
SERVER_PID=$!

expected="$MY_PATH/expected.txt"
observed="output.txt"
\rm -f $observed

curl --request GET \
--url http://localhost:8080 \
-o $observed
if cmp -s "$expected" "$observed"; then
echo "All good"
else
echo "Unexpected response from server"
exit 1
fi
\rm -f $observed

kill $SERVER_PID

exit 0

0 comments on commit da92b77

Please sign in to comment.