|
| 1 | +#include "unit_test_parsing_request_params.h" |
| 2 | +#include <vector> |
| 3 | +#include <wsjcpp_core.h> |
| 4 | +#include <wsjcpp_jsonrpc20.h> |
| 5 | + |
| 6 | +class WsjcppJsonRpc20HandlerGameCreate : public WsjcppJsonRpc20HandlerBase { |
| 7 | + public: |
| 8 | + WsjcppJsonRpc20HandlerGameCreate(); |
| 9 | + virtual void handle(WsjcppJsonRpc20Request *pRequest) override; |
| 10 | +}; |
| 11 | + |
| 12 | + |
| 13 | +// --------------------------------------------------------------------- |
| 14 | +// WsjcppJsonRpc20HandlerGameCreate |
| 15 | + |
| 16 | +REGISTRY_WSJCPP_JSONRPC20_HANDLER(WsjcppJsonRpc20HandlerGameCreate) |
| 17 | + |
| 18 | +WsjcppJsonRpc20HandlerGameCreate::WsjcppJsonRpc20HandlerGameCreate() |
| 19 | +: WsjcppJsonRpc20HandlerBase("game_create", "Some example of description") { |
| 20 | + TAG = "WsjcppJsonRpc20HandlerGameCreate"; |
| 21 | + setAccessUnauthorized(false); |
| 22 | + setAccessUser(true); |
| 23 | + setAccessTester(true); |
| 24 | + setAccessAdmin(true); |
| 25 | + setActivatedFromVersion("v0.0.2"); |
| 26 | + // void setDeprecatedFromVersion(""); |
| 27 | + |
| 28 | + // description of input params |
| 29 | + requireStringParam("uuid", "object uuid") |
| 30 | + .addValidator(new WsjcppValidatorUUID()); |
| 31 | + optionalStringParam("name", "Name of object") |
| 32 | + .addValidator(new WsjcppValidatorStringLength(3,10)); |
| 33 | + |
| 34 | + requireIntegerParam("cost", "Name of object") |
| 35 | + .addValidator(new WsjcppValidatorIntegerMinValue(3)) |
| 36 | + .addValidator(new WsjcppValidatorIntegerMaxValue(1000)); |
| 37 | + optionalIntegerParam("age", "Name of object") |
| 38 | + .addValidator(new WsjcppValidatorIntegerMinValue(0)); |
| 39 | + |
| 40 | + requireBooleanParam("public", "True if object is public"); |
| 41 | + optionalBooleanParam("activated", "If object can handle"); |
| 42 | + |
| 43 | + optionalJsonParam("custom", "Some custom json"); |
| 44 | +} |
| 45 | + |
| 46 | +// --------------------------------------------------------------------- |
| 47 | + |
| 48 | +void WsjcppJsonRpc20HandlerGameCreate::handle(WsjcppJsonRpc20Request *pRequest) { |
| 49 | + WsjcppLog::err(TAG, "Not implemented"); |
| 50 | + // TODO |
| 51 | + pRequest->fail(WsjcppJsonRpc20Error(501, "NOT_IMPLEMENTED")); |
| 52 | +} |
| 53 | + |
| 54 | +// --------------------------------------------------------------------- |
| 55 | +// UnitTestParsingRequestParams |
| 56 | + |
| 57 | +REGISTRY_WSJCPP_UNIT_TEST(UnitTestParsingRequestParams) |
| 58 | + |
| 59 | +UnitTestParsingRequestParams::UnitTestParsingRequestParams() |
| 60 | + : WsjcppUnitTestBase("UnitTestParsingRequestParams") { |
| 61 | +} |
| 62 | + |
| 63 | +// --------------------------------------------------------------------- |
| 64 | + |
| 65 | +void UnitTestParsingRequestParams::init() { |
| 66 | + // nothing |
| 67 | +} |
| 68 | + |
| 69 | +// --------------------------------------------------------------------- |
| 70 | + |
| 71 | +bool UnitTestParsingRequestParams::run() { |
| 72 | + bool bTestSuccess = true; |
| 73 | + |
| 74 | + WsjcppJsonRpc20WebSocketServer *pWebSocketServer = new WsjcppJsonRpc20WebSocketServer(); |
| 75 | + FakeWebSocketClient *pFakeClient = new FakeWebSocketClient(); |
| 76 | + WsjcppJsonRpc20WebSocketClient *pClient = pFakeClient; |
| 77 | + |
| 78 | + WsjcppJsonRpc20HandlerBase *pHandlerServerApi = WsjcppJsonRpc20::findJsonRpc20Handler("game_create"); |
| 79 | + |
| 80 | + // parseIncomeData empty - wrong json |
| 81 | + { |
| 82 | + WsjcppJsonRpc20Request *pRequest0 = new WsjcppJsonRpc20Request(pClient, pWebSocketServer); |
| 83 | + compareB(bTestSuccess, "parseIncomeData empty", pRequest0->parseIncomeData(""), false); |
| 84 | + std::string sResponse0 = pFakeClient->getLastTextMessage(); |
| 85 | + nlohmann::json jsonResponse0 = nlohmann::json::parse(sResponse0); |
| 86 | + std::string sErrorMessage = jsonResponse0["error"]["message"]; |
| 87 | + compareS(bTestSuccess, "parseIncomeData empty. error.message", sErrorMessage, "WRONG_JSON"); |
| 88 | + int nErrorCode = jsonResponse0["error"]["code"]; |
| 89 | + compareN(bTestSuccess, "parseIncomeData empty. error.code", nErrorCode, 400); |
| 90 | + std::string sId = jsonResponse0["id"]; |
| 91 | + compareS(bTestSuccess, "parseIncomeData empty. id", sId, "unknown_id"); |
| 92 | + std::string sMethod = jsonResponse0["method"]; |
| 93 | + compareS(bTestSuccess, "parseIncomeData empty. method", sMethod, "unknown_method"); |
| 94 | + } |
| 95 | + |
| 96 | + // missing 'id' in request |
| 97 | + { |
| 98 | + nlohmann::json jsonRequest; |
| 99 | + jsonRequest["jsonrpc"] = "2.0"; |
| 100 | + jsonRequest["method"] = "game_create"; |
| 101 | + std::string sRequest = jsonRequest.dump(); |
| 102 | + WsjcppJsonRpc20Request *pRequest = new WsjcppJsonRpc20Request(pClient, pWebSocketServer); |
| 103 | + compareB(bTestSuccess, "parseIncomeData missing id", pRequest->parseIncomeData(sRequest), false); |
| 104 | + std::string sResponse = pFakeClient->getLastTextMessage(); |
| 105 | + nlohmann::json jsonResponse = nlohmann::json::parse(sResponse); |
| 106 | + std::string sErrorMessage = jsonResponse["error"]["message"]; |
| 107 | + compareS(bTestSuccess, "parseIncomeData missing id. error.message", sErrorMessage, "NOT_FOUND_ID_IN_REQUEST"); |
| 108 | + int nErrorCode = jsonResponse["error"]["code"]; |
| 109 | + compareN(bTestSuccess, "parseIncomeData missing id. error.code", nErrorCode, 400); |
| 110 | + std::string sId = jsonResponse["id"]; |
| 111 | + compareS(bTestSuccess, "parseIncomeData missing id. id", sId, "unknown_id"); |
| 112 | + std::string sMethod = jsonResponse["method"]; |
| 113 | + compareS(bTestSuccess, "parseIncomeData missing id. method", sMethod, "game_create"); |
| 114 | + } |
| 115 | + |
| 116 | + // missing 'method' in request |
| 117 | + { |
| 118 | + nlohmann::json jsonRequest; |
| 119 | + jsonRequest["jsonrpc"] = "2.0"; |
| 120 | + jsonRequest["id"] = "id1"; |
| 121 | + std::string sRequest = jsonRequest.dump(); |
| 122 | + WsjcppJsonRpc20Request *pRequest = new WsjcppJsonRpc20Request(pClient, pWebSocketServer); |
| 123 | + compareB(bTestSuccess, "parseIncomeData missing method", pRequest->parseIncomeData(sRequest), false); |
| 124 | + std::string sResponse = pFakeClient->getLastTextMessage(); |
| 125 | + // std::cout << sResponse << std::endl; |
| 126 | + nlohmann::json jsonResponse = nlohmann::json::parse(sResponse); |
| 127 | + std::string sErrorMessage = jsonResponse["error"]["message"]; |
| 128 | + compareS(bTestSuccess, "parseIncomeData missing method. error. message", sErrorMessage, "NOT_FOUND_METHOD_IN_REQUEST"); |
| 129 | + int nErrorCode = jsonResponse["error"]["code"]; |
| 130 | + compareN(bTestSuccess, "parseIncomeData missing method. error. code", nErrorCode, 400); |
| 131 | + std::string sId = jsonResponse["id"]; |
| 132 | + compareS(bTestSuccess, "parseIncomeData missing method. id", sId, "id1"); |
| 133 | + std::string sMethod = jsonResponse["method"]; |
| 134 | + compareS(bTestSuccess, "parseIncomeData missing method. method", sMethod, "unknown_method"); |
| 135 | + } |
| 136 | + |
| 137 | + // missing 'jsonrpc' in request |
| 138 | + { |
| 139 | + nlohmann::json jsonRequest; |
| 140 | + jsonRequest["id"] = "id1"; |
| 141 | + jsonRequest["method"] = "game_create"; |
| 142 | + std::string sRequest = jsonRequest.dump(); |
| 143 | + WsjcppJsonRpc20Request *pRequest = new WsjcppJsonRpc20Request(pClient, pWebSocketServer); |
| 144 | + compareB(bTestSuccess, "parseIncomeData missing jsonrpc", pRequest->parseIncomeData(sRequest), false); |
| 145 | + std::string sResponse = pFakeClient->getLastTextMessage(); |
| 146 | + // std::cout << sResponse << std::endl; |
| 147 | + nlohmann::json jsonResponse0 = nlohmann::json::parse(sResponse); |
| 148 | + std::string sErrorMessage = jsonResponse0["error"]["message"]; |
| 149 | + compareS(bTestSuccess, "parseIncomeData missing jsonrpc. error.message", sErrorMessage, "NOT_FOUND_FIELD_JSONRPC_IN_REQUEST"); |
| 150 | + int nErrorCode = jsonResponse0["error"]["code"]; |
| 151 | + compareN(bTestSuccess, "parseIncomeData missing jsonrpc. error.code", nErrorCode, 400); |
| 152 | + std::string sId = jsonResponse0["id"]; |
| 153 | + compareS(bTestSuccess, "parseIncomeData missing jsonrpc. id", sId, "id1"); |
| 154 | + std::string sMethod = jsonResponse0["method"]; |
| 155 | + compareS(bTestSuccess, "parseIncomeData missing jsonrpc. method", sMethod, "game_create"); |
| 156 | + } |
| 157 | + |
| 158 | + // field 'jsonrpc' is non string in request |
| 159 | + { |
| 160 | + nlohmann::json jsonRequest; |
| 161 | + jsonRequest["id"] = "id1"; |
| 162 | + jsonRequest["jsonrpc"] = 2; |
| 163 | + jsonRequest["method"] = "game_create"; |
| 164 | + std::string sRequest = jsonRequest.dump(); |
| 165 | + WsjcppJsonRpc20Request *pRequest = new WsjcppJsonRpc20Request(pClient, pWebSocketServer); |
| 166 | + compareB(bTestSuccess, "parseIncomeData jsonrpc is non string", pRequest->parseIncomeData(sRequest), false); |
| 167 | + std::string sResponse = pFakeClient->getLastTextMessage(); |
| 168 | + // std::cout << sResponse << std::endl; |
| 169 | + nlohmann::json jsonResponse0 = nlohmann::json::parse(sResponse); |
| 170 | + std::string sErrorMessage = jsonResponse0["error"]["message"]; |
| 171 | + compareS(bTestSuccess, "parseIncomeData jsonrpc is non string. error.message", sErrorMessage, "FIELD_JSONRPC_EXPECTED_AS_STRING_IN_REQUEST"); |
| 172 | + int nErrorCode = jsonResponse0["error"]["code"]; |
| 173 | + compareN(bTestSuccess, "parseIncomeData jsonrpc is non string. error.code", nErrorCode, 400); |
| 174 | + std::string sId = jsonResponse0["id"]; |
| 175 | + compareS(bTestSuccess, "parseIncomeData jsonrpc is non string. id", sId, "id1"); |
| 176 | + std::string sMethod = jsonResponse0["method"]; |
| 177 | + compareS(bTestSuccess, "parseIncomeData jsonrpc is non string. method", sMethod, "game_create"); |
| 178 | + } |
| 179 | + |
| 180 | + // field 'jsonrpc' is no "2.0" in request |
| 181 | + { |
| 182 | + nlohmann::json jsonRequest; |
| 183 | + jsonRequest["id"] = "id1"; |
| 184 | + jsonRequest["jsonrpc"] = "2.1"; |
| 185 | + jsonRequest["method"] = "game_create"; |
| 186 | + std::string sRequest = jsonRequest.dump(); |
| 187 | + WsjcppJsonRpc20Request *pRequest = new WsjcppJsonRpc20Request(pClient, pWebSocketServer); |
| 188 | + compareB(bTestSuccess, "parseIncomeData jsonrpc is no 2.0", pRequest->parseIncomeData(sRequest), false); |
| 189 | + std::string sResponse = pFakeClient->getLastTextMessage(); |
| 190 | + // std::cout << sResponse << std::endl; |
| 191 | + nlohmann::json jsonResponse0 = nlohmann::json::parse(sResponse); |
| 192 | + std::string sErrorMessage = jsonResponse0["error"]["message"]; |
| 193 | + compareS(bTestSuccess, "parseIncomeData jsonrpc is no 2.0. error.message", sErrorMessage, "FIELD_JSONRPC_EXPECTED_2_DOT_0_IN_REQUEST"); |
| 194 | + int nErrorCode = jsonResponse0["error"]["code"]; |
| 195 | + compareN(bTestSuccess, "parseIncomeData jsonrpc is no 2.0. error.code", nErrorCode, 400); |
| 196 | + std::string sId = jsonResponse0["id"]; |
| 197 | + compareS(bTestSuccess, "parseIncomeData jsonrpc is no 2.0. id", sId, "id1"); |
| 198 | + std::string sMethod = jsonResponse0["method"]; |
| 199 | + compareS(bTestSuccess, "parseIncomeData jsonrpc is no 2.0. method", sMethod, "game_create"); |
| 200 | + } |
| 201 | + |
| 202 | + |
| 203 | + |
| 204 | + nlohmann::json requestJson; |
| 205 | + requestJson["jsonrpc"] = "2.0"; |
| 206 | + requestJson["method"] = "game_create"; |
| 207 | + requestJson["id"] = "id1"; |
| 208 | + requestJson["params"] = nlohmann::json::array(); |
| 209 | + |
| 210 | + // pRequest0->parseIncomeData(requestJson.dump()); |
| 211 | + |
| 212 | + // pHandlerServerApi->handle(pRequest0); |
| 213 | + |
| 214 | + return bTestSuccess; |
| 215 | +} |
| 216 | + |
0 commit comments