From da92b772667db9077a211013eb105fb674d4e1aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benjamin=20Labb=C3=A9?= Date: Fri, 6 Oct 2023 10:41:24 +0200 Subject: [PATCH] Add two dummy unit tests, to validate the minimal requirements : lib linking and server start --- CMakeLists.txt | 41 +++++++++------- QHttpServerConfig-src.cmake | 15 ++++++ examples/CMakeLists.txt | 3 ++ examples/bodydata/CMakeLists.txt | 16 +++++++ examples/bodydata/bodydata.cpp | 6 +-- examples/bodydata/test/bodydata.sh | 38 +++++++++++++++ examples/bodydata/test/expected.html | 1 + examples/greeting/CMakeLists.txt | 16 +++++++ examples/greeting/test/bad_user_expected.txt | 1 + examples/greeting/test/expected.html | 1 + examples/greeting/test/greeting.sh | 50 ++++++++++++++++++++ examples/helloworld/CMakeLists.txt | 16 +++++++ examples/helloworld/helloworld.cpp | 1 - examples/helloworld/test/expected.txt | 1 + examples/helloworld/test/helloworld.sh | 37 +++++++++++++++ 15 files changed, 222 insertions(+), 21 deletions(-) create mode 100644 QHttpServerConfig-src.cmake create mode 100644 examples/CMakeLists.txt create mode 100644 examples/bodydata/CMakeLists.txt create mode 100755 examples/bodydata/test/bodydata.sh create mode 100644 examples/bodydata/test/expected.html create mode 100644 examples/greeting/CMakeLists.txt create mode 100644 examples/greeting/test/bad_user_expected.txt create mode 100644 examples/greeting/test/expected.html create mode 100755 examples/greeting/test/greeting.sh create mode 100644 examples/helloworld/CMakeLists.txt create mode 100644 examples/helloworld/test/expected.txt create mode 100755 examples/helloworld/test/helloworld.sh diff --git a/CMakeLists.txt b/CMakeLists.txt index 1aa63fd..ea5e192 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -7,7 +7,7 @@ if (NOT (${CMAKE_SYSTEM_NAME} STREQUAL "Windows")) endif() endif() -#enable_testing() +enable_testing() include(SystemSpecificInformations.cmake) @@ -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) @@ -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")) @@ -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 @@ -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") @@ -163,16 +166,20 @@ SET(CPACK_PACKAGE_VENDOR "Nikhil Marathe ") SET(CPACK_PACKAGE_CONTACT "Gaël de Chalendar ") 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) diff --git a/QHttpServerConfig-src.cmake b/QHttpServerConfig-src.cmake new file mode 100644 index 0000000..42a84ee --- /dev/null +++ b/QHttpServerConfig-src.cmake @@ -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} ") diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt new file mode 100644 index 0000000..8188137 --- /dev/null +++ b/examples/CMakeLists.txt @@ -0,0 +1,3 @@ +add_subdirectory(helloworld) +add_subdirectory(greeting) +#add_subdirectory(bodydata) diff --git a/examples/bodydata/CMakeLists.txt b/examples/bodydata/CMakeLists.txt new file mode 100644 index 0000000..66e0bfa --- /dev/null +++ b/examples/bodydata/CMakeLists.txt @@ -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} +) diff --git a/examples/bodydata/bodydata.cpp b/examples/bodydata/bodydata.cpp index 1a2c7a1..e616b12 100644 --- a/examples/bodydata/bodydata.cpp +++ b/examples/bodydata/bodydata.cpp @@ -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 @@ -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("BodyData App

Hello %1!

").arg(name); resp->write(bodyStart.toUtf8()); diff --git a/examples/bodydata/test/bodydata.sh b/examples/bodydata/test/bodydata.sh new file mode 100755 index 0000000..22bcfac --- /dev/null +++ b/examples/bodydata/test/bodydata.sh @@ -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 \ No newline at end of file diff --git a/examples/bodydata/test/expected.html b/examples/bodydata/test/expected.html new file mode 100644 index 0000000..4561256 --- /dev/null +++ b/examples/bodydata/test/expected.html @@ -0,0 +1 @@ +BodyData App

Hello abc!

\ No newline at end of file diff --git a/examples/greeting/CMakeLists.txt b/examples/greeting/CMakeLists.txt new file mode 100644 index 0000000..a3c3f7c --- /dev/null +++ b/examples/greeting/CMakeLists.txt @@ -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} +) diff --git a/examples/greeting/test/bad_user_expected.txt b/examples/greeting/test/bad_user_expected.txt new file mode 100644 index 0000000..5068264 --- /dev/null +++ b/examples/greeting/test/bad_user_expected.txt @@ -0,0 +1 @@ +You aren't allowed here! \ No newline at end of file diff --git a/examples/greeting/test/expected.html b/examples/greeting/test/expected.html new file mode 100644 index 0000000..24b850a --- /dev/null +++ b/examples/greeting/test/expected.html @@ -0,0 +1 @@ +Greeting App

Hello abc!

\ No newline at end of file diff --git a/examples/greeting/test/greeting.sh b/examples/greeting/test/greeting.sh new file mode 100755 index 0000000..3119ba8 --- /dev/null +++ b/examples/greeting/test/greeting.sh @@ -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 \ No newline at end of file diff --git a/examples/helloworld/CMakeLists.txt b/examples/helloworld/CMakeLists.txt new file mode 100644 index 0000000..a112392 --- /dev/null +++ b/examples/helloworld/CMakeLists.txt @@ -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} +) diff --git a/examples/helloworld/helloworld.cpp b/examples/helloworld/helloworld.cpp index 428eacd..3a00ddd 100644 --- a/examples/helloworld/helloworld.cpp +++ b/examples/helloworld/helloworld.cpp @@ -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); diff --git a/examples/helloworld/test/expected.txt b/examples/helloworld/test/expected.txt new file mode 100644 index 0000000..5e1c309 --- /dev/null +++ b/examples/helloworld/test/expected.txt @@ -0,0 +1 @@ +Hello World \ No newline at end of file diff --git a/examples/helloworld/test/helloworld.sh b/examples/helloworld/test/helloworld.sh new file mode 100755 index 0000000..05ea70d --- /dev/null +++ b/examples/helloworld/test/helloworld.sh @@ -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 \ No newline at end of file