Skip to content

Commit

Permalink
reenable tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dirk-thomas committed Aug 24, 2018
1 parent 42996a9 commit b2546f5
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 57 deletions.
5 changes: 1 addition & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
8 changes: 2 additions & 6 deletions package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,6 @@
<exec_depend>libssl-dev</exec_depend>
<exec_depend>boost</exec_depend>

<!--
<test_depend>rostest</test_depend>
<test_depend>rospy</test_depend>
<test_depend>roslib</test_depend>
<test_depend>python-websocket</test_depend>
-->
<test_depend>launch_testing</test_depend>
<test_depend>python3-websocket</test_depend>
</package>
26 changes: 17 additions & 9 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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 $<TARGET_FILE:test_web_server>)
configure_file(
"${CMAKE_CURRENT_SOURCE_DIR}/tests.py.in"
"${generated_test_file}.py.genexp"
@ONLY)
file(GENERATE
OUTPUT "${generated_test_file}_$<CONFIG>.py"
INPUT "${generated_test_file}.py.genexp"
)
ament_add_test(tests
COMMAND "${generated_test_file}_$<CONFIG>.py"
GENERATE_RESULT_FOR_RETURN_CODE_ZERO)
35 changes: 17 additions & 18 deletions test/simple_http_requests_test.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down Expand Up @@ -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("<html></html>\n", response.read())
self.assertEqual(b"<html></html>\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/")
Expand All @@ -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()
8 changes: 4 additions & 4 deletions test/test_web_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
#include "async_web_server_cpp/websocket_connection.hpp"
#include "async_web_server_cpp/websocket_request_handler.hpp"
#include <signal.h>
#include <ros/package.h>

using namespace async_web_server_cpp;

Expand Down Expand Up @@ -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);

Expand Down
48 changes: 48 additions & 0 deletions test/tests.py.in
Original file line number Diff line number Diff line change
@@ -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())
5 changes: 0 additions & 5 deletions test/tests.test

This file was deleted.

23 changes: 12 additions & 11 deletions test/websocket_test.py
Original file line number Diff line number Diff line change
@@ -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()
Expand All @@ -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()

0 comments on commit b2546f5

Please sign in to comment.