From b2546f5aa59bc9138098ab49fe9f45e5ce2873b2 Mon Sep 17 00:00:00 2001 From: Dirk Thomas Date: Wed, 22 Aug 2018 15:08:59 -0700 Subject: [PATCH] reenable tests --- CMakeLists.txt | 5 +--- package.xml | 8 ++---- test/CMakeLists.txt | 26 +++++++++++------ test/simple_http_requests_test.py | 35 +++++++++++----------- test/test_web_server.cpp | 8 +++--- test/tests.py.in | 48 +++++++++++++++++++++++++++++++ test/tests.test | 5 ---- test/websocket_test.py | 23 ++++++++------- 8 files changed, 101 insertions(+), 57 deletions(-) create mode 100755 test/tests.py.in delete mode 100644 test/tests.test diff --git a/CMakeLists.txt b/CMakeLists.txt index b5f95e7..aa182f6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -62,8 +62,5 @@ install(DIRECTORY include/${PROJECT_NAME}/ if(BUILD_TESTING) - # TODO reenable tests - # find_package(catkin REQUIRED rospy roslib) - # find_package(rostest) - # add_subdirectory(test) + add_subdirectory(test) endif() diff --git a/package.xml b/package.xml index 008d83b..e1fabd3 100644 --- a/package.xml +++ b/package.xml @@ -20,10 +20,6 @@ libssl-dev boost - + launch_testing + python3-websocket diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index f42bd54..f8b2fe2 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -1,10 +1,18 @@ -add_executable(test_web_server EXCLUDE_FROM_ALL test_web_server.cpp) -target_link_libraries(test_web_server ${PROJECT_NAME} ${catkin_LIBRARIES}) +add_executable(test_web_server test_web_server.cpp) +set_target_properties(test_web_server PROPERTIES + COMPILE_DEFINITIONS "ASYNC_WEB_SERVER_CPP_TEST_DIR=\"${CMAKE_CURRENT_SOURCE_DIR}\"") +target_link_libraries(test_web_server ${PROJECT_NAME}) -if(TARGET tests) - add_dependencies(tests - test_web_server - ) -endif() - -add_rostest(tests.test) +set(generated_test_file "${CMAKE_CURRENT_BINARY_DIR}/tests") +set(TEST_WEB_SERVER_EXECUTABLE $) +configure_file( + "${CMAKE_CURRENT_SOURCE_DIR}/tests.py.in" + "${generated_test_file}.py.genexp" + @ONLY) +file(GENERATE + OUTPUT "${generated_test_file}_$.py" + INPUT "${generated_test_file}.py.genexp" +) +ament_add_test(tests + COMMAND "${generated_test_file}_$.py" + GENERATE_RESULT_FOR_RETURN_CODE_ZERO) diff --git a/test/simple_http_requests_test.py b/test/simple_http_requests_test.py index 4a5e5f3..d59c191 100755 --- a/test/simple_http_requests_test.py +++ b/test/simple_http_requests_test.py @@ -1,9 +1,9 @@ -#! /usr/bin/env python +#!/usr/bin/env python3 -import httplib -import rospy -import unittest +import http.client as httplib import time +import unittest + class TestSimpleHttpRequests(unittest.TestCase): def setUp(self): @@ -44,49 +44,49 @@ def test_default_action(self): response = self.conn.getresponse() self.assertEqual(404, response.status) - def test_default_action(self): + def test_default_action2(self): self.conn.request("GET", "/a_static_response") response = self.conn.getresponse() self.assertEqual(200, response.status) - self.assertEqual("A RESPONSE", response.read()) + self.assertEqual(b"A RESPONSE", response.read()) def test_http_echo1(self): - test_content = "hello HELLO"*1000 # make sure to exceed MTU + test_content = "hello HELLO" * 1000 # make sure to exceed MTU self.conn.request("GET", "/http_body_echo", test_content) response = self.conn.getresponse() self.assertEqual(200, response.status) - self.assertEqual(test_content, response.read()) + self.assertEqual(test_content.encode(), response.read()) def test_http_echo2(self): - test_content = "THIS is A test"*1000 # make sure to exceed MTU + test_content = "THIS is A test" * 1000 # make sure to exceed MTU self.conn.request("POST", "/http_body_echo", test_content) response = self.conn.getresponse() self.assertEqual(200, response.status) - self.assertEqual(test_content, response.read()) + self.assertEqual(test_content.encode(), response.read()) def test_http_path_echo(self): self.conn.request("GET", "/http_path_echo/this_is_a_test") response = self.conn.getresponse() self.assertEqual(200, response.status) - self.assertEqual("/http_path_echo/this_is_a_test", response.read()) + self.assertEqual(b"/http_path_echo/this_is_a_test", response.read()) def test_http_query_echo(self): self.conn.request("GET", "/http_query_echo?hello=1&b=test&c=10") response = self.conn.getresponse() self.assertEqual(200, response.status) - self.assertEqual("b=test\nc=10\nhello=1\n", response.read()) + self.assertEqual(b"b=test\nc=10\nhello=1\n", response.read()) def test_file(self): self.conn.request("GET", "/test_file") response = self.conn.getresponse() self.assertEqual(200, response.status) - self.assertEqual("\n", response.read()) + self.assertEqual(b"\n", response.read()) def test_file_from_filesystem(self): self.conn.request("GET", "/test_files/test_dir/test_file.txt") response = self.conn.getresponse() self.assertEqual(200, response.status) - self.assertEqual("test\n", response.read()) + self.assertEqual(b"test\n", response.read()) def test_directory_listing_forbidden_from_filesystem1(self): self.conn.request("GET", "/test_files/test_dir/") @@ -103,9 +103,8 @@ def test_directory_listing_from_filesystem(self): response = self.conn.getresponse() self.assertEqual(200, response.status) + if __name__ == '__main__': - time.sleep(1) # ensure server is up + time.sleep(1) # ensure server is up - import rostest - rospy.init_node('simple_http_requests_test') - rostest.rosrun('async_web_server_cpp', 'simple_http_requests', TestSimpleHttpRequests) + unittest.main() diff --git a/test/test_web_server.cpp b/test/test_web_server.cpp index d1e8487..1cd5236 100644 --- a/test/test_web_server.cpp +++ b/test/test_web_server.cpp @@ -3,7 +3,6 @@ #include "async_web_server_cpp/websocket_connection.hpp" #include "async_web_server_cpp/websocket_request_handler.hpp" #include -#include using namespace async_web_server_cpp; @@ -79,14 +78,15 @@ int main(int argc, char **argv) handler_group.addHandlerForPath("/websocket_echo", WebsocketHttpRequestHandler(websocket_echo)); + auto test_dir = std::string(ASYNC_WEB_SERVER_CPP_TEST_DIR); handler_group.addHandlerForPath("/test_files/.+", HttpReply::from_filesystem(HttpReply::ok, - "/test_files/", ros::package::getPath("async_web_server_cpp") + "/test", + "/test_files/", test_dir, false)); handler_group.addHandlerForPath("/test_files_with_dir/.+", HttpReply::from_filesystem(HttpReply::ok, - "/test_files_with_dir/", ros::package::getPath("async_web_server_cpp") + "/test", + "/test_files_with_dir/", test_dir, true)); handler_group.addHandlerForPath("/test_file", HttpReply::from_file(HttpReply::ok, "text/html", - ros::package::getPath("async_web_server_cpp") + "/test/test.html")); + test_dir + "/test.html")); HttpServer server("0.0.0.0", "9849", handler_group, 1); diff --git a/test/tests.py.in b/test/tests.py.in new file mode 100755 index 0000000..4474938 --- /dev/null +++ b/test/tests.py.in @@ -0,0 +1,48 @@ +#!/usr/bin/env python3 + +import os +import sys + +from launch import LaunchDescription +from launch import LaunchService +from launch.actions import ExecuteProcess + +from launch_testing import LaunchTestService + + +def main(argv=sys.argv[1:]): + ld = LaunchDescription() + + web_server_action = ExecuteProcess( + cmd=['@TEST_WEB_SERVER_EXECUTABLE@']) + ld.add_action(web_server_action) + + test_directory = '@CMAKE_CURRENT_SOURCE_DIR@' + + test1_action = ExecuteProcess( + cmd=[ + sys.executable, '-u', + os.path.join(test_directory, 'simple_http_requests_test.py')], + name='simple_http_requests_test', + ) + ld.add_action(test1_action) + + test2_action = ExecuteProcess( + cmd=[ + sys.executable, '-u', + os.path.join(test_directory, 'websocket_test.py')], + name='websocket_test', + ) + ld.add_action(test2_action) + + lts = LaunchTestService() + lts.add_test_action(ld, test1_action) + lts.add_test_action(ld, test2_action) + + ls = LaunchService(argv=argv) + ls.include_launch_description(ld) + return lts.run(ls) + + +if __name__ == '__main__': + sys.exit(main()) diff --git a/test/tests.test b/test/tests.test deleted file mode 100644 index 143fb8b..0000000 --- a/test/tests.test +++ /dev/null @@ -1,5 +0,0 @@ - - - - - diff --git a/test/websocket_test.py b/test/websocket_test.py index 67516d9..bfd5872 100755 --- a/test/websocket_test.py +++ b/test/websocket_test.py @@ -1,13 +1,15 @@ -#! /usr/bin/env python +#!/usr/bin/env python3 -import websocket -import rospy -import unittest import time +import unittest + +import websocket + class TestWebsocket(unittest.TestCase): def setUp(self): - self.ws = websocket.create_connection("ws://localhost:9849/websocket_echo") + self.ws = websocket.create_connection( + "ws://localhost:9849/websocket_echo") def tearDown(self): self.ws.close() @@ -23,16 +25,15 @@ def test_ok(self): self.ws.ping("test ping") ping_echo = self.ws.recv_frame() self.assertEqual(9, ping_echo.opcode) - self.assertEqual("test ping", ping_echo.data) + self.assertEqual(b"test ping", ping_echo.data) self.ws.pong("test pong") pong_echo = self.ws.recv_frame() self.assertEqual(10, pong_echo.opcode) - self.assertEqual("test pong", pong_echo.data) + self.assertEqual(b"test pong", pong_echo.data) + if __name__ == '__main__': - time.sleep(1) # ensure server is up + time.sleep(1) # ensure server is up - import rostest - rospy.init_node('websocket_test') - rostest.rosrun('async_web_server_cpp', 'websocket', TestWebsocket) + unittest.main()