Skip to content

Commit d14094c

Browse files
committed
Implemeneted test for request server_api
1 parent 040244e commit d14094c

14 files changed

+336
-48
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ example-of-exported/py3/dist/*
55
example-of-exported/py3/*.egg-info
66
example-of-exported/py3/build
77
wsjcpp-jsonrpc2
8-
wsjcpp-jsonrpc20
8+
wsjcpp-jsonrpc20
9+
__pycache__

CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ list (APPEND WSJCPP_SOURCES "src/wsjcpp_jsonrpc20_export_cli_python.cpp")
2020
# examples
2121
list (APPEND WSJCPP_SOURCES "./src/examples/wsjcpp_json_rpc20_handler_game_create.h")
2222
list (APPEND WSJCPP_SOURCES "./src/examples/wsjcpp_json_rpc20_handler_game_create.cpp")
23+
list (APPEND WSJCPP_SOURCES "./src/examples/wsjcpp_json_rpc20_handler_auth.h")
24+
list (APPEND WSJCPP_SOURCES "./src/examples/wsjcpp_json_rpc20_handler_auth.cpp")
2325

2426
#### BEGIN_WSJCPP_APPEND
2527
#### END_WSJCPP_APPEND
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
2+
#include "wsjcpp_json_rpc20_handler_auth.h"
3+
#include <wsjcpp_core.h>
4+
#include <wsjcpp_jsonrpc20.h>
5+
6+
// ---------------------------------------------------------------------
7+
// WsjcppJsonRpc20HandlerAuthLogin
8+
9+
REGISTRY_WSJCPP_JSONRPC20_HANDLER(WsjcppJsonRpc20HandlerAuthLogin)
10+
11+
WsjcppJsonRpc20HandlerAuthLogin::WsjcppJsonRpc20HandlerAuthLogin()
12+
: WsjcppJsonRpc20HandlerBase("auth_login", "Auth by login and password") {
13+
TAG = "WsjcppJsonRpc20HandlerAuthLogin";
14+
setAccessUnauthorized(true);
15+
setAccessUser(false);
16+
setAccessTester(false);
17+
setAccessAdmin(false);
18+
setActivatedFromVersion("v0.0.2");
19+
// void setDeprecatedFromVersion("");
20+
21+
// description of input params
22+
requireStringParam("login", "User Login")
23+
.addValidator(new WsjcppValidatorStringLength(5,10));
24+
requireStringParam("password", "User Password")
25+
.addValidator(new WsjcppValidatorStringLength(5,20));
26+
}
27+
28+
// ---------------------------------------------------------------------
29+
30+
void WsjcppJsonRpc20HandlerAuthLogin::handle(WsjcppJsonRpc20Request *pRequest) {
31+
WsjcppLog::err(TAG, "Not implemented");
32+
// TODO
33+
pRequest->fail(WsjcppJsonRpc20Error(501, "NOT_IMPLEMENTED"));
34+
}
35+
36+
37+
// ---------------------------------------------------------------------
38+
// WsjcppJsonRpc20HandlerAuthLogoff
39+
40+
REGISTRY_WSJCPP_JSONRPC20_HANDLER(WsjcppJsonRpc20HandlerAuthLogoff)
41+
42+
WsjcppJsonRpc20HandlerAuthLogoff::WsjcppJsonRpc20HandlerAuthLogoff()
43+
: WsjcppJsonRpc20HandlerBase("auth_logoff", "Logoff") {
44+
TAG = "WsjcppJsonRpc20HandlerAuthLogoff";
45+
setAccessUnauthorized(false);
46+
setAccessUser(true);
47+
setAccessTester(true);
48+
setAccessAdmin(true);
49+
setActivatedFromVersion("v0.0.2");
50+
// void setDeprecatedFromVersion("");
51+
}
52+
53+
// ---------------------------------------------------------------------
54+
55+
void WsjcppJsonRpc20HandlerAuthLogoff::handle(WsjcppJsonRpc20Request *pRequest) {
56+
WsjcppLog::err(TAG, "Not implemented");
57+
// TODO
58+
pRequest->fail(WsjcppJsonRpc20Error(501, "NOT_IMPLEMENTED"));
59+
}
60+
61+
62+
// ---------------------------------------------------------------------
63+
// WsjcppJsonRpc20HandlerAuthToken
64+
65+
REGISTRY_WSJCPP_JSONRPC20_HANDLER(WsjcppJsonRpc20HandlerAuthToken)
66+
67+
WsjcppJsonRpc20HandlerAuthToken::WsjcppJsonRpc20HandlerAuthToken()
68+
: WsjcppJsonRpc20HandlerBase("auth_token", "Auth by token") {
69+
TAG = "WsjcppJsonRpc20HandlerAuthToken";
70+
setAccessUnauthorized(true);
71+
setAccessUser(false);
72+
setAccessTester(false);
73+
setAccessAdmin(false);
74+
setActivatedFromVersion("v0.0.2");
75+
// void setDeprecatedFromVersion("");
76+
77+
// description of input params
78+
requireStringParam("token", "Token")
79+
.addValidator(new WsjcppValidatorStringLength(10,100));
80+
}
81+
82+
// ---------------------------------------------------------------------
83+
84+
void WsjcppJsonRpc20HandlerAuthToken::handle(WsjcppJsonRpc20Request *pRequest) {
85+
WsjcppLog::err(TAG, "Not implemented");
86+
// TODO
87+
pRequest->fail(WsjcppJsonRpc20Error(501, "NOT_IMPLEMENTED"));
88+
}
89+
90+
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#ifndef WSJCPP_JSON_RPC20_HANDLER_AUTH_H
2+
#define WSJCPP_JSON_RPC20_HANDLER_AUTH_H
3+
4+
#include <wsjcpp_jsonrpc20.h>
5+
6+
class WsjcppJsonRpc20HandlerAuthLogin : public WsjcppJsonRpc20HandlerBase {
7+
public:
8+
WsjcppJsonRpc20HandlerAuthLogin();
9+
virtual void handle(WsjcppJsonRpc20Request *pRequest) override;
10+
};
11+
12+
class WsjcppJsonRpc20HandlerAuthLogoff : public WsjcppJsonRpc20HandlerBase {
13+
public:
14+
WsjcppJsonRpc20HandlerAuthLogoff();
15+
virtual void handle(WsjcppJsonRpc20Request *pRequest) override;
16+
};
17+
18+
class WsjcppJsonRpc20HandlerAuthToken : public WsjcppJsonRpc20HandlerBase {
19+
public:
20+
WsjcppJsonRpc20HandlerAuthToken();
21+
virtual void handle(WsjcppJsonRpc20Request *pRequest) override;
22+
};
23+
24+
#endif // WSJCPP_JSON_RPC20_HANDLER_AUTH_H

src/examples/wsjcpp_json_rpc20_handler_game_create.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
REGISTRY_WSJCPP_JSONRPC20_HANDLER(WsjcppJsonRpc20HandlerGameCreate)
1010

1111
WsjcppJsonRpc20HandlerGameCreate::WsjcppJsonRpc20HandlerGameCreate()
12-
: WsjcppJsonRpc20HandlerBase("game_create", "TODO description") {
12+
: WsjcppJsonRpc20HandlerBase("game_create", "Some example of description") {
1313
TAG = "WsjcppJsonRpc20HandlerGameCreate";
1414
setAccessUnauthorized(false);
1515
setAccessUser(true);

src/main.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ int main(int argc, const char* argv[]) {
1515
WsjcppLog::setPrefixLogFile("wsjcpp");
1616
WsjcppLog::setLogDirectory(".logs");
1717

18+
19+
20+
21+
1822
std::cout << "Generate client python library" << std::endl;
1923

2024
std::string sExportDir = "./example-of-exported-client-libraries";
@@ -30,6 +34,8 @@ int main(int argc, const char* argv[]) {
3034
exportCliPython.setUrl("https://github.com/wsjcpp/wsjcpp-jsonrpc20");
3135
exportCliPython.setDownloadUrl("https://github.com/wsjcpp/wsjcpp-jsonrpc20/archive/" + std::string(WSJCPP_APP_NAME) + ".tar.gz");
3236
exportCliPython.setKeywords({std::string(WSJCPP_APP_NAME), "wsjcpp", "wsjcpp-jsonrpc20", "example-python-client"});
37+
exportCliPython.addLoginMethod("auth_login", "token");
38+
exportCliPython.addLogoffMethod("auth_logoff");
3339

3440
if (!exportCliPython.doExportLib()) {
3541
std::cout << "Failed!" << std::endl;

src/wsjcpp_jsonrpc20.cpp

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -714,18 +714,11 @@ bool WsjcppJsonRpc20Request::parseIncomeData(const std::string &sIncomeData) {
714714
return false;
715715
}
716716
m_jsonRequest = nlohmann::json::parse(sIncomeData);
717-
718-
if (m_jsonRequest["cmd"].is_string()) { // deprecated
719-
m_sMethod = m_jsonRequest["cmd"];
720-
}
721717

722718
if (m_jsonRequest["method"].is_string()) {
723719
m_sMethod = m_jsonRequest["method"];
724720
}
725721

726-
if (m_jsonRequest["m"].is_string()) { // deprecated
727-
m_sId = m_jsonRequest["m"];
728-
}
729722

730723
if (m_jsonRequest["id"].is_string()) {
731724
m_sId = m_jsonRequest["id"];
@@ -1130,6 +1123,7 @@ WsjcppJsonRpc20HandlerServerApi::WsjcppJsonRpc20HandlerServerApi()
11301123
: WsjcppJsonRpc20HandlerBase("server_api", "This method Will be return list of all handlers") {
11311124

11321125
setAccessUnauthorized(true);
1126+
setAccessTester(true);
11331127
setAccessUser(true);
11341128
setAccessAdmin(true);
11351129
}
@@ -1152,6 +1146,7 @@ void WsjcppJsonRpc20HandlerServerApi::handle(WsjcppJsonRpc20Request *pRequest) {
11521146

11531147
nlohmann::json jsonHandlers = nlohmann::json::array();
11541148
std::map<std::string, WsjcppJsonRpc20HandlerBase *>::iterator it = g_pWsjcppJsonRpc20HandlerList->begin();
1149+
int nDataLength = 0;
11551150
while (it != g_pWsjcppJsonRpc20HandlerList->end()) {
11561151
std::string sCmd = it->first;
11571152
WsjcppJsonRpc20HandlerBase *pHandler = g_pWsjcppJsonRpc20HandlerList->at(sCmd);
@@ -1160,20 +1155,28 @@ void WsjcppJsonRpc20HandlerServerApi::handle(WsjcppJsonRpc20Request *pRequest) {
11601155

11611156
jsonHandler["method"] = pHandler->getMethodName();
11621157
jsonHandler["description"] = pHandler->getDescription();
1163-
jsonHandler["access_unauthorized"] = pHandler->haveUnauthorizedAccess();
1164-
jsonHandler["access_user"] = pHandler->haveUserAccess();
1165-
jsonHandler["access_tester"] = pHandler->haveTesterAccess();
1166-
jsonHandler["access_admin"] = pHandler->haveAdminAccess();
1158+
1159+
nlohmann::json jsonAccess;
1160+
jsonAccess["unauthorized"] = pHandler->haveUnauthorizedAccess();
1161+
jsonAccess["user"] = pHandler->haveUserAccess();
1162+
jsonAccess["tester"] = pHandler->haveTesterAccess();
1163+
jsonAccess["admin"] = pHandler->haveAdminAccess();
1164+
jsonHandler["access"] = jsonAccess;
11671165

11681166
nlohmann::json jsonInputs = nlohmann::json::array();
11691167
std::vector<WsjcppJsonRpc20ParamDef> ins = pHandler->inputs();
1170-
for (unsigned int i = 0; i < ins.size(); i++) {
1171-
jsonInputs.push_back(ins[i].toJson());
1168+
if (ins.size() > 0) {
1169+
for (unsigned int i = 0; i < ins.size(); i++) {
1170+
jsonInputs.push_back(ins[i].toJson());
1171+
}
1172+
jsonHandler["params"] = jsonInputs;
11721173
}
1173-
jsonHandler["params"] = jsonInputs;
11741174
jsonHandlers.push_back(jsonHandler);
11751175
it++;
1176+
nDataLength++;
11761177
}
1178+
11771179
jsonResponse["data"] = jsonHandlers;
1180+
jsonResponse["data_length"] = nDataLength;
11781181
pRequest->done(jsonResponse);
11791182
}

src/wsjcpp_jsonrpc20.h

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,6 @@
77
#include <iostream>
88
#include <algorithm>
99

10-
class WsjcppJsonRpc20Error;
11-
class WsjcppJsonRpc20UserSession;
12-
class WsjcppJsonRpc20ParamDef;
13-
class WsjcppJsonRpc20WebSocketClient;
14-
class WsjcppJsonRpc20WebSocketServer;
15-
class WsjcppJsonRpc20ParamDef;
16-
class WsjcppJsonRpc20Request;
17-
class WsjcppJsonRpc20BaseHandler;
18-
class WsjcppJsonRpc20;
19-
2010
// ---------------------------------------------------------------------
2111
// WsjcppJsonRpc20Error
2212

@@ -116,6 +106,37 @@ class WsjcppJsonRpc20WebSocketClient {
116106
WsjcppJsonRpc20UserSession *m_pUserSession;
117107
};
118108

109+
// ---------------------------------------------------------------------
110+
111+
class FakeWebSocketClient : public WsjcppJsonRpc20WebSocketClient {
112+
public:
113+
virtual void onDisconnected() override {
114+
// nothing
115+
};
116+
virtual std::string getPeerIpAddress() override {
117+
return "none";
118+
};
119+
120+
virtual int getPeerPort() override{
121+
return 0;
122+
};
123+
124+
virtual std::string getRequestUrl() override {
125+
return "none";
126+
};
127+
128+
virtual void sendTextMessage(const std::string &sTextMessage) override {
129+
m_sLastTextMessage = sTextMessage;
130+
};
131+
132+
std::string getLastTextMessage() {
133+
return m_sLastTextMessage;
134+
}
135+
136+
private:
137+
std::string m_sLastTextMessage;
138+
};
139+
119140
// ---------------------------------------------------------------------
120141
// WsjcppJsonRpc20WebSocketServer
121142

0 commit comments

Comments
 (0)