2016-09-18 19:19:23 +02:00
|
|
|
|
// ***************************************************************************
|
|
|
|
|
//
|
|
|
|
|
// Delphi MVC Framework
|
|
|
|
|
//
|
2019-01-08 12:48:27 +01:00
|
|
|
|
// Copyright (c) 2010-2019 Daniele Teti and the DMVCFramework Team
|
2016-09-18 19:19:23 +02:00
|
|
|
|
//
|
|
|
|
|
// https://github.com/danieleteti/delphimvcframework
|
|
|
|
|
//
|
|
|
|
|
// ***************************************************************************
|
|
|
|
|
//
|
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
|
//
|
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
//
|
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
|
// limitations under the License.
|
|
|
|
|
//
|
|
|
|
|
// *************************************************************************** }
|
|
|
|
|
|
2015-12-22 12:29:25 +01:00
|
|
|
|
unit LiveServerTestU;
|
|
|
|
|
|
|
|
|
|
interface
|
|
|
|
|
|
|
|
|
|
uses
|
2017-08-20 02:36:22 +02:00
|
|
|
|
DUnitX.TestFramework,
|
2018-05-16 19:46:29 +02:00
|
|
|
|
MVCFramework.RESTClient,
|
2019-05-09 20:53:52 +02:00
|
|
|
|
MVCFramework.JSONRPC.Client,
|
2019-06-25 17:32:30 +02:00
|
|
|
|
System.DateUtils,
|
|
|
|
|
System.Hash;
|
2015-12-22 12:29:25 +01:00
|
|
|
|
|
2017-04-14 16:43:31 +02:00
|
|
|
|
const
|
|
|
|
|
|
2018-05-16 19:46:29 +02:00
|
|
|
|
{$IFDEF LINUX_SERVER}
|
2017-10-16 22:57:27 +02:00
|
|
|
|
TEST_SERVER_ADDRESS = '192.168.1.8';
|
2017-04-14 16:43:31 +02:00
|
|
|
|
|
2018-05-16 19:46:29 +02:00
|
|
|
|
{$ELSE}
|
2017-04-14 16:43:31 +02:00
|
|
|
|
TEST_SERVER_ADDRESS = '127.0.0.1';
|
|
|
|
|
|
2018-05-16 19:46:29 +02:00
|
|
|
|
{$ENDIF}
|
2017-04-14 16:43:31 +02:00
|
|
|
|
|
2019-05-09 20:53:52 +02:00
|
|
|
|
|
2015-12-22 12:29:25 +01:00
|
|
|
|
type
|
2017-08-21 15:31:31 +02:00
|
|
|
|
|
2017-08-20 02:36:22 +02:00
|
|
|
|
TBaseServerTest = class(TObject)
|
2015-12-22 12:29:25 +01:00
|
|
|
|
protected
|
|
|
|
|
RESTClient: TRESTClient;
|
|
|
|
|
procedure DoLoginWith(UserName: string);
|
|
|
|
|
procedure DoLogout;
|
|
|
|
|
|
2017-08-21 15:31:31 +02:00
|
|
|
|
public
|
2017-08-20 02:36:22 +02:00
|
|
|
|
[SetUp]
|
|
|
|
|
procedure SetUp;
|
|
|
|
|
[TearDown]
|
|
|
|
|
procedure TearDown;
|
2015-12-22 12:29:25 +01:00
|
|
|
|
|
|
|
|
|
end;
|
|
|
|
|
|
2019-02-24 20:43:06 +01:00
|
|
|
|
[TestFixture]
|
2015-12-22 12:29:25 +01:00
|
|
|
|
TServerTest = class(TBaseServerTest)
|
2019-05-10 00:46:03 +02:00
|
|
|
|
private
|
|
|
|
|
|
2017-08-21 15:31:31 +02:00
|
|
|
|
public
|
2018-08-08 17:11:45 +02:00
|
|
|
|
[Test]
|
2019-02-24 20:43:06 +01:00
|
|
|
|
[TestCase('request url /fault', '/fault')]
|
|
|
|
|
[TestCase('request url /fault2', '/fault2')]
|
2018-08-08 17:11:45 +02:00
|
|
|
|
procedure TestControllerWithExceptionInCreate(const URLSegment: string);
|
|
|
|
|
|
2017-08-21 15:31:31 +02:00
|
|
|
|
[Test]
|
2015-12-22 12:29:25 +01:00
|
|
|
|
procedure TestReqWithParams;
|
2017-11-23 17:29:51 +01:00
|
|
|
|
|
|
|
|
|
// URL_MAPPED_PARAMS_ALLOWED_CHARS = ' <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>@\[\]\{\}\(\)\=;&#\.\_\,%\w\d\x2D\x3A';
|
|
|
|
|
[Test]
|
|
|
|
|
[TestCase('1', ' <20>,<2C>')]
|
|
|
|
|
[TestCase('2', '<27>,<2C>,<2C>')]
|
2019-02-24 20:43:06 +01:00
|
|
|
|
[TestCase('3', '<27>,@,[')]
|
|
|
|
|
[TestCase('4', '],{,}')]
|
|
|
|
|
[TestCase('5', '(,),\')]
|
|
|
|
|
[TestCase('6', '=,;,&')]
|
|
|
|
|
[TestCase('7', '#,.,_')]
|
|
|
|
|
[TestCase('8', '%, , ')]
|
2017-11-23 17:29:51 +01:00
|
|
|
|
procedure TestReqWithURLMappedParams(const par1, par2, par3: string);
|
2017-08-21 15:31:31 +02:00
|
|
|
|
[Test]
|
2015-12-22 12:29:25 +01:00
|
|
|
|
procedure TestPOSTWithParamsAndJSONBody;
|
2017-08-21 15:31:31 +02:00
|
|
|
|
[Test]
|
2015-12-22 12:29:25 +01:00
|
|
|
|
procedure TestPOSTWithObjectJSONBody;
|
2017-08-21 15:31:31 +02:00
|
|
|
|
[Test]
|
2019-06-25 17:32:30 +02:00
|
|
|
|
procedure TestCustomerEcho;
|
|
|
|
|
[Test]
|
2019-05-10 00:46:03 +02:00
|
|
|
|
procedure TestXHTTPMethodOverride_POST_as_PUT;
|
|
|
|
|
[Test]
|
2015-12-22 12:29:25 +01:00
|
|
|
|
procedure TestPUTWithParamsAndJSONBody;
|
2017-08-21 15:31:31 +02:00
|
|
|
|
[Test]
|
2016-04-24 19:08:21 +02:00
|
|
|
|
procedure TestCookies;
|
2017-08-21 15:31:31 +02:00
|
|
|
|
[Test]
|
2016-06-28 13:42:14 +02:00
|
|
|
|
procedure TestSessionWithLogin;
|
2017-08-21 15:31:31 +02:00
|
|
|
|
[Test]
|
2015-12-22 12:29:25 +01:00
|
|
|
|
procedure TestSession;
|
2017-08-21 15:31:31 +02:00
|
|
|
|
[Test]
|
2016-04-03 22:35:27 +02:00
|
|
|
|
procedure TestInvalidateSession;
|
2017-08-21 15:31:31 +02:00
|
|
|
|
[Test]
|
2015-12-22 12:29:25 +01:00
|
|
|
|
procedure TestAsynchRequestPOST;
|
2017-08-21 15:31:31 +02:00
|
|
|
|
[Test]
|
2015-12-22 12:29:25 +01:00
|
|
|
|
procedure TestAsynchRequestPUT;
|
2017-08-21 15:31:31 +02:00
|
|
|
|
[Test]
|
2015-12-22 12:29:25 +01:00
|
|
|
|
procedure TestAsynchRequestGET;
|
2017-08-21 15:31:31 +02:00
|
|
|
|
[Test]
|
2015-12-22 12:29:25 +01:00
|
|
|
|
procedure TestAsynchRequestDELETE;
|
2017-08-21 15:31:31 +02:00
|
|
|
|
[Test]
|
2015-12-22 12:29:25 +01:00
|
|
|
|
procedure TestEncodingRenderJSONValue;
|
2017-08-21 15:31:31 +02:00
|
|
|
|
[Test]
|
2016-09-29 18:17:12 +02:00
|
|
|
|
procedure TestRenderWrappedList;
|
2017-08-21 15:31:31 +02:00
|
|
|
|
[Test]
|
2019-03-08 09:33:41 +01:00
|
|
|
|
procedure TestRenderActionInCollections;
|
|
|
|
|
[Test]
|
2018-10-23 16:18:34 +02:00
|
|
|
|
procedure TestRenderWrappedListWithCompression;
|
|
|
|
|
[Test]
|
2016-11-18 18:02:48 +01:00
|
|
|
|
procedure TestRenderStreamAndFreeWithOwnerFalse;
|
2017-08-21 15:31:31 +02:00
|
|
|
|
[Test]
|
2016-11-18 18:02:48 +01:00
|
|
|
|
procedure TestRenderStreamAndFreeWithOwnerTrue;
|
2017-08-21 15:31:31 +02:00
|
|
|
|
[Test]
|
2019-03-05 20:55:37 +01:00
|
|
|
|
procedure TestGetImagePng;
|
2017-08-21 15:31:31 +02:00
|
|
|
|
[Test]
|
2015-12-22 12:29:25 +01:00
|
|
|
|
procedure TestProducesConsumes01;
|
2017-08-21 15:31:31 +02:00
|
|
|
|
[Test]
|
2015-12-22 12:29:25 +01:00
|
|
|
|
procedure TestProducesConsumes02;
|
2017-08-21 15:31:31 +02:00
|
|
|
|
[Test]
|
2016-12-07 19:35:36 +01:00
|
|
|
|
procedure TestProducesConsumes03;
|
2017-08-21 15:31:31 +02:00
|
|
|
|
[Test]
|
2015-12-22 12:29:25 +01:00
|
|
|
|
procedure TestProducesConsumesWithWrongAcceptHeader;
|
2017-08-21 15:31:31 +02:00
|
|
|
|
[Test]
|
2015-12-22 12:29:25 +01:00
|
|
|
|
procedure TestExceptionInMVCAfterCreate;
|
2017-08-21 15:31:31 +02:00
|
|
|
|
[Test]
|
2015-12-22 12:29:25 +01:00
|
|
|
|
procedure TestExceptionInMVCBeforeDestroy;
|
2017-08-21 15:31:31 +02:00
|
|
|
|
[Test]
|
2017-01-05 12:44:34 +01:00
|
|
|
|
procedure TestActionFiltersOnBeforeAction;
|
2017-08-21 15:31:31 +02:00
|
|
|
|
[Test]
|
2015-12-22 12:29:25 +01:00
|
|
|
|
procedure TestMiddlewareSpeedMiddleware;
|
2017-08-21 15:31:31 +02:00
|
|
|
|
[Test]
|
2015-12-22 12:29:25 +01:00
|
|
|
|
procedure TestMiddlewareHandler;
|
2017-08-21 15:31:31 +02:00
|
|
|
|
[Test]
|
2015-12-22 12:29:25 +01:00
|
|
|
|
procedure TestPostAListOfObjects;
|
2016-09-16 23:54:54 +02:00
|
|
|
|
// test authentication/authorization with BasicAuth
|
2016-09-20 11:10:40 +02:00
|
|
|
|
procedure TestBasicAuth01;
|
2017-08-21 15:31:31 +02:00
|
|
|
|
[Test]
|
2016-09-20 11:10:40 +02:00
|
|
|
|
procedure TestBasicAuth02;
|
2017-08-21 15:31:31 +02:00
|
|
|
|
[Test]
|
2016-09-20 11:10:40 +02:00
|
|
|
|
procedure TestBasicAuth03;
|
2017-08-21 15:31:31 +02:00
|
|
|
|
[Test]
|
2016-09-20 11:10:40 +02:00
|
|
|
|
procedure TestBasicAuth04;
|
2017-08-21 15:31:31 +02:00
|
|
|
|
[Test]
|
2016-09-20 11:10:40 +02:00
|
|
|
|
procedure TestBasicAuth05;
|
2016-09-16 23:54:54 +02:00
|
|
|
|
// test authentication/authorization with CustomAuth
|
2017-08-21 15:31:31 +02:00
|
|
|
|
[Test]
|
2016-09-16 23:54:54 +02:00
|
|
|
|
procedure TestCustomAuthRequestWithoutLogin;
|
2017-08-21 15:31:31 +02:00
|
|
|
|
[Test]
|
2016-09-16 23:54:54 +02:00
|
|
|
|
procedure TestCustomAuthRequestsWithValidLogin;
|
2017-08-21 15:31:31 +02:00
|
|
|
|
[Test]
|
2016-12-28 10:00:07 +01:00
|
|
|
|
procedure TestCustomAuthRequestsWithValidLogin_HTML;
|
2017-08-21 15:31:31 +02:00
|
|
|
|
[Test]
|
2016-09-16 23:54:54 +02:00
|
|
|
|
procedure TestCustomAuthWrongRequestBodies;
|
2017-08-21 15:31:31 +02:00
|
|
|
|
[Test]
|
2016-09-18 19:19:23 +02:00
|
|
|
|
procedure TestCustomAuthLoginLogout;
|
2016-09-16 23:54:54 +02:00
|
|
|
|
|
2016-06-22 17:49:16 +02:00
|
|
|
|
// typed actions
|
2017-08-21 15:31:31 +02:00
|
|
|
|
[Test]
|
2016-06-22 17:49:16 +02:00
|
|
|
|
procedure TestTypedString1;
|
2017-08-21 15:31:31 +02:00
|
|
|
|
[Test]
|
2016-06-22 17:49:16 +02:00
|
|
|
|
procedure TestTypedInteger1;
|
2017-08-21 15:31:31 +02:00
|
|
|
|
[Test]
|
2016-06-22 17:49:16 +02:00
|
|
|
|
procedure TestTypedInt641;
|
2017-08-21 15:31:31 +02:00
|
|
|
|
[Test]
|
2016-06-22 17:49:16 +02:00
|
|
|
|
procedure TestTypedSingle1;
|
2017-08-21 15:31:31 +02:00
|
|
|
|
[Test]
|
2016-06-22 17:49:16 +02:00
|
|
|
|
procedure TestTypedDouble1;
|
2017-08-21 15:31:31 +02:00
|
|
|
|
[Test]
|
2016-06-22 17:49:16 +02:00
|
|
|
|
procedure TestTypedExtended1;
|
2017-08-21 15:31:31 +02:00
|
|
|
|
[Test]
|
2016-06-22 17:49:16 +02:00
|
|
|
|
procedure TestTypedAll;
|
2017-08-21 15:31:31 +02:00
|
|
|
|
[Test]
|
2016-06-22 17:49:16 +02:00
|
|
|
|
procedure TestTypedDateTimeTypes;
|
2017-08-21 15:31:31 +02:00
|
|
|
|
[Test]
|
2016-06-22 17:49:16 +02:00
|
|
|
|
procedure TestTypedBooleans;
|
2018-10-31 01:07:23 +01:00
|
|
|
|
[Test]
|
|
|
|
|
procedure TestStringDictionary;
|
|
|
|
|
[Test]
|
|
|
|
|
procedure TestWrongJSONBody;
|
2019-09-01 20:35:19 +02:00
|
|
|
|
|
|
|
|
|
// test responses objects
|
|
|
|
|
[Test]
|
|
|
|
|
procedure TestResponseCreated;
|
|
|
|
|
[Test]
|
|
|
|
|
procedure TestResponseNoContent;
|
|
|
|
|
[Test]
|
|
|
|
|
procedure TestResponseAccepted;
|
2015-12-22 12:29:25 +01:00
|
|
|
|
end;
|
|
|
|
|
|
2017-09-26 01:02:09 +02:00
|
|
|
|
[TestFixture]
|
|
|
|
|
TJSONRPCServerTest = class(TBaseServerTest)
|
2018-05-16 19:46:29 +02:00
|
|
|
|
protected
|
|
|
|
|
FExecutor: IMVCJSONRPCExecutor;
|
2019-01-18 18:11:27 +01:00
|
|
|
|
FExecutor2: IMVCJSONRPCExecutor;
|
2017-09-26 01:02:09 +02:00
|
|
|
|
public
|
2018-05-16 19:46:29 +02:00
|
|
|
|
[SetUp]
|
|
|
|
|
procedure SetUp;
|
2017-09-26 01:02:09 +02:00
|
|
|
|
[Test]
|
|
|
|
|
procedure TestRequestWithoutParams;
|
|
|
|
|
[Test]
|
|
|
|
|
procedure TestRequestToNotFoundMethod;
|
|
|
|
|
[Test]
|
2017-09-28 00:14:34 +02:00
|
|
|
|
procedure TestRequestWithParams_I_I_ret_I;
|
2017-09-26 01:02:09 +02:00
|
|
|
|
[Test]
|
2017-09-28 00:14:34 +02:00
|
|
|
|
procedure TestRequestWithParams_I_I_I_ret_O;
|
|
|
|
|
[Test]
|
|
|
|
|
procedure TestRequest_S_I_ret_S;
|
|
|
|
|
[Test]
|
|
|
|
|
procedure TestRequestWithParams_I_I_ret_A;
|
2019-01-18 18:11:27 +01:00
|
|
|
|
[Test]
|
|
|
|
|
procedure TestRequestWithParams_DT_T_ret_DT;
|
2017-09-26 01:02:09 +02:00
|
|
|
|
end;
|
|
|
|
|
|
2015-12-22 12:29:25 +01:00
|
|
|
|
implementation
|
|
|
|
|
|
|
|
|
|
uses
|
2017-04-29 23:56:56 +02:00
|
|
|
|
System.Math,
|
2018-11-24 16:56:21 +01:00
|
|
|
|
System.JSON,
|
2017-04-29 23:56:56 +02:00
|
|
|
|
MVCFramework.Serializer.Defaults,
|
2018-10-23 16:18:34 +02:00
|
|
|
|
JsonDataObjects,
|
|
|
|
|
MVCFramework.Serializer.JsonDataObjects,
|
2015-12-22 12:29:25 +01:00
|
|
|
|
MVCFramework.Commons,
|
|
|
|
|
System.SyncObjs,
|
|
|
|
|
System.Generics.Collections,
|
|
|
|
|
System.SysUtils,
|
|
|
|
|
BusinessObjectsU,
|
2017-05-25 16:57:49 +02:00
|
|
|
|
MVCFramework.Serializer.Commons,
|
2017-04-29 23:56:56 +02:00
|
|
|
|
Soap.EncdDecd,
|
|
|
|
|
System.Classes,
|
2018-05-16 19:46:29 +02:00
|
|
|
|
MVCFramework.SystemJSONUtils,
|
|
|
|
|
IdCookie,
|
2019-06-25 17:32:30 +02:00
|
|
|
|
MVCFramework.JSONRPC,
|
2019-07-07 17:25:11 +02:00
|
|
|
|
MVCFramework.Serializer.Intf
|
2019-09-01 20:35:19 +02:00
|
|
|
|
{$IFDEF MSWINDOWS}
|
|
|
|
|
,
|
|
|
|
|
MVCFramework.Serializer.JsonDataObjects.OptionalCustomTypes
|
|
|
|
|
,
|
|
|
|
|
Vcl.Graphics
|
|
|
|
|
{$ENDIF}
|
|
|
|
|
;
|
2015-12-22 12:29:25 +01:00
|
|
|
|
|
|
|
|
|
{ TServerTest }
|
|
|
|
|
|
|
|
|
|
procedure TBaseServerTest.DoLogout;
|
|
|
|
|
var
|
|
|
|
|
res: IRESTResponse;
|
|
|
|
|
begin
|
|
|
|
|
res := RESTClient.doGET('/logout', []);
|
2017-08-20 02:36:22 +02:00
|
|
|
|
Assert.isTrue(res.ResponseCode = HTTP_STATUS.OK, 'Logout Failed');
|
2015-12-22 12:29:25 +01:00
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TBaseServerTest.SetUp;
|
|
|
|
|
begin
|
|
|
|
|
inherited;
|
2017-04-14 16:43:31 +02:00
|
|
|
|
RESTClient := TRESTClient.Create(TEST_SERVER_ADDRESS, 9999);
|
2015-12-22 12:29:25 +01:00
|
|
|
|
RESTClient.ReadTimeout(60 * 1000 * 30);
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TBaseServerTest.TearDown;
|
|
|
|
|
begin
|
|
|
|
|
inherited;
|
|
|
|
|
RESTClient.Free;
|
|
|
|
|
end;
|
|
|
|
|
|
2017-01-05 12:44:34 +01:00
|
|
|
|
procedure TServerTest.TestActionFiltersOnBeforeAction;
|
|
|
|
|
var
|
|
|
|
|
res: IRESTResponse;
|
|
|
|
|
begin
|
|
|
|
|
res := RESTClient.doGET('/actionfilters/beforeaction/alwayscalled', []);
|
2017-08-21 15:31:31 +02:00
|
|
|
|
Assert.areEqual<Integer>(HTTP_STATUS.OK, res.ResponseCode);
|
2017-01-05 12:44:34 +01:00
|
|
|
|
|
|
|
|
|
res := RESTClient.doGET('/actionfilters/beforeaction/nevercalled', []);
|
2017-08-21 15:31:31 +02:00
|
|
|
|
Assert.areEqual<Integer>(HTTP_STATUS.NotFound, res.ResponseCode);
|
2017-01-05 12:44:34 +01:00
|
|
|
|
end;
|
|
|
|
|
|
2015-12-22 12:29:25 +01:00
|
|
|
|
procedure TServerTest.TestAsynchRequestDELETE;
|
|
|
|
|
var
|
|
|
|
|
evt: TEvent;
|
|
|
|
|
r: TWaitResult;
|
|
|
|
|
OK: boolean;
|
|
|
|
|
begin
|
|
|
|
|
OK := false;
|
|
|
|
|
evt := TEvent.Create;
|
|
|
|
|
try
|
|
|
|
|
RESTClient.Asynch(
|
|
|
|
|
procedure(Response: IRESTResponse)
|
|
|
|
|
begin
|
|
|
|
|
OK := true;
|
|
|
|
|
evt.SetEvent;
|
|
|
|
|
end,
|
|
|
|
|
procedure(E: Exception)
|
|
|
|
|
begin
|
|
|
|
|
OK := false;
|
|
|
|
|
end).doDELETE('/req/with/params', ['1', '2', '3']);
|
|
|
|
|
|
|
|
|
|
// wait for thred finish
|
|
|
|
|
repeat
|
|
|
|
|
r := evt.WaitFor(2000);
|
|
|
|
|
until r = TWaitResult.wrSignaled;
|
|
|
|
|
|
2018-05-16 19:46:29 +02:00
|
|
|
|
Assert.areEqual(true, OK);
|
2015-12-22 12:29:25 +01:00
|
|
|
|
finally
|
|
|
|
|
evt.Free;
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TServerTest.TestAsynchRequestGET;
|
|
|
|
|
var
|
|
|
|
|
evt: TEvent;
|
|
|
|
|
r: TWaitResult;
|
2018-11-24 16:56:21 +01:00
|
|
|
|
j: System.JSON.TJSONObject;
|
2015-12-22 12:29:25 +01:00
|
|
|
|
begin
|
|
|
|
|
j := nil;
|
|
|
|
|
evt := TEvent.Create;
|
|
|
|
|
try
|
|
|
|
|
RESTClient.Asynch(
|
|
|
|
|
procedure(Response: IRESTResponse)
|
|
|
|
|
begin
|
|
|
|
|
try
|
2017-05-25 16:57:49 +02:00
|
|
|
|
j := TSystemJSON.StringAsJSONObject(Response.BodyAsString);
|
2015-12-22 12:29:25 +01:00
|
|
|
|
except
|
|
|
|
|
// test should not block...never!
|
|
|
|
|
end;
|
|
|
|
|
evt.SetEvent;
|
|
|
|
|
end,
|
|
|
|
|
procedure(E: Exception)
|
|
|
|
|
begin
|
|
|
|
|
end).doGET('/req/with/params', ['1', '2', '3']);
|
|
|
|
|
|
|
|
|
|
// wait for thred finish
|
|
|
|
|
repeat
|
|
|
|
|
r := evt.WaitFor(2000);
|
|
|
|
|
until r = TWaitResult.wrSignaled;
|
|
|
|
|
|
2017-08-20 02:36:22 +02:00
|
|
|
|
Assert.isTrue(Assigned(j));
|
2018-05-16 19:46:29 +02:00
|
|
|
|
Assert.areEqual('1', j.Get('par1').JsonValue.Value);
|
2015-12-22 12:29:25 +01:00
|
|
|
|
j.Free;
|
|
|
|
|
finally
|
|
|
|
|
evt.Free;
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TServerTest.TestAsynchRequestPOST;
|
|
|
|
|
var
|
|
|
|
|
evt: TEvent;
|
|
|
|
|
r: TWaitResult;
|
2018-11-24 16:56:21 +01:00
|
|
|
|
j: System.JSON.TJSONObject;
|
2015-12-22 12:29:25 +01:00
|
|
|
|
begin
|
|
|
|
|
j := nil;
|
|
|
|
|
evt := TEvent.Create;
|
|
|
|
|
try
|
2017-04-26 14:39:18 +02:00
|
|
|
|
|
2015-12-22 12:29:25 +01:00
|
|
|
|
RESTClient.Asynch(
|
|
|
|
|
procedure(Response: IRESTResponse)
|
|
|
|
|
begin
|
|
|
|
|
try
|
2017-05-25 16:57:49 +02:00
|
|
|
|
j := TSystemJSON.StringAsJSONObject(Response.BodyAsString);
|
2015-12-22 12:29:25 +01:00
|
|
|
|
except
|
|
|
|
|
// test should not block...never!
|
|
|
|
|
end;
|
|
|
|
|
evt.SetEvent;
|
|
|
|
|
end,
|
|
|
|
|
procedure(E: Exception)
|
|
|
|
|
begin
|
2016-04-03 22:35:27 +02:00
|
|
|
|
end).doPOST('/echo', ['1', '2', '3'],
|
2018-11-24 16:56:21 +01:00
|
|
|
|
TSystemJSON.JSONValueToString(System.JSON.TJSONObject.Create(TJSONPair.Create('from client', 'hello world'))));
|
2015-12-22 12:29:25 +01:00
|
|
|
|
|
|
|
|
|
// wait for thred finish
|
|
|
|
|
repeat
|
|
|
|
|
r := evt.WaitFor(2000);
|
|
|
|
|
until r = TWaitResult.wrSignaled;
|
|
|
|
|
|
2017-08-20 02:36:22 +02:00
|
|
|
|
Assert.isTrue(Assigned(j));
|
2018-05-16 19:46:29 +02:00
|
|
|
|
Assert.areEqual('from server', j.Get('echo').JsonValue.Value);
|
2015-12-22 12:29:25 +01:00
|
|
|
|
j.Free;
|
|
|
|
|
finally
|
|
|
|
|
evt.Free;
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TServerTest.TestAsynchRequestPUT;
|
|
|
|
|
var
|
|
|
|
|
evt: TEvent;
|
|
|
|
|
r: TWaitResult;
|
2018-11-24 16:56:21 +01:00
|
|
|
|
j: System.JSON.TJSONObject;
|
2015-12-22 12:29:25 +01:00
|
|
|
|
begin
|
|
|
|
|
j := nil;
|
|
|
|
|
evt := TEvent.Create;
|
|
|
|
|
try
|
|
|
|
|
RESTClient.Asynch(
|
|
|
|
|
procedure(Response: IRESTResponse)
|
|
|
|
|
begin
|
|
|
|
|
try
|
2017-05-25 16:57:49 +02:00
|
|
|
|
j := TSystemJSON.StringAsJSONObject(Response.BodyAsString);
|
2015-12-22 12:29:25 +01:00
|
|
|
|
except
|
|
|
|
|
// test should not block...never!
|
|
|
|
|
end;
|
|
|
|
|
evt.SetEvent;
|
|
|
|
|
end,
|
|
|
|
|
procedure(E: Exception)
|
|
|
|
|
begin
|
2016-04-03 22:35:27 +02:00
|
|
|
|
end).doPUT('/echo', ['1', '2', '3'],
|
2019-01-08 12:48:27 +01:00
|
|
|
|
TSystemJSON.JSONValueToString(System.JSON.TJSONObject.Create(System.JSON.TJSONPair.Create('from client',
|
|
|
|
|
'hello world'))));
|
2015-12-22 12:29:25 +01:00
|
|
|
|
|
|
|
|
|
// wait for thred finish
|
|
|
|
|
repeat
|
|
|
|
|
r := evt.WaitFor(2000);
|
|
|
|
|
until r = TWaitResult.wrSignaled;
|
|
|
|
|
|
2017-08-20 02:36:22 +02:00
|
|
|
|
Assert.isTrue(Assigned(j));
|
2018-05-16 19:46:29 +02:00
|
|
|
|
Assert.areEqual('from server', j.Get('echo').JsonValue.Value);
|
2015-12-22 12:29:25 +01:00
|
|
|
|
j.Free;
|
|
|
|
|
finally
|
|
|
|
|
evt.Free;
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
|
2016-09-20 11:10:40 +02:00
|
|
|
|
procedure TServerTest.TestBasicAuth01;
|
2015-12-22 12:29:25 +01:00
|
|
|
|
var
|
2019-03-05 20:55:37 +01:00
|
|
|
|
lRes: IRESTResponse;
|
2015-12-22 12:29:25 +01:00
|
|
|
|
begin
|
2016-04-03 22:35:27 +02:00
|
|
|
|
RESTClient.Authentication('user1', 'user1');
|
2018-05-16 19:46:29 +02:00
|
|
|
|
Assert.areEqual('user1', RESTClient.UserName);
|
|
|
|
|
Assert.areEqual('user1', RESTClient.Password);
|
2019-03-05 20:55:37 +01:00
|
|
|
|
lRes := RESTClient.doGET('/private/role1', []);
|
|
|
|
|
Assert.areEqual<Integer>(HTTP_STATUS.OK, lRes.ResponseCode);
|
2015-12-22 12:29:25 +01:00
|
|
|
|
end;
|
|
|
|
|
|
2016-09-20 11:10:40 +02:00
|
|
|
|
procedure TServerTest.TestBasicAuth02;
|
2015-12-22 12:29:25 +01:00
|
|
|
|
var
|
2019-03-05 20:55:37 +01:00
|
|
|
|
lRes: IRESTResponse;
|
2015-12-22 12:29:25 +01:00
|
|
|
|
begin
|
|
|
|
|
RESTClient.UserName := '';
|
|
|
|
|
RESTClient.Password := '';
|
|
|
|
|
RESTClient.UseBasicAuthentication := false;
|
2019-03-05 20:55:37 +01:00
|
|
|
|
lRes := RESTClient.doGET('/private/role1', []);
|
|
|
|
|
Assert.areEqual<Integer>(HTTP_STATUS.Unauthorized, lRes.ResponseCode);
|
2015-12-22 12:29:25 +01:00
|
|
|
|
end;
|
|
|
|
|
|
2016-09-20 11:10:40 +02:00
|
|
|
|
procedure TServerTest.TestBasicAuth03;
|
2015-12-22 12:29:25 +01:00
|
|
|
|
var
|
2019-03-05 20:55:37 +01:00
|
|
|
|
lRes: IRESTResponse;
|
2015-12-22 12:29:25 +01:00
|
|
|
|
begin
|
|
|
|
|
RESTClient.UserName := 'user1';
|
|
|
|
|
RESTClient.Password := 'user1';
|
|
|
|
|
RESTClient.UseBasicAuthentication := true;
|
2019-03-05 20:55:37 +01:00
|
|
|
|
lRes := RESTClient.doGET('/private/role2', []);
|
|
|
|
|
Assert.areEqual<Integer>(HTTP_STATUS.Forbidden, lRes.ResponseCode);
|
2015-12-22 12:29:25 +01:00
|
|
|
|
end;
|
|
|
|
|
|
2016-09-20 11:10:40 +02:00
|
|
|
|
procedure TServerTest.TestBasicAuth04;
|
2015-12-22 12:29:25 +01:00
|
|
|
|
var
|
2019-03-05 20:55:37 +01:00
|
|
|
|
lRes: IRESTResponse;
|
2015-12-22 12:29:25 +01:00
|
|
|
|
begin
|
|
|
|
|
RESTClient.UserName := 'user1';
|
|
|
|
|
RESTClient.Password := 'user1';
|
|
|
|
|
RESTClient.UseBasicAuthentication := true;
|
2019-03-05 20:55:37 +01:00
|
|
|
|
lRes := RESTClient.doGET('/private/role1', []);
|
|
|
|
|
Assert.areEqual<Integer>(HTTP_STATUS.OK, lRes.ResponseCode);
|
|
|
|
|
lRes := RESTClient.doGET('/people', []);
|
|
|
|
|
Assert.areEqual<Integer>(HTTP_STATUS.OK, lRes.ResponseCode);
|
2015-12-22 12:29:25 +01:00
|
|
|
|
end;
|
|
|
|
|
|
2016-09-20 11:10:40 +02:00
|
|
|
|
procedure TServerTest.TestBasicAuth05;
|
2016-04-03 22:35:27 +02:00
|
|
|
|
var
|
2019-03-05 20:55:37 +01:00
|
|
|
|
lRes: IRESTResponse;
|
2016-04-03 22:35:27 +02:00
|
|
|
|
begin
|
|
|
|
|
RESTClient.UserName := 'user1';
|
|
|
|
|
RESTClient.Password := 'user1';
|
|
|
|
|
RESTClient.UseBasicAuthentication := true;
|
|
|
|
|
|
|
|
|
|
// first
|
2019-03-05 20:55:37 +01:00
|
|
|
|
lRes := RESTClient.doGET('/private/role1session?value=danieleteti', []);
|
|
|
|
|
Assert.areEqual<Integer>(HTTP_STATUS.OK, lRes.ResponseCode);
|
|
|
|
|
lRes := RESTClient.doGET('/private/role1session', []);
|
|
|
|
|
Assert.areEqual<Integer>(HTTP_STATUS.OK, lRes.ResponseCode);
|
|
|
|
|
Assert.areEqual('danieleteti', lRes.BodyAsString);
|
2016-04-03 22:35:27 +02:00
|
|
|
|
|
|
|
|
|
// second
|
2019-03-05 20:55:37 +01:00
|
|
|
|
lRes := RESTClient.doGET('/private/role1session?value=johndoe', []);
|
|
|
|
|
Assert.areEqual<Integer>(HTTP_STATUS.OK, lRes.ResponseCode);
|
|
|
|
|
lRes := RESTClient.doGET('/private/role1session', []);
|
|
|
|
|
Assert.areEqual<Integer>(HTTP_STATUS.OK, lRes.ResponseCode);
|
|
|
|
|
Assert.areEqual('johndoe', lRes.BodyAsString);
|
2016-04-03 22:35:27 +02:00
|
|
|
|
end;
|
|
|
|
|
|
2019-02-24 20:43:06 +01:00
|
|
|
|
procedure TServerTest.TestControllerWithExceptionInCreate(const URLSegment: string);
|
2018-08-08 17:11:45 +02:00
|
|
|
|
var
|
|
|
|
|
res: IRESTResponse;
|
|
|
|
|
begin
|
|
|
|
|
res := RESTClient.doGET(URLSegment, []);
|
2018-10-23 16:18:34 +02:00
|
|
|
|
Assert.areEqual(HTTP_STATUS.InternalServerError, res.ResponseCode);
|
2019-03-05 20:55:37 +01:00
|
|
|
|
// Assert.Contains(res.ContentType, 'text/plain', true, 'Is not a text/plain in case of error');
|
2019-02-24 20:43:06 +01:00
|
|
|
|
Assert.Contains(res.ContentType, 'application/json', true, 'Is not a application/json in case of error');
|
2018-08-08 17:11:45 +02:00
|
|
|
|
Assert.Contains(res.BodyAsString, 'Cannot create controller', true, 'Exception message in body is not correct');
|
2019-03-05 20:55:37 +01:00
|
|
|
|
// Assert.Contains(res.BodyAsString, 'Cannot create controller', true, 'Exception message in body is not correct');
|
2018-08-08 17:11:45 +02:00
|
|
|
|
end;
|
|
|
|
|
|
2016-04-24 19:08:21 +02:00
|
|
|
|
procedure TServerTest.TestCookies;
|
|
|
|
|
var
|
|
|
|
|
res: IRESTResponse;
|
|
|
|
|
I: Integer;
|
|
|
|
|
begin
|
|
|
|
|
res := RESTClient.doGET('/lotofcookies', []);
|
2017-08-21 15:31:31 +02:00
|
|
|
|
Assert.areEqual<Integer>(HTTP_STATUS.OK, res.ResponseCode);
|
2018-05-16 19:46:29 +02:00
|
|
|
|
Assert.areEqual(4, res.Cookies.Count, 'Wrong number of cookies');
|
2016-04-24 19:08:21 +02:00
|
|
|
|
for I := 0 to 3 do
|
|
|
|
|
begin
|
2018-05-16 19:46:29 +02:00
|
|
|
|
Assert.areEqual('usersettings' + IntToStr(I + 1), res.Cookies.Cookies[I].CookieName);
|
|
|
|
|
Assert.areEqual('usersettings' + IntToStr(I + 1) + '-value', res.Cookies.Cookies[I].Value);
|
|
|
|
|
Assert.areEqual('/usersettings' + IntToStr(I + 1), res.Cookies.Cookies[I].Path);
|
2016-04-24 19:08:21 +02:00
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
end;
|
|
|
|
|
|
2016-09-16 23:54:54 +02:00
|
|
|
|
procedure TServerTest.TestCustomAuthRequestWithoutLogin;
|
|
|
|
|
var
|
2019-03-05 20:55:37 +01:00
|
|
|
|
lRes: IRESTResponse;
|
2016-09-16 23:54:54 +02:00
|
|
|
|
begin
|
2019-03-05 20:55:37 +01:00
|
|
|
|
lRes := RESTClient.doGET('/privatecustom/role1', []);
|
|
|
|
|
Assert.areEqual<Integer>(HTTP_STATUS.Unauthorized, lRes.ResponseCode);
|
|
|
|
|
Assert.areEqual('/system/users/logged', lRes.HeaderValue('X-LOGIN-URL'));
|
|
|
|
|
Assert.areEqual('POST', lRes.HeaderValue('X-LOGIN-METHOD'));
|
|
|
|
|
|
|
|
|
|
lRes := RESTClient.doGET('/privatecustom/role2', []);
|
|
|
|
|
Assert.areEqual<Integer>(HTTP_STATUS.Unauthorized, lRes.ResponseCode);
|
|
|
|
|
Assert.areEqual('/system/users/logged', lRes.HeaderValue('X-LOGIN-URL'));
|
|
|
|
|
Assert.areEqual('POST', lRes.HeaderValue('X-LOGIN-METHOD'));
|
2016-09-16 23:54:54 +02:00
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TServerTest.TestCustomAuthRequestsWithValidLogin;
|
|
|
|
|
var
|
2019-03-05 20:55:37 +01:00
|
|
|
|
lRes: IRESTResponse;
|
2018-11-24 16:56:21 +01:00
|
|
|
|
lJSON: System.JSON.TJSONObject;
|
2016-09-16 23:54:54 +02:00
|
|
|
|
lCookieValue: string;
|
|
|
|
|
begin
|
2018-11-24 16:56:21 +01:00
|
|
|
|
lJSON := System.JSON.TJSONObject.Create;
|
2016-09-16 23:54:54 +02:00
|
|
|
|
try
|
|
|
|
|
lJSON.AddPair('username', 'user1');
|
|
|
|
|
lJSON.AddPair('password', 'user1');
|
2019-03-05 20:55:37 +01:00
|
|
|
|
lRes := RESTClient.doPOST('/system/users/logged', [], TSystemJSON.JSONValueToString(lJSON, false));
|
|
|
|
|
Assert.areEqual('application/json', lRes.ContentType);
|
|
|
|
|
Assert.areEqual<Integer>(HTTP_STATUS.OK, lRes.ResponseCode);
|
|
|
|
|
Assert.areEqual('/system/users/logged', lRes.HeaderValue('X-LOGOUT-URL'));
|
|
|
|
|
Assert.areEqual('DELETE', lRes.HeaderValue('X-LOGOUT-METHOD'));
|
|
|
|
|
Assert.areEqual('{"status":"OK"}', lRes.BodyAsString);
|
|
|
|
|
lCookieValue := lRes.Cookies[lRes.Cookies.GetCookieIndex(TMVCConstants.SESSION_TOKEN_NAME)].Value;
|
2017-08-20 02:36:22 +02:00
|
|
|
|
Assert.AreNotEqual('', lCookieValue, 'Session cookie not returned after login');
|
2018-05-16 19:46:29 +02:00
|
|
|
|
Assert.isFalse(lCookieValue.Contains('invalid'), 'Returned an invalid session token');
|
2016-12-28 10:00:07 +01:00
|
|
|
|
|
2019-03-05 20:55:37 +01:00
|
|
|
|
lRes := RESTClient.doGET('/privatecustom/role2', []);
|
|
|
|
|
Assert.areEqual<Integer>(HTTP_STATUS.Forbidden, lRes.ResponseCode,
|
2016-12-28 10:00:07 +01:00
|
|
|
|
'Authorization not respected for not allowed action');
|
|
|
|
|
|
2019-03-05 20:55:37 +01:00
|
|
|
|
lRes := RESTClient.doGET('/privatecustom/role1', []);
|
|
|
|
|
Assert.areEqual<Integer>(HTTP_STATUS.OK, lRes.ResponseCode, 'Authorization not respected for allowed action');
|
2016-12-28 10:00:07 +01:00
|
|
|
|
finally
|
|
|
|
|
lJSON.Free;
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TServerTest.TestCustomAuthRequestsWithValidLogin_HTML;
|
|
|
|
|
var
|
2019-03-05 20:55:37 +01:00
|
|
|
|
lRes: IRESTResponse;
|
2018-11-24 16:56:21 +01:00
|
|
|
|
lJSON: System.JSON.TJSONObject;
|
2016-12-28 10:00:07 +01:00
|
|
|
|
lCookieValue: string;
|
|
|
|
|
begin
|
2018-11-24 16:56:21 +01:00
|
|
|
|
lJSON := System.JSON.TJSONObject.Create;
|
2016-12-28 10:00:07 +01:00
|
|
|
|
try
|
|
|
|
|
lJSON.AddPair('username', 'user1');
|
|
|
|
|
lJSON.AddPair('password', 'user1');
|
2019-03-05 20:55:37 +01:00
|
|
|
|
lRes := RESTClient.Accept('text/html').doPOST('/system/users/logged', [],
|
2018-05-16 19:46:29 +02:00
|
|
|
|
TSystemJSON.JSONValueToString(lJSON, false));
|
2019-03-05 20:55:37 +01:00
|
|
|
|
Assert.areEqual('application/json', lRes.ContentType);
|
|
|
|
|
Assert.areEqual<Integer>(HTTP_STATUS.OK, lRes.ResponseCode);
|
|
|
|
|
Assert.areEqual('/system/users/logged', lRes.HeaderValue('X-LOGOUT-URL'));
|
|
|
|
|
Assert.areEqual('DELETE', lRes.HeaderValue('X-LOGOUT-METHOD'));
|
|
|
|
|
Assert.areEqual('{"status":"OK"}', lRes.BodyAsString);
|
|
|
|
|
lCookieValue := lRes.Cookies[lRes.Cookies.GetCookieIndex(TMVCConstants.SESSION_TOKEN_NAME)].Value;
|
2017-08-20 02:36:22 +02:00
|
|
|
|
Assert.AreNotEqual('', lCookieValue, 'Session cookie not returned after login');
|
2018-05-16 19:46:29 +02:00
|
|
|
|
Assert.isFalse(lCookieValue.Contains('invalid'), 'Returned an invalid session token');
|
2016-09-16 23:54:54 +02:00
|
|
|
|
|
2019-03-05 20:55:37 +01:00
|
|
|
|
lRes := RESTClient.doGET('/privatecustom/role2', []);
|
|
|
|
|
Assert.areEqual<Integer>(HTTP_STATUS.Forbidden, lRes.ResponseCode,
|
2016-09-16 23:54:54 +02:00
|
|
|
|
'Authorization not respected for not allowed action');
|
|
|
|
|
|
2019-03-05 20:55:37 +01:00
|
|
|
|
lRes := RESTClient.doGET('/privatecustom/role1', []);
|
|
|
|
|
Assert.areEqual<Integer>(HTTP_STATUS.OK, lRes.ResponseCode, 'Authorization not respected for allowed action');
|
2016-09-16 23:54:54 +02:00
|
|
|
|
finally
|
|
|
|
|
lJSON.Free;
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TServerTest.TestCustomAuthWrongRequestBodies;
|
|
|
|
|
var
|
2019-03-05 20:55:37 +01:00
|
|
|
|
lRes: IRESTResponse;
|
2018-11-24 16:56:21 +01:00
|
|
|
|
lJSON: System.JSON.TJSONObject;
|
2016-09-16 23:54:54 +02:00
|
|
|
|
begin
|
2018-11-24 16:56:21 +01:00
|
|
|
|
lJSON := System.JSON.TJSONObject.Create;
|
2016-09-16 23:54:54 +02:00
|
|
|
|
try
|
|
|
|
|
// no request body
|
2019-03-05 20:55:37 +01:00
|
|
|
|
lRes := RESTClient.doPOST('/system/users/logged', []);
|
|
|
|
|
Assert.areEqual<Integer>(HTTP_STATUS.BadRequest, lRes.ResponseCode,
|
2016-09-16 23:54:54 +02:00
|
|
|
|
'Empty request body doesn''t return HTTP 400 Bad Request');
|
|
|
|
|
|
|
|
|
|
// wrong request body 1
|
2019-03-05 20:55:37 +01:00
|
|
|
|
lRes := RESTClient.doPOST('/system/users/logged', [], TSystemJSON.JSONValueToString(lJSON, false));
|
|
|
|
|
Assert.areEqual<Integer>(HTTP_STATUS.Unauthorized, lRes.ResponseCode,
|
2016-09-16 23:54:54 +02:00
|
|
|
|
'Invalid json doesn''t return HTTP 401 Unauthorized');
|
|
|
|
|
|
|
|
|
|
// wrong request body 2
|
|
|
|
|
lJSON.AddPair('username', '');
|
|
|
|
|
lJSON.AddPair('password', '');
|
2019-03-05 20:55:37 +01:00
|
|
|
|
lRes := RESTClient.doPOST('/system/users/logged', [], TSystemJSON.JSONValueToString(lJSON, false));
|
|
|
|
|
Assert.areEqual<Integer>(HTTP_STATUS.Unauthorized, lRes.ResponseCode,
|
2016-09-16 23:54:54 +02:00
|
|
|
|
'Empty username and password doesn''t return HTTP 401 Unauthorized');
|
|
|
|
|
|
|
|
|
|
// wrong username and password 3
|
2017-08-21 15:31:31 +02:00
|
|
|
|
lJSON.RemovePair('username').Free;
|
|
|
|
|
lJSON.RemovePair('password').Free;
|
2016-09-16 23:54:54 +02:00
|
|
|
|
lJSON.AddPair('username', 'notvaliduser');
|
|
|
|
|
lJSON.AddPair('password', 'notvalidpassword');
|
2019-03-05 20:55:37 +01:00
|
|
|
|
lRes := RESTClient.doPOST('/system/users/logged', [], TSystemJSON.JSONValueToString(lJSON, false));
|
|
|
|
|
Assert.areEqual<Integer>(HTTP_STATUS.Unauthorized, lRes.ResponseCode,
|
2016-09-16 23:54:54 +02:00
|
|
|
|
'Wrong username and password doesn''t return HTTP 401 Unauthorized');
|
|
|
|
|
finally
|
|
|
|
|
lJSON.Free;
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
|
2019-06-25 17:32:30 +02:00
|
|
|
|
procedure TServerTest.TestCustomerEcho;
|
|
|
|
|
var
|
|
|
|
|
r: IRESTResponse;
|
|
|
|
|
lCustomer: TCustomer;
|
|
|
|
|
lSer: IMVCSerializer;
|
|
|
|
|
begin
|
|
|
|
|
lCustomer := TCustomer.Create;
|
|
|
|
|
try
|
|
|
|
|
lCustomer.Name := 'bit Time Professionals';
|
|
|
|
|
lCustomer.ContactFirst := 'Daniele'; // transient
|
|
|
|
|
lCustomer.ContactLast := 'Teti'; // transient
|
|
|
|
|
lCustomer.AddressLine1 := 'Via Roma 10';
|
|
|
|
|
lCustomer.AddressLine2 := '00100, ROMA';
|
|
|
|
|
lCustomer.Logo.SetSize(100, 100);
|
|
|
|
|
lCustomer.Logo.Canvas.FillRect(Rect(0, 0, 100, 100));
|
|
|
|
|
lCustomer.Logo.Canvas.Font.Color := clRed;
|
|
|
|
|
lCustomer.Logo.Canvas.TextOut(10, 50, 'From Client');
|
|
|
|
|
lCustomer.Logo.SaveToFile('pippo_client_before_send.bmp');
|
|
|
|
|
lSer := GetDefaultSerializer;
|
|
|
|
|
RegisterOptionalCustomTypesSerializers(lSer);
|
|
|
|
|
r := RESTClient.Accept(TMVCMediaType.APPLICATION_JSON).doPOST('/customerecho', [],
|
|
|
|
|
lSer.SerializeObject(lCustomer));
|
|
|
|
|
finally
|
|
|
|
|
lCustomer.Free;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
lCustomer := TCustomer.Create;
|
|
|
|
|
try
|
|
|
|
|
lSer := GetDefaultSerializer;
|
|
|
|
|
RegisterOptionalCustomTypesSerializers(lSer);
|
|
|
|
|
lSer.DeserializeObject(r.BodyAsString, lCustomer);
|
|
|
|
|
Assert.areEqual('bit Time Professionals changed', lCustomer.Name);
|
|
|
|
|
Assert.areEqual('', lCustomer.ContactFirst);
|
|
|
|
|
Assert.areEqual('', lCustomer.ContactLast);
|
|
|
|
|
lCustomer.Logo.SaveToFile('customer_logo.bmp');
|
|
|
|
|
Assert.areEqual('de2a29ec62fc1f0b3abbb6b74223d214', THashMD5.GetHashStringFromFile('customer_logo.bmp'));
|
|
|
|
|
finally
|
|
|
|
|
lCustomer.Free;
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
|
2016-09-18 19:19:23 +02:00
|
|
|
|
procedure TServerTest.TestCustomAuthLoginLogout;
|
|
|
|
|
var
|
2019-03-05 20:55:37 +01:00
|
|
|
|
lRes: IRESTResponse;
|
2018-11-24 16:56:21 +01:00
|
|
|
|
lJSON: System.JSON.TJSONObject;
|
2016-09-18 19:19:23 +02:00
|
|
|
|
lLogoutUrl: string;
|
|
|
|
|
lValue: string;
|
|
|
|
|
I: Integer;
|
|
|
|
|
lPieces: TArray<string>;
|
2016-09-29 18:17:12 +02:00
|
|
|
|
lPass: boolean;
|
2016-09-16 23:54:54 +02:00
|
|
|
|
begin
|
2018-11-24 16:56:21 +01:00
|
|
|
|
lJSON := System.JSON.TJSONObject.Create;
|
2016-09-18 19:19:23 +02:00
|
|
|
|
try
|
|
|
|
|
lJSON.AddPair('username', 'user1');
|
|
|
|
|
lJSON.AddPair('password', 'user1');
|
2019-03-05 20:55:37 +01:00
|
|
|
|
lRes := RESTClient.doPOST('/system/users/logged', [], TSystemJSON.JSONValueToString(lJSON, false));
|
2016-09-16 23:54:54 +02:00
|
|
|
|
|
2019-03-05 20:55:37 +01:00
|
|
|
|
Assert.areEqual<Integer>(HTTP_STATUS.OK, lRes.ResponseCode);
|
|
|
|
|
lLogoutUrl := lRes.HeaderValue('X-LOGOUT-URL');
|
2016-09-18 19:19:23 +02:00
|
|
|
|
|
2019-03-05 20:55:37 +01:00
|
|
|
|
lRes := RESTClient.doDELETE(lLogoutUrl, []);
|
2016-09-18 19:19:23 +02:00
|
|
|
|
lPass := false;
|
2019-03-05 20:55:37 +01:00
|
|
|
|
for I := 0 to lRes.Headers.Count do
|
2016-09-18 19:19:23 +02:00
|
|
|
|
begin
|
2019-03-05 20:55:37 +01:00
|
|
|
|
lValue := lRes.Headers[I];
|
2016-09-18 19:19:23 +02:00
|
|
|
|
if lValue.StartsWith('Set-Cookie') then
|
|
|
|
|
begin
|
|
|
|
|
lPieces := lValue.Split([':']);
|
|
|
|
|
lValue := lPieces[1].Trim;
|
2018-05-16 19:46:29 +02:00
|
|
|
|
if lValue.StartsWith(TMVCConstants.SESSION_TOKEN_NAME) and lValue.Contains('invalid') then
|
2016-09-18 19:19:23 +02:00
|
|
|
|
begin
|
|
|
|
|
lPass := true;
|
|
|
|
|
Break;
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
end;
|
2017-08-20 02:36:22 +02:00
|
|
|
|
Assert.isTrue(lPass, 'No session cookie cleanup in the response');
|
2016-09-18 19:19:23 +02:00
|
|
|
|
finally
|
|
|
|
|
lJSON.Free;
|
|
|
|
|
end;
|
2016-09-16 23:54:54 +02:00
|
|
|
|
|
|
|
|
|
end;
|
|
|
|
|
|
2015-12-22 12:29:25 +01:00
|
|
|
|
procedure TServerTest.TestEncodingRenderJSONValue;
|
|
|
|
|
var
|
|
|
|
|
res: IRESTResponse;
|
|
|
|
|
s: string;
|
2018-11-24 16:56:21 +01:00
|
|
|
|
lJSONObj: System.JSON.TJSONObject;
|
2015-12-22 12:29:25 +01:00
|
|
|
|
begin
|
|
|
|
|
res := RESTClient.doGET('/encoding', []);
|
|
|
|
|
|
2017-05-25 16:57:49 +02:00
|
|
|
|
lJSONObj := TSystemJSON.StringAsJSONObject(res.BodyAsString);
|
2017-04-26 14:39:18 +02:00
|
|
|
|
s := lJSONObj.Get('name1').JsonValue.Value;
|
2018-05-16 19:46:29 +02:00
|
|
|
|
Assert.areEqual('j<>rn', s);
|
2017-04-26 14:39:18 +02:00
|
|
|
|
lJSONObj.Free;
|
2015-12-22 12:29:25 +01:00
|
|
|
|
|
2017-05-25 16:57:49 +02:00
|
|
|
|
lJSONObj := TSystemJSON.StringAsJSONObject(res.BodyAsString);
|
2017-04-26 14:39:18 +02:00
|
|
|
|
s := lJSONObj.Get('name3').JsonValue.Value;
|
2018-05-16 19:46:29 +02:00
|
|
|
|
Assert.areEqual('<27><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>', s);
|
2017-04-26 14:39:18 +02:00
|
|
|
|
lJSONObj.Free;
|
2015-12-22 12:29:25 +01:00
|
|
|
|
|
2017-05-25 16:57:49 +02:00
|
|
|
|
lJSONObj := TSystemJSON.StringAsJSONObject(res.BodyAsString);
|
2017-04-26 14:39:18 +02:00
|
|
|
|
s := lJSONObj.Get('name2').JsonValue.Value;
|
2018-05-16 19:46:29 +02:00
|
|
|
|
Assert.areEqual('<27>to je Unicode?', s, 'If this test fail, check http://qc.embarcadero.com/wc/qcmain.aspx?d=119779');
|
2017-04-26 14:39:18 +02:00
|
|
|
|
lJSONObj.Free;
|
2015-12-22 12:29:25 +01:00
|
|
|
|
{ WARNING!!! }
|
|
|
|
|
{
|
|
|
|
|
If this test fail, check
|
|
|
|
|
http://qc.embarcadero.com/wc/qcmain.aspx?d=119779
|
|
|
|
|
}
|
|
|
|
|
end;
|
|
|
|
|
|
2019-03-08 09:33:41 +01:00
|
|
|
|
procedure TServerTest.TestRenderActionInCollections;
|
|
|
|
|
var
|
|
|
|
|
lRes: IRESTResponse;
|
|
|
|
|
lJArr: TJDOJsonArray;
|
|
|
|
|
I: Integer;
|
|
|
|
|
begin
|
|
|
|
|
lRes := RESTClient.doGET('/people/renderaction', []);
|
|
|
|
|
lJArr := TJsonBaseObject.Parse(lRes.BodyAsString) as TJDOJsonArray;
|
|
|
|
|
try
|
|
|
|
|
for I := 0 to lJArr.Count - 1 do
|
|
|
|
|
begin
|
2019-05-09 20:53:52 +02:00
|
|
|
|
Assert.isTrue(lJArr[I].A[TMVCConstants.HATEOAS_PROP_NAME].Count = 2, '_links doesn''t exists');
|
|
|
|
|
Assert.areEqual(lJArr[I].A[TMVCConstants.HATEOAS_PROP_NAME].O[0].s[HATEOAS.REL], 'test0');
|
|
|
|
|
Assert.areEqual(lJArr[I].A[TMVCConstants.HATEOAS_PROP_NAME].O[1].s[HATEOAS.REL], 'test1');
|
2019-03-08 09:33:41 +01:00
|
|
|
|
end;
|
|
|
|
|
finally
|
|
|
|
|
lJArr.Free;
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
|
2016-11-18 18:02:48 +01:00
|
|
|
|
procedure TServerTest.TestRenderStreamAndFreeWithOwnerFalse;
|
|
|
|
|
var
|
2019-03-05 20:55:37 +01:00
|
|
|
|
lRes: IRESTResponse;
|
2016-11-18 18:02:48 +01:00
|
|
|
|
begin
|
2019-03-05 20:55:37 +01:00
|
|
|
|
lRes := RESTClient.doGET('/renderstreamandfreewithownerfalse', []);
|
|
|
|
|
Assert.areEqual<Integer>(200, lRes.ResponseCode);
|
2016-11-18 18:02:48 +01:00
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TServerTest.TestRenderStreamAndFreeWithOwnerTrue;
|
|
|
|
|
var
|
2019-03-05 20:55:37 +01:00
|
|
|
|
lRes: IRESTResponse;
|
2016-11-18 18:02:48 +01:00
|
|
|
|
begin
|
2019-03-05 20:55:37 +01:00
|
|
|
|
lRes := RESTClient.doGET('/renderstreamandfreewithownertrue', []);
|
|
|
|
|
Assert.areEqual<Integer>(200, lRes.ResponseCode);
|
2016-11-18 18:02:48 +01:00
|
|
|
|
end;
|
|
|
|
|
|
2016-09-29 18:17:12 +02:00
|
|
|
|
procedure TServerTest.TestRenderWrappedList;
|
|
|
|
|
var
|
2019-03-05 20:55:37 +01:00
|
|
|
|
lRes: IRESTResponse;
|
2018-11-24 16:56:21 +01:00
|
|
|
|
lJSONArr: System.JSON.TJSONArray;
|
2016-09-29 18:17:12 +02:00
|
|
|
|
I: Integer;
|
2018-11-24 16:56:21 +01:00
|
|
|
|
lJSONObj: System.JSON.TJSONObject;
|
2016-09-29 18:17:12 +02:00
|
|
|
|
begin
|
2019-03-05 20:55:37 +01:00
|
|
|
|
lRes := RESTClient.doGET('/wrappedpeople', []);
|
2016-09-29 18:17:12 +02:00
|
|
|
|
|
2019-03-05 20:55:37 +01:00
|
|
|
|
lJSONArr := TSystemJSON.StringAsJSONArray(lRes.BodyAsString);
|
2017-04-26 14:39:18 +02:00
|
|
|
|
try
|
|
|
|
|
for I := 0 to lJSONArr.Count - 1 do
|
|
|
|
|
begin
|
2018-11-24 16:56:21 +01:00
|
|
|
|
lJSONObj := lJSONArr.Items[I] as System.JSON.TJSONObject;
|
2017-08-20 02:36:22 +02:00
|
|
|
|
Assert.isFalse(lJSONObj.GetValue<string>('firstname').IsEmpty);
|
2017-04-26 14:39:18 +02:00
|
|
|
|
end;
|
|
|
|
|
finally
|
|
|
|
|
lJSONArr.Free;
|
2016-09-29 18:17:12 +02:00
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
end;
|
|
|
|
|
|
2018-10-23 16:18:34 +02:00
|
|
|
|
procedure TServerTest.TestRenderWrappedListWithCompression;
|
|
|
|
|
var
|
2019-03-05 20:55:37 +01:00
|
|
|
|
lRes: IRESTResponse;
|
2019-03-08 09:33:41 +01:00
|
|
|
|
lJSONArr: TJDOJsonArray;
|
2018-10-23 16:18:34 +02:00
|
|
|
|
I: Integer;
|
|
|
|
|
lCompType: string;
|
|
|
|
|
j: Integer;
|
|
|
|
|
const
|
2019-02-24 20:43:06 +01:00
|
|
|
|
CompressionTypes: array [1 .. 9] of string = ('deflate', 'gzip', 'deflate,gzip', 'gzip,deflate', 'gzip,invalid',
|
|
|
|
|
'deflate,notvalid', 'notvalid,gzip', 'invalid', '');
|
|
|
|
|
CompressionTypeResult: array [1 .. 9] of string = ('deflate', 'gzip', 'deflate', 'gzip', 'gzip', 'deflate',
|
|
|
|
|
'gzip', '', '');
|
2018-10-23 16:18:34 +02:00
|
|
|
|
begin
|
|
|
|
|
j := 1;
|
|
|
|
|
for lCompType in CompressionTypes do
|
|
|
|
|
begin
|
|
|
|
|
RESTClient.RequestHeaders.Values['Accept-Encoding'] := lCompType;
|
2019-03-05 20:55:37 +01:00
|
|
|
|
lRes := RESTClient.doGET('/wrappedpeople', [], ['count'], ['100']);
|
|
|
|
|
Assert.areEqual(CompressionTypeResult[j], lRes.HeaderValue('Content-Encoding'));
|
|
|
|
|
lJSONArr := TMVCJsonDataObjectsSerializer.ParseArray(lRes.BodyAsString);
|
2018-10-23 16:18:34 +02:00
|
|
|
|
try
|
|
|
|
|
for I := 0 to lJSONArr.Count - 1 do
|
|
|
|
|
begin
|
|
|
|
|
Assert.isFalse(lJSONArr.O[I].s['firstname'].IsEmpty);
|
|
|
|
|
Assert.isFalse(lJSONArr.O[I].s['lastname'].IsEmpty);
|
|
|
|
|
Assert.isFalse(lJSONArr.O[I].s['dob'].IsEmpty);
|
|
|
|
|
Assert.areEqual<TJsonDataType>(jdtBool, lJSONArr.O[I].Types['married']);
|
|
|
|
|
end;
|
|
|
|
|
finally
|
|
|
|
|
lJSONArr.Free;
|
|
|
|
|
end;
|
|
|
|
|
Inc(j);
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
|
2015-12-22 12:29:25 +01:00
|
|
|
|
procedure TServerTest.TestExceptionInMVCAfterCreate;
|
|
|
|
|
var
|
|
|
|
|
res: IRESTResponse;
|
|
|
|
|
begin
|
|
|
|
|
res := RESTClient.doGET('/exception/aftercreate/nevercalled', []);
|
2017-08-21 15:31:31 +02:00
|
|
|
|
Assert.areEqual<Integer>(HTTP_STATUS.InternalServerError, res.ResponseCode);
|
2015-12-22 12:29:25 +01:00
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TServerTest.TestExceptionInMVCBeforeDestroy;
|
|
|
|
|
var
|
|
|
|
|
res: IRESTResponse;
|
|
|
|
|
begin
|
|
|
|
|
res := RESTClient.doGET('/exception/beforedestroy/nevercalled', []);
|
2017-08-21 15:31:31 +02:00
|
|
|
|
Assert.areEqual<Integer>(HTTP_STATUS.InternalServerError, res.ResponseCode);
|
2015-12-22 12:29:25 +01:00
|
|
|
|
end;
|
|
|
|
|
|
2019-03-05 20:55:37 +01:00
|
|
|
|
procedure TServerTest.TestGetImagePng;
|
|
|
|
|
var
|
|
|
|
|
c1: TRESTClient;
|
|
|
|
|
lRes: IRESTResponse;
|
|
|
|
|
begin
|
|
|
|
|
c1 := TRESTClient.Create(TEST_SERVER_ADDRESS, 9999);
|
|
|
|
|
try
|
|
|
|
|
// c1.Accept(TMVCMediaType.IMAGE_PNG);
|
|
|
|
|
lRes := c1.doGET('/image/png', []);
|
|
|
|
|
Assert.areEqual(200, lRes.ResponseCode);
|
|
|
|
|
Assert.areEqual('image/png', lRes.ContentType);
|
|
|
|
|
Assert.areEqual(249, Integer(lRes.Body.Size));
|
|
|
|
|
finally
|
|
|
|
|
c1.Free;
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
|
2016-04-03 22:35:27 +02:00
|
|
|
|
procedure TServerTest.TestInvalidateSession;
|
|
|
|
|
var
|
|
|
|
|
c1: TRESTClient;
|
|
|
|
|
res: IRESTResponse;
|
|
|
|
|
begin
|
2017-04-14 16:43:31 +02:00
|
|
|
|
c1 := TRESTClient.Create(TEST_SERVER_ADDRESS, 9999);
|
2016-04-03 22:35:27 +02:00
|
|
|
|
try
|
|
|
|
|
c1.Accept(TMVCMediaType.APPLICATION_JSON);
|
|
|
|
|
c1.doPOST('/session', ['daniele teti']); // imposto un valore in sessione
|
|
|
|
|
res := c1.doGET('/session', []); // rileggo il valore dalla sessione
|
2018-05-16 19:46:29 +02:00
|
|
|
|
Assert.areEqual('daniele teti', res.BodyAsString);
|
2016-04-03 22:35:27 +02:00
|
|
|
|
c1.SessionID := '';
|
|
|
|
|
res := c1.doGET('/session', []); // rileggo il valore dalla sessione
|
2018-05-16 19:46:29 +02:00
|
|
|
|
Assert.areEqual('', res.BodyAsString);
|
2016-04-03 22:35:27 +02:00
|
|
|
|
finally
|
|
|
|
|
c1.Free;
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
|
2015-12-22 12:29:25 +01:00
|
|
|
|
procedure TServerTest.TestMiddlewareHandler;
|
|
|
|
|
var
|
|
|
|
|
r: IRESTResponse;
|
|
|
|
|
begin
|
2018-05-16 19:46:29 +02:00
|
|
|
|
r := RESTClient.Accept(TMVCMediaType.APPLICATION_JSON).doGET('/handledbymiddleware', []);
|
|
|
|
|
Assert.areEqual('This is a middleware response', r.BodyAsString);
|
2017-08-21 15:31:31 +02:00
|
|
|
|
Assert.areEqual<Integer>(HTTP_STATUS.OK, r.ResponseCode);
|
2015-12-22 12:29:25 +01:00
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TServerTest.TestMiddlewareSpeedMiddleware;
|
|
|
|
|
var
|
|
|
|
|
r: IRESTResponse;
|
|
|
|
|
P: TPerson;
|
|
|
|
|
begin
|
|
|
|
|
P := TPerson.Create;
|
|
|
|
|
try
|
|
|
|
|
P.FirstName := StringOfChar('*', 1000);
|
|
|
|
|
P.LastName := StringOfChar('*', 1000);
|
|
|
|
|
P.DOB := EncodeDate(1979, 1, 1);
|
|
|
|
|
P.Married := true;
|
2016-04-03 22:35:27 +02:00
|
|
|
|
r := RESTClient.Accept(TMVCMediaType.APPLICATION_JSON)
|
2017-04-26 14:39:18 +02:00
|
|
|
|
.doPOST('/objects', [], GetDefaultSerializer.SerializeObject(P));
|
2015-12-22 12:29:25 +01:00
|
|
|
|
finally
|
|
|
|
|
P.Free;
|
|
|
|
|
end;
|
|
|
|
|
|
2017-08-20 02:36:22 +02:00
|
|
|
|
Assert.AreNotEqual('', r.HeaderValue('request_gen_time'));
|
2015-12-22 12:29:25 +01:00
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
// procedure TServerTest.TestPATCHWithParamsAndJSONBody;
|
|
|
|
|
// var
|
|
|
|
|
// r: IRESTResponse;
|
|
|
|
|
// json: TJSONObject;
|
|
|
|
|
// begin
|
|
|
|
|
// json := TJSONObject.Create;
|
|
|
|
|
// json.AddPair('client', 'clientdata');
|
|
|
|
|
// r := RESTClient.doPATCH('/echo', ['1', '2', '3'], json);
|
2017-09-08 16:59:02 +02:00
|
|
|
|
// Assert.AreEqual('clientdata', r.BodyAsJsonObject.Get('client').JsonValue.Value);
|
|
|
|
|
// Assert.AreEqual('from server', r.BodyAsJsonObject.Get('echo').JsonValue.Value);
|
2015-12-22 12:29:25 +01:00
|
|
|
|
// end;
|
|
|
|
|
|
|
|
|
|
procedure TServerTest.TestPostAListOfObjects;
|
|
|
|
|
var
|
2019-03-05 20:55:37 +01:00
|
|
|
|
lRes: IRESTResponse;
|
2015-12-22 12:29:25 +01:00
|
|
|
|
LCustomers: TObjectList<TCustomer>;
|
2019-06-25 17:32:30 +02:00
|
|
|
|
lSer: IMVCSerializer;
|
2015-12-22 12:29:25 +01:00
|
|
|
|
begin
|
|
|
|
|
LCustomers := TCustomer.GetList;
|
|
|
|
|
try
|
2019-06-25 17:32:30 +02:00
|
|
|
|
lSer := GetDefaultSerializer;
|
|
|
|
|
RegisterOptionalCustomTypesSerializers(lSer); // TBitmap
|
|
|
|
|
lRes := RESTClient.doPOST('/customers/list', [], lSer.SerializeCollection(LCustomers));
|
2019-03-05 20:55:37 +01:00
|
|
|
|
Assert.areEqual<Integer>(HTTP_STATUS.OK, lRes.ResponseCode);
|
2015-12-22 12:29:25 +01:00
|
|
|
|
finally
|
|
|
|
|
LCustomers.Free;
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TServerTest.TestPOSTWithObjectJSONBody;
|
|
|
|
|
var
|
|
|
|
|
r: IRESTResponse;
|
|
|
|
|
P: TPerson;
|
|
|
|
|
begin
|
|
|
|
|
P := TPerson.Create;
|
|
|
|
|
try
|
|
|
|
|
P.FirstName := 'Daniele';
|
|
|
|
|
P.LastName := '<27><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>';
|
|
|
|
|
P.DOB := EncodeDate(1979, 1, 1);
|
|
|
|
|
P.Married := true;
|
|
|
|
|
try
|
2016-01-01 23:00:22 +01:00
|
|
|
|
r := RESTClient.Accept(TMVCMediaType.APPLICATION_JSON)
|
2018-05-16 19:46:29 +02:00
|
|
|
|
.doPOST('/objects', [], GetDefaultSerializer.SerializeObject(P)
|
2017-04-26 14:39:18 +02:00
|
|
|
|
{ Mapper.ObjectToJSONObject(P) }
|
|
|
|
|
);
|
2015-12-22 12:29:25 +01:00
|
|
|
|
except
|
2017-08-20 02:36:22 +02:00
|
|
|
|
Assert.Fail('If this test fail, check http://qc.embarcadero.com/wc/qcmain.aspx?d=119779');
|
2015-12-22 12:29:25 +01:00
|
|
|
|
{ WARNING!!! }
|
|
|
|
|
{
|
|
|
|
|
If this test fail, check
|
|
|
|
|
http://qc.embarcadero.com/wc/qcmain.aspx?d=119779
|
|
|
|
|
}
|
|
|
|
|
raise;
|
|
|
|
|
|
|
|
|
|
end;
|
|
|
|
|
finally
|
|
|
|
|
P.Free;
|
|
|
|
|
end;
|
2017-04-26 14:39:18 +02:00
|
|
|
|
|
|
|
|
|
P := TPerson.Create;
|
2015-12-22 12:29:25 +01:00
|
|
|
|
try
|
2017-04-26 14:39:18 +02:00
|
|
|
|
GetDefaultSerializer.DeserializeObject(r.BodyAsString, P);
|
|
|
|
|
// P := Mapper.JSONObjectToObject<TPerson>(r.BodyAsJsonObject);
|
2018-05-16 19:46:29 +02:00
|
|
|
|
Assert.areEqual('Daniele', P.FirstName);
|
|
|
|
|
Assert.areEqual('<27><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>', P.LastName);
|
|
|
|
|
Assert.areEqual(true, P.Married);
|
|
|
|
|
Assert.areEqual(EncodeDate(1979, 1, 1), P.DOB);
|
2015-12-22 12:29:25 +01:00
|
|
|
|
finally
|
|
|
|
|
P.Free;
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TServerTest.TestPOSTWithParamsAndJSONBody;
|
|
|
|
|
var
|
|
|
|
|
r: IRESTResponse;
|
2018-11-24 16:56:21 +01:00
|
|
|
|
JSON: System.JSON.TJSONObject;
|
2015-12-22 12:29:25 +01:00
|
|
|
|
begin
|
2018-11-24 16:56:21 +01:00
|
|
|
|
JSON := System.JSON.TJSONObject.Create;
|
2015-12-22 12:29:25 +01:00
|
|
|
|
JSON.AddPair('client', 'clientdata');
|
2017-04-26 14:39:18 +02:00
|
|
|
|
r := RESTClient.doPOST('/echo', ['1', '2', '3'], TSystemJSON.JSONValueToString(JSON));
|
2017-05-25 16:57:49 +02:00
|
|
|
|
JSON := TSystemJSON.StringAsJSONObject(r.BodyAsString);
|
2017-04-26 14:39:18 +02:00
|
|
|
|
try
|
2018-05-16 19:46:29 +02:00
|
|
|
|
Assert.areEqual('clientdata', JSON.Get('client').JsonValue.Value);
|
|
|
|
|
Assert.areEqual('from server', JSON.Get('echo').JsonValue.Value);
|
2017-04-26 14:39:18 +02:00
|
|
|
|
finally
|
|
|
|
|
JSON.Free;
|
|
|
|
|
end;
|
2015-12-22 12:29:25 +01:00
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TServerTest.TestProducesConsumesWithWrongAcceptHeader;
|
|
|
|
|
var
|
|
|
|
|
res: IRESTResponse;
|
|
|
|
|
begin
|
2016-04-03 22:35:27 +02:00
|
|
|
|
res := RESTClient.Accept('text/plain')
|
|
|
|
|
// action is waiting for a accept: application/json
|
|
|
|
|
.ContentType('application/json').doPOST('/testconsumes', [],
|
2017-04-26 14:39:18 +02:00
|
|
|
|
TSystemJSON.JSONValueToString(TJSONString.Create('Hello World')));
|
2017-08-21 15:31:31 +02:00
|
|
|
|
Assert.areEqual<Integer>(HTTP_STATUS.NotFound, res.ResponseCode);
|
2015-12-22 12:29:25 +01:00
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TServerTest.TestProducesConsumes01;
|
|
|
|
|
var
|
|
|
|
|
res: IRESTResponse;
|
|
|
|
|
begin
|
2018-05-16 19:46:29 +02:00
|
|
|
|
res := RESTClient.Accept('application/json').ContentType('application/json').ContentCharSet('utf-8')
|
|
|
|
|
.doPOST('/testconsumes', [], TSystemJSON.JSONValueToString(TJSONString.Create('Hello World')));
|
2017-08-21 15:31:31 +02:00
|
|
|
|
Assert.areEqual<Integer>(HTTP_STATUS.OK, res.ResponseCode);
|
2018-05-16 19:46:29 +02:00
|
|
|
|
Assert.areEqual('Hello World', res.BodyAsString);
|
|
|
|
|
Assert.areEqual('application/json', res.ContentType);
|
2018-10-23 16:18:34 +02:00
|
|
|
|
Assert.areEqual('utf-8', res.ContentTypeCharset);
|
2015-12-22 12:29:25 +01:00
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TServerTest.TestProducesConsumes02;
|
|
|
|
|
var
|
|
|
|
|
res: IRESTResponse;
|
|
|
|
|
begin
|
2018-05-16 19:46:29 +02:00
|
|
|
|
res := RESTClient.Accept('text/plain').ContentType('text/plain').doPOST('/testconsumes', [], 'Hello World');
|
|
|
|
|
Assert.areEqual('Hello World', res.BodyAsString);
|
|
|
|
|
Assert.areEqual('text/plain', res.ContentType);
|
2018-10-23 16:18:34 +02:00
|
|
|
|
Assert.areEqual('UTF-8', res.ContentTypeCharset);
|
2015-12-22 12:29:25 +01:00
|
|
|
|
|
|
|
|
|
res := RESTClient.Accept('text/plain').ContentType('application/json')
|
|
|
|
|
.doPOST('/testconsumes', [], '{"name": "Daniele"}');
|
2017-08-21 15:31:31 +02:00
|
|
|
|
Assert.areEqual<Integer>(HTTP_STATUS.NotFound, res.ResponseCode);
|
2015-12-22 12:29:25 +01:00
|
|
|
|
end;
|
|
|
|
|
|
2016-12-07 19:35:36 +01:00
|
|
|
|
procedure TServerTest.TestProducesConsumes03;
|
|
|
|
|
var
|
|
|
|
|
res: IRESTResponse;
|
|
|
|
|
begin
|
2018-05-16 19:46:29 +02:00
|
|
|
|
res := RESTClient.Accept(TMVCMediaType.TEXT_PLAIN)
|
|
|
|
|
.ContentType(BuildContentType(TMVCMediaType.TEXT_PLAIN, TMVCCharSet.ISO88591)).doPOST('/testconsumes/textiso8859_1',
|
|
|
|
|
[], '<27><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>');
|
2017-08-21 15:31:31 +02:00
|
|
|
|
Assert.areEqual<Integer>(HTTP_STATUS.OK, res.ResponseCode);
|
2017-09-08 16:59:02 +02:00
|
|
|
|
Assert.AreNotEqual('<27><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>', res.BodyAsString, 'non iso8859-1 text is rendered ok whan should not');
|
2018-05-16 19:46:29 +02:00
|
|
|
|
Assert.areEqual(TMVCMediaType.TEXT_PLAIN, res.ContentType);
|
2018-10-23 16:18:34 +02:00
|
|
|
|
Assert.areEqual(TMVCCharSet.ISO88591, res.ContentTypeCharset.ToLower);
|
2018-05-16 19:46:29 +02:00
|
|
|
|
|
|
|
|
|
res := RESTClient.Accept(TMVCMediaType.TEXT_PLAIN)
|
|
|
|
|
.ContentType(BuildContentType(TMVCMediaType.TEXT_PLAIN, TMVCCharSet.ISO88591)).doPOST('/testconsumes/textiso8859_1',
|
|
|
|
|
[], 'this is an iso8859-1 text');
|
2017-09-08 16:59:02 +02:00
|
|
|
|
Assert.areEqual<Integer>(HTTP_STATUS.OK, res.ResponseCode);
|
2018-05-16 19:46:29 +02:00
|
|
|
|
Assert.areEqual('this is an iso8859-1 text', res.BodyAsString);
|
|
|
|
|
Assert.areEqual(TMVCMediaType.TEXT_PLAIN, res.ContentType);
|
2018-10-23 16:18:34 +02:00
|
|
|
|
Assert.areEqual(TMVCCharSet.ISO88591, res.ContentTypeCharset.ToLower);
|
2016-12-07 19:35:36 +01:00
|
|
|
|
|
|
|
|
|
end;
|
|
|
|
|
|
2015-12-22 12:29:25 +01:00
|
|
|
|
procedure TServerTest.TestPUTWithParamsAndJSONBody;
|
|
|
|
|
var
|
|
|
|
|
r: IRESTResponse;
|
2018-11-24 16:56:21 +01:00
|
|
|
|
JSON: System.JSON.TJSONObject;
|
2015-12-22 12:29:25 +01:00
|
|
|
|
begin
|
2018-11-24 16:56:21 +01:00
|
|
|
|
JSON := System.JSON.TJSONObject.Create;
|
2015-12-22 12:29:25 +01:00
|
|
|
|
JSON.AddPair('client', 'clientdata');
|
2017-04-26 14:39:18 +02:00
|
|
|
|
r := RESTClient.doPUT('/echo', ['1', '2', '3'], TSystemJSON.JSONValueToString(JSON));
|
|
|
|
|
|
2017-05-25 16:57:49 +02:00
|
|
|
|
JSON := TSystemJSON.StringAsJSONObject(r.BodyAsString);
|
2017-04-26 14:39:18 +02:00
|
|
|
|
try
|
2018-05-16 19:46:29 +02:00
|
|
|
|
Assert.areEqual('clientdata', JSON.Get('client').JsonValue.Value);
|
|
|
|
|
Assert.areEqual('from server', JSON.Get('echo').JsonValue.Value);
|
2017-04-26 14:39:18 +02:00
|
|
|
|
finally
|
|
|
|
|
JSON.Free;
|
|
|
|
|
end;
|
2015-12-22 12:29:25 +01:00
|
|
|
|
end;
|
|
|
|
|
|
2019-05-10 00:46:03 +02:00
|
|
|
|
procedure TServerTest.TestXHTTPMethodOverride_POST_as_PUT;
|
|
|
|
|
var
|
|
|
|
|
r: IRESTResponse;
|
|
|
|
|
JSON: System.JSON.TJSONObject;
|
|
|
|
|
begin
|
|
|
|
|
JSON := System.JSON.TJSONObject.Create;
|
|
|
|
|
JSON.AddPair('client', 'clientdata');
|
|
|
|
|
r := RESTClient
|
|
|
|
|
.Header(TMVCConstants.X_HTTP_Method_Override, 'PUT')
|
|
|
|
|
.doPOST('/echo', ['1', '2', '3'], TSystemJSON.JSONValueToString(JSON));
|
|
|
|
|
|
|
|
|
|
JSON := TSystemJSON.StringAsJSONObject(r.BodyAsString);
|
|
|
|
|
try
|
|
|
|
|
Assert.areEqual('clientdata', JSON.Get('client').JsonValue.Value);
|
|
|
|
|
Assert.areEqual('from server', JSON.Get('echo').JsonValue.Value);
|
|
|
|
|
finally
|
|
|
|
|
JSON.Free;
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
|
2015-12-22 12:29:25 +01:00
|
|
|
|
procedure TServerTest.TestReqWithParams;
|
|
|
|
|
var
|
2016-05-13 17:20:08 +02:00
|
|
|
|
ss: TStringStream;
|
2018-11-24 16:56:21 +01:00
|
|
|
|
lJSON: System.JSON.TJSONObject;
|
2017-11-23 17:29:51 +01:00
|
|
|
|
r: IRESTResponse;
|
2015-12-22 12:29:25 +01:00
|
|
|
|
begin
|
|
|
|
|
r := RESTClient.doGET('/unknownurl/bla/bla', []);
|
2016-05-13 17:20:08 +02:00
|
|
|
|
|
|
|
|
|
ss := TStringStream.Create;
|
|
|
|
|
try
|
|
|
|
|
ss.CopyFrom(r.Body, 0);
|
2018-05-16 19:46:29 +02:00
|
|
|
|
Assert.areEqual(ss.DataString, r.BodyAsString,
|
2017-11-23 17:29:51 +01:00
|
|
|
|
'In case of protocol error, the body doesn''t contain the same of BodyAsString');
|
2016-05-13 17:20:08 +02:00
|
|
|
|
finally
|
|
|
|
|
ss.Free;
|
|
|
|
|
end;
|
|
|
|
|
|
2017-08-21 15:31:31 +02:00
|
|
|
|
Assert.areEqual<Integer>(HTTP_STATUS.NotFound, r.ResponseCode, '/unknownurl/bla/bla');
|
2015-12-22 12:29:25 +01:00
|
|
|
|
|
|
|
|
|
r := RESTClient.doGET('/req/with/params/', []);
|
2017-08-21 15:31:31 +02:00
|
|
|
|
Assert.areEqual<Integer>(HTTP_STATUS.NotFound, r.ResponseCode, '/req/with/params/');
|
2015-12-22 12:29:25 +01:00
|
|
|
|
|
|
|
|
|
r := RESTClient.doGET('/req/with/params', []);
|
2017-08-21 15:31:31 +02:00
|
|
|
|
Assert.areEqual<Integer>(HTTP_STATUS.NotFound, r.ResponseCode, '/req/with/params');
|
2015-12-22 12:29:25 +01:00
|
|
|
|
|
|
|
|
|
r := RESTClient.doGET('/req/with/params', ['1', '2', '3']);
|
2017-08-21 15:31:31 +02:00
|
|
|
|
Assert.areEqual<Integer>(HTTP_STATUS.OK, r.ResponseCode);
|
2015-12-22 12:29:25 +01:00
|
|
|
|
|
2017-05-25 16:57:49 +02:00
|
|
|
|
lJSON := TSystemJSON.StringAsJSONObject(r.BodyAsString);
|
2017-04-26 14:39:18 +02:00
|
|
|
|
try
|
2018-05-16 19:46:29 +02:00
|
|
|
|
Assert.areEqual('1', lJSON.Get('par1').JsonValue.Value);
|
|
|
|
|
Assert.areEqual('2', lJSON.Get('par2').JsonValue.Value);
|
|
|
|
|
Assert.areEqual('3', lJSON.Get('par3').JsonValue.Value);
|
|
|
|
|
Assert.areEqual('GET', lJSON.Get('method').JsonValue.Value);
|
2015-12-22 12:29:25 +01:00
|
|
|
|
|
2017-04-26 14:39:18 +02:00
|
|
|
|
r := RESTClient.doPOST('/req/with/params', ['1', '2', '3']);
|
2017-08-21 15:31:31 +02:00
|
|
|
|
Assert.areEqual<Integer>(HTTP_STATUS.NotFound, r.ResponseCode);
|
2015-12-22 12:29:25 +01:00
|
|
|
|
|
2017-04-26 14:39:18 +02:00
|
|
|
|
r := RESTClient.doPUT('/req/with/params', ['1', '2', '3']);
|
2017-08-21 15:31:31 +02:00
|
|
|
|
Assert.areEqual<Integer>(HTTP_STATUS.NotFound, r.ResponseCode);
|
2017-04-26 14:39:18 +02:00
|
|
|
|
|
|
|
|
|
r := RESTClient.doDELETE('/req/with/params', ['1', '2', '3']);
|
2017-08-21 15:31:31 +02:00
|
|
|
|
Assert.areEqual<Integer>(HTTP_STATUS.OK, r.ResponseCode);
|
2018-05-16 19:46:29 +02:00
|
|
|
|
Assert.areEqual('', r.BodyAsString);
|
2017-04-26 14:39:18 +02:00
|
|
|
|
finally
|
|
|
|
|
lJSON.Free;
|
|
|
|
|
end;
|
2017-11-23 17:29:51 +01:00
|
|
|
|
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TServerTest.TestReqWithURLMappedParams(const par1, par2, par3: string);
|
|
|
|
|
var
|
|
|
|
|
r: IRESTResponse;
|
|
|
|
|
begin
|
|
|
|
|
r := RESTClient.doGET('/req/with/params', [par1, par2, par3]);
|
|
|
|
|
Assert.areEqual<Integer>(HTTP_STATUS.OK, r.ResponseCode,
|
|
|
|
|
Format('URL mapped fails for these characters: "%s","%s","%s"', [par1, par2, par3]));
|
2015-12-22 12:29:25 +01:00
|
|
|
|
end;
|
|
|
|
|
|
2019-09-01 20:35:19 +02:00
|
|
|
|
procedure TServerTest.TestResponseAccepted;
|
|
|
|
|
var
|
|
|
|
|
r: IRESTResponse;
|
|
|
|
|
lJSON: TJDOJsonObject;
|
|
|
|
|
begin
|
|
|
|
|
r := RESTClient.doPOST('/responses/accepted', []);
|
|
|
|
|
Assert.areEqual<Integer>(HTTP_STATUS.Accepted, r.ResponseCode);
|
|
|
|
|
Assert.isTrue(r.ResponseText.Contains('thisisthereason'));
|
|
|
|
|
lJSON := StringToJSON(r.BodyAsString);
|
|
|
|
|
try
|
|
|
|
|
Assert.areEqual(2, lJSON.O['task'].Count);
|
|
|
|
|
Assert.areEqual('http://pippo.it/1234', lJSON.O['task'].s['href']);
|
|
|
|
|
Assert.areEqual('1234', lJSON.O['task'].s['id']);
|
|
|
|
|
finally
|
|
|
|
|
lJSON.Free;
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TServerTest.TestResponseCreated;
|
|
|
|
|
var
|
|
|
|
|
r: IRESTResponse;
|
|
|
|
|
begin
|
|
|
|
|
r := RESTClient.doPOST('/responses/created', []);
|
|
|
|
|
Assert.areEqual<Integer>(HTTP_STATUS.Created, r.ResponseCode);
|
|
|
|
|
Assert.isTrue(r.ResponseText.Contains('thisisthereason'));
|
|
|
|
|
Assert.IsEmpty(r.BodyAsString);
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TServerTest.TestResponseNoContent;
|
|
|
|
|
var
|
|
|
|
|
r: IRESTResponse;
|
|
|
|
|
begin
|
|
|
|
|
r := RESTClient.doGET('/responses/nocontent', []);
|
|
|
|
|
Assert.areEqual<Integer>(HTTP_STATUS.NoContent, r.ResponseCode);
|
|
|
|
|
Assert.isTrue(r.ResponseText.Contains('thisisthereason'));
|
|
|
|
|
Assert.IsEmpty(r.BodyAsString);
|
|
|
|
|
end;
|
|
|
|
|
|
2017-04-14 16:43:31 +02:00
|
|
|
|
// procedure TServerTest.TestSerializationType;
|
|
|
|
|
// var
|
|
|
|
|
// LResp: IRESTResponse;
|
|
|
|
|
// LPersonProps, LPersonFlds: TPerson;
|
|
|
|
|
// LObj: TObject;
|
|
|
|
|
// begin
|
|
|
|
|
// LResp := RESTClient.doGET('/people', ['1']);
|
|
|
|
|
// LPersonProps := Mapper.JSONObjectToObject<TPerson>(LResp.BodyAsJsonObject);
|
|
|
|
|
// try
|
|
|
|
|
// LResp := RESTClient.doGET('/people', ['1', 'asfields']);
|
|
|
|
|
// LObj := Mapper.JSONObjectFieldsToObject(LResp.BodyAsJsonObject);
|
|
|
|
|
// try
|
2017-09-08 16:59:02 +02:00
|
|
|
|
// Assert.AreEqual('BusinessObjectsU.TPerson', LObj.QualifiedClassName);
|
2017-04-14 16:43:31 +02:00
|
|
|
|
// LPersonFlds := TPerson(LObj);
|
2017-08-20 02:36:22 +02:00
|
|
|
|
// Assert.isTrue(LPersonFlds.Equals(LPersonProps),
|
2017-04-14 16:43:31 +02:00
|
|
|
|
// 'Object tranferred using field serialization is different from the object serialized in the default way');
|
|
|
|
|
// finally
|
|
|
|
|
// LObj.Free;
|
|
|
|
|
// end;
|
|
|
|
|
// finally
|
|
|
|
|
// LPersonProps.Free;
|
|
|
|
|
// end;
|
|
|
|
|
// end;
|
2015-12-22 12:29:25 +01:00
|
|
|
|
|
|
|
|
|
procedure TServerTest.TestSession;
|
|
|
|
|
var
|
|
|
|
|
c1: TRESTClient;
|
|
|
|
|
res: IRESTResponse;
|
2017-06-22 16:18:58 +02:00
|
|
|
|
s: string;
|
2015-12-22 12:29:25 +01:00
|
|
|
|
begin
|
2017-04-14 16:43:31 +02:00
|
|
|
|
c1 := TRESTClient.Create(TEST_SERVER_ADDRESS, 9999);
|
2015-12-22 12:29:25 +01:00
|
|
|
|
try
|
2016-01-01 23:00:22 +01:00
|
|
|
|
c1.Accept(TMVCMediaType.APPLICATION_JSON);
|
2017-06-22 16:18:58 +02:00
|
|
|
|
res := c1.doPOST('/session', ['daniele teti']); // imposto un valore in sessione
|
|
|
|
|
s := res.HeaderValue('Set-Cookie');
|
2017-08-20 02:36:22 +02:00
|
|
|
|
Assert.isFalse(s.Contains('Expires'), 'Session cookie contains "expires" attribute');
|
2015-12-22 12:29:25 +01:00
|
|
|
|
res := c1.doGET('/session', []); // rileggo il valore dalla sessione
|
2018-05-16 19:46:29 +02:00
|
|
|
|
Assert.areEqual('daniele teti', res.BodyAsString);
|
2016-01-01 23:00:22 +01:00
|
|
|
|
c1.Accept(TMVCMediaType.TEXT_PLAIN);
|
2015-12-22 12:29:25 +01:00
|
|
|
|
res := c1.doGET('/session', []);
|
|
|
|
|
// rileggo il valore dalla sessione
|
2018-05-16 19:46:29 +02:00
|
|
|
|
Assert.areEqual('daniele teti', res.BodyAsString);
|
2015-12-22 12:29:25 +01:00
|
|
|
|
|
|
|
|
|
// aggiungo altri cookies
|
|
|
|
|
res := c1.doGET('/lotofcookies', []); // rileggo il valore dalla sessione
|
2017-08-21 15:31:31 +02:00
|
|
|
|
Assert.areEqual<Integer>(HTTP_STATUS.OK, res.ResponseCode);
|
2016-01-01 23:00:22 +01:00
|
|
|
|
c1.Accept(TMVCMediaType.TEXT_PLAIN);
|
2015-12-22 12:29:25 +01:00
|
|
|
|
res := c1.doGET('/session', []); // rileggo il valore dalla sessione
|
2018-05-16 19:46:29 +02:00
|
|
|
|
Assert.areEqual('daniele teti', res.BodyAsString);
|
2015-12-22 12:29:25 +01:00
|
|
|
|
finally
|
|
|
|
|
c1.Free;
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
|
2016-06-28 13:42:14 +02:00
|
|
|
|
procedure TServerTest.TestSessionWithLogin;
|
|
|
|
|
begin
|
|
|
|
|
DoLoginWith('daniele');
|
|
|
|
|
DoLogout;
|
|
|
|
|
end;
|
|
|
|
|
|
2018-10-31 01:07:23 +01:00
|
|
|
|
procedure TServerTest.TestStringDictionary;
|
|
|
|
|
var
|
2019-03-05 20:55:37 +01:00
|
|
|
|
lRes: IRESTResponse;
|
2018-10-31 01:07:23 +01:00
|
|
|
|
lSer: TMVCJsonDataObjectsSerializer;
|
|
|
|
|
lDict: TMVCStringDictionary;
|
|
|
|
|
begin
|
2019-03-05 20:55:37 +01:00
|
|
|
|
lRes := RESTClient.doPOST('/stringdictionary', [], '{"prop1":"value1","prop2":"value2"}');
|
|
|
|
|
Assert.areEqual(200, lRes.ResponseCode);
|
2018-10-31 01:07:23 +01:00
|
|
|
|
lSer := TMVCJsonDataObjectsSerializer.Create;
|
|
|
|
|
try
|
|
|
|
|
lDict := TMVCStringDictionary.Create;
|
|
|
|
|
try
|
2019-03-05 20:55:37 +01:00
|
|
|
|
lSer.DeserializeObject(lRes.BodyAsString, lDict);
|
2018-10-31 01:07:23 +01:00
|
|
|
|
Assert.areEqual(3, lDict.Count);
|
|
|
|
|
Assert.areEqual('value1', lDict['prop1']);
|
|
|
|
|
Assert.areEqual('value2', lDict['prop2']);
|
|
|
|
|
Assert.areEqual('changed', lDict['fromserver']);
|
|
|
|
|
finally
|
|
|
|
|
lDict.Free;
|
|
|
|
|
end;
|
|
|
|
|
finally
|
|
|
|
|
lSer.Free;
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
|
2016-06-22 17:49:16 +02:00
|
|
|
|
procedure TServerTest.TestTypedAll;
|
|
|
|
|
var
|
|
|
|
|
res: IRESTResponse;
|
2018-11-24 16:56:21 +01:00
|
|
|
|
lJObj: System.JSON.TJSONObject;
|
2016-06-22 17:49:16 +02:00
|
|
|
|
begin
|
|
|
|
|
// ----------------------'/typed/all/($ParString)/($ParInteger)/($ParInt64)/($ParSingle)/($ParDouble)/($ParExtended)')', []);
|
2018-05-16 19:46:29 +02:00
|
|
|
|
res := RESTClient.doGET('/typed/all/mystring/1234/12345678/12.3/1234.5678/1234.5678', []);
|
2017-08-20 02:36:22 +02:00
|
|
|
|
Assert.isTrue(res.ResponseCode = HTTP_STATUS.OK, 'Cannot route');
|
2017-05-25 16:57:49 +02:00
|
|
|
|
lJObj := TSystemJSON.StringAsJSONObject(res.BodyAsString);
|
2017-04-26 14:39:18 +02:00
|
|
|
|
try
|
2018-05-16 19:46:29 +02:00
|
|
|
|
Assert.areEqual('mystring', lJObj.GetValue('ParString').Value, 'ParString');
|
|
|
|
|
Assert.areEqual(1234, TJSONNumber(lJObj.GetValue('ParInteger')).AsInt, 'ParInteger');
|
|
|
|
|
Assert.areEqual(int64(12345678), TJSONNumber(lJObj.GetValue('ParInt64')).AsInt64, 'ParInt64');
|
|
|
|
|
Assert.areEqual(12.3, RoundTo(TJSONNumber(lJObj.GetValue('ParSingle')).AsDouble, -1), 'ParSingle');
|
|
|
|
|
Assert.areEqual(1234.5678, RoundTo(TJSONNumber(lJObj.GetValue('ParDouble')).AsDouble, -4), 'ParDouble');
|
|
|
|
|
Assert.areEqual(1234.5678, RoundTo(TJSONNumber(lJObj.GetValue('ParExtended')).AsDouble, -4), 'ParExtended');
|
2017-04-26 14:39:18 +02:00
|
|
|
|
finally
|
|
|
|
|
lJObj.Free;
|
|
|
|
|
end;
|
2016-06-22 17:49:16 +02:00
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TServerTest.TestTypedBooleans;
|
|
|
|
|
var
|
|
|
|
|
res: IRESTResponse;
|
|
|
|
|
begin
|
|
|
|
|
res := RESTClient.doGET('/typed/booleans/true/false/1/0', []);
|
2017-08-20 02:36:22 +02:00
|
|
|
|
Assert.isTrue(res.ResponseCode = HTTP_STATUS.OK, 'Cannot route');
|
2018-05-16 19:46:29 +02:00
|
|
|
|
Assert.areEqual('true.false.true.false', res.BodyAsString.ToLower);
|
2016-06-22 17:49:16 +02:00
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TServerTest.TestTypedDouble1;
|
|
|
|
|
var
|
|
|
|
|
res: IRESTResponse;
|
|
|
|
|
begin
|
|
|
|
|
res := RESTClient.doGET('/typed/double1/1234.5678', []);
|
2017-08-20 02:36:22 +02:00
|
|
|
|
Assert.isTrue(res.ResponseCode = HTTP_STATUS.OK, 'Cannot route');
|
2018-05-16 19:46:29 +02:00
|
|
|
|
Assert.areEqual('1234.5678 modified from server', res.BodyAsString);
|
2016-06-22 17:49:16 +02:00
|
|
|
|
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TServerTest.TestTypedExtended1;
|
|
|
|
|
var
|
|
|
|
|
res: IRESTResponse;
|
|
|
|
|
begin
|
|
|
|
|
res := RESTClient.doGET('/typed/extended1/1234.5678', []);
|
2017-08-20 02:36:22 +02:00
|
|
|
|
Assert.isTrue(res.ResponseCode = HTTP_STATUS.OK, 'Cannot route');
|
2018-05-16 19:46:29 +02:00
|
|
|
|
Assert.areEqual('1234.5678 modified from server', res.BodyAsString);
|
2016-06-22 17:49:16 +02:00
|
|
|
|
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TServerTest.TestTypedInt641;
|
|
|
|
|
var
|
|
|
|
|
res: IRESTResponse;
|
|
|
|
|
begin
|
|
|
|
|
res := RESTClient.doGET('/typed/int641/12345678', []);
|
2017-08-20 02:36:22 +02:00
|
|
|
|
Assert.isTrue(res.ResponseCode = HTTP_STATUS.OK, 'Cannot route');
|
2018-05-16 19:46:29 +02:00
|
|
|
|
Assert.areEqual('12345678 modified from server', res.BodyAsString);
|
2016-06-22 17:49:16 +02:00
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TServerTest.TestTypedInteger1;
|
|
|
|
|
var
|
|
|
|
|
res: IRESTResponse;
|
|
|
|
|
begin
|
|
|
|
|
res := RESTClient.doGET('/typed/integer1/1234', []);
|
2017-08-20 02:36:22 +02:00
|
|
|
|
Assert.isTrue(res.ResponseCode = HTTP_STATUS.OK, 'Cannot route');
|
2018-05-16 19:46:29 +02:00
|
|
|
|
Assert.areEqual('1234 modified from server', res.BodyAsString);
|
2016-06-22 17:49:16 +02:00
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TServerTest.TestTypedSingle1;
|
|
|
|
|
var
|
|
|
|
|
res: IRESTResponse;
|
|
|
|
|
begin
|
|
|
|
|
res := RESTClient.doGET('/typed/single1/1234.5', []);
|
2017-08-20 02:36:22 +02:00
|
|
|
|
Assert.isTrue(res.ResponseCode = HTTP_STATUS.OK, 'Cannot route');
|
2018-05-16 19:46:29 +02:00
|
|
|
|
Assert.areEqual('1234.5 modified from server', res.BodyAsString);
|
2016-06-22 17:49:16 +02:00
|
|
|
|
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TServerTest.TestTypedString1;
|
|
|
|
|
var
|
|
|
|
|
res: IRESTResponse;
|
|
|
|
|
begin
|
|
|
|
|
res := RESTClient.doGET('/typed/string1/daniele', []);
|
2017-08-20 02:36:22 +02:00
|
|
|
|
Assert.isTrue(res.ResponseCode = HTTP_STATUS.OK, 'Cannot route');
|
2018-05-16 19:46:29 +02:00
|
|
|
|
Assert.areEqual('daniele modified from server', res.BodyAsString);
|
2016-06-22 17:49:16 +02:00
|
|
|
|
end;
|
|
|
|
|
|
2018-10-31 01:07:23 +01:00
|
|
|
|
procedure TServerTest.TestWrongJSONBody;
|
|
|
|
|
var
|
2019-03-05 20:55:37 +01:00
|
|
|
|
lRes: IRESTResponse;
|
2018-10-31 01:07:23 +01:00
|
|
|
|
begin
|
2019-03-05 20:55:37 +01:00
|
|
|
|
lRes := RESTClient.doPOST('/stringdictionary', [], '{"prop1","value1"}');
|
|
|
|
|
Assert.areEqual(HTTP_STATUS.BadRequest, lRes.ResponseCode);
|
2018-10-31 01:07:23 +01:00
|
|
|
|
end;
|
|
|
|
|
|
2016-06-22 17:49:16 +02:00
|
|
|
|
procedure TServerTest.TestTypedDateTimeTypes;
|
|
|
|
|
var
|
|
|
|
|
res: IRESTResponse;
|
|
|
|
|
begin
|
|
|
|
|
// TDate, wrong and correct
|
|
|
|
|
res := RESTClient.doGET('/typed/tdate1/20161012', []);
|
2017-08-21 15:31:31 +02:00
|
|
|
|
Assert.areEqual<Integer>(HTTP_STATUS.InternalServerError, res.ResponseCode, 'wrong TDate');
|
2016-06-22 17:49:16 +02:00
|
|
|
|
|
|
|
|
|
res := RESTClient.doGET('/typed/tdate1/2016-10-12', []);
|
2017-08-21 15:31:31 +02:00
|
|
|
|
Assert.areEqual<Integer>(HTTP_STATUS.OK, res.ResponseCode);
|
2018-05-16 19:46:29 +02:00
|
|
|
|
Assert.areEqual('2016-10-12 modified from server', res.BodyAsString);
|
2016-06-22 17:49:16 +02:00
|
|
|
|
|
|
|
|
|
// TDateTime, wrong and correct
|
2017-08-29 11:54:22 +02:00
|
|
|
|
res := RESTClient.doGET('/typed/tdatetime1/20161', []);
|
2018-05-16 19:46:29 +02:00
|
|
|
|
Assert.areEqual<Integer>(HTTP_STATUS.InternalServerError, res.ResponseCode, 'wrong TDateTime (1)');
|
2017-08-29 11:54:22 +02:00
|
|
|
|
|
2017-09-08 16:59:02 +02:00
|
|
|
|
// Wrong
|
2017-08-29 11:54:22 +02:00
|
|
|
|
res := RESTClient.doGET('/typed/tdatetime1/20161012121212', []);
|
|
|
|
|
Assert.areEqual<Integer>(HTTP_STATUS.InternalServerError, res.ResponseCode, 'wrong TDateTime (2)');
|
2016-06-22 17:49:16 +02:00
|
|
|
|
|
2017-09-08 16:59:02 +02:00
|
|
|
|
// Correct without 'T'
|
2016-06-22 17:49:16 +02:00
|
|
|
|
res := RESTClient.doGET('/typed/tdatetime1/2016-10-12 12:12:12', []);
|
2017-08-29 11:54:22 +02:00
|
|
|
|
Assert.areEqual<Integer>(HTTP_STATUS.OK, res.ResponseCode, 'wrong TDateTime (3)');
|
2018-05-16 19:46:29 +02:00
|
|
|
|
Assert.areEqual('2016-10-12T12:12:12.000Z modified from server', res.BodyAsString);
|
2017-08-29 11:54:22 +02:00
|
|
|
|
|
2017-09-08 16:59:02 +02:00
|
|
|
|
// Correct in extended form
|
2017-08-29 11:54:22 +02:00
|
|
|
|
res := RESTClient.doGET('/typed/tdatetime1/2016-10-12T12:12:12', []);
|
|
|
|
|
Assert.areEqual<Integer>(HTTP_STATUS.OK, res.ResponseCode);
|
2018-05-16 19:46:29 +02:00
|
|
|
|
Assert.areEqual('2016-10-12T12:12:12.000Z modified from server', res.BodyAsString);
|
2017-08-29 11:54:22 +02:00
|
|
|
|
|
2017-09-08 16:59:02 +02:00
|
|
|
|
// Correct in extended form with timezone
|
2017-08-29 11:54:22 +02:00
|
|
|
|
res := RESTClient.doGET('/typed/tdatetime1/2016-10-12T12:12:12.000Z', []);
|
2017-08-21 15:31:31 +02:00
|
|
|
|
Assert.areEqual<Integer>(HTTP_STATUS.OK, res.ResponseCode);
|
2018-05-16 19:46:29 +02:00
|
|
|
|
Assert.areEqual('2016-10-12T12:12:12.000Z modified from server', res.BodyAsString);
|
2016-06-22 17:49:16 +02:00
|
|
|
|
|
|
|
|
|
// TTime, wrong and correct
|
|
|
|
|
res := RESTClient.doGET('/typed/ttime1/121212', []);
|
2017-08-21 15:31:31 +02:00
|
|
|
|
Assert.areEqual<Integer>(HTTP_STATUS.InternalServerError, res.ResponseCode, 'wrong TTime');
|
2016-06-22 17:49:16 +02:00
|
|
|
|
|
|
|
|
|
res := RESTClient.doGET('/typed/ttime1/12:12:12', []);
|
2017-08-21 15:31:31 +02:00
|
|
|
|
Assert.areEqual<Integer>(HTTP_STATUS.OK, res.ResponseCode);
|
2018-05-16 19:46:29 +02:00
|
|
|
|
Assert.areEqual('12:12:12 modified from server', res.BodyAsString);
|
2016-06-22 17:49:16 +02:00
|
|
|
|
|
|
|
|
|
end;
|
|
|
|
|
|
2015-12-22 12:29:25 +01:00
|
|
|
|
procedure TBaseServerTest.DoLoginWith(UserName: string);
|
|
|
|
|
var
|
|
|
|
|
res: IRESTResponse;
|
|
|
|
|
begin
|
|
|
|
|
res := RESTClient.doGET('/login', [UserName]);
|
2017-08-20 02:36:22 +02:00
|
|
|
|
Assert.isTrue(res.ResponseCode = HTTP_STATUS.OK, 'Login Failed');
|
2015-12-22 12:29:25 +01:00
|
|
|
|
end;
|
|
|
|
|
|
2017-09-26 01:02:09 +02:00
|
|
|
|
{ TJSONRPCServerTest }
|
|
|
|
|
|
2018-05-16 19:46:29 +02:00
|
|
|
|
procedure TJSONRPCServerTest.SetUp;
|
|
|
|
|
begin
|
|
|
|
|
FExecutor := TMVCJSONRPCExecutor.Create('http://localhost:9999/jsonrpc', false);
|
2019-01-18 18:11:27 +01:00
|
|
|
|
FExecutor2 := TMVCJSONRPCExecutor.Create('http://localhost:9999/jsonrpcclass', false);
|
2018-05-16 19:46:29 +02:00
|
|
|
|
end;
|
|
|
|
|
|
2017-09-26 01:02:09 +02:00
|
|
|
|
procedure TJSONRPCServerTest.TestRequestToNotFoundMethod;
|
|
|
|
|
var
|
2019-01-08 12:48:27 +01:00
|
|
|
|
lReq: IJSONRPCRequest;
|
|
|
|
|
lResp: IJSONRPCResponse;
|
2017-09-26 01:02:09 +02:00
|
|
|
|
begin
|
2018-05-16 19:46:29 +02:00
|
|
|
|
lReq := TJSONRPCRequest.Create;
|
2019-01-08 12:48:27 +01:00
|
|
|
|
lReq.Method := 'nonexist';
|
|
|
|
|
lReq.RequestID := 1234;
|
|
|
|
|
lResp := FExecutor.ExecuteRequest(lReq);
|
|
|
|
|
Assert.IsNotNull(lResp.Error);
|
|
|
|
|
Assert.areEqual(-32601, lResp.Error.Code);
|
|
|
|
|
Assert.isTrue(lResp.Error.ErrMessage.StartsWith('Method "nonexist" not found.'));
|
2017-09-26 01:02:09 +02:00
|
|
|
|
end;
|
|
|
|
|
|
2019-01-18 18:11:27 +01:00
|
|
|
|
procedure TJSONRPCServerTest.TestRequestWithParams_DT_T_ret_DT;
|
|
|
|
|
var
|
|
|
|
|
lReq: IJSONRPCRequest;
|
|
|
|
|
lRPCResp: IJSONRPCResponse;
|
2019-03-05 20:55:37 +01:00
|
|
|
|
lRes: TDateTime;
|
2019-01-18 18:11:27 +01:00
|
|
|
|
lYear: Word;
|
|
|
|
|
lMonth: Word;
|
|
|
|
|
lDay: Word;
|
|
|
|
|
lHour: Word;
|
|
|
|
|
lMinute: Word;
|
|
|
|
|
lSecond: Word;
|
|
|
|
|
lMillisecond: Word;
|
|
|
|
|
begin
|
|
|
|
|
lReq := TJSONRPCRequest.Create;
|
|
|
|
|
lReq.Method := 'addtimetodatetime';
|
|
|
|
|
lReq.Params.Add(EncodeDate(2000, 10, 1) + EncodeTime(12, 0, 0, 0), TJSONRPCParamDataType.pdtDateTime);
|
|
|
|
|
lReq.Params.Add(EncodeTime(1, 0, 0, 0), TJSONRPCParamDataType.pdtTime);
|
|
|
|
|
lReq.RequestID := 1234;
|
|
|
|
|
|
|
|
|
|
lRPCResp := FExecutor2.ExecuteRequest(lReq);
|
2019-03-05 20:55:37 +01:00
|
|
|
|
lRes := lRPCResp.Result.AsType<TDateTime>();
|
|
|
|
|
DecodeDateTime(lRes, lYear, lMonth, lDay, lHour, lMinute, lSecond, lMillisecond);
|
2019-02-24 20:43:06 +01:00
|
|
|
|
Assert.areEqual(2000, lYear);
|
2019-01-18 18:11:27 +01:00
|
|
|
|
end;
|
|
|
|
|
|
2017-09-26 01:02:09 +02:00
|
|
|
|
procedure TJSONRPCServerTest.TestRequestWithoutParams;
|
|
|
|
|
var
|
2019-01-08 12:48:27 +01:00
|
|
|
|
lReq: IJSONRPCNotification;
|
2017-09-26 01:02:09 +02:00
|
|
|
|
begin
|
2018-05-16 19:46:29 +02:00
|
|
|
|
lReq := TJSONRPCNotification.Create;
|
2019-01-08 12:48:27 +01:00
|
|
|
|
lReq.Method := 'mynotify';
|
|
|
|
|
FExecutor.ExecuteNotification(lReq);
|
2019-01-18 18:11:27 +01:00
|
|
|
|
FExecutor2.ExecuteNotification(lReq);
|
2019-01-08 12:48:27 +01:00
|
|
|
|
Assert.Pass();
|
2017-09-26 01:02:09 +02:00
|
|
|
|
end;
|
|
|
|
|
|
2017-09-28 00:14:34 +02:00
|
|
|
|
procedure TJSONRPCServerTest.TestRequestWithParams_I_I_ret_I;
|
|
|
|
|
var
|
2019-01-08 12:48:27 +01:00
|
|
|
|
lReq: IJSONRPCRequest;
|
|
|
|
|
lResp: IJSONRPCResponse;
|
2017-09-26 01:02:09 +02:00
|
|
|
|
begin
|
2017-09-28 00:14:34 +02:00
|
|
|
|
lReq := TJSONRPCRequest.Create;
|
2019-01-08 12:48:27 +01:00
|
|
|
|
lReq.RequestID := 1234;
|
|
|
|
|
lReq.Method := 'subtract';
|
|
|
|
|
lReq.Params.Add(18);
|
|
|
|
|
lReq.Params.Add(8);
|
|
|
|
|
lResp := FExecutor.ExecuteRequest(lReq);
|
|
|
|
|
Assert.areEqual(10, lResp.Result.AsInteger);
|
|
|
|
|
Assert.areEqual(1234, lResp.RequestID.AsInteger);
|
2019-01-18 18:11:27 +01:00
|
|
|
|
|
|
|
|
|
lResp := FExecutor2.ExecuteRequest(lReq);
|
|
|
|
|
Assert.areEqual(10, lResp.Result.AsInteger);
|
|
|
|
|
Assert.areEqual(1234, lResp.RequestID.AsInteger);
|
2017-09-26 01:02:09 +02:00
|
|
|
|
end;
|
|
|
|
|
|
2017-09-28 00:14:34 +02:00
|
|
|
|
procedure TJSONRPCServerTest.TestRequestWithParams_I_I_ret_A;
|
|
|
|
|
var
|
2019-01-08 12:48:27 +01:00
|
|
|
|
lReq: IJSONRPCRequest;
|
|
|
|
|
lRPCResp: IJSONRPCResponse;
|
2019-03-08 09:33:41 +01:00
|
|
|
|
lArr: TJDOJsonArray;
|
2017-09-28 00:14:34 +02:00
|
|
|
|
I: Integer;
|
|
|
|
|
x: Integer;
|
|
|
|
|
begin
|
|
|
|
|
lReq := TJSONRPCRequest.Create;
|
2019-01-08 12:48:27 +01:00
|
|
|
|
lReq.Method := 'GetListFromTo';
|
|
|
|
|
lReq.Params.Add(1);
|
|
|
|
|
lReq.Params.Add(5);
|
|
|
|
|
lReq.RequestID := 1234;
|
2019-01-18 18:11:27 +01:00
|
|
|
|
|
2019-01-08 12:48:27 +01:00
|
|
|
|
lRPCResp := FExecutor.ExecuteRequest(lReq);
|
2019-03-08 09:33:41 +01:00
|
|
|
|
lArr := TJDOJsonArray(lRPCResp.Result.AsObject);
|
2019-01-08 12:48:27 +01:00
|
|
|
|
x := 1;
|
|
|
|
|
for I := 0 to lArr.Count - 1 do
|
|
|
|
|
begin
|
|
|
|
|
Assert.areEqual(x, lArr[I].IntValue);
|
|
|
|
|
Inc(x);
|
2017-09-28 00:14:34 +02:00
|
|
|
|
end;
|
2019-01-18 18:11:27 +01:00
|
|
|
|
|
|
|
|
|
lRPCResp := FExecutor2.ExecuteRequest(lReq);
|
2019-03-08 09:33:41 +01:00
|
|
|
|
lArr := TJDOJsonArray(lRPCResp.Result.AsObject);
|
2019-01-18 18:11:27 +01:00
|
|
|
|
x := 1;
|
|
|
|
|
for I := 0 to lArr.Count - 1 do
|
|
|
|
|
begin
|
|
|
|
|
Assert.areEqual(x, lArr[I].IntValue);
|
|
|
|
|
Inc(x);
|
|
|
|
|
end;
|
|
|
|
|
|
2017-09-28 00:14:34 +02:00
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TJSONRPCServerTest.TestRequestWithParams_I_I_I_ret_O;
|
|
|
|
|
var
|
2019-01-08 12:48:27 +01:00
|
|
|
|
lReq: IJSONRPCRequest;
|
|
|
|
|
lRPCResp: IJSONRPCResponse;
|
2017-09-28 00:14:34 +02:00
|
|
|
|
lS: string;
|
2017-09-26 01:02:09 +02:00
|
|
|
|
begin
|
2017-09-28 00:14:34 +02:00
|
|
|
|
lReq := TJSONRPCRequest.Create;
|
2019-01-08 12:48:27 +01:00
|
|
|
|
lReq.Method := 'add';
|
|
|
|
|
lReq.Params.Add(3);
|
|
|
|
|
lReq.Params.Add(4);
|
|
|
|
|
lReq.Params.Add(5);
|
|
|
|
|
lReq.RequestID := 1234;
|
2019-01-18 18:11:27 +01:00
|
|
|
|
|
2019-01-08 12:48:27 +01:00
|
|
|
|
lRPCResp := FExecutor.ExecuteRequest(lReq);
|
|
|
|
|
lS := (lRPCResp.Result.AsObject as TJDOJsonObject).ToJSON();
|
|
|
|
|
Assert.areEqual(12, TJDOJsonObject(lRPCResp.Result.AsObject).I['res']);
|
2019-01-18 18:11:27 +01:00
|
|
|
|
|
|
|
|
|
lRPCResp := FExecutor2.ExecuteRequest(lReq);
|
|
|
|
|
lS := (lRPCResp.Result.AsObject as TJDOJsonObject).ToJSON();
|
|
|
|
|
Assert.areEqual(12, TJDOJsonObject(lRPCResp.Result.AsObject).I['res']);
|
2017-09-28 00:14:34 +02:00
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TJSONRPCServerTest.TestRequest_S_I_ret_S;
|
|
|
|
|
var
|
2019-01-08 12:48:27 +01:00
|
|
|
|
lReq: IJSONRPCRequest;
|
|
|
|
|
lRPCResp: IJSONRPCResponse;
|
2017-09-28 00:14:34 +02:00
|
|
|
|
begin
|
|
|
|
|
lReq := TJSONRPCRequest.Create;
|
2019-01-08 12:48:27 +01:00
|
|
|
|
lReq.Method := 'MultiplyString';
|
|
|
|
|
lReq.Params.Add('Daniele');
|
|
|
|
|
lReq.Params.Add(4);
|
|
|
|
|
lReq.RequestID := 1234;
|
|
|
|
|
lRPCResp := FExecutor.ExecuteRequest(lReq);
|
|
|
|
|
Assert.areEqual('DanieleDanieleDanieleDaniele', lRPCResp.Result.AsString);
|
2019-01-18 18:11:27 +01:00
|
|
|
|
|
|
|
|
|
lRPCResp := FExecutor2.ExecuteRequest(lReq);
|
|
|
|
|
Assert.areEqual('DanieleDanieleDanieleDaniele', lRPCResp.Result.AsString);
|
2017-09-26 01:02:09 +02:00
|
|
|
|
end;
|
|
|
|
|
|
2015-12-22 12:29:25 +01:00
|
|
|
|
initialization
|
|
|
|
|
|
2017-08-20 02:36:22 +02:00
|
|
|
|
TDUnitX.RegisterTestFixture(TServerTest);
|
2017-09-26 01:02:09 +02:00
|
|
|
|
TDUnitX.RegisterTestFixture(TJSONRPCServerTest);
|
2015-12-22 12:29:25 +01:00
|
|
|
|
|
|
|
|
|
end.
|