230 lines
6.6 KiB
C++
230 lines
6.6 KiB
C++
/**
|
|
* @file requesthandler.cpp
|
|
* @brief Test for the request-reply handler
|
|
* @copyright 2015 Dual Inventive Technology Centre B.V.
|
|
*
|
|
* This tests the request reply handler.
|
|
*/
|
|
#include <unistd.h>
|
|
#include <iostream>
|
|
#include <string>
|
|
#include <functional>
|
|
#include <di/Zmq.h>
|
|
#include <di/Log.h>
|
|
#include <di/JsonDiff.h>
|
|
#include <di/types.h>
|
|
#include <di/RequestHandler.h>
|
|
|
|
#include <gtest/gtest.h>
|
|
|
|
using namespace std;
|
|
using namespace Di;
|
|
|
|
static bool strReplace(std::string *str, const std::string& from, const std::string& to) {
|
|
size_t start_pos = str->find(from);
|
|
if(start_pos == std::string::npos)
|
|
return false;
|
|
str->replace(start_pos, from.length(), to);
|
|
return true;
|
|
}
|
|
|
|
TEST(RequestHandler, no_uri) {
|
|
shared_ptr<RequestHandler> r = make_shared<RequestHandler>("");
|
|
}
|
|
|
|
TEST(RequestHandler, late_connect) {
|
|
shared_ptr<Json> reply;
|
|
bool more;
|
|
|
|
shared_ptr<RequestHandler> r = make_shared<RequestHandler>("tcp://localhost:49999");
|
|
|
|
EXPECT_EQ(DNOK, r->sendRequest("01234567801234567890123456789012", "project:test"));
|
|
|
|
shared_ptr<Zmq::Context> ctx = make_shared<Zmq::Context>();
|
|
shared_ptr<Zmq::Socket> zs = make_shared<Zmq::Socket>(ctx, ZMQ_REP);
|
|
zs->bind("tcp://*:49999");
|
|
|
|
EXPECT_EQ(DNE_PARAM, r->sendRequest("01234567801234567890123456789012", "project:test"));
|
|
|
|
// Request 1 (echo)
|
|
auto str = zs->recv(&more);
|
|
EXPECT_NE(nullptr, str);
|
|
EXPECT_TRUE(zs->send(*str));
|
|
EXPECT_EQ(DNOK, r->receiveReply(&reply));
|
|
}
|
|
|
|
TEST(RequestHandler, normal) {
|
|
shared_ptr<Json> reply;
|
|
bool more;
|
|
|
|
shared_ptr<RequestHandler> r = make_shared<RequestHandler>("tcp://localhost:49999");
|
|
|
|
shared_ptr<Zmq::Context> ctx = make_shared<Zmq::Context>();
|
|
shared_ptr<Zmq::Socket> zs = make_shared<Zmq::Socket>(ctx, ZMQ_REP);
|
|
zs->bind("tcp://*:49999");
|
|
|
|
// Request 1 (echo)
|
|
EXPECT_EQ(0, r->sendRequest("01234567801234567890123456789012", "project:test"));
|
|
auto str = zs->recv(&more);
|
|
EXPECT_NE(nullptr, str);
|
|
auto js = make_shared<Json>(str);
|
|
EXPECT_TRUE(zs->send(*str));
|
|
EXPECT_EQ(0, r->receiveReply(&reply));
|
|
JsonDiff jd;
|
|
EXPECT_TRUE(jd.diff(js, reply));
|
|
}
|
|
|
|
TEST(RequestHandler, error_reply) {
|
|
shared_ptr<Json> reply;
|
|
shared_ptr<Json> x, js;
|
|
bool more;
|
|
|
|
shared_ptr<RequestHandler> r = make_shared<RequestHandler>("tcp://localhost:49999");
|
|
|
|
shared_ptr<Zmq::Context> ctx = make_shared<Zmq::Context>();
|
|
shared_ptr<Zmq::Socket> zs = make_shared<Zmq::Socket>(ctx, ZMQ_REP);
|
|
zs->bind("tcp://*:49999");
|
|
|
|
// Request 2 (add error in reply)
|
|
EXPECT_EQ(0, r->sendRequest("01234567801234567890123456789012", "project:test"));
|
|
auto str = zs->recv(&more);
|
|
EXPECT_NE(nullptr, str);
|
|
|
|
js = make_shared<Json>(str);
|
|
x = make_shared<Json>(Json::J_OBJECT);
|
|
x->setNumber("code", DNE_BACKEND_DEVICE_ERROR);
|
|
js->setObject("error", x);
|
|
auto str2 = js->dumpString();
|
|
strReplace(&str2, "\"req\"", "\"rep\"");
|
|
js = make_shared<Json>(str2);
|
|
EXPECT_TRUE(zs->send(str2));
|
|
EXPECT_EQ(DNE_BACKEND_DEVICE_ERROR, r->receiveReply(&reply));
|
|
JsonDiff jd;
|
|
EXPECT_TRUE(jd.diff(js, reply));
|
|
}
|
|
|
|
TEST(RequestHandler, error_reply_nocode) {
|
|
shared_ptr<Json> reply;
|
|
shared_ptr<Json> x, js;
|
|
bool more;
|
|
|
|
shared_ptr<RequestHandler> r = make_shared<RequestHandler>("tcp://localhost:49999");
|
|
|
|
shared_ptr<Zmq::Context> ctx = make_shared<Zmq::Context>();
|
|
shared_ptr<Zmq::Socket> zs = make_shared<Zmq::Socket>(ctx, ZMQ_REP);
|
|
zs->bind("tcp://*:49999");
|
|
|
|
// Request 3 (error without error-code in reply)
|
|
EXPECT_EQ(0, r->sendRequest("01234567801234567990123456789012", "project:test"));
|
|
auto str = zs->recv(&more);
|
|
EXPECT_NE(nullptr, str);
|
|
|
|
js = make_shared<Json>(str);
|
|
x = make_shared<Json>(Json::J_OBJECT);
|
|
js->setObject("error", x);
|
|
auto str2 = js->dumpString();
|
|
strReplace(&str2, "\"req\"", "\"rep\"");
|
|
js = make_shared<Json>(str2);
|
|
EXPECT_TRUE(zs->send(str2));
|
|
EXPECT_EQ(DNE_PROTO, r->receiveReply(&reply));
|
|
JsonDiff jd;
|
|
EXPECT_TRUE(jd.diff(js, reply));
|
|
}
|
|
|
|
TEST(RequestHandler, invalid_json) {
|
|
shared_ptr<Json> reply;
|
|
shared_ptr<Json> x, js;
|
|
bool more;
|
|
|
|
shared_ptr<RequestHandler> r = make_shared<RequestHandler>("tcp://localhost:49999");
|
|
|
|
shared_ptr<Zmq::Context> ctx = make_shared<Zmq::Context>();
|
|
shared_ptr<Zmq::Socket> zs = make_shared<Zmq::Socket>(ctx, ZMQ_REP);
|
|
zs->bind("tcp://*:49999");
|
|
|
|
// Request 4 (invalid JSON reply)
|
|
EXPECT_EQ(0, r->sendRequest("01234567890123567890123456789012", "project:test"));
|
|
auto str = zs->recv(&more);
|
|
EXPECT_NE(nullptr, str);
|
|
|
|
auto str2 = "this is invalid json :)";
|
|
EXPECT_TRUE(zs->send(str2));
|
|
EXPECT_EQ(DNE_PROTO, r->receiveReply(&reply));
|
|
}
|
|
|
|
TEST(RequestHandler, noreply) {
|
|
shared_ptr<Json> reply;
|
|
shared_ptr<Json> x, js;
|
|
bool more;
|
|
|
|
shared_ptr<RequestHandler> r = make_shared<RequestHandler>("tcp://localhost:49999");
|
|
|
|
shared_ptr<Zmq::Context> ctx = make_shared<Zmq::Context>();
|
|
shared_ptr<Zmq::Socket> zs = make_shared<Zmq::Socket>(ctx, ZMQ_REP);
|
|
zs->bind("tcp://*:49999");
|
|
|
|
// Request 5 (no need for reply)
|
|
EXPECT_EQ(0, r->sendRequest("01234567890123456789012345789012", "project:test"));
|
|
auto str = zs->recv(&more);
|
|
EXPECT_NE(nullptr, str);
|
|
EXPECT_TRUE(zs->send(*str));
|
|
EXPECT_EQ(0, r->receiveReply(nullptr));
|
|
}
|
|
|
|
TEST(RequestHandler, params) {
|
|
shared_ptr<Json> reply;
|
|
shared_ptr<Json> x, js;
|
|
bool more;
|
|
|
|
shared_ptr<RequestHandler> r = make_shared<RequestHandler>("tcp://localhost:49999");
|
|
|
|
shared_ptr<Zmq::Context> ctx = make_shared<Zmq::Context>();
|
|
shared_ptr<Zmq::Socket> zs = make_shared<Zmq::Socket>(ctx, ZMQ_REP);
|
|
zs->bind("tcp://*:49999");
|
|
|
|
js = make_shared<Json>(Json::J_OBJECT);
|
|
js->setNull("project");
|
|
js->setString("test", "rik");
|
|
|
|
// Request 6 (with params)
|
|
EXPECT_EQ(0, r->sendRequest("01234567890123456789001234567891", "project:test", js));
|
|
auto str = zs->recv(&more);
|
|
EXPECT_NE(nullptr, str);
|
|
|
|
EXPECT_TRUE(zs->send(*str));
|
|
EXPECT_EQ(0, r->receiveReply(&reply));
|
|
}
|
|
|
|
TEST(RequestHandler, timeout) {
|
|
shared_ptr<Json> reply;
|
|
shared_ptr<Json> x, js;
|
|
bool more;
|
|
|
|
shared_ptr<RequestHandler> r = make_shared<RequestHandler>("tcp://localhost:49999");
|
|
r->setTimeout(100);
|
|
|
|
shared_ptr<Zmq::Context> ctx = make_shared<Zmq::Context>();
|
|
shared_ptr<Zmq::Socket> zs = make_shared<Zmq::Socket>(ctx, ZMQ_REP);
|
|
zs->bind("tcp://*:49999");
|
|
|
|
js = make_shared<Json>(Json::J_OBJECT);
|
|
js->setNull("project");
|
|
js->setString("test", "rik");
|
|
|
|
EXPECT_EQ(0, r->sendRequest("01234567890123456789001234567891", "project:test", js));
|
|
auto str = zs->recv(&more);
|
|
EXPECT_NE(nullptr, str);
|
|
|
|
EXPECT_EQ(DNE_TIMEOUT, r->receiveReply(nullptr));
|
|
}
|
|
|
|
TEST(RequestHandler, send_while_disconnected) {
|
|
shared_ptr<RequestHandler> r = make_shared<RequestHandler>("tcp://www.google.com:45678");
|
|
|
|
r->setTimeout(100);
|
|
|
|
EXPECT_EQ(DNOK, r->sendRequest("01234567890123456789012345789012", "project:test"));
|
|
EXPECT_EQ(DNE_TIMEOUT, r->receiveReply(nullptr));
|
|
}
|
|
|