diff --git a/.gitignore b/.gitignore index e9ac6b0..18ef062 100644 --- a/.gitignore +++ b/.gitignore @@ -13,4 +13,5 @@ compile_commands.json # executables http_examples https_examples -parse_test \ No newline at end of file +io_test +parse_test diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 2bf0b85..b86e421 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -1,11 +1,17 @@ set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-access-control") +add_executable(io_test io_test.cpp) +target_link_libraries(io_test ${Boost_LIBRARIES}) +target_link_libraries(io_test ${CMAKE_THREAD_LIBS_INIT}) + add_executable(parse_test parse_test.cpp) target_link_libraries(parse_test ${Boost_LIBRARIES}) target_link_libraries(parse_test ${CMAKE_THREAD_LIBS_INIT}) if(MSYS) #TODO: Is MSYS true when MSVC is true? + target_link_libraries(io_test ws2_32 wsock32) target_link_libraries(parse_test ws2_32 wsock32) endif() +add_test(io_test io_test) add_test(parse_test parse_test) diff --git a/test/io_test.cpp b/test/io_test.cpp new file mode 100644 index 0000000..e8b1c5d --- /dev/null +++ b/test/io_test.cpp @@ -0,0 +1,68 @@ +#include "server_http.hpp" +#include "client_http.hpp" + +#include + +using namespace std; + +typedef SimpleWeb::Server HttpServer; +typedef SimpleWeb::Client HttpClient; + +int main() { + HttpServer server(8080, 1); + + server.resource["^/string$"]["POST"]=[](shared_ptr response, shared_ptr request) { + auto content=request->content.string(); + + *response << "HTTP/1.1 200 OK\r\nContent-Length: " << content.length() << "\r\n\r\n" << content; + }; + + server.resource["^/info$"]["GET"]=[](shared_ptr response, shared_ptr request) { + stringstream content_stream; + content_stream << request->method << " " << request->path << " " << request->http_version << " "; + content_stream << request->header.find("test parameter")->second; + + content_stream.seekp(0, ios::end); + + *response << "HTTP/1.1 200 OK\r\nContent-Length: " << content_stream.tellp() << "\r\n\r\n" << content_stream.rdbuf(); + }; + + server.resource["^/match/([0-9]+)$"]["GET"]=[&server](shared_ptr response, shared_ptr request) { + string number=request->path_match[1]; + *response << "HTTP/1.1 200 OK\r\nContent-Length: " << number.length() << "\r\n\r\n" << number; + }; + + thread server_thread([&server](){ + //Start server + server.start(); + }); + + this_thread::sleep_for(chrono::seconds(1)); + HttpClient client("localhost:8080"); + + { + stringstream output; + auto r=client.request("POST", "/string", "A string"); + output << r->content.rdbuf(); + assert(output.str()=="A string"); + } + + { + stringstream output; + auto r=client.request("GET", "/info", "", {{"Test Parameter", "test value"}}); + output << r->content.rdbuf(); + assert(output.str()=="GET /info 1.1 test value"); + } + + { + stringstream output; + auto r=client.request("GET", "/match/123"); + output << r->content.rdbuf(); + assert(output.str()=="123"); + } + + server.stop(); + server_thread.join(); + + return 0; +}