Added .clang-format file and applied style to source files
This commit is contained in:
parent
3ee9f8dc52
commit
e50d2fc63a
13 changed files with 2552 additions and 2538 deletions
|
|
@ -1,77 +1,72 @@
|
|||
#include <vector>
|
||||
#include <cassert>
|
||||
#include <vector>
|
||||
|
||||
#include "crypto.hpp"
|
||||
|
||||
using namespace std;
|
||||
using namespace SimpleWeb;
|
||||
|
||||
const vector<pair<string, string> > base64_string_tests = {
|
||||
const vector<pair<string, string>> base64_string_tests = {
|
||||
{"", ""},
|
||||
{"f" , "Zg=="},
|
||||
{"f", "Zg=="},
|
||||
{"fo", "Zm8="},
|
||||
{"foo", "Zm9v"},
|
||||
{"foob", "Zm9vYg=="},
|
||||
{"fooba", "Zm9vYmE="},
|
||||
{"foobar", "Zm9vYmFy"}
|
||||
};
|
||||
{"foobar", "Zm9vYmFy"}};
|
||||
|
||||
const vector<pair<string, string> > md5_string_tests = {
|
||||
const vector<pair<string, string>> md5_string_tests = {
|
||||
{"", "d41d8cd98f00b204e9800998ecf8427e"},
|
||||
{"The quick brown fox jumps over the lazy dog", "9e107d9d372bb6826bd81d3542a419d6"}
|
||||
};
|
||||
{"The quick brown fox jumps over the lazy dog", "9e107d9d372bb6826bd81d3542a419d6"}};
|
||||
|
||||
const vector<pair<string, string> > sha1_string_tests = {
|
||||
const vector<pair<string, string>> sha1_string_tests = {
|
||||
{"", "da39a3ee5e6b4b0d3255bfef95601890afd80709"},
|
||||
{"The quick brown fox jumps over the lazy dog", "2fd4e1c67a2d28fced849ee1bb76e7391b93eb12"}
|
||||
};
|
||||
{"The quick brown fox jumps over the lazy dog", "2fd4e1c67a2d28fced849ee1bb76e7391b93eb12"}};
|
||||
|
||||
const vector<pair<string, string> > sha256_string_tests = {
|
||||
const vector<pair<string, string>> sha256_string_tests = {
|
||||
{"", "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"},
|
||||
{"The quick brown fox jumps over the lazy dog", "d7a8fbb307d7809469ca9abcb0082e4f8d5651e46d3cdb762d02d0bf37c9e592"}
|
||||
};
|
||||
{"The quick brown fox jumps over the lazy dog", "d7a8fbb307d7809469ca9abcb0082e4f8d5651e46d3cdb762d02d0bf37c9e592"}};
|
||||
|
||||
const vector<pair<string, string> > sha512_string_tests = {
|
||||
const vector<pair<string, string>> sha512_string_tests = {
|
||||
{"", "cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e"},
|
||||
{"The quick brown fox jumps over the lazy dog", "07e547d9586f6a73f73fbac0435ed76951218fb7d0c8d788a309d785436bbb642e93a252a954f23912547d1e8a3b5ed6e1bfd7097821233fa0538f3db854fee6"}
|
||||
};
|
||||
{"The quick brown fox jumps over the lazy dog", "07e547d9586f6a73f73fbac0435ed76951218fb7d0c8d788a309d785436bbb642e93a252a954f23912547d1e8a3b5ed6e1bfd7097821233fa0538f3db854fee6"}};
|
||||
|
||||
int main() {
|
||||
for(auto& string_test: base64_string_tests) {
|
||||
assert(Crypto::Base64::encode(string_test.first)==string_test.second);
|
||||
assert(Crypto::Base64::decode(string_test.second)==string_test.first);
|
||||
}
|
||||
|
||||
for(auto& string_test: md5_string_tests) {
|
||||
assert(Crypto::to_hex_string(Crypto::md5(string_test.first)) == string_test.second);
|
||||
stringstream ss(string_test.first);
|
||||
assert(Crypto::to_hex_string(Crypto::md5(ss)) == string_test.second);
|
||||
}
|
||||
|
||||
for(auto& string_test: sha1_string_tests) {
|
||||
assert(Crypto::to_hex_string(Crypto::sha1(string_test.first)) == string_test.second);
|
||||
stringstream ss(string_test.first);
|
||||
assert(Crypto::to_hex_string(Crypto::sha1(ss)) == string_test.second);
|
||||
}
|
||||
|
||||
for(auto& string_test: sha256_string_tests) {
|
||||
assert(Crypto::to_hex_string(Crypto::sha256(string_test.first)) == string_test.second);
|
||||
stringstream ss(string_test.first);
|
||||
assert(Crypto::to_hex_string(Crypto::sha256(ss)) == string_test.second);
|
||||
}
|
||||
|
||||
for(auto& string_test: sha512_string_tests) {
|
||||
assert(Crypto::to_hex_string(Crypto::sha512(string_test.first)) == string_test.second);
|
||||
stringstream ss(string_test.first);
|
||||
assert(Crypto::to_hex_string(Crypto::sha512(ss)) == string_test.second);
|
||||
}
|
||||
|
||||
//Testing iterations
|
||||
assert(Crypto::to_hex_string(Crypto::sha1("Test", 1)) == "640ab2bae07bedc4c163f679a746f7ab7fb5d1fa");
|
||||
assert(Crypto::to_hex_string(Crypto::sha1("Test", 2)) == "af31c6cbdecd88726d0a9b3798c71ef41f1624d5");
|
||||
stringstream ss("Test");
|
||||
assert(Crypto::to_hex_string(Crypto::sha1(ss, 2)) == "af31c6cbdecd88726d0a9b3798c71ef41f1624d5");
|
||||
|
||||
assert(Crypto::to_hex_string(Crypto::pbkdf2("Password", "Salt", 4096, 128 / 8)) == "f66df50f8aaa11e4d9721e1312ff2e66");
|
||||
assert(Crypto::to_hex_string(Crypto::pbkdf2("Password", "Salt", 8192, 512 / 8)) == "a941ccbc34d1ee8ebbd1d34824a419c3dc4eac9cbc7c36ae6c7ca8725e2b618a6ad22241e787af937b0960cf85aa8ea3a258f243e05d3cc9b08af5dd93be046c");
|
||||
for(auto &string_test : base64_string_tests) {
|
||||
assert(Crypto::Base64::encode(string_test.first) == string_test.second);
|
||||
assert(Crypto::Base64::decode(string_test.second) == string_test.first);
|
||||
}
|
||||
|
||||
for(auto &string_test : md5_string_tests) {
|
||||
assert(Crypto::to_hex_string(Crypto::md5(string_test.first)) == string_test.second);
|
||||
stringstream ss(string_test.first);
|
||||
assert(Crypto::to_hex_string(Crypto::md5(ss)) == string_test.second);
|
||||
}
|
||||
|
||||
for(auto &string_test : sha1_string_tests) {
|
||||
assert(Crypto::to_hex_string(Crypto::sha1(string_test.first)) == string_test.second);
|
||||
stringstream ss(string_test.first);
|
||||
assert(Crypto::to_hex_string(Crypto::sha1(ss)) == string_test.second);
|
||||
}
|
||||
|
||||
for(auto &string_test : sha256_string_tests) {
|
||||
assert(Crypto::to_hex_string(Crypto::sha256(string_test.first)) == string_test.second);
|
||||
stringstream ss(string_test.first);
|
||||
assert(Crypto::to_hex_string(Crypto::sha256(ss)) == string_test.second);
|
||||
}
|
||||
|
||||
for(auto &string_test : sha512_string_tests) {
|
||||
assert(Crypto::to_hex_string(Crypto::sha512(string_test.first)) == string_test.second);
|
||||
stringstream ss(string_test.first);
|
||||
assert(Crypto::to_hex_string(Crypto::sha512(ss)) == string_test.second);
|
||||
}
|
||||
|
||||
//Testing iterations
|
||||
assert(Crypto::to_hex_string(Crypto::sha1("Test", 1)) == "640ab2bae07bedc4c163f679a746f7ab7fb5d1fa");
|
||||
assert(Crypto::to_hex_string(Crypto::sha1("Test", 2)) == "af31c6cbdecd88726d0a9b3798c71ef41f1624d5");
|
||||
stringstream ss("Test");
|
||||
assert(Crypto::to_hex_string(Crypto::sha1(ss, 2)) == "af31c6cbdecd88726d0a9b3798c71ef41f1624d5");
|
||||
|
||||
assert(Crypto::to_hex_string(Crypto::pbkdf2("Password", "Salt", 4096, 128 / 8)) == "f66df50f8aaa11e4d9721e1312ff2e66");
|
||||
assert(Crypto::to_hex_string(Crypto::pbkdf2("Password", "Salt", 8192, 512 / 8)) == "a941ccbc34d1ee8ebbd1d34824a419c3dc4eac9cbc7c36ae6c7ca8725e2b618a6ad22241e787af937b0960cf85aa8ea3a258f243e05d3cc9b08af5dd93be046c");
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
#include "server_http.hpp"
|
||||
#include "client_http.hpp"
|
||||
#include "server_http.hpp"
|
||||
|
||||
#include <cassert>
|
||||
|
||||
|
|
@ -9,235 +9,239 @@ typedef SimpleWeb::Server<SimpleWeb::HTTP> HttpServer;
|
|||
typedef SimpleWeb::Client<SimpleWeb::HTTP> HttpClient;
|
||||
|
||||
int main() {
|
||||
HttpServer server;
|
||||
server.config.port=8080;
|
||||
|
||||
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["^/string2$"]["POST"]=[](shared_ptr<HttpServer::Response> response, shared_ptr<HttpServer::Request> request) {
|
||||
response->write(request->content.string());
|
||||
};
|
||||
|
||||
server.resource["^/string3$"]["POST"]=[](shared_ptr<HttpServer::Response> response, shared_ptr<HttpServer::Request> request) {
|
||||
std::stringstream stream;
|
||||
stream << request->content.rdbuf();
|
||||
response->write(stream);
|
||||
};
|
||||
|
||||
server.resource["^/string4$"]["POST"]=[](shared_ptr<HttpServer::Response> response, shared_ptr<HttpServer::Request> /*request*/) {
|
||||
response->write(SimpleWeb::StatusCode::client_error_forbidden, {{"Test1", "test2"}, {"tesT3", "test4"}});
|
||||
};
|
||||
|
||||
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;
|
||||
HttpServer server;
|
||||
server.config.port = 8080;
|
||||
|
||||
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;
|
||||
};
|
||||
|
||||
server.resource["^/header$"]["GET"]=[](shared_ptr<HttpServer::Response> response, shared_ptr<HttpServer::Request> request) {
|
||||
auto content=request->header.find("test1")->second+request->header.find("test2")->second;
|
||||
|
||||
*response << "HTTP/1.1 200 OK\r\nContent-Length: " << content.length() << "\r\n\r\n" << content;
|
||||
};
|
||||
|
||||
thread server_thread([&server](){
|
||||
//Start server
|
||||
server.start();
|
||||
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["^/string2$"]["POST"] = [](shared_ptr<HttpServer::Response> response, shared_ptr<HttpServer::Request> request) {
|
||||
response->write(request->content.string());
|
||||
};
|
||||
|
||||
server.resource["^/string3$"]["POST"] = [](shared_ptr<HttpServer::Response> response, shared_ptr<HttpServer::Request> request) {
|
||||
std::stringstream stream;
|
||||
stream << request->content.rdbuf();
|
||||
response->write(stream);
|
||||
};
|
||||
|
||||
server.resource["^/string4$"]["POST"] = [](shared_ptr<HttpServer::Response> response, shared_ptr<HttpServer::Request> /*request*/) {
|
||||
response->write(SimpleWeb::StatusCode::client_error_forbidden, {{"Test1", "test2"}, {"tesT3", "test4"}});
|
||||
};
|
||||
|
||||
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;
|
||||
};
|
||||
|
||||
server.resource["^/header$"]["GET"] = [](shared_ptr<HttpServer::Response> response, shared_ptr<HttpServer::Request> request) {
|
||||
auto content = request->header.find("test1")->second + request->header.find("test2")->second;
|
||||
|
||||
*response << "HTTP/1.1 200 OK\r\nContent-Length: " << content.length() << "\r\n\r\n"
|
||||
<< content;
|
||||
};
|
||||
|
||||
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");
|
||||
assert(SimpleWeb::status_code(r->status_code) == SimpleWeb::StatusCode::success_ok);
|
||||
output << r->content.rdbuf();
|
||||
assert(output.str() == "A string");
|
||||
}
|
||||
|
||||
{
|
||||
stringstream output;
|
||||
auto r = client.request("POST", "/string", "A string");
|
||||
assert(SimpleWeb::status_code(r->status_code) == SimpleWeb::StatusCode::success_ok);
|
||||
assert(r->content.string() == "A string");
|
||||
}
|
||||
|
||||
{
|
||||
stringstream output;
|
||||
auto r = client.request("POST", "/string2", "A string");
|
||||
assert(SimpleWeb::status_code(r->status_code) == SimpleWeb::StatusCode::success_ok);
|
||||
output << r->content.rdbuf();
|
||||
assert(output.str() == "A string");
|
||||
}
|
||||
|
||||
{
|
||||
stringstream output;
|
||||
auto r = client.request("POST", "/string3", "A string");
|
||||
assert(SimpleWeb::status_code(r->status_code) == SimpleWeb::StatusCode::success_ok);
|
||||
output << r->content.rdbuf();
|
||||
assert(output.str() == "A string");
|
||||
}
|
||||
|
||||
{
|
||||
stringstream output;
|
||||
auto r = client.request("POST", "/string4", "A string");
|
||||
assert(SimpleWeb::status_code(r->status_code) == SimpleWeb::StatusCode::client_error_forbidden);
|
||||
assert(r->header.size() == 3);
|
||||
assert(r->header.find("test1")->second == "test2");
|
||||
assert(r->header.find("tEst3")->second == "test4");
|
||||
assert(r->header.find("content-length")->second == "0");
|
||||
output << r->content.rdbuf();
|
||||
assert(output.str() == "");
|
||||
}
|
||||
|
||||
{
|
||||
stringstream output;
|
||||
stringstream content("A string");
|
||||
auto r = client.request("POST", "/string", content);
|
||||
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");
|
||||
}
|
||||
}
|
||||
{
|
||||
HttpClient client("localhost:8080");
|
||||
|
||||
HttpClient::Connection *connection;
|
||||
{
|
||||
// test performing the stream version of the request methods first
|
||||
stringstream output;
|
||||
stringstream content("A string");
|
||||
auto r = client.request("POST", "/string", content);
|
||||
output << r->content.rdbuf();
|
||||
assert(output.str() == "A string");
|
||||
assert(client.connections->size() == 1);
|
||||
connection = client.connections->front().get();
|
||||
}
|
||||
|
||||
{
|
||||
stringstream output;
|
||||
auto r = client.request("POST", "/string", "A string");
|
||||
output << r->content.rdbuf();
|
||||
assert(output.str() == "A string");
|
||||
assert(client.connections->size() == 1);
|
||||
assert(connection == client.connections->front().get());
|
||||
}
|
||||
|
||||
{
|
||||
stringstream output;
|
||||
auto r = client.request("GET", "/header", "", {{"test1", "test"}, {"test2", "ing"}});
|
||||
output << r->content.rdbuf();
|
||||
assert(output.str() == "testing");
|
||||
assert(client.connections->size() == 1);
|
||||
assert(connection == client.connections->front().get());
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
HttpClient client("localhost:8080");
|
||||
bool call = false;
|
||||
client.request("GET", "/match/123", [&call](shared_ptr<HttpClient::Response> response, const SimpleWeb::error_code &ec) {
|
||||
assert(!ec);
|
||||
stringstream output;
|
||||
output << response->content.rdbuf();
|
||||
assert(output.str() == "123");
|
||||
call = true;
|
||||
});
|
||||
|
||||
this_thread::sleep_for(chrono::seconds(1));
|
||||
client.io_service->run();
|
||||
assert(call);
|
||||
|
||||
{
|
||||
HttpClient client("localhost:8080");
|
||||
|
||||
{
|
||||
stringstream output;
|
||||
auto r=client.request("POST", "/string", "A string");
|
||||
assert(SimpleWeb::status_code(r->status_code)==SimpleWeb::StatusCode::success_ok);
|
||||
output << r->content.rdbuf();
|
||||
assert(output.str()=="A string");
|
||||
}
|
||||
|
||||
{
|
||||
stringstream output;
|
||||
auto r=client.request("POST", "/string", "A string");
|
||||
assert(SimpleWeb::status_code(r->status_code)==SimpleWeb::StatusCode::success_ok);
|
||||
assert(r->content.string()=="A string");
|
||||
}
|
||||
|
||||
{
|
||||
stringstream output;
|
||||
auto r=client.request("POST", "/string2", "A string");
|
||||
assert(SimpleWeb::status_code(r->status_code)==SimpleWeb::StatusCode::success_ok);
|
||||
output << r->content.rdbuf();
|
||||
assert(output.str()=="A string");
|
||||
}
|
||||
|
||||
{
|
||||
stringstream output;
|
||||
auto r=client.request("POST", "/string3", "A string");
|
||||
assert(SimpleWeb::status_code(r->status_code)==SimpleWeb::StatusCode::success_ok);
|
||||
output << r->content.rdbuf();
|
||||
assert(output.str()=="A string");
|
||||
}
|
||||
|
||||
{
|
||||
stringstream output;
|
||||
auto r=client.request("POST", "/string4", "A string");
|
||||
assert(SimpleWeb::status_code(r->status_code)==SimpleWeb::StatusCode::client_error_forbidden);
|
||||
assert(r->header.size()==3);
|
||||
assert(r->header.find("test1")->second=="test2");
|
||||
assert(r->header.find("tEst3")->second=="test4");
|
||||
assert(r->header.find("content-length")->second=="0");
|
||||
output << r->content.rdbuf();
|
||||
assert(output.str()=="");
|
||||
}
|
||||
|
||||
{
|
||||
stringstream output;
|
||||
stringstream content("A string");
|
||||
auto r=client.request("POST", "/string", content);
|
||||
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");
|
||||
}
|
||||
}
|
||||
{
|
||||
HttpClient client("localhost:8080");
|
||||
|
||||
HttpClient::Connection *connection;
|
||||
{
|
||||
// test performing the stream version of the request methods first
|
||||
stringstream output;
|
||||
stringstream content("A string");
|
||||
auto r=client.request("POST", "/string", content);
|
||||
output << r->content.rdbuf();
|
||||
assert(output.str()=="A string");
|
||||
assert(client.connections->size()==1);
|
||||
connection=client.connections->front().get();
|
||||
}
|
||||
|
||||
{
|
||||
stringstream output;
|
||||
auto r=client.request("POST", "/string", "A string");
|
||||
output << r->content.rdbuf();
|
||||
assert(output.str()=="A string");
|
||||
assert(client.connections->size()==1);
|
||||
assert(connection==client.connections->front().get());
|
||||
}
|
||||
|
||||
{
|
||||
stringstream output;
|
||||
auto r=client.request("GET", "/header", "", {{"test1", "test"}, {"test2", "ing"}});
|
||||
output << r->content.rdbuf();
|
||||
assert(output.str()=="testing");
|
||||
assert(client.connections->size()==1);
|
||||
assert(connection==client.connections->front().get());
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
HttpClient client("localhost:8080");
|
||||
bool call=false;
|
||||
client.request("GET", "/match/123", [&call](shared_ptr<HttpClient::Response> response, const SimpleWeb::error_code &ec) {
|
||||
vector<int> calls(100);
|
||||
vector<thread> threads;
|
||||
for(size_t c = 0; c < 100; ++c) {
|
||||
calls[c] = 0;
|
||||
threads.emplace_back([c, &client, &calls] {
|
||||
client.request("GET", "/match/123", [c, &calls](shared_ptr<HttpClient::Response> response, const SimpleWeb::error_code &ec) {
|
||||
assert(!ec);
|
||||
stringstream output;
|
||||
output << response->content.rdbuf();
|
||||
assert(output.str()=="123");
|
||||
call=true;
|
||||
assert(output.str() == "123");
|
||||
calls[c] = 1;
|
||||
});
|
||||
});
|
||||
client.io_service->run();
|
||||
}
|
||||
for(auto &thread : threads)
|
||||
thread.join();
|
||||
assert(client.connections->size() == 100);
|
||||
client.io_service->reset();
|
||||
client.io_service->run();
|
||||
assert(client.connections->size() == 1);
|
||||
for(auto call : calls)
|
||||
assert(call);
|
||||
|
||||
{
|
||||
vector<int> calls(100);
|
||||
vector<thread> threads;
|
||||
for(size_t c=0;c<100;++c) {
|
||||
calls[c]=0;
|
||||
threads.emplace_back([c, &client, &calls] {
|
||||
client.request("GET", "/match/123", [c, &calls](shared_ptr<HttpClient::Response> response, const SimpleWeb::error_code &ec) {
|
||||
assert(!ec);
|
||||
stringstream output;
|
||||
output << response->content.rdbuf();
|
||||
assert(output.str()=="123");
|
||||
calls[c]=1;
|
||||
});
|
||||
});
|
||||
}
|
||||
for(auto &thread: threads)
|
||||
thread.join();
|
||||
assert(client.connections->size()==100);
|
||||
client.io_service->reset();
|
||||
client.io_service->run();
|
||||
assert(client.connections->size()==1);
|
||||
for(auto call: calls)
|
||||
assert(call);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
{
|
||||
HttpClient client("localhost:8080");
|
||||
assert(client.connections->size() == 0);
|
||||
for(size_t c = 0; c < 5000; ++c) {
|
||||
auto r1 = client.request("POST", "/string", "A string");
|
||||
assert(SimpleWeb::status_code(r1->status_code) == SimpleWeb::StatusCode::success_ok);
|
||||
assert(r1->content.string() == "A string");
|
||||
assert(client.connections->size() == 1);
|
||||
|
||||
stringstream content("A string");
|
||||
auto r2 = client.request("POST", "/string", content);
|
||||
assert(SimpleWeb::status_code(r2->status_code) == SimpleWeb::StatusCode::success_ok);
|
||||
assert(r2->content.string() == "A string");
|
||||
assert(client.connections->size() == 1);
|
||||
}
|
||||
}
|
||||
|
||||
for(size_t c = 0; c < 500; ++c) {
|
||||
{
|
||||
HttpClient client("localhost:8080");
|
||||
assert(client.connections->size()==0);
|
||||
for(size_t c=0;c<5000;++c) {
|
||||
auto r1=client.request("POST", "/string", "A string");
|
||||
assert(SimpleWeb::status_code(r1->status_code)==SimpleWeb::StatusCode::success_ok);
|
||||
assert(r1->content.string()=="A string");
|
||||
assert(client.connections->size()==1);
|
||||
|
||||
stringstream content("A string");
|
||||
auto r2 = client.request("POST", "/string", content);
|
||||
assert(SimpleWeb::status_code(r2->status_code) == SimpleWeb::StatusCode::success_ok);
|
||||
assert(r2->content.string() == "A string");
|
||||
assert(client.connections->size() == 1);
|
||||
}
|
||||
HttpClient client("localhost:8080");
|
||||
auto r = client.request("POST", "/string", "A string");
|
||||
assert(SimpleWeb::status_code(r->status_code) == SimpleWeb::StatusCode::success_ok);
|
||||
assert(r->content.string() == "A string");
|
||||
assert(client.connections->size() == 1);
|
||||
}
|
||||
|
||||
for(size_t c=0;c<500;++c) {
|
||||
{
|
||||
HttpClient client("localhost:8080");
|
||||
auto r=client.request("POST", "/string", "A string");
|
||||
assert(SimpleWeb::status_code(r->status_code)==SimpleWeb::StatusCode::success_ok);
|
||||
assert(r->content.string()=="A string");
|
||||
assert(client.connections->size()==1);
|
||||
}
|
||||
|
||||
{
|
||||
HttpClient client("localhost:8080");
|
||||
stringstream content("A string");
|
||||
auto r = client.request("POST", "/string", content);
|
||||
assert(SimpleWeb::status_code(r->status_code) == SimpleWeb::StatusCode::success_ok);
|
||||
assert(r->content.string() == "A string");
|
||||
assert(client.connections->size() == 1);
|
||||
}
|
||||
|
||||
{
|
||||
HttpClient client("localhost:8080");
|
||||
stringstream content("A string");
|
||||
auto r = client.request("POST", "/string", content);
|
||||
assert(SimpleWeb::status_code(r->status_code) == SimpleWeb::StatusCode::success_ok);
|
||||
assert(r->content.string() == "A string");
|
||||
assert(client.connections->size() == 1);
|
||||
}
|
||||
|
||||
server.stop();
|
||||
server_thread.join();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
server.stop();
|
||||
server_thread.join();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,198 +1,198 @@
|
|||
#include "server_http.hpp"
|
||||
#include "client_http.hpp"
|
||||
#include <iostream>
|
||||
#include "server_http.hpp"
|
||||
#include <cassert>
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
using namespace SimpleWeb;
|
||||
|
||||
class ServerTest : public ServerBase<HTTP> {
|
||||
public:
|
||||
ServerTest() : ServerBase<HTTP>::ServerBase(8080) {}
|
||||
|
||||
void accept() {}
|
||||
|
||||
void parse_request_test() {
|
||||
HTTP socket(*io_service);
|
||||
std::shared_ptr<Request> request(new Request(socket));
|
||||
|
||||
std::ostream stream(&request->content.streambuf);
|
||||
stream << "GET /test/ HTTP/1.1\r\n";
|
||||
stream << "TestHeader: test\r\n";
|
||||
stream << "TestHeader2:test2\r\n";
|
||||
stream << "TestHeader3:test3a\r\n";
|
||||
stream << "TestHeader3:test3b\r\n";
|
||||
stream << "\r\n";
|
||||
|
||||
assert(parse_request(request));
|
||||
|
||||
assert(request->method=="GET");
|
||||
assert(request->path=="/test/");
|
||||
assert(request->http_version=="1.1");
|
||||
|
||||
assert(request->header.size()==4);
|
||||
auto header_it=request->header.find("TestHeader");
|
||||
assert(header_it!=request->header.end() && header_it->second=="test");
|
||||
header_it=request->header.find("TestHeader2");
|
||||
assert(header_it!=request->header.end() && header_it->second=="test2");
|
||||
|
||||
header_it=request->header.find("testheader");
|
||||
assert(header_it!=request->header.end() && header_it->second=="test");
|
||||
header_it=request->header.find("testheader2");
|
||||
assert(header_it!=request->header.end() && header_it->second=="test2");
|
||||
|
||||
auto range=request->header.equal_range("testheader3");
|
||||
auto first=range.first;
|
||||
auto second=first;
|
||||
++second;
|
||||
assert(range.first!=request->header.end() && range.second!=request->header.end() &&
|
||||
((first->second=="test3a" && second->second=="test3b") ||
|
||||
(first->second=="test3b" && second->second=="test3a")));
|
||||
}
|
||||
ServerTest() : ServerBase<HTTP>::ServerBase(8080) {}
|
||||
|
||||
void accept() {}
|
||||
|
||||
void parse_request_test() {
|
||||
HTTP socket(*io_service);
|
||||
std::shared_ptr<Request> request(new Request(socket));
|
||||
|
||||
std::ostream stream(&request->content.streambuf);
|
||||
stream << "GET /test/ HTTP/1.1\r\n";
|
||||
stream << "TestHeader: test\r\n";
|
||||
stream << "TestHeader2:test2\r\n";
|
||||
stream << "TestHeader3:test3a\r\n";
|
||||
stream << "TestHeader3:test3b\r\n";
|
||||
stream << "\r\n";
|
||||
|
||||
assert(parse_request(request));
|
||||
|
||||
assert(request->method == "GET");
|
||||
assert(request->path == "/test/");
|
||||
assert(request->http_version == "1.1");
|
||||
|
||||
assert(request->header.size() == 4);
|
||||
auto header_it = request->header.find("TestHeader");
|
||||
assert(header_it != request->header.end() && header_it->second == "test");
|
||||
header_it = request->header.find("TestHeader2");
|
||||
assert(header_it != request->header.end() && header_it->second == "test2");
|
||||
|
||||
header_it = request->header.find("testheader");
|
||||
assert(header_it != request->header.end() && header_it->second == "test");
|
||||
header_it = request->header.find("testheader2");
|
||||
assert(header_it != request->header.end() && header_it->second == "test2");
|
||||
|
||||
auto range = request->header.equal_range("testheader3");
|
||||
auto first = range.first;
|
||||
auto second = first;
|
||||
++second;
|
||||
assert(range.first != request->header.end() && range.second != request->header.end() &&
|
||||
((first->second == "test3a" && second->second == "test3b") ||
|
||||
(first->second == "test3b" && second->second == "test3a")));
|
||||
}
|
||||
};
|
||||
|
||||
class ClientTest : public ClientBase<HTTP> {
|
||||
public:
|
||||
ClientTest(const std::string& server_port_path) : ClientBase<HTTP>::ClientBase(server_port_path, 80) {}
|
||||
|
||||
std::shared_ptr<Connection> create_connection() override {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
void constructor_parse_test1() {
|
||||
assert(host=="test.org");
|
||||
assert(port==8080);
|
||||
}
|
||||
|
||||
void constructor_parse_test2() {
|
||||
assert(host=="test.org");
|
||||
assert(port==80);
|
||||
}
|
||||
|
||||
void parse_response_header_test() {
|
||||
std::shared_ptr<Response> response(new Response());
|
||||
|
||||
ostream stream(&response->content_buffer);
|
||||
stream << "HTTP/1.1 200 OK\r\n";
|
||||
stream << "TestHeader: test\r\n";
|
||||
stream << "TestHeader2:test2\r\n";
|
||||
stream << "TestHeader3:test3a\r\n";
|
||||
stream << "TestHeader3:test3b\r\n";
|
||||
stream << "\r\n";
|
||||
|
||||
parse_response_header(response);
|
||||
|
||||
assert(response->http_version=="1.1");
|
||||
assert(response->status_code=="200 OK");
|
||||
|
||||
assert(response->header.size()==4);
|
||||
auto header_it=response->header.find("TestHeader");
|
||||
assert(header_it!=response->header.end() && header_it->second=="test");
|
||||
header_it=response->header.find("TestHeader2");
|
||||
assert(header_it!=response->header.end() && header_it->second=="test2");
|
||||
|
||||
header_it=response->header.find("testheader");
|
||||
assert(header_it!=response->header.end() && header_it->second=="test");
|
||||
header_it=response->header.find("testheader2");
|
||||
assert(header_it!=response->header.end() && header_it->second=="test2");
|
||||
|
||||
auto range=response->header.equal_range("testheader3");
|
||||
auto first=range.first;
|
||||
auto second=first;
|
||||
++second;
|
||||
assert(range.first!=response->header.end() && range.second!=response->header.end() &&
|
||||
((first->second=="test3a" && second->second=="test3b") ||
|
||||
(first->second=="test3b" && second->second=="test3a")));
|
||||
}
|
||||
ClientTest(const std::string &server_port_path) : ClientBase<HTTP>::ClientBase(server_port_path, 80) {}
|
||||
|
||||
std::shared_ptr<Connection> create_connection() override {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
void constructor_parse_test1() {
|
||||
assert(host == "test.org");
|
||||
assert(port == 8080);
|
||||
}
|
||||
|
||||
void constructor_parse_test2() {
|
||||
assert(host == "test.org");
|
||||
assert(port == 80);
|
||||
}
|
||||
|
||||
void parse_response_header_test() {
|
||||
std::shared_ptr<Response> response(new Response());
|
||||
|
||||
ostream stream(&response->content_buffer);
|
||||
stream << "HTTP/1.1 200 OK\r\n";
|
||||
stream << "TestHeader: test\r\n";
|
||||
stream << "TestHeader2:test2\r\n";
|
||||
stream << "TestHeader3:test3a\r\n";
|
||||
stream << "TestHeader3:test3b\r\n";
|
||||
stream << "\r\n";
|
||||
|
||||
parse_response_header(response);
|
||||
|
||||
assert(response->http_version == "1.1");
|
||||
assert(response->status_code == "200 OK");
|
||||
|
||||
assert(response->header.size() == 4);
|
||||
auto header_it = response->header.find("TestHeader");
|
||||
assert(header_it != response->header.end() && header_it->second == "test");
|
||||
header_it = response->header.find("TestHeader2");
|
||||
assert(header_it != response->header.end() && header_it->second == "test2");
|
||||
|
||||
header_it = response->header.find("testheader");
|
||||
assert(header_it != response->header.end() && header_it->second == "test");
|
||||
header_it = response->header.find("testheader2");
|
||||
assert(header_it != response->header.end() && header_it->second == "test2");
|
||||
|
||||
auto range = response->header.equal_range("testheader3");
|
||||
auto first = range.first;
|
||||
auto second = first;
|
||||
++second;
|
||||
assert(range.first != response->header.end() && range.second != response->header.end() &&
|
||||
((first->second == "test3a" && second->second == "test3b") ||
|
||||
(first->second == "test3b" && second->second == "test3a")));
|
||||
}
|
||||
};
|
||||
|
||||
int main() {
|
||||
assert(case_insensitive_equal("Test", "tesT"));
|
||||
assert(case_insensitive_equal("tesT", "test"));
|
||||
assert(!case_insensitive_equal("test", "tseT"));
|
||||
CaseInsensitiveEqual equal;
|
||||
assert(equal("Test", "tesT"));
|
||||
assert(equal("tesT", "test"));
|
||||
assert(!equal("test", "tset"));
|
||||
CaseInsensitiveHash hash;
|
||||
assert(hash("Test")==hash("tesT"));
|
||||
assert(hash("tesT")==hash("test"));
|
||||
assert(hash("test")!=hash("tset"));
|
||||
|
||||
auto percent_decoded="testing æøå !#$&'()*+,/:;=?@[]";
|
||||
auto percent_encoded="testing+æøå+%21%23%24%26%27%28%29%2A%2B%2C%2F%3A%3B%3D%3F%40%5B%5D";
|
||||
assert(Percent::encode(percent_decoded)==percent_encoded);
|
||||
assert(Percent::decode(percent_encoded)==percent_decoded);
|
||||
assert(Percent::decode(Percent::encode(percent_decoded))==percent_decoded);
|
||||
|
||||
SimpleWeb::CaseInsensitiveMultimap fields={{"test1", "æøå"}, {"test2", "!#$&'()*+,/:;=?@[]"}};
|
||||
auto query_string1="test1=æøå&test2=%21%23%24%26%27%28%29%2A%2B%2C%2F%3A%3B%3D%3F%40%5B%5D";
|
||||
auto query_string2="test2=%21%23%24%26%27%28%29%2A%2B%2C%2F%3A%3B%3D%3F%40%5B%5D&test1=æøå";
|
||||
auto query_string_result=QueryString::create(fields);
|
||||
assert(query_string_result==query_string1 || query_string_result==query_string2);
|
||||
auto fields_result1=QueryString::parse(query_string1);
|
||||
auto fields_result2=QueryString::parse(query_string2);
|
||||
assert(fields_result1==fields_result2 && fields_result1==fields);
|
||||
|
||||
ServerTest serverTest;
|
||||
serverTest.io_service=std::make_shared<asio::io_service>();
|
||||
|
||||
serverTest.parse_request_test();
|
||||
|
||||
ClientTest clientTest("test.org:8080");
|
||||
clientTest.constructor_parse_test1();
|
||||
|
||||
ClientTest clientTest2("test.org");
|
||||
clientTest2.constructor_parse_test2();
|
||||
|
||||
clientTest2.parse_response_header_test();
|
||||
assert(case_insensitive_equal("Test", "tesT"));
|
||||
assert(case_insensitive_equal("tesT", "test"));
|
||||
assert(!case_insensitive_equal("test", "tseT"));
|
||||
CaseInsensitiveEqual equal;
|
||||
assert(equal("Test", "tesT"));
|
||||
assert(equal("tesT", "test"));
|
||||
assert(!equal("test", "tset"));
|
||||
CaseInsensitiveHash hash;
|
||||
assert(hash("Test") == hash("tesT"));
|
||||
assert(hash("tesT") == hash("test"));
|
||||
assert(hash("test") != hash("tset"));
|
||||
|
||||
auto percent_decoded = "testing æøå !#$&'()*+,/:;=?@[]";
|
||||
auto percent_encoded = "testing+æøå+%21%23%24%26%27%28%29%2A%2B%2C%2F%3A%3B%3D%3F%40%5B%5D";
|
||||
assert(Percent::encode(percent_decoded) == percent_encoded);
|
||||
assert(Percent::decode(percent_encoded) == percent_decoded);
|
||||
assert(Percent::decode(Percent::encode(percent_decoded)) == percent_decoded);
|
||||
|
||||
SimpleWeb::CaseInsensitiveMultimap fields = {{"test1", "æøå"}, {"test2", "!#$&'()*+,/:;=?@[]"}};
|
||||
auto query_string1 = "test1=æøå&test2=%21%23%24%26%27%28%29%2A%2B%2C%2F%3A%3B%3D%3F%40%5B%5D";
|
||||
auto query_string2 = "test2=%21%23%24%26%27%28%29%2A%2B%2C%2F%3A%3B%3D%3F%40%5B%5D&test1=æøå";
|
||||
auto query_string_result = QueryString::create(fields);
|
||||
assert(query_string_result == query_string1 || query_string_result == query_string2);
|
||||
auto fields_result1 = QueryString::parse(query_string1);
|
||||
auto fields_result2 = QueryString::parse(query_string2);
|
||||
assert(fields_result1 == fields_result2 && fields_result1 == fields);
|
||||
|
||||
ServerTest serverTest;
|
||||
serverTest.io_service = std::make_shared<asio::io_service>();
|
||||
|
||||
serverTest.parse_request_test();
|
||||
|
||||
ClientTest clientTest("test.org:8080");
|
||||
clientTest.constructor_parse_test1();
|
||||
|
||||
ClientTest clientTest2("test.org");
|
||||
clientTest2.constructor_parse_test2();
|
||||
|
||||
clientTest2.parse_response_header_test();
|
||||
|
||||
|
||||
asio::io_service io_service;
|
||||
asio::ip::tcp::socket socket(io_service);
|
||||
SimpleWeb::Server<HTTP>::Request request(socket);
|
||||
asio::io_service io_service;
|
||||
asio::ip::tcp::socket socket(io_service);
|
||||
SimpleWeb::Server<HTTP>::Request request(socket);
|
||||
{
|
||||
request.path = "/?";
|
||||
auto queries = request.parse_query_string();
|
||||
assert(queries.empty());
|
||||
}
|
||||
{
|
||||
request.path = "/";
|
||||
auto queries = request.parse_query_string();
|
||||
assert(queries.empty());
|
||||
}
|
||||
{
|
||||
request.path = "/?=";
|
||||
auto queries = request.parse_query_string();
|
||||
assert(queries.empty());
|
||||
}
|
||||
{
|
||||
request.path = "/?=test";
|
||||
auto queries = request.parse_query_string();
|
||||
assert(queries.empty());
|
||||
}
|
||||
{
|
||||
request.path = "/?a=1%202%20%203&b=3+4&c&d=æ%25ø%26å%3F";
|
||||
auto queries = request.parse_query_string();
|
||||
{
|
||||
request.path = "/?";
|
||||
auto queries = request.parse_query_string();
|
||||
assert(queries.empty());
|
||||
auto range = queries.equal_range("a");
|
||||
assert(range.first != range.second);
|
||||
assert(range.first->second == "1 2 3");
|
||||
}
|
||||
{
|
||||
request.path = "/";
|
||||
auto queries = request.parse_query_string();
|
||||
assert(queries.empty());
|
||||
auto range = queries.equal_range("b");
|
||||
assert(range.first != range.second);
|
||||
assert(range.first->second == "3 4");
|
||||
}
|
||||
{
|
||||
request.path = "/?=";
|
||||
auto queries = request.parse_query_string();
|
||||
assert(queries.empty());
|
||||
auto range = queries.equal_range("c");
|
||||
assert(range.first != range.second);
|
||||
assert(range.first->second == "");
|
||||
}
|
||||
{
|
||||
request.path = "/?=test";
|
||||
auto queries = request.parse_query_string();
|
||||
assert(queries.empty());
|
||||
}
|
||||
{
|
||||
request.path = "/?a=1%202%20%203&b=3+4&c&d=æ%25ø%26å%3F";
|
||||
auto queries = request.parse_query_string();
|
||||
{
|
||||
auto range = queries.equal_range("a");
|
||||
assert(range.first != range.second);
|
||||
assert(range.first->second == "1 2 3");
|
||||
}
|
||||
{
|
||||
auto range = queries.equal_range("b");
|
||||
assert(range.first != range.second);
|
||||
assert(range.first->second == "3 4");
|
||||
}
|
||||
{
|
||||
auto range = queries.equal_range("c");
|
||||
assert(range.first != range.second);
|
||||
assert(range.first->second == "");
|
||||
}
|
||||
{
|
||||
auto range = queries.equal_range("d");
|
||||
assert(range.first != range.second);
|
||||
assert(range.first->second == "æ%ø&å?");
|
||||
}
|
||||
auto range = queries.equal_range("d");
|
||||
assert(range.first != range.second);
|
||||
assert(range.first->second == "æ%ø&å?");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue