Merge pull request #32 from an-tao/dev

Add a test client for the simple example
This commit is contained in:
An Tao 2019-01-19 14:02:09 +08:00 committed by GitHub
commit 8c6c549b9d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 440 additions and 16 deletions

View File

@ -21,7 +21,8 @@ add_dependencies(webapp drogon_ctl)
AUX_SOURCE_DIRECTORY(client_example DIR_CLIENT)
AUX_SOURCE_DIRECTORY(benchmark DIR_BENCHMARK)
AUX_SOURCE_DIRECTORY(simple_example_test DIR_TEST)
add_executable(client ${DIR_CLIENT})
add_executable(benchmark ${DIR_BENCHMARK})
add_executable(webapp_test ${DIR_TEST})

View File

@ -97,15 +97,12 @@ int main()
{
std::cout << banner << std::endl;
// drogon::HttpAppFramework::instance().addListener("0.0.0.0",12345);
app().addListener("0.0.0.0", 8080);
//#ifdef USE_OPENSSL
// //https
// drogon::HttpAppFramework::instance().setSSLFiles("server.pem","server.pem");
// drogon::HttpAppFramework::instance().addListener("0.0.0.0",4430,true);
// drogon::HttpAppFramework::instance().addListener("0.0.0.0",4431,true);
//#endif
app().addListener("0.0.0.0", 8848);
#ifdef USE_OPENSSL
//https
drogon::HttpAppFramework::instance().setSSLFiles("../../trantor/trantor/tests/server.pem", "../../trantor/trantor/tests/server.pem");
drogon::HttpAppFramework::instance().addListener("0.0.0.0", 8849, true);
#endif
app().setThreadNum(4);
// trantor::Logger::setLogLevel(trantor::Logger::TRACE);
//class function

View File

@ -0,0 +1,423 @@
/**
*
* test.cc
* An Tao
*
* Copyright 2018, An Tao. All rights reserved.
* https://github.com/an-tao/drogon
* Use of this source code is governed by a MIT license
* that can be found in the License file.
*
* Drogon
*
*/
//Make a http client to test the example server app;
#include <drogon/drogon.h>
#include <mutex>
#define RESET "\033[0m"
#define RED "\033[31m" /* Red */
#define GREEN "\033[32m" /* Green */
using namespace drogon;
void outputGood(const HttpRequestPtr &req)
{
static int i = 0;
static std::mutex mtx;
{
std::lock_guard<std::mutex> lock(mtx);
i++;
std::cout << i << GREEN << '\t' << "Good" << '\t' << RED << req->methodString()
<< " " << req->path() << RESET << std::endl;
}
}
void doTest(const HttpClientPtr &client)
{
/// 1 Get /
auto req = HttpRequest::newHttpRequest();
req->setMethod(drogon::Get);
req->setPath("/");
client->sendRequest(req, [=](ReqResult result, const HttpResponsePtr &resp) {
if (result == ReqResult::Ok)
{
//LOG_DEBUG << resp->getBody();
if (resp->getBody() == "<p>Hello, world!</p>")
{
outputGood(req);
}
else
{
LOG_ERROR << "Error!";
exit(1);
}
}
else
{
LOG_ERROR << "Error!";
exit(1);
}
});
/// 2. Get /slow to test simple controller, session and filter (cookie is not supported by HttpClient now)
req = HttpRequest::newHttpRequest();
req->setMethod(drogon::Get);
req->setPath("/slow");
client->sendRequest(req, [=](ReqResult result, const HttpResponsePtr &resp) {
if (result == ReqResult::Ok)
{
//LOG_DEBUG << resp->getBody();
if (resp->getBody() == "<p>Hello, world!</p>")
{
outputGood(req);
}
else
{
LOG_ERROR << "Error!";
exit(1);
}
}
else
{
LOG_ERROR << "Error!";
exit(1);
}
});
/// 3. Post to /tpost to test Http Method constraint
req = HttpRequest::newHttpRequest();
req->setMethod(drogon::Get);
req->setPath("/tpost");
client->sendRequest(req, [=](ReqResult result, const HttpResponsePtr &resp) {
if (result == ReqResult::Ok)
{
//LOG_DEBUG << resp->getBody();
if (resp->statusCode() == HttpResponse::k405MethodNotAllowed)
{
outputGood(req);
}
else
{
LOG_ERROR << "Error!";
exit(1);
}
}
else
{
LOG_ERROR << "Error!";
exit(1);
}
});
req = HttpRequest::newHttpRequest();
req->setMethod(drogon::Post);
req->setPath("/tpost");
client->sendRequest(req, [=](ReqResult result, const HttpResponsePtr &resp) {
if (result == ReqResult::Ok)
{
if (resp->getBody() == "<p>Hello, world!</p>")
{
outputGood(req);
}
else
{
LOG_ERROR << "Error!";
exit(1);
}
}
else
{
LOG_ERROR << "Error!";
exit(1);
}
});
/// 4. Test HttpController
req = HttpRequest::newHttpRequest();
req->setMethod(drogon::Post);
req->setPath("/api/v1/apitest");
client->sendRequest(req, [=](ReqResult result, const HttpResponsePtr &resp) {
if (result == ReqResult::Ok)
{
if (resp->getBody() == "ROOT Post!!!")
{
outputGood(req);
}
else
{
LOG_DEBUG << resp->getBody();
LOG_ERROR << "Error!";
exit(1);
}
}
else
{
LOG_ERROR << "Error!";
exit(1);
}
});
req = HttpRequest::newHttpRequest();
req->setMethod(drogon::Get);
req->setPath("/api/v1/apitest");
client->sendRequest(req, [=](ReqResult result, const HttpResponsePtr &resp) {
if (result == ReqResult::Ok)
{
if (resp->getBody() == "ROOT Get!!!")
{
outputGood(req);
}
else
{
LOG_DEBUG << resp->getBody();
LOG_ERROR << "Error!";
exit(1);
}
}
else
{
LOG_ERROR << "Error!";
exit(1);
}
});
req = HttpRequest::newHttpRequest();
req->setMethod(drogon::Get);
req->setPath("/api/v1/apitest/get/abc/123");
client->sendRequest(req, [=](ReqResult result, const HttpResponsePtr &resp) {
if (result == ReqResult::Ok)
{
if (resp->getBody().find("<td>p1</td>\n <td>123</td>") != std::string::npos &&
resp->getBody().find("<td>p2</td>\n <td>abc</td>") != std::string::npos)
{
outputGood(req);
}
else
{
LOG_DEBUG << resp->getBody();
LOG_ERROR << "Error!";
exit(1);
}
}
else
{
LOG_ERROR << "Error!";
exit(1);
}
});
req = HttpRequest::newHttpRequest();
req->setMethod(drogon::Get);
req->setPath("/api/v1/apitest/3.14/List?P2=1234");
client->sendRequest(req, [=](ReqResult result, const HttpResponsePtr &resp) {
if (result == ReqResult::Ok)
{
if (resp->getBody().find("<td>p1</td>\n <td>3.140000</td>") != std::string::npos &&
resp->getBody().find("<td>p2</td>\n <td>1234</td>") != std::string::npos)
{
outputGood(req);
}
else
{
LOG_DEBUG << resp->getBody();
LOG_ERROR << "Error!";
exit(1);
}
}
else
{
LOG_ERROR << "Error!";
exit(1);
}
});
req = HttpRequest::newHttpRequest();
req->setMethod(drogon::Get);
req->setPath("/api/v1/apitest/static");
client->sendRequest(req, [=](ReqResult result, const HttpResponsePtr &resp) {
if (result == ReqResult::Ok)
{
if (resp->getBody() == "staticApi,hello!!")
{
outputGood(req);
}
else
{
LOG_DEBUG << resp->getBody();
LOG_ERROR << "Error!";
exit(1);
}
}
else
{
LOG_ERROR << "Error!";
exit(1);
}
});
app().loop()->runAfter(0.5, [=]() mutable {
req = HttpRequest::newHttpRequest();
req->setMethod(drogon::Post);
req->setPath("/api/v1/apitest/static");
client->sendRequest(req, [=](ReqResult result, const HttpResponsePtr &resp) {
if (result == ReqResult::Ok)
{
if (resp->getBody() == "staticApi,hello!!")
{
outputGood(req);
}
else
{
LOG_DEBUG << resp->getBody();
LOG_ERROR << "Error!";
exit(1);
}
}
else
{
LOG_ERROR << "Error!";
exit(1);
}
});
});
req = HttpRequest::newHttpRequest();
req->setMethod(drogon::Get);
req->setPath("/api/v1/apitest/get/111");
client->sendRequest(req, [=](ReqResult result, const HttpResponsePtr &resp) {
if (result == ReqResult::Ok)
{
if (resp->getBody().length() == 5123)
{
outputGood(req);
}
else
{
//LOG_DEBUG << resp->getBody();
LOG_ERROR << "Error!";
exit(1);
}
}
else
{
LOG_ERROR << "Error!";
exit(1);
}
});
/// Test gzip
req = HttpRequest::newHttpRequest();
req->setMethod(drogon::Get);
req->addHeader("accept-encoding", "gzip");
req->setPath("/api/v1/apitest/get/111");
client->sendRequest(req, [=](ReqResult result, const HttpResponsePtr &resp) {
if (result == ReqResult::Ok)
{
if (resp->getBody().length() == 1754)
{
outputGood(req);
}
else
{
LOG_DEBUG << resp->getBody().length();
LOG_ERROR << "Error!";
exit(1);
}
}
else
{
LOG_ERROR << "Error!";
exit(1);
}
});
/// Test static function
req = HttpRequest::newHttpRequest();
req->setMethod(drogon::Get);
req->setPath("/api/v1/handle11/11/22/?p3=33&p4=44");
client->sendRequest(req, [=](ReqResult result, const HttpResponsePtr &resp) {
if (result == ReqResult::Ok)
{
if (resp->getBody().find("<td>int p1</td>\n <td>11</td>") != std::string::npos &&
resp->getBody().find("<td>int p4</td>\n <td>44</td>") != std::string::npos &&
resp->getBody().find("<td>string p2</td>\n <td>22</td>") != std::string::npos &&
resp->getBody().find("<td>string p3</td>\n <td>33</td>") != std::string::npos)
{
outputGood(req);
}
else
{
LOG_DEBUG << resp->getBody();
LOG_ERROR << "Error!";
exit(1);
}
}
else
{
LOG_ERROR << "Error!";
exit(1);
}
});
/// Test lambda
req = HttpRequest::newHttpRequest();
req->setMethod(drogon::Get);
req->setPath("/api/v1/handle2/111/222");
client->sendRequest(req, [=](ReqResult result, const HttpResponsePtr &resp) {
if (result == ReqResult::Ok)
{
if (resp->getBody().find("<td>a</td>\n <td>111</td>") != std::string::npos &&
resp->getBody().find("<td>b</td>\n <td>222.000000</td>") != std::string::npos)
{
outputGood(req);
}
else
{
LOG_DEBUG << resp->getBody();
LOG_ERROR << "Error!";
exit(1);
}
}
else
{
LOG_ERROR << "Error!";
exit(1);
}
});
/// Test std::bind and std::function
req = HttpRequest::newHttpRequest();
req->setMethod(drogon::Get);
req->setPath("/api/v1/handle4/444/333/111");
client->sendRequest(req, [=](ReqResult result, const HttpResponsePtr &resp) {
if (result == ReqResult::Ok)
{
if (resp->getBody().find("<td>int p1</td>\n <td>111</td>") != std::string::npos &&
resp->getBody().find("<td>int p4</td>\n <td>444</td>") != std::string::npos &&
resp->getBody().find("<td>string p3</td>\n <td>333</td>") != std::string::npos &&
resp->getBody().find("<td>string p2</td>\n <td></td>") != std::string::npos)
{
outputGood(req);
}
else
{
LOG_DEBUG << resp->getBody();
LOG_ERROR << "Error!";
exit(1);
}
}
else
{
LOG_ERROR << "Error!";
exit(1);
}
});
}
int main()
{
auto client = HttpClient::newHttpClient("http://127.0.0.1:8848");
doTest(client);
#ifdef USE_OPENSSL
auto sslClient = HttpClient::newHttpClient("https://127.0.0.1:8849");
doTest(sslClient);
#endif
app().run();
}

View File

@ -3,7 +3,7 @@
GIT_VER=$(git log|grep ^commit|wc -l|sed -e "s/^ *//")
MD5=$(git log|head -1|awk '{printf $2}')
TMP_FILE=/tmp/version
echo "#define VERSION \"0.9.16.$GIT_VER\"" > ${TMP_FILE}
echo "#define VERSION \"0.9.17.$GIT_VER\"" > ${TMP_FILE}
echo "#define VERSION_MD5 \"$MD5\"" >> ${TMP_FILE}
if [ ! -f $1 ];then
mv -f ${TMP_FILE} $1

View File

@ -55,6 +55,9 @@ class HttpRequest
virtual const std::string &getHeader(const std::string &field, const std::string &defaultVal = std::string()) const = 0;
virtual const std::string &getHeader(std::string &&field, const std::string &defaultVal = std::string()) const = 0;
/// Set the header string identified by the @param field
virtual void addHeader(const std::string &field, const std::string &value) = 0;
/// Get the cookie string identified by the @param field
virtual const std::string &getCookie(const std::string &field, const std::string &defaultVal = std::string()) const = 0;

View File

@ -334,7 +334,7 @@ class HttpRequestImpl : public HttpRequest
{
_content = content;
}
void addHeader(const std::string &key, const std::string &value)
virtual void addHeader(const std::string &key, const std::string &value) override
{
_headers[key] = value;
}

View File

@ -1,4 +1,4 @@
link_libraries(drogon trantor pthread dl)
add_executable(mytest1 test1.cc)
add_executable(mysql_test1 test1.cc)
#add_executable(test2 test2.cc)

View File

@ -1,4 +1,4 @@
link_libraries(drogon trantor pthread dl)
add_executable(test1 test1.cc)
add_executable(test2 test2.cc)
add_executable(pg_test1 test1.cc)
add_executable(pg_test2 test2.cc)

@ -1 +1 @@
Subproject commit f18818dea97d2ee8e7f60a10d2f289b9cecbd6e6
Subproject commit 4ba9e71d070b6c6498b4f0c9fd8e66bdde97ed97