Added io_test
This commit is contained in:
parent
d149601bad
commit
a7c6d6fdb6
3 changed files with 76 additions and 1 deletions
3
.gitignore
vendored
3
.gitignore
vendored
|
|
@ -13,4 +13,5 @@ compile_commands.json
|
|||
# executables
|
||||
http_examples
|
||||
https_examples
|
||||
parse_test
|
||||
io_test
|
||||
parse_test
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
68
test/io_test.cpp
Normal file
68
test/io_test.cpp
Normal file
|
|
@ -0,0 +1,68 @@
|
|||
#include "server_http.hpp"
|
||||
#include "client_http.hpp"
|
||||
|
||||
#include <cassert>
|
||||
|
||||
using namespace std;
|
||||
|
||||
typedef SimpleWeb::Server<SimpleWeb::HTTP> HttpServer;
|
||||
typedef SimpleWeb::Client<SimpleWeb::HTTP> HttpClient;
|
||||
|
||||
int main() {
|
||||
HttpServer server(8080, 1);
|
||||
|
||||
server.resource["^/string$"]["POST"]=[](shared_ptr<HttpServer::Response> response, shared_ptr<HttpServer::Request> 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<HttpServer::Response> response, shared_ptr<HttpServer::Request> 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<HttpServer::Response> response, shared_ptr<HttpServer::Request> 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;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue