2017-09-26 01:02:09 +02:00
|
|
|
unit TestServerControllerJSONRPCU;
|
|
|
|
|
|
|
|
interface
|
|
|
|
|
|
|
|
uses
|
2022-07-05 16:18:03 +02:00
|
|
|
MVCFramework, MVCFramework.Commons, MVCFramework.JSONRPC, JsonDataObjects,
|
|
|
|
BusinessObjectsU;
|
2017-09-26 01:02:09 +02:00
|
|
|
|
|
|
|
type
|
|
|
|
TTestJSONRPCController = class(TMVCJSONRPCController)
|
|
|
|
public
|
2021-02-23 18:00:32 +01:00
|
|
|
[MVCInheritable]
|
2020-05-02 16:39:32 +02:00
|
|
|
function Subtract(Value1, Value2: Int64): Integer;
|
2021-02-23 18:00:32 +01:00
|
|
|
[MVCInheritable]
|
2017-09-26 01:02:09 +02:00
|
|
|
procedure MyNotify;
|
2021-02-23 18:00:32 +01:00
|
|
|
[MVCInheritable]
|
2020-08-06 17:40:56 +02:00
|
|
|
function MyRequest: Boolean;
|
2021-02-23 18:00:32 +01:00
|
|
|
[MVCInheritable]
|
2020-05-02 16:39:32 +02:00
|
|
|
function Add(Value1, Value2, Value3: Int64): TJsonObject;
|
2021-02-23 18:00:32 +01:00
|
|
|
[MVCInheritable]
|
2017-09-28 00:14:34 +02:00
|
|
|
function GetListFromTo(aFrom, aTo: Int64): TJsonArray;
|
2021-02-23 18:00:32 +01:00
|
|
|
[MVCInheritable]
|
2017-09-28 00:14:34 +02:00
|
|
|
function MultiplyString(aString: string; Multiplier: Int64): string;
|
2022-07-05 16:18:03 +02:00
|
|
|
|
2017-09-26 01:02:09 +02:00
|
|
|
end;
|
|
|
|
|
2021-02-23 18:00:32 +01:00
|
|
|
[MVCJSONRPCAllowGET]
|
|
|
|
TTestJSONRPCControllerWithGet = class(TTestJSONRPCController)
|
|
|
|
|
|
|
|
end;
|
|
|
|
|
2019-01-18 18:11:27 +01:00
|
|
|
TTestJSONRPCClass = class(TObject)
|
|
|
|
public
|
2021-02-23 18:00:32 +01:00
|
|
|
[MVCInheritable]
|
2020-05-02 16:39:32 +02:00
|
|
|
function Subtract(Value1, Value2: Int64): Integer;
|
2021-02-23 18:00:32 +01:00
|
|
|
[MVCInheritable]
|
2019-01-18 18:11:27 +01:00
|
|
|
procedure MyNotify;
|
2021-02-23 18:00:32 +01:00
|
|
|
[MVCInheritable]
|
2020-05-02 16:39:32 +02:00
|
|
|
function Add(Value1, Value2, Value3: Int64): TJsonObject;
|
2021-02-23 18:00:32 +01:00
|
|
|
[MVCInheritable]
|
2019-01-18 18:11:27 +01:00
|
|
|
function GetListFromTo(aFrom, aTo: Int64): TJsonArray;
|
2021-02-23 18:00:32 +01:00
|
|
|
[MVCInheritable]
|
2019-01-18 18:11:27 +01:00
|
|
|
function MultiplyString(aString: string; Multiplier: Int64): string;
|
2021-02-23 18:00:32 +01:00
|
|
|
[MVCInheritable]
|
2019-01-18 18:11:27 +01:00
|
|
|
function AddTimeToDateTime(aDateTime: TDateTime; aTime: TTime): TDateTime;
|
2022-07-05 16:18:03 +02:00
|
|
|
|
2022-08-10 17:57:59 +02:00
|
|
|
//enums support
|
|
|
|
[MVCInheritable]
|
|
|
|
function ProcessEnums(Value1: TEnumTest; Value2: TEnumTest): TEnumTest;
|
|
|
|
|
|
|
|
//sets support
|
|
|
|
[MVCInheritable]
|
|
|
|
function ProcessSets(Value1: TSetOfEnumTest; Value2: TEnumTest): TSetOfEnumTest;
|
|
|
|
|
2022-07-05 16:18:03 +02:00
|
|
|
//records support
|
|
|
|
[MVCInheritable]
|
|
|
|
function GetSingleRecord: TSimpleRecord;
|
|
|
|
[MVCInheritable]
|
|
|
|
function GetArrayOfRecords: TArray<TSimpleRecord>;
|
|
|
|
[MVCInheritable]
|
|
|
|
function EchoSingleRecord(const SimpleRecord: TSimpleRecord): TSimpleRecord;
|
2022-07-07 15:09:31 +02:00
|
|
|
[MVCInheritable]
|
|
|
|
function GetSingleComplexRecord: TComplexRecord;
|
2022-07-20 00:27:55 +02:00
|
|
|
[MVCInheritable]
|
2022-07-23 16:36:20 +02:00
|
|
|
function EchoSingleComplexRecord(const ComplexRecord: TComplexRecord): TComplexRecord;
|
|
|
|
[MVCInheritable]
|
2022-07-20 00:27:55 +02:00
|
|
|
function EchoArrayOfRecords(const ComplexRecordArray: TComplexRecordArray): TComplexRecordArray;
|
2019-01-18 18:11:27 +01:00
|
|
|
end;
|
|
|
|
|
2021-02-23 18:00:32 +01:00
|
|
|
[MVCJSONRPCAllowGET]
|
|
|
|
TTestJSONRPCClassWithGET = class(TTestJSONRPCClass)
|
|
|
|
|
|
|
|
end;
|
|
|
|
|
2020-08-05 09:50:06 +02:00
|
|
|
TTestJSONRPCHookClass = class(TObject)
|
|
|
|
private
|
|
|
|
fJSONReq: TJsonObject;
|
|
|
|
fHistory: string;
|
|
|
|
fJSONRPCKind: TJSONRPCRequestType;
|
|
|
|
public
|
|
|
|
procedure OnBeforeRoutingHook(const Context: TWebContext; const JSON: TJsonObject);
|
|
|
|
procedure OnBeforeCallHook(const Context: TWebContext; const JSON: TJsonObject);
|
|
|
|
procedure OnAfterCallHook(const Context: TWebContext; const JSON: TJsonObject);
|
2021-02-23 18:00:32 +01:00
|
|
|
[MVCInheritable]
|
2020-08-05 09:50:06 +02:00
|
|
|
function error_OnBeforeRoutingHook: Boolean;
|
2021-02-23 18:00:32 +01:00
|
|
|
[MVCInheritable]
|
2020-08-05 09:50:06 +02:00
|
|
|
function error_OnBeforeCallHook: Boolean;
|
2021-02-23 18:00:32 +01:00
|
|
|
[MVCInheritable]
|
2020-08-05 09:50:06 +02:00
|
|
|
function error_OnAfterCallHook: Boolean;
|
2021-02-23 18:00:32 +01:00
|
|
|
[MVCInheritable]
|
2020-08-05 09:50:06 +02:00
|
|
|
procedure Notif1;
|
2021-02-23 18:00:32 +01:00
|
|
|
[MVCInheritable]
|
2020-08-06 17:40:56 +02:00
|
|
|
procedure NotifWithError;
|
2021-02-23 18:00:32 +01:00
|
|
|
[MVCInheritable]
|
2020-08-05 09:50:06 +02:00
|
|
|
function Request1: string;
|
2021-02-23 18:00:32 +01:00
|
|
|
[MVCInheritable]
|
2020-08-05 09:50:06 +02:00
|
|
|
function RequestWithError: string;
|
|
|
|
end;
|
|
|
|
|
2021-02-23 18:00:32 +01:00
|
|
|
[MVCJSONRPCAllowGET]
|
|
|
|
TTestJSONRPCHookClassWithGet = class(TTestJSONRPCHookClass)
|
|
|
|
|
|
|
|
end;
|
|
|
|
|
2017-09-26 01:02:09 +02:00
|
|
|
implementation
|
|
|
|
|
|
|
|
uses
|
|
|
|
System.SysUtils, MVCFramework.Logger, System.StrUtils;
|
|
|
|
|
|
|
|
{ TTestJSONRPCController }
|
|
|
|
|
2020-05-02 16:39:32 +02:00
|
|
|
function TTestJSONRPCController.Add(Value1, Value2, Value3: Int64): TJsonObject;
|
2017-09-26 01:02:09 +02:00
|
|
|
begin
|
|
|
|
Result := TJsonObject.Create;
|
2020-05-02 16:39:32 +02:00
|
|
|
Result.I['res'] := Value1 + Value2 + Value3;
|
2017-09-28 00:14:34 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
function TTestJSONRPCController.GetListFromTo(aFrom, aTo: Int64): TJsonArray;
|
|
|
|
var
|
2019-08-02 12:32:23 +02:00
|
|
|
I: Cardinal;
|
2017-09-28 00:14:34 +02:00
|
|
|
begin
|
|
|
|
Result := TJsonArray.Create;
|
|
|
|
for I := aFrom to aTo do
|
|
|
|
Result.Add(I);
|
|
|
|
end;
|
|
|
|
|
2022-07-07 15:09:31 +02:00
|
|
|
function TTestJSONRPCClass.GetSingleComplexRecord: TComplexRecord;
|
|
|
|
begin
|
|
|
|
Result := TComplexRecord.Create;
|
|
|
|
end;
|
|
|
|
|
2022-07-05 16:18:03 +02:00
|
|
|
function TTestJSONRPCClass.GetSingleRecord: TSimpleRecord;
|
|
|
|
begin
|
|
|
|
Result := TSimpleRecord.Create;
|
|
|
|
end;
|
|
|
|
|
2021-02-23 18:00:32 +01:00
|
|
|
function TTestJSONRPCController.MultiplyString(aString: string; Multiplier: Int64): string;
|
2017-09-28 00:14:34 +02:00
|
|
|
var
|
|
|
|
I: Integer;
|
|
|
|
begin
|
|
|
|
Result := aString;
|
|
|
|
for I := 2 to Multiplier do
|
|
|
|
begin
|
|
|
|
Result := Result + aString;
|
|
|
|
end;
|
2017-09-26 01:02:09 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TTestJSONRPCController.MyNotify;
|
|
|
|
begin
|
|
|
|
// this is a notify with no parameters and no result code
|
|
|
|
Self.ClassName;
|
|
|
|
end;
|
|
|
|
|
2020-08-06 17:40:56 +02:00
|
|
|
function TTestJSONRPCController.MyRequest: Boolean;
|
|
|
|
begin
|
|
|
|
Result := True;
|
|
|
|
end;
|
|
|
|
|
2020-05-02 16:39:32 +02:00
|
|
|
function TTestJSONRPCController.Subtract(Value1, Value2: Int64): Integer;
|
2017-09-26 01:02:09 +02:00
|
|
|
begin
|
2020-05-02 16:39:32 +02:00
|
|
|
Result := Value1 - Value2;
|
2017-09-26 01:02:09 +02:00
|
|
|
end;
|
|
|
|
|
2019-01-18 18:11:27 +01:00
|
|
|
{ TTestJSONRPCClass }
|
|
|
|
|
2020-05-02 16:39:32 +02:00
|
|
|
function TTestJSONRPCClass.Add(Value1, Value2, Value3: Int64): TJsonObject;
|
2019-01-18 18:11:27 +01:00
|
|
|
begin
|
|
|
|
Result := TJsonObject.Create;
|
2020-05-02 16:39:32 +02:00
|
|
|
Result.I['res'] := Value1 + Value2 + Value3;
|
2019-01-18 18:11:27 +01:00
|
|
|
end;
|
|
|
|
|
2021-02-23 18:00:32 +01:00
|
|
|
function TTestJSONRPCClass.AddTimeToDateTime(aDateTime: TDateTime; aTime: TTime): TDateTime;
|
2019-01-18 18:11:27 +01:00
|
|
|
begin
|
|
|
|
Result := aDateTime + aTime;
|
|
|
|
end;
|
|
|
|
|
2022-07-20 00:27:55 +02:00
|
|
|
function TTestJSONRPCClass.EchoArrayOfRecords(
|
|
|
|
const ComplexRecordArray: TComplexRecordArray): TComplexRecordArray;
|
|
|
|
begin
|
|
|
|
Result := ComplexRecordArray;
|
|
|
|
end;
|
|
|
|
|
2022-07-23 16:36:20 +02:00
|
|
|
function TTestJSONRPCClass.EchoSingleComplexRecord(
|
|
|
|
const ComplexRecord: TComplexRecord): TComplexRecord;
|
|
|
|
begin
|
|
|
|
Result := ComplexRecord;
|
|
|
|
end;
|
|
|
|
|
2022-07-05 16:18:03 +02:00
|
|
|
function TTestJSONRPCClass.EchoSingleRecord(
|
|
|
|
const SimpleRecord: TSimpleRecord): TSimpleRecord;
|
|
|
|
begin
|
|
|
|
Result := SimpleRecord;
|
|
|
|
end;
|
|
|
|
|
|
|
|
function TTestJSONRPCClass.GetArrayOfRecords: TArray<TSimpleRecord>;
|
|
|
|
begin
|
|
|
|
SetLength(Result, 3);
|
|
|
|
Result[0] := TSimpleRecord.Create;
|
|
|
|
Result[1] := TSimpleRecord.Create;
|
|
|
|
Result[2] := TSimpleRecord.Create;
|
|
|
|
|
|
|
|
Result[0].IntegerProperty := 0;
|
|
|
|
Result[1].IntegerProperty := 1;
|
|
|
|
Result[2].IntegerProperty := 2;
|
|
|
|
end;
|
|
|
|
|
2019-01-18 18:11:27 +01:00
|
|
|
function TTestJSONRPCClass.GetListFromTo(aFrom, aTo: Int64): TJsonArray;
|
|
|
|
var
|
2019-08-02 12:32:23 +02:00
|
|
|
I: Cardinal;
|
2019-01-18 18:11:27 +01:00
|
|
|
begin
|
|
|
|
Result := TJsonArray.Create;
|
|
|
|
for I := aFrom to aTo do
|
|
|
|
Result.Add(I);
|
|
|
|
end;
|
|
|
|
|
2021-02-23 18:00:32 +01:00
|
|
|
function TTestJSONRPCClass.MultiplyString(aString: string; Multiplier: Int64): string;
|
2019-01-18 18:11:27 +01:00
|
|
|
var
|
|
|
|
I: Integer;
|
|
|
|
begin
|
|
|
|
Result := aString;
|
|
|
|
for I := 2 to Multiplier do
|
|
|
|
begin
|
|
|
|
Result := Result + aString;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TTestJSONRPCClass.MyNotify;
|
|
|
|
begin
|
|
|
|
// this is a notify with no parameters and no result code
|
|
|
|
Self.ClassName;
|
|
|
|
end;
|
|
|
|
|
2022-08-10 17:57:59 +02:00
|
|
|
function TTestJSONRPCClass.ProcessEnums(Value1, Value2: TEnumTest): TEnumTest;
|
|
|
|
begin
|
|
|
|
Result := TEnumTest((Ord(Value1) + Ord(Value2)) mod 3);
|
|
|
|
end;
|
|
|
|
|
|
|
|
function TTestJSONRPCClass.ProcessSets(Value1: TSetOfEnumTest;
|
|
|
|
Value2: TEnumTest): TSetOfEnumTest;
|
|
|
|
begin
|
|
|
|
Include(Value1, Value2);
|
|
|
|
Result := Value1;
|
|
|
|
end;
|
|
|
|
|
2020-05-02 16:39:32 +02:00
|
|
|
function TTestJSONRPCClass.Subtract(Value1, Value2: Int64): Integer;
|
2019-01-18 18:11:27 +01:00
|
|
|
begin
|
2020-05-02 16:39:32 +02:00
|
|
|
Result := Value1 - Value2;
|
2019-01-18 18:11:27 +01:00
|
|
|
end;
|
|
|
|
|
2020-08-05 09:50:06 +02:00
|
|
|
{ TTestJSONRPCHookClass }
|
|
|
|
|
|
|
|
function TTestJSONRPCHookClass.error_OnAfterCallHook: Boolean;
|
|
|
|
begin
|
|
|
|
// do nothing
|
2020-08-06 17:40:56 +02:00
|
|
|
Result := True;
|
2020-08-05 09:50:06 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
function TTestJSONRPCHookClass.error_OnBeforeCallHook: Boolean;
|
|
|
|
begin
|
|
|
|
// do nothing
|
2020-08-06 17:40:56 +02:00
|
|
|
Result := True;
|
2020-08-05 09:50:06 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
function TTestJSONRPCHookClass.error_OnBeforeRoutingHook: Boolean;
|
|
|
|
begin
|
|
|
|
// do nothing
|
2020-08-06 17:40:56 +02:00
|
|
|
Result := True;
|
2020-08-05 09:50:06 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TTestJSONRPCHookClass.Notif1;
|
|
|
|
begin
|
|
|
|
// do nothing
|
|
|
|
end;
|
|
|
|
|
2020-08-06 17:40:56 +02:00
|
|
|
procedure TTestJSONRPCHookClass.NotifWithError;
|
|
|
|
begin
|
|
|
|
raise Exception.Create('BOOM NOTIF');
|
|
|
|
end;
|
|
|
|
|
2020-08-05 09:50:06 +02:00
|
|
|
procedure TTestJSONRPCHookClass.OnAfterCallHook(const Context: TWebContext; const JSON: TJsonObject);
|
|
|
|
begin
|
2020-08-06 00:28:54 +02:00
|
|
|
try
|
|
|
|
if SameText(fJSONReq.S['method'], 'error_OnAfterCallHook') then
|
|
|
|
raise Exception.Create('error_OnAfterCallHook');
|
|
|
|
|
|
|
|
fHistory := fHistory + '|OnAfterCallHook';
|
|
|
|
|
|
|
|
// do nothing
|
|
|
|
if fJSONRPCKind = TJSONRPCRequestType.Request then
|
|
|
|
begin
|
|
|
|
Assert(Assigned(JSON));
|
|
|
|
LogD('TTestJSONRPCHookClass.OnAfterCallHook: ' + JSON.ToJSON());
|
|
|
|
end
|
|
|
|
else
|
|
|
|
begin
|
2020-08-06 17:40:56 +02:00
|
|
|
if Assigned(JSON) then
|
|
|
|
Assert(JSON.Contains('error'), 'ERROR! Notification has a response but is not an error');
|
2020-08-06 00:28:54 +02:00
|
|
|
LogD('TTestJSONRPCHookClass.OnAfterCallHook: Param is nil');
|
|
|
|
end;
|
2020-08-06 17:40:56 +02:00
|
|
|
if Assigned(JSON) then
|
|
|
|
if JSON.Contains('error') then
|
|
|
|
fHistory := fHistory + '|error';
|
2020-08-06 00:28:54 +02:00
|
|
|
Context.Response.CustomHeaders.Values['x-history'] := fHistory;
|
|
|
|
finally
|
|
|
|
FreeAndNil(fJSONReq);
|
2020-08-05 09:50:06 +02:00
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TTestJSONRPCHookClass.OnBeforeCallHook(const Context: TWebContext; const JSON: TJsonObject);
|
|
|
|
begin
|
|
|
|
if SameText(JSON.S['method'], 'error_OnBeforeCallHook') then
|
|
|
|
raise Exception.Create('error_OnBeforeCallHook');
|
|
|
|
|
|
|
|
fHistory := fHistory + '|OnBeforeCallHook';
|
|
|
|
Assert(Assigned(JSON), 'JSON not assigned in OnBeforeCallHook');
|
|
|
|
LogD('TTestJSONRPCHookClass.OnBeforeCallHook: ' + JSON.ToJSON());
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TTestJSONRPCHookClass.OnBeforeRoutingHook(const Context: TWebContext; const JSON: TJsonObject);
|
|
|
|
begin
|
2021-04-04 18:16:27 +02:00
|
|
|
fJSONReq := JSON.Clone as TJsonObject;
|
2020-08-05 09:50:06 +02:00
|
|
|
|
|
|
|
if SameText(JSON.S['method'], 'error_OnBeforeRoutingHook') then
|
|
|
|
raise Exception.Create('error_OnBeforeRoutingHook');
|
|
|
|
|
|
|
|
fHistory := 'OnBeforeRoutingHook';
|
|
|
|
// do nothing
|
|
|
|
Assert(Assigned(JSON), 'JSON not assigned in OnBeforeRoutingHook');
|
|
|
|
LogD('TTestJSONRPCHookClass.OnBeforeRoutingHook: ' + JSON.ToJSON());
|
|
|
|
if JSON.Contains('id') then
|
|
|
|
fJSONRPCKind := TJSONRPCRequestType.Request
|
|
|
|
else
|
|
|
|
fJSONRPCKind := TJSONRPCRequestType.Notification;
|
|
|
|
end;
|
|
|
|
|
|
|
|
function TTestJSONRPCHookClass.Request1: string;
|
|
|
|
begin
|
|
|
|
Result := 'empty';
|
|
|
|
end;
|
|
|
|
|
|
|
|
function TTestJSONRPCHookClass.RequestWithError: string;
|
|
|
|
begin
|
2020-08-06 17:40:56 +02:00
|
|
|
raise Exception.Create('BOOM REQUEST');
|
2020-08-05 09:50:06 +02:00
|
|
|
end;
|
|
|
|
|
2017-09-26 01:02:09 +02:00
|
|
|
end.
|