2016-09-18 19:19:23 +02:00
|
|
|
|
// ***************************************************************************
|
|
|
|
|
//
|
|
|
|
|
// Delphi MVC Framework
|
|
|
|
|
//
|
2020-01-06 16:49:18 +01:00
|
|
|
|
// Copyright (c) 2010-2020 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,
|
2020-09-12 03:24:38 +02:00
|
|
|
|
MVCFramework.RESTClient.Intf,
|
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
|
|
|
|
|
|
|
|
|
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
|
2020-09-12 03:24:38 +02:00
|
|
|
|
RESTClient: IMVCRESTClient;
|
2015-12-22 12:29:25 +01:00
|
|
|
|
procedure DoLoginWith(UserName: string);
|
|
|
|
|
procedure DoLogout;
|
2017-08-21 15:31:31 +02:00
|
|
|
|
public
|
2020-05-27 11:28:22 +02:00
|
|
|
|
[Setup]
|
|
|
|
|
procedure Setup; virtual;
|
2017-08-20 02:36:22 +02:00
|
|
|
|
[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)
|
2017-08-21 15:31:31 +02:00
|
|
|
|
public
|
2018-08-08 17:11:45 +02:00
|
|
|
|
[Test]
|
2020-03-08 17:54:10 +01:00
|
|
|
|
[TestCase('request url /fault', '/exception/fault')]
|
|
|
|
|
[TestCase('request url /fault2', '/exception/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]
|
2020-04-21 17:04:04 +02:00
|
|
|
|
procedure TestObjectDict;
|
|
|
|
|
[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
|
2020-03-05 18:34:00 +01:00
|
|
|
|
[Test]
|
2016-09-20 11:10:40 +02:00
|
|
|
|
procedure TestBasicAuth01;
|
2017-08-21 15:31:31 +02:00
|
|
|
|
[Test]
|
2020-12-12 19:26:18 +01:00
|
|
|
|
//[Category('this')]
|
2020-11-27 15:13:17 +01:00
|
|
|
|
procedure TestEntityWithArrays;
|
|
|
|
|
[Test]
|
2020-12-12 19:26:18 +01:00
|
|
|
|
//[Category('this')]
|
2020-11-27 15:13:17 +01:00
|
|
|
|
procedure TestEntityWithEmptyArrays;
|
|
|
|
|
[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]
|
2020-11-04 19:06:54 +01:00
|
|
|
|
[Category('datetime')]
|
2016-06-22 17:49:16 +02:00
|
|
|
|
procedure TestTypedDateTimeTypes;
|
2017-08-21 15:31:31 +02:00
|
|
|
|
[Test]
|
2020-11-04 19:06:54 +01:00
|
|
|
|
[Category('datetime')]
|
|
|
|
|
procedure TestTypedDateTimeTypes_UTC;
|
|
|
|
|
[Test]
|
2016-06-22 17:49:16 +02:00
|
|
|
|
procedure TestTypedBooleans;
|
2018-10-31 01:07:23 +01:00
|
|
|
|
[Test]
|
2020-04-09 16:04:45 +02:00
|
|
|
|
procedure TestTypedTGuid1;
|
|
|
|
|
[Test]
|
2018-10-31 01:07:23 +01:00
|
|
|
|
procedure TestStringDictionary;
|
|
|
|
|
[Test]
|
|
|
|
|
procedure TestWrongJSONBody;
|
2020-04-08 13:00:32 +02:00
|
|
|
|
[Test]
|
|
|
|
|
procedure TestTypedIntegerWrongParam1;
|
2019-09-01 20:35:19 +02:00
|
|
|
|
|
2020-09-08 15:25:44 +02:00
|
|
|
|
// test exceptions rendering
|
|
|
|
|
[Test]
|
|
|
|
|
[Category('renders,exceptions')]
|
|
|
|
|
procedure TestEMVCException1;
|
|
|
|
|
[Test]
|
|
|
|
|
[Category('renders,exceptions')]
|
|
|
|
|
procedure TestEMVCException2;
|
|
|
|
|
[Test]
|
|
|
|
|
[Category('renders,exceptions')]
|
|
|
|
|
procedure TestEMVCException3;
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
[Category('renders,exceptions')]
|
|
|
|
|
procedure TestEMVCException4;
|
|
|
|
|
|
2020-02-03 12:11:42 +01:00
|
|
|
|
// test nullables
|
|
|
|
|
[Test]
|
2020-02-03 13:19:55 +01:00
|
|
|
|
procedure TestDeserializeNullablesWithValue;
|
|
|
|
|
[Test]
|
|
|
|
|
procedure TestDeserializeNullablesWithNulls;
|
2020-02-03 12:11:42 +01:00
|
|
|
|
[Test]
|
|
|
|
|
procedure TestSerializeAndDeserializeNullables;
|
2020-04-18 23:32:24 +02:00
|
|
|
|
[Test]
|
|
|
|
|
procedure TestSerializeAndDeserializeNullables_ISSUE_362;
|
|
|
|
|
[Test]
|
|
|
|
|
procedure TestSerializeAndDeserializeNullables_Passing_Integers_InsteadOf_Floats;
|
2020-02-03 12:11:42 +01:00
|
|
|
|
|
2019-09-01 20:35:19 +02:00
|
|
|
|
// test responses objects
|
|
|
|
|
[Test]
|
|
|
|
|
procedure TestResponseCreated;
|
|
|
|
|
[Test]
|
|
|
|
|
procedure TestResponseNoContent;
|
|
|
|
|
[Test]
|
|
|
|
|
procedure TestResponseAccepted;
|
2020-02-13 23:33:30 +01:00
|
|
|
|
|
|
|
|
|
// test web server
|
|
|
|
|
[Test]
|
2020-09-16 15:56:14 +02:00
|
|
|
|
[Category('staticfiles')]
|
2020-02-13 23:33:30 +01:00
|
|
|
|
procedure TestDirectoryTraversal1;
|
2020-05-02 16:39:32 +02:00
|
|
|
|
[Test]
|
2020-09-14 15:52:50 +02:00
|
|
|
|
[Category('staticfiles')]
|
2020-05-02 16:39:32 +02:00
|
|
|
|
procedure TestDirectoryTraversal2;
|
|
|
|
|
[Test]
|
2020-09-16 15:56:14 +02:00
|
|
|
|
[Category('staticfiles')]
|
2020-05-14 17:41:20 +02:00
|
|
|
|
procedure TestDirectoryRedirect;
|
|
|
|
|
[Test]
|
2020-09-21 12:42:38 +02:00
|
|
|
|
[Category('staticfiles')]
|
2020-09-14 15:52:50 +02:00
|
|
|
|
procedure TestFileWithFolderName;
|
|
|
|
|
[Test]
|
2020-09-16 15:56:14 +02:00
|
|
|
|
[Category('staticfiles')]
|
2020-05-02 16:39:32 +02:00
|
|
|
|
procedure TestSPASupport;
|
2020-04-13 16:22:15 +02:00
|
|
|
|
// test server side views
|
|
|
|
|
[Test]
|
|
|
|
|
procedure TestViewDataViewDataSet;
|
|
|
|
|
|
2020-09-08 15:25:44 +02:00
|
|
|
|
// test issues
|
|
|
|
|
[Test]
|
2020-09-14 15:52:50 +02:00
|
|
|
|
[Category('renders')]
|
2020-09-08 15:25:44 +02:00
|
|
|
|
procedure TestIssue406;
|
|
|
|
|
|
2015-12-22 12:29:25 +01:00
|
|
|
|
end;
|
|
|
|
|
|
2017-09-26 01:02:09 +02:00
|
|
|
|
[TestFixture]
|
2020-05-27 11:28:22 +02:00
|
|
|
|
TJSONRPCServerTest = class(TObject)
|
2018-05-16 19:46:29 +02:00
|
|
|
|
protected
|
|
|
|
|
FExecutor: IMVCJSONRPCExecutor;
|
2019-01-18 18:11:27 +01:00
|
|
|
|
FExecutor2: IMVCJSONRPCExecutor;
|
2020-08-05 09:50:06 +02:00
|
|
|
|
FExecutor3: IMVCJSONRPCExecutor;
|
2017-09-26 01:02:09 +02:00
|
|
|
|
public
|
2020-05-25 19:34:14 +02:00
|
|
|
|
[Setup]
|
2020-05-27 11:28:22 +02:00
|
|
|
|
procedure Setup;
|
2017-09-26 01:02:09 +02:00
|
|
|
|
[Test]
|
|
|
|
|
procedure TestRequestWithoutParams;
|
|
|
|
|
[Test]
|
2020-08-06 17:40:56 +02:00
|
|
|
|
procedure TestNotificationWithoutParams;
|
|
|
|
|
[Test]
|
|
|
|
|
procedure TestNotificationWhichRaisesError;
|
|
|
|
|
[Test]
|
2017-09-26 01:02:09 +02:00
|
|
|
|
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]
|
2020-05-02 16:39:32 +02:00
|
|
|
|
procedure TestRequestWithNamedParams_I_I_ret_I;
|
|
|
|
|
[Test]
|
2017-09-28 00:14:34 +02:00
|
|
|
|
procedure TestRequestWithParams_I_I_I_ret_O;
|
|
|
|
|
[Test]
|
2020-05-02 16:39:32 +02:00
|
|
|
|
procedure TestRequestWithNamedParams_I_I_I_ret_O;
|
|
|
|
|
[Test]
|
|
|
|
|
procedure TestRequestWithWrongNamedParams;
|
|
|
|
|
[Test]
|
2017-09-28 00:14:34 +02:00
|
|
|
|
procedure TestRequest_S_I_ret_S;
|
|
|
|
|
[Test]
|
2020-05-02 16:39:32 +02:00
|
|
|
|
procedure TestRequest_NamedParams_S_I_ret_S;
|
|
|
|
|
[Test]
|
2017-09-28 00:14:34 +02:00
|
|
|
|
procedure TestRequestWithParams_I_I_ret_A;
|
2019-01-18 18:11:27 +01:00
|
|
|
|
[Test]
|
|
|
|
|
procedure TestRequestWithParams_DT_T_ret_DT;
|
2020-08-05 09:50:06 +02:00
|
|
|
|
// hooks tests
|
|
|
|
|
[Test]
|
|
|
|
|
procedure TestHooks;
|
|
|
|
|
[Test]
|
|
|
|
|
procedure TestHooksWhenMethodRaisesError;
|
|
|
|
|
[Test]
|
2020-08-06 17:40:56 +02:00
|
|
|
|
procedure TestHooksWhenOnAfterCallHookRaisesError;
|
|
|
|
|
[Test]
|
|
|
|
|
procedure TestHooksNotif;
|
|
|
|
|
[Test]
|
|
|
|
|
procedure TestHooksNotifWhenOnBeforeRoutingHookRaisesError;
|
|
|
|
|
[Test]
|
|
|
|
|
procedure TestHooksNotifWhenOnBeforeCallHookRaisesError;
|
|
|
|
|
[Test]
|
|
|
|
|
procedure TestHooksNotifWhenOnAfterCallHookRaisesError;
|
2020-08-05 09:50:06 +02:00
|
|
|
|
[Test]
|
|
|
|
|
procedure TestHooksWhenOnBeforeCallHookRaisesError;
|
|
|
|
|
[Test]
|
2020-08-06 17:40:56 +02:00
|
|
|
|
procedure TestHooksWhenOnBeforeRoutingHookRaisesError;
|
2020-08-05 09:50:06 +02:00
|
|
|
|
|
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}
|
|
|
|
|
,
|
2020-05-27 11:28:22 +02:00
|
|
|
|
MVCFramework.Serializer.JsonDataObjects.OptionalCustomTypes,
|
2019-09-01 20:35:19 +02:00
|
|
|
|
Vcl.Graphics
|
|
|
|
|
{$ENDIF}
|
2020-11-27 15:13:17 +01:00
|
|
|
|
, TestConstsU, MVCFramework.Tests.Serializer.Entities;
|
2020-05-27 11:28:22 +02:00
|
|
|
|
|
2020-08-05 09:50:06 +02:00
|
|
|
|
function GetServer: string;
|
2020-05-27 11:28:22 +02:00
|
|
|
|
begin
|
|
|
|
|
Result := 'http://' + TEST_SERVER_ADDRESS + ':9999';
|
|
|
|
|
end;
|
2015-12-22 12:29:25 +01:00
|
|
|
|
|
|
|
|
|
{ TServerTest }
|
|
|
|
|
|
|
|
|
|
procedure TBaseServerTest.DoLogout;
|
|
|
|
|
var
|
2020-09-12 03:24:38 +02:00
|
|
|
|
res: IMVCRESTResponse;
|
2015-12-22 12:29:25 +01:00
|
|
|
|
begin
|
2020-09-12 03:24:38 +02:00
|
|
|
|
res := RESTClient.Get('/logout');
|
|
|
|
|
Assert.isTrue(res.StatusCode = HTTP_STATUS.OK, 'Logout Failed');
|
2015-12-22 12:29:25 +01:00
|
|
|
|
end;
|
|
|
|
|
|
2020-05-27 11:28:22 +02:00
|
|
|
|
procedure TBaseServerTest.Setup;
|
2015-12-22 12:29:25 +01:00
|
|
|
|
begin
|
|
|
|
|
inherited;
|
2020-09-12 03:24:38 +02:00
|
|
|
|
RESTClient := TMVCRESTClient.New.BaseURL(TEST_SERVER_ADDRESS, 9999);
|
2020-09-06 15:38:25 +02:00
|
|
|
|
RESTClient.ReadTimeout(60 * 1000 * 30);
|
2015-12-22 12:29:25 +01:00
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TBaseServerTest.TearDown;
|
|
|
|
|
begin
|
|
|
|
|
inherited;
|
2020-09-12 03:24:38 +02:00
|
|
|
|
RESTClient := nil;
|
2015-12-22 12:29:25 +01:00
|
|
|
|
end;
|
|
|
|
|
|
2017-01-05 12:44:34 +01:00
|
|
|
|
procedure TServerTest.TestActionFiltersOnBeforeAction;
|
|
|
|
|
var
|
2020-09-12 03:24:38 +02:00
|
|
|
|
res: IMVCRESTResponse;
|
2017-01-05 12:44:34 +01:00
|
|
|
|
begin
|
2020-09-12 03:24:38 +02:00
|
|
|
|
res := RESTClient.Get('/actionfilters/beforeaction/alwayscalled');
|
|
|
|
|
Assert.areEqual<Integer>(HTTP_STATUS.OK, res.StatusCode);
|
2017-01-05 12:44:34 +01:00
|
|
|
|
|
2020-09-12 03:24:38 +02:00
|
|
|
|
res := RESTClient.Get('/actionfilters/beforeaction/nevercalled');
|
|
|
|
|
Assert.areEqual<Integer>(HTTP_STATUS.NotFound, res.StatusCode);
|
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
|
2020-09-12 03:24:38 +02:00
|
|
|
|
RESTClient.Async(
|
|
|
|
|
procedure(Response: IMVCRESTResponse)
|
2015-12-22 12:29:25 +01:00
|
|
|
|
begin
|
|
|
|
|
OK := true;
|
|
|
|
|
evt.SetEvent;
|
|
|
|
|
end,
|
|
|
|
|
procedure(E: Exception)
|
|
|
|
|
begin
|
|
|
|
|
OK := false;
|
2020-09-12 03:24:38 +02:00
|
|
|
|
end)
|
|
|
|
|
.AddPathParam('par1', 1)
|
|
|
|
|
.AddPathParam('par2', 2)
|
|
|
|
|
.AddPathParam('par3', 3)
|
|
|
|
|
.Delete('/req/with/params/($par1)/($par2)/($par3)');
|
2015-12-22 12:29:25 +01:00
|
|
|
|
|
|
|
|
|
// 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
|
2020-09-12 03:24:38 +02:00
|
|
|
|
RESTClient.Async(
|
|
|
|
|
procedure(Response: IMVCRESTResponse)
|
2015-12-22 12:29:25 +01:00
|
|
|
|
begin
|
|
|
|
|
try
|
2020-09-12 03:24:38 +02:00
|
|
|
|
j := TSystemJSON.StringAsJSONObject(Response.Content);
|
2015-12-22 12:29:25 +01:00
|
|
|
|
except
|
|
|
|
|
// test should not block...never!
|
|
|
|
|
end;
|
|
|
|
|
evt.SetEvent;
|
|
|
|
|
end,
|
|
|
|
|
procedure(E: Exception)
|
|
|
|
|
begin
|
2020-09-12 03:24:38 +02:00
|
|
|
|
end)
|
|
|
|
|
.AddPathParam('par1', 1)
|
|
|
|
|
.AddPathParam('par2', 2)
|
|
|
|
|
.AddPathParam('par3', 3)
|
|
|
|
|
.Get('/req/with/params/($par1)/($par2)/($par3)');
|
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('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;
|
2020-08-26 15:09:47 +02:00
|
|
|
|
evt := TEvent.Create;
|
2015-12-22 12:29:25 +01:00
|
|
|
|
try
|
2020-09-06 15:38:25 +02:00
|
|
|
|
|
2020-09-12 03:24:38 +02:00
|
|
|
|
RESTClient.Async(
|
|
|
|
|
procedure(Response: IMVCRESTResponse)
|
2015-12-22 12:29:25 +01:00
|
|
|
|
begin
|
|
|
|
|
try
|
2020-09-12 03:24:38 +02:00
|
|
|
|
j := TSystemJSON.StringAsJSONObject(Response.Content);
|
2015-12-22 12:29:25 +01:00
|
|
|
|
except
|
|
|
|
|
// test should not block...never!
|
|
|
|
|
end;
|
|
|
|
|
evt.SetEvent;
|
|
|
|
|
end,
|
|
|
|
|
procedure(E: Exception)
|
|
|
|
|
begin
|
2020-09-12 03:24:38 +02:00
|
|
|
|
end)
|
|
|
|
|
.AddPathParam('par1', 1)
|
|
|
|
|
.AddPathParam('par2', 2)
|
|
|
|
|
.AddPathParam('par3', 3)
|
|
|
|
|
.Post('/echo/($par1)/($par2)/($par3)',
|
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
|
2020-09-12 03:24:38 +02:00
|
|
|
|
RESTClient.Async(
|
|
|
|
|
procedure(Response: IMVCRESTResponse)
|
2015-12-22 12:29:25 +01:00
|
|
|
|
begin
|
|
|
|
|
try
|
2020-09-12 03:24:38 +02:00
|
|
|
|
j := TSystemJSON.StringAsJSONObject(Response.Content);
|
2015-12-22 12:29:25 +01:00
|
|
|
|
except
|
|
|
|
|
// test should not block...never!
|
|
|
|
|
end;
|
|
|
|
|
evt.SetEvent;
|
|
|
|
|
end,
|
|
|
|
|
procedure(E: Exception)
|
|
|
|
|
begin
|
2020-09-12 03:24:38 +02:00
|
|
|
|
end)
|
|
|
|
|
.AddPathParam('par1', 1)
|
|
|
|
|
.AddPathParam('par2', 2)
|
|
|
|
|
.AddPathParam('par3', 3)
|
|
|
|
|
.Put('/echo/($par1)/($par2)/($par3)',
|
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
|
2020-09-12 03:24:38 +02:00
|
|
|
|
lRes: IMVCRESTResponse;
|
2015-12-22 12:29:25 +01:00
|
|
|
|
begin
|
2020-09-24 20:03:11 +02:00
|
|
|
|
RESTClient.SetBasicAuthorization('user1', 'user1');
|
|
|
|
|
Assert.AreEqual('Basic dXNlcjE6dXNlcjE=', RESTClient.Authorization);
|
2020-09-12 03:24:38 +02:00
|
|
|
|
lRes := RESTClient.Get('/private/role1');
|
|
|
|
|
Assert.areEqual<Integer>(HTTP_STATUS.OK, lRes.StatusCode);
|
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
|
2020-09-12 03:24:38 +02:00
|
|
|
|
lRes: IMVCRESTResponse;
|
2015-12-22 12:29:25 +01:00
|
|
|
|
begin
|
2020-09-12 03:24:38 +02:00
|
|
|
|
lRes := RESTClient.Get('/private/role1');
|
|
|
|
|
Assert.areEqual<Integer>(HTTP_STATUS.Unauthorized, lRes.StatusCode);
|
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
|
2020-09-12 03:24:38 +02:00
|
|
|
|
lRes: IMVCRESTResponse;
|
2015-12-22 12:29:25 +01:00
|
|
|
|
begin
|
2020-09-24 20:03:11 +02:00
|
|
|
|
RESTClient.SetBasicAuthorization('user1', 'user1');
|
2020-09-12 03:24:38 +02:00
|
|
|
|
lRes := RESTClient.Get('/private/role2');
|
|
|
|
|
Assert.areEqual<Integer>(HTTP_STATUS.Forbidden, lRes.StatusCode);
|
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
|
2020-09-12 03:24:38 +02:00
|
|
|
|
lRes: IMVCRESTResponse;
|
2015-12-22 12:29:25 +01:00
|
|
|
|
begin
|
2020-09-24 20:03:11 +02:00
|
|
|
|
RESTClient.SetBasicAuthorization('user1', 'user1');
|
2020-09-12 03:24:38 +02:00
|
|
|
|
lRes := RESTClient.Get('/private/role1');
|
|
|
|
|
Assert.areEqual<Integer>(HTTP_STATUS.OK, lRes.StatusCode);
|
|
|
|
|
lRes := RESTClient.Get('/people');
|
|
|
|
|
Assert.areEqual<Integer>(HTTP_STATUS.OK, lRes.StatusCode);
|
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
|
2020-09-12 03:24:38 +02:00
|
|
|
|
lRes: IMVCRESTResponse;
|
2016-04-03 22:35:27 +02:00
|
|
|
|
begin
|
2020-09-24 20:03:11 +02:00
|
|
|
|
RESTClient.SetBasicAuthorization('user1', 'user1');
|
2016-04-03 22:35:27 +02:00
|
|
|
|
|
|
|
|
|
// first
|
2020-09-12 03:24:38 +02:00
|
|
|
|
lRes := RESTClient.Get('/private/role1session?value=danieleteti');
|
|
|
|
|
Assert.areEqual<Integer>(HTTP_STATUS.OK, lRes.StatusCode);
|
|
|
|
|
lRes := RESTClient.Get('/private/role1session');
|
|
|
|
|
Assert.areEqual<Integer>(HTTP_STATUS.OK, lRes.StatusCode);
|
|
|
|
|
Assert.areEqual('danieleteti', lRes.Content);
|
2016-04-03 22:35:27 +02:00
|
|
|
|
|
|
|
|
|
// second
|
2020-09-12 03:24:38 +02:00
|
|
|
|
lRes := RESTClient.Get('/private/role1session?value=johndoe');
|
|
|
|
|
Assert.areEqual<Integer>(HTTP_STATUS.OK, lRes.StatusCode);
|
|
|
|
|
lRes := RESTClient.Get('/private/role1session');
|
|
|
|
|
Assert.areEqual<Integer>(HTTP_STATUS.OK, lRes.StatusCode);
|
|
|
|
|
Assert.areEqual('johndoe', lRes.Content);
|
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
|
2020-09-12 03:24:38 +02:00
|
|
|
|
res: IMVCRESTResponse;
|
2018-08-08 17:11:45 +02:00
|
|
|
|
begin
|
2020-09-12 03:24:38 +02:00
|
|
|
|
res := RESTClient.Get(URLSegment);
|
|
|
|
|
Assert.areEqual(HTTP_STATUS.InternalServerError, res.StatusCode);
|
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');
|
2020-09-12 03:24:38 +02:00
|
|
|
|
Assert.Contains(res.Content, 'Cannot create controller', true, 'Exception message in body is not correct');
|
2020-09-06 15:38:25 +02: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
|
2020-09-12 03:24:38 +02:00
|
|
|
|
res: IMVCRESTResponse;
|
2016-04-24 19:08:21 +02:00
|
|
|
|
I: Integer;
|
|
|
|
|
begin
|
2020-09-12 03:24:38 +02:00
|
|
|
|
res := RESTClient.Get('/lotofcookies');
|
|
|
|
|
Assert.areEqual<Integer>(HTTP_STATUS.OK, res.StatusCode);
|
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
|
2020-09-12 03:24:38 +02:00
|
|
|
|
Assert.areEqual('usersettings' + IntToStr(I + 1), res.Cookies[I].Name);
|
|
|
|
|
Assert.areEqual('usersettings' + IntToStr(I + 1) + '-value', res.Cookies[I].Value);
|
|
|
|
|
Assert.areEqual('/usersettings' + IntToStr(I + 1) + '/', res.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
|
2020-09-12 03:24:38 +02:00
|
|
|
|
lRes: IMVCRESTResponse;
|
2016-09-16 23:54:54 +02:00
|
|
|
|
begin
|
2020-09-12 03:24:38 +02:00
|
|
|
|
lRes := RESTClient.Get('/privatecustom/role1');
|
|
|
|
|
Assert.AreEqual<Integer>(HTTP_STATUS.Unauthorized, lRes.StatusCode);
|
2019-03-05 20:55:37 +01:00
|
|
|
|
Assert.areEqual('/system/users/logged', lRes.HeaderValue('X-LOGIN-URL'));
|
|
|
|
|
Assert.areEqual('POST', lRes.HeaderValue('X-LOGIN-METHOD'));
|
|
|
|
|
|
2020-09-12 03:24:38 +02:00
|
|
|
|
lRes := RESTClient.Get('/privatecustom/role2');
|
|
|
|
|
Assert.areEqual<Integer>(HTTP_STATUS.Unauthorized, lRes.StatusCode);
|
2019-03-05 20:55:37 +01:00
|
|
|
|
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
|
2020-09-12 03:24:38 +02:00
|
|
|
|
lRes: IMVCRESTResponse;
|
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');
|
2020-09-12 03:24:38 +02:00
|
|
|
|
lRes := RESTClient.Post('/system/users/logged', TSystemJSON.JSONValueToString(lJSON, false));
|
2019-03-05 20:55:37 +01:00
|
|
|
|
Assert.areEqual('application/json', lRes.ContentType);
|
2020-09-12 03:24:38 +02:00
|
|
|
|
Assert.areEqual<Integer>(HTTP_STATUS.OK, lRes.StatusCode);
|
2019-03-05 20:55:37 +01:00
|
|
|
|
Assert.areEqual('/system/users/logged', lRes.HeaderValue('X-LOGOUT-URL'));
|
|
|
|
|
Assert.areEqual('DELETE', lRes.HeaderValue('X-LOGOUT-METHOD'));
|
2020-09-12 03:24:38 +02:00
|
|
|
|
Assert.areEqual('{"status":"OK"}', lRes.Content);
|
|
|
|
|
lCookieValue := lRes.CookieByName(TMVCConstants.SESSION_TOKEN_NAME).Value;
|
2017-08-20 02:36:22 +02:00
|
|
|
|
Assert.AreNotEqual('', lCookieValue, 'Session cookie not returned after login');
|
2020-09-12 03:24:38 +02:00
|
|
|
|
Assert.IsFalse(lCookieValue.Contains('invalid'), 'Returned an invalid session token');
|
2016-12-28 10:00:07 +01:00
|
|
|
|
|
2020-09-12 03:24:38 +02:00
|
|
|
|
lRes := RESTClient.Get('/privatecustom/role2');
|
|
|
|
|
Assert.areEqual<Integer>(HTTP_STATUS.Forbidden, lRes.StatusCode,
|
2016-12-28 10:00:07 +01:00
|
|
|
|
'Authorization not respected for not allowed action');
|
|
|
|
|
|
2020-09-12 03:24:38 +02:00
|
|
|
|
lRes := RESTClient.Get('/privatecustom/role1');
|
|
|
|
|
Assert.areEqual<Integer>(HTTP_STATUS.OK, lRes.StatusCode, 'Authorization not respected for allowed action');
|
2016-12-28 10:00:07 +01:00
|
|
|
|
finally
|
|
|
|
|
lJSON.Free;
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TServerTest.TestCustomAuthRequestsWithValidLogin_HTML;
|
|
|
|
|
var
|
2020-09-12 03:24:38 +02:00
|
|
|
|
lRes: IMVCRESTResponse;
|
2018-11-24 16:56:21 +01:00
|
|
|
|
lJSON: System.JSON.TJSONObject;
|
2016-12-28 10:00:07 +01:00
|
|
|
|
lCookieValue: string;
|
2020-09-12 03:24:38 +02:00
|
|
|
|
lContentType: string;
|
|
|
|
|
lContentCharset: string;
|
2016-12-28 10:00:07 +01:00
|
|
|
|
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');
|
2020-09-12 03:24:38 +02:00
|
|
|
|
lRes := RESTClient.Accept('text/html').Post('/system/users/logged', TSystemJSON.JSONValueToString(lJSON, false));
|
|
|
|
|
SplitContentMediaTypeAndCharset(lRes.ContentType, lContentType, lContentCharset);
|
|
|
|
|
Assert.AreEqual(lContentType, TMVCMediaType.APPLICATION_JSON);
|
|
|
|
|
Assert.areEqual<Integer>(HTTP_STATUS.OK, lRes.StatusCode);
|
2019-03-05 20:55:37 +01:00
|
|
|
|
Assert.areEqual('/system/users/logged', lRes.HeaderValue('X-LOGOUT-URL'));
|
|
|
|
|
Assert.areEqual('DELETE', lRes.HeaderValue('X-LOGOUT-METHOD'));
|
2020-09-12 03:24:38 +02:00
|
|
|
|
Assert.areEqual('{"status":"OK"}', lRes.Content);
|
|
|
|
|
lCookieValue := lRes.CookieByName(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
|
|
|
|
|
2020-09-12 03:24:38 +02:00
|
|
|
|
lRes := RESTClient.Get('/privatecustom/role2');
|
|
|
|
|
Assert.areEqual<Integer>(HTTP_STATUS.Forbidden, lRes.StatusCode,
|
2016-09-16 23:54:54 +02:00
|
|
|
|
'Authorization not respected for not allowed action');
|
|
|
|
|
|
2020-09-12 03:24:38 +02:00
|
|
|
|
lRes := RESTClient.Get('/privatecustom/role1');
|
|
|
|
|
Assert.areEqual<Integer>(HTTP_STATUS.OK, lRes.StatusCode, 'Authorization not respected for allowed action');
|
2016-09-16 23:54:54 +02:00
|
|
|
|
finally
|
|
|
|
|
lJSON.Free;
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TServerTest.TestCustomAuthWrongRequestBodies;
|
|
|
|
|
var
|
2020-09-12 03:24:38 +02:00
|
|
|
|
lRes: IMVCRESTResponse;
|
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
|
2020-09-18 01:06:24 +02:00
|
|
|
|
lRes := RESTClient.AddBody('',TMVCMediaType.APPLICATION_JSON).Post('/system/users/logged');
|
2020-08-26 01:07:21 +02:00
|
|
|
|
Assert.areEqual<Integer>(HTTP_STATUS.BadRequest, lRes.StatusCode,
|
2016-09-16 23:54:54 +02:00
|
|
|
|
'Empty request body doesn''t return HTTP 400 Bad Request');
|
|
|
|
|
|
|
|
|
|
// wrong request body 1
|
2020-09-12 03:24:38 +02:00
|
|
|
|
lRes := RESTClient.Post('/system/users/logged', TSystemJSON.JSONValueToString(lJSON, false));
|
|
|
|
|
Assert.areEqual<Integer>(HTTP_STATUS.Unauthorized, lRes.StatusCode,
|
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', '');
|
2020-09-12 03:24:38 +02:00
|
|
|
|
lRes := RESTClient.Post('/system/users/logged', TSystemJSON.JSONValueToString(lJSON, false));
|
|
|
|
|
Assert.areEqual<Integer>(HTTP_STATUS.Unauthorized, lRes.StatusCode,
|
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');
|
2020-09-12 03:24:38 +02:00
|
|
|
|
lRes := RESTClient.Post('/system/users/logged', TSystemJSON.JSONValueToString(lJSON, false));
|
|
|
|
|
Assert.areEqual<Integer>(HTTP_STATUS.Unauthorized, lRes.StatusCode,
|
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
|
2020-09-12 03:24:38 +02:00
|
|
|
|
r: IMVCRESTResponse;
|
2019-06-25 17:32:30 +02:00
|
|
|
|
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');
|
2021-02-04 14:11:33 +01:00
|
|
|
|
lCustomer.Logo.SaveToFile('customer_logo_before_to_send.bmp');
|
2019-06-25 17:32:30 +02:00
|
|
|
|
lSer := GetDefaultSerializer;
|
|
|
|
|
RegisterOptionalCustomTypesSerializers(lSer);
|
2020-09-12 03:24:38 +02:00
|
|
|
|
r := RESTClient.Accept(TMVCMediaType.APPLICATION_JSON).Post('/customerecho', lSer.SerializeObject(lCustomer));
|
2019-06-25 17:32:30 +02:00
|
|
|
|
finally
|
|
|
|
|
lCustomer.Free;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
lCustomer := TCustomer.Create;
|
|
|
|
|
try
|
|
|
|
|
lSer := GetDefaultSerializer;
|
|
|
|
|
RegisterOptionalCustomTypesSerializers(lSer);
|
2020-09-12 03:24:38 +02:00
|
|
|
|
lSer.DeserializeObject(r.Content, lCustomer);
|
2019-06-25 17:32:30 +02:00
|
|
|
|
Assert.areEqual('bit Time Professionals changed', lCustomer.Name);
|
|
|
|
|
Assert.areEqual('', lCustomer.ContactFirst);
|
|
|
|
|
Assert.areEqual('', lCustomer.ContactLast);
|
2021-02-04 14:11:33 +01:00
|
|
|
|
lCustomer.Logo.SaveToFile('customer_logo_after_received.bmp');
|
|
|
|
|
Assert.areEqual('de2a29ec62fc1f0b3abbb6b74223d214', THashMD5.GetHashStringFromFile('customer_logo_after_received.bmp'));
|
2019-06-25 17:32:30 +02:00
|
|
|
|
finally
|
|
|
|
|
lCustomer.Free;
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
|
2016-09-18 19:19:23 +02:00
|
|
|
|
procedure TServerTest.TestCustomAuthLoginLogout;
|
|
|
|
|
var
|
2020-09-12 03:24:38 +02:00
|
|
|
|
lRes: IMVCRESTResponse;
|
2018-11-24 16:56:21 +01:00
|
|
|
|
lJSON: System.JSON.TJSONObject;
|
2016-09-18 19:19:23 +02:00
|
|
|
|
lLogoutUrl: string;
|
2020-09-12 03:24:38 +02:00
|
|
|
|
lPass: Boolean;
|
|
|
|
|
lCookie: TCookie;
|
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');
|
2020-09-12 03:24:38 +02:00
|
|
|
|
lRes := RESTClient.Post('/system/users/logged', TSystemJSON.JSONValueToString(lJSON, false));
|
2016-09-16 23:54:54 +02:00
|
|
|
|
|
2020-09-12 03:24:38 +02:00
|
|
|
|
Assert.areEqual<Integer>(HTTP_STATUS.OK, lRes.StatusCode);
|
2019-03-05 20:55:37 +01:00
|
|
|
|
lLogoutUrl := lRes.HeaderValue('X-LOGOUT-URL');
|
2016-09-18 19:19:23 +02:00
|
|
|
|
|
2020-09-12 03:24:38 +02:00
|
|
|
|
lRes := RESTClient.Delete(lLogoutUrl);
|
|
|
|
|
lPass := False;
|
|
|
|
|
for lCookie in lRes.Cookies do
|
2016-09-18 19:19:23 +02:00
|
|
|
|
begin
|
2020-09-12 03:24:38 +02:00
|
|
|
|
if lCookie.Value.Contains('invalid') then
|
2020-09-06 15:38:25 +02:00
|
|
|
|
begin
|
2020-09-12 03:24:38 +02:00
|
|
|
|
lPass := True;
|
|
|
|
|
Break;
|
2020-09-06 15:38:25 +02:00
|
|
|
|
end;
|
2016-09-18 19:19:23 +02:00
|
|
|
|
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;
|
|
|
|
|
|
2020-09-08 15:25:44 +02:00
|
|
|
|
procedure TServerTest.TestEMVCException1;
|
|
|
|
|
var
|
2020-09-12 03:24:38 +02:00
|
|
|
|
res: IMVCRESTResponse;
|
2020-09-08 15:25:44 +02:00
|
|
|
|
lJSON: TJSONObject;
|
|
|
|
|
begin
|
2020-09-12 03:24:38 +02:00
|
|
|
|
res := RESTClient.Get('/exception/emvcexception1');
|
|
|
|
|
Assert.areEqual<Integer>(HTTP_STATUS.InternalServerError, res.StatusCode);
|
|
|
|
|
lJSON := StrToJSONObject(res.Content);
|
2020-09-08 15:25:44 +02:00
|
|
|
|
try
|
|
|
|
|
Assert.areEqual<string>('message', lJSON.S['message'], lJSON.ToJSON());
|
|
|
|
|
Assert.areEqual<string>('EMVCException', lJSON.S['classname'], lJSON.ToJSON());
|
|
|
|
|
Assert.areEqual<Integer>(500, lJSON.I['statuscode'], lJSON.ToJSON());
|
|
|
|
|
Assert.areEqual<string>('error', lJSON.S['reasonstring'], lJSON.ToJSON());
|
|
|
|
|
Assert.areEqual(0, lJSON.A['items'].Count, lJSON.ToJSON());
|
|
|
|
|
Assert.isTrue(lJSON.IsNull('data'), lJSON.ToJSON());
|
|
|
|
|
finally
|
|
|
|
|
lJSON.Free;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TServerTest.TestEMVCException2;
|
|
|
|
|
var
|
2020-09-12 03:24:38 +02:00
|
|
|
|
res: IMVCRESTResponse;
|
2020-09-08 15:25:44 +02:00
|
|
|
|
lJSON: TJSONObject;
|
|
|
|
|
begin
|
2020-09-12 03:24:38 +02:00
|
|
|
|
res := RESTClient.Get('/exception/emvcexception2');
|
|
|
|
|
Assert.areEqual<Integer>(HTTP_STATUS.BadRequest, res.StatusCode);
|
|
|
|
|
lJSON := StrToJSONObject(res.Content);
|
2020-09-08 15:25:44 +02:00
|
|
|
|
try
|
|
|
|
|
Assert.areEqual<string>('message', lJSON.S['message'], lJSON.ToJSON());
|
|
|
|
|
Assert.areEqual<string>('EMVCException', lJSON.S['classname'], lJSON.ToJSON());
|
|
|
|
|
Assert.areEqual<Integer>(HTTP_STATUS.BadRequest, lJSON.I['statuscode'], lJSON.ToJSON());
|
|
|
|
|
Assert.areEqual<string>('error', lJSON.S['reasonstring'], lJSON.ToJSON());
|
|
|
|
|
Assert.areEqual(0, lJSON.A['items'].Count, lJSON.ToJSON());
|
|
|
|
|
Assert.isTrue(lJSON.IsNull('data'), lJSON.ToJSON());
|
|
|
|
|
finally
|
|
|
|
|
lJSON.Free;
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TServerTest.TestEMVCException3;
|
|
|
|
|
var
|
2020-09-12 03:24:38 +02:00
|
|
|
|
res: IMVCRESTResponse;
|
2020-09-08 15:25:44 +02:00
|
|
|
|
lJSON: TJSONObject;
|
|
|
|
|
begin
|
2020-09-12 03:24:38 +02:00
|
|
|
|
res := RESTClient.Get('/exception/emvcexception3');
|
|
|
|
|
Assert.areEqual<Integer>(HTTP_STATUS.Created, res.StatusCode);
|
|
|
|
|
lJSON := StrToJSONObject(res.Content);
|
2020-09-08 15:25:44 +02:00
|
|
|
|
try
|
|
|
|
|
Assert.areEqual('message', lJSON.S['message'], lJSON.ToJSON());
|
|
|
|
|
Assert.areEqual('EMVCException', lJSON.S['classname'], lJSON.ToJSON());
|
|
|
|
|
Assert.areEqual(HTTP_STATUS.Created, lJSON.I['statuscode'], lJSON.ToJSON());
|
|
|
|
|
Assert.areEqual('error', lJSON.S['reasonstring'], lJSON.ToJSON());
|
|
|
|
|
Assert.areEqual(999, lJSON.I['apperrorcode'], lJSON.ToJSON());
|
|
|
|
|
Assert.areEqual(0, lJSON.A['items'].Count, lJSON.ToJSON());
|
|
|
|
|
Assert.isTrue(lJSON.IsNull('data'), lJSON.ToJSON());
|
|
|
|
|
finally
|
|
|
|
|
lJSON.Free;
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TServerTest.TestEMVCException4;
|
|
|
|
|
var
|
2020-09-12 03:24:38 +02:00
|
|
|
|
res: IMVCRESTResponse;
|
2020-09-08 15:25:44 +02:00
|
|
|
|
lJSON: TJSONObject;
|
|
|
|
|
begin
|
2020-09-12 03:24:38 +02:00
|
|
|
|
res := RESTClient.Get('/exception/emvcexception4');
|
|
|
|
|
Assert.areEqual<Integer>(HTTP_STATUS.Created, res.StatusCode);
|
|
|
|
|
lJSON := StrToJSONObject(res.Content);
|
2020-09-08 15:25:44 +02:00
|
|
|
|
try
|
|
|
|
|
Assert.areEqual('message', lJSON.S['message'], lJSON.ToJSON());
|
|
|
|
|
Assert.areEqual('detailedmessage', lJSON.S['detailedmessage'], lJSON.ToJSON());
|
|
|
|
|
Assert.areEqual('EMVCException', lJSON.S['classname'], lJSON.ToJSON());
|
|
|
|
|
Assert.areEqual(HTTP_STATUS.Created, lJSON.I['statuscode'], lJSON.ToJSON());
|
|
|
|
|
Assert.areEqual('error', lJSON.S['reasonstring'], lJSON.ToJSON());
|
|
|
|
|
Assert.areEqual(999, lJSON.I['apperrorcode'], lJSON.ToJSON());
|
|
|
|
|
Assert.areEqual(2, lJSON.A['items'].Count, lJSON.ToJSON());
|
|
|
|
|
Assert.areEqual('erritem1', lJSON.A['items'].O[0].S['message'], lJSON.ToJSON());
|
|
|
|
|
Assert.areEqual('erritem2', lJSON.A['items'].O[1].S['message'], lJSON.ToJSON());
|
|
|
|
|
Assert.isTrue(lJSON.IsNull('data'), lJSON.ToJSON());
|
|
|
|
|
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
|
2020-09-12 03:24:38 +02:00
|
|
|
|
res: IMVCRESTResponse;
|
2020-09-08 15:25:44 +02:00
|
|
|
|
S: string;
|
2018-11-24 16:56:21 +01:00
|
|
|
|
lJSONObj: System.JSON.TJSONObject;
|
2015-12-22 12:29:25 +01:00
|
|
|
|
begin
|
2020-09-12 03:24:38 +02:00
|
|
|
|
res := RESTClient.Get('/encoding');
|
2015-12-22 12:29:25 +01:00
|
|
|
|
|
2020-09-12 03:24:38 +02:00
|
|
|
|
lJSONObj := TSystemJSON.StringAsJSONObject(res.Content);
|
2020-09-08 15:25:44 +02:00
|
|
|
|
S := lJSONObj.Get('name1').JsonValue.Value;
|
|
|
|
|
Assert.areEqual('j<>rn', S);
|
2017-04-26 14:39:18 +02:00
|
|
|
|
lJSONObj.Free;
|
2015-12-22 12:29:25 +01:00
|
|
|
|
|
2020-09-12 03:24:38 +02:00
|
|
|
|
lJSONObj := TSystemJSON.StringAsJSONObject(res.Content);
|
2020-09-08 15:25:44 +02:00
|
|
|
|
S := lJSONObj.Get('name3').JsonValue.Value;
|
|
|
|
|
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
|
|
|
|
|
2020-09-12 03:24:38 +02:00
|
|
|
|
lJSONObj := TSystemJSON.StringAsJSONObject(res.Content);
|
2020-09-08 15:25:44 +02:00
|
|
|
|
S := lJSONObj.Get('name2').JsonValue.Value;
|
|
|
|
|
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;
|
|
|
|
|
|
2020-11-27 15:13:17 +01:00
|
|
|
|
procedure TServerTest.TestEntityWithArrays;
|
|
|
|
|
var
|
2020-11-29 14:34:02 +01:00
|
|
|
|
lRes: IMVCRESTResponse;
|
2020-11-27 15:13:17 +01:00
|
|
|
|
lObj1, lObj2: TEntityWithArray;
|
|
|
|
|
lBody: String;
|
|
|
|
|
begin
|
|
|
|
|
lObj1 := TEntityWithArray.Create;
|
|
|
|
|
try
|
|
|
|
|
lObj1.Names := ['one','two','three'];
|
|
|
|
|
lObj1.Values := [1,2,3];
|
|
|
|
|
lObj1.Booleans := [true, false];
|
|
|
|
|
lBody := GetDefaultSerializer.SerializeObject(lObj1);
|
|
|
|
|
|
2020-11-29 14:34:02 +01:00
|
|
|
|
lRes := RESTClient.Post('/entitywitharrays', lBody);
|
2020-11-27 15:13:17 +01:00
|
|
|
|
lObj2 := TEntityWithArray.Create;
|
|
|
|
|
try
|
2020-11-29 14:34:02 +01:00
|
|
|
|
GetDefaultSerializer.DeserializeObject(lRes.Content, lObj2);
|
2020-11-27 15:13:17 +01:00
|
|
|
|
|
|
|
|
|
Assert.AreEqual(4, Length(lObj2.Names));
|
|
|
|
|
Assert.AreEqual(lObj1.Names[0], lObj2.Names[0]);
|
|
|
|
|
Assert.AreEqual(lObj1.Names[1], lObj2.Names[1]);
|
|
|
|
|
Assert.AreEqual(lObj1.Names[2], lObj2.Names[2]);
|
|
|
|
|
Assert.AreEqual('added', lObj2.Names[3]);
|
|
|
|
|
|
|
|
|
|
Assert.AreEqual(4, Length(lObj2.Values));
|
|
|
|
|
Assert.AreEqual(lObj1.Values[0], lObj2.Values[0]);
|
|
|
|
|
Assert.AreEqual(lObj1.Values[1], lObj2.Values[1]);
|
|
|
|
|
Assert.AreEqual(lObj1.Values[2], lObj2.Values[2]);
|
|
|
|
|
Assert.AreEqual(99, lObj2.Values[3]);
|
|
|
|
|
|
|
|
|
|
Assert.AreEqual(3, Length(lObj2.Booleans));
|
|
|
|
|
Assert.AreEqual(lObj1.Booleans[0], lObj2.Booleans[0]);
|
|
|
|
|
Assert.AreEqual(lObj1.Booleans[1], lObj2.Booleans[1]);
|
|
|
|
|
Assert.AreEqual(True, lObj2.Booleans[2]);
|
|
|
|
|
finally
|
|
|
|
|
lObj2.Free;
|
|
|
|
|
end;
|
|
|
|
|
finally
|
|
|
|
|
lObj1.Free;
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TServerTest.TestEntityWithEmptyArrays;
|
|
|
|
|
var
|
2020-11-29 14:34:02 +01:00
|
|
|
|
lRes: IMVCRESTResponse;
|
2020-11-27 15:13:17 +01:00
|
|
|
|
lObj1, lObj2: TEntityWithArray;
|
|
|
|
|
lBody: String;
|
|
|
|
|
begin
|
|
|
|
|
lObj1 := TEntityWithArray.Create;
|
|
|
|
|
try
|
|
|
|
|
lBody := GetDefaultSerializer.SerializeObject(lObj1);
|
|
|
|
|
|
2020-11-29 14:34:02 +01:00
|
|
|
|
lRes := RESTClient.Post('/entitywitharrays', lBody);
|
2020-11-27 15:13:17 +01:00
|
|
|
|
lObj2 := TEntityWithArray.Create;
|
|
|
|
|
try
|
2020-11-29 14:34:02 +01:00
|
|
|
|
GetDefaultSerializer.DeserializeObject(lRes.Content, lObj2);
|
2020-11-27 15:13:17 +01:00
|
|
|
|
|
|
|
|
|
Assert.AreEqual(1, Length(lObj2.Names));
|
|
|
|
|
Assert.AreEqual('added', lObj2.Names[0]);
|
|
|
|
|
|
|
|
|
|
Assert.AreEqual(1, Length(lObj2.Values));
|
|
|
|
|
Assert.AreEqual(99, lObj2.Values[0]);
|
|
|
|
|
|
|
|
|
|
Assert.AreEqual(1, Length(lObj2.Booleans));
|
|
|
|
|
Assert.AreEqual(True, lObj2.Booleans[0]);
|
|
|
|
|
finally
|
|
|
|
|
lObj2.Free;
|
|
|
|
|
end;
|
|
|
|
|
finally
|
|
|
|
|
lObj1.Free;
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
|
2019-03-08 09:33:41 +01:00
|
|
|
|
procedure TServerTest.TestRenderActionInCollections;
|
|
|
|
|
var
|
2020-09-12 03:24:38 +02:00
|
|
|
|
lRes: IMVCRESTResponse;
|
2019-03-08 09:33:41 +01:00
|
|
|
|
lJArr: TJDOJsonArray;
|
|
|
|
|
I: Integer;
|
|
|
|
|
begin
|
2020-09-12 03:24:38 +02:00
|
|
|
|
lRes := RESTClient.Get('/people/renderaction');
|
|
|
|
|
lJArr := TJsonBaseObject.Parse(lRes.Content) as TJDOJsonArray;
|
2019-03-08 09:33:41 +01:00
|
|
|
|
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');
|
2020-09-08 15:25:44 +02:00
|
|
|
|
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
|
2020-09-12 03:24:38 +02:00
|
|
|
|
lRes: IMVCRESTResponse;
|
2016-11-18 18:02:48 +01:00
|
|
|
|
begin
|
2020-09-12 03:24:38 +02:00
|
|
|
|
lRes := RESTClient.Get('/renderstreamandfreewithownerfalse');
|
|
|
|
|
Assert.areEqual<Integer>(200, lRes.StatusCode);
|
2016-11-18 18:02:48 +01:00
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TServerTest.TestRenderStreamAndFreeWithOwnerTrue;
|
|
|
|
|
var
|
2020-09-12 03:24:38 +02:00
|
|
|
|
lRes: IMVCRESTResponse;
|
2016-11-18 18:02:48 +01:00
|
|
|
|
begin
|
2020-09-12 03:24:38 +02:00
|
|
|
|
lRes := RESTClient.Get('/renderstreamandfreewithownertrue');
|
|
|
|
|
Assert.areEqual<Integer>(200, lRes.StatusCode);
|
2016-11-18 18:02:48 +01:00
|
|
|
|
end;
|
|
|
|
|
|
2016-09-29 18:17:12 +02:00
|
|
|
|
procedure TServerTest.TestRenderWrappedList;
|
|
|
|
|
var
|
2020-09-12 03:24:38 +02:00
|
|
|
|
lRes: IMVCRESTResponse;
|
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
|
2020-09-12 03:24:38 +02:00
|
|
|
|
lRes := RESTClient.Get('/wrappedpeople');
|
2016-09-29 18:17:12 +02:00
|
|
|
|
|
2020-09-12 03:24:38 +02:00
|
|
|
|
lJSONArr := TSystemJSON.StringAsJSONArray(lRes.Content);
|
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
|
2020-09-12 03:24:38 +02:00
|
|
|
|
lRes: IMVCRESTResponse;
|
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
|
2020-09-06 15:38:25 +02: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
|
2020-09-12 03:24:38 +02:00
|
|
|
|
RESTClient.AcceptEncoding(lCompType);
|
|
|
|
|
lRes := RESTClient
|
|
|
|
|
.AddQueryStringParam('count', 100)
|
|
|
|
|
.Get('/wrappedpeople');
|
2019-03-05 20:55:37 +01:00
|
|
|
|
Assert.areEqual(CompressionTypeResult[j], lRes.HeaderValue('Content-Encoding'));
|
2020-09-12 03:24:38 +02:00
|
|
|
|
lJSONArr := TMVCJsonDataObjectsSerializer.ParseArray(lRes.Content);
|
2018-10-23 16:18:34 +02:00
|
|
|
|
try
|
|
|
|
|
for I := 0 to lJSONArr.Count - 1 do
|
|
|
|
|
begin
|
2020-09-08 15:25:44 +02:00
|
|
|
|
Assert.isFalse(lJSONArr.O[I].S['firstname'].IsEmpty);
|
|
|
|
|
Assert.isFalse(lJSONArr.O[I].S['lastname'].IsEmpty);
|
|
|
|
|
Assert.isFalse(lJSONArr.O[I].S['dob'].IsEmpty);
|
2018-10-23 16:18:34 +02:00
|
|
|
|
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
|
2020-09-12 03:24:38 +02:00
|
|
|
|
res: IMVCRESTResponse;
|
2015-12-22 12:29:25 +01:00
|
|
|
|
begin
|
2020-09-12 03:24:38 +02:00
|
|
|
|
res := RESTClient.Get('/exception/aftercreate/nevercalled');
|
|
|
|
|
Assert.areEqual<Integer>(HTTP_STATUS.InternalServerError, res.StatusCode);
|
2015-12-22 12:29:25 +01:00
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TServerTest.TestExceptionInMVCBeforeDestroy;
|
|
|
|
|
var
|
2020-09-12 03:24:38 +02:00
|
|
|
|
res: IMVCRESTResponse;
|
2015-12-22 12:29:25 +01:00
|
|
|
|
begin
|
2020-09-12 03:24:38 +02:00
|
|
|
|
res := RESTClient.Get('/exception/beforedestroy/nevercalled');
|
|
|
|
|
Assert.areEqual<Integer>(HTTP_STATUS.InternalServerError, res.StatusCode);
|
2015-12-22 12:29:25 +01:00
|
|
|
|
end;
|
|
|
|
|
|
2020-09-14 15:52:50 +02:00
|
|
|
|
procedure TServerTest.TestFileWithFolderName;
|
|
|
|
|
var
|
2020-09-16 20:01:50 +02:00
|
|
|
|
lRes: IMVCRESTResponse;
|
2020-09-14 15:52:50 +02:00
|
|
|
|
begin
|
2020-09-16 20:01:50 +02:00
|
|
|
|
lRes := RESTClient.Accept(TMVCMediaType.TEXT_HTML).Get('');
|
|
|
|
|
Assert.areEqual(404, lRes.StatusCode, '<empty>');
|
2020-09-14 15:52:50 +02:00
|
|
|
|
|
2020-09-16 20:01:50 +02:00
|
|
|
|
lRes := RESTClient.Accept(TMVCMediaType.TEXT_HTML).Get('/static/index.html');
|
|
|
|
|
Assert.areEqual(200, lRes.StatusCode, '/static/index.html');
|
2020-09-14 15:52:50 +02:00
|
|
|
|
|
2020-09-16 20:01:50 +02:00
|
|
|
|
lRes := RESTClient.Accept(TMVCMediaType.TEXT_HTML).Get('/static.html');
|
|
|
|
|
Assert.areEqual(404, lRes.StatusCode, '/static.html');
|
2020-09-16 15:56:14 +02:00
|
|
|
|
|
2020-09-18 01:06:24 +02:00
|
|
|
|
lRes := RESTClient.HandleRedirects(False).Accept(TMVCMediaType.TEXT_HTML).Get('/static');
|
2020-09-16 20:01:50 +02:00
|
|
|
|
Assert.areEqual(301, lRes.StatusCode, '/static');
|
2020-09-16 15:56:14 +02:00
|
|
|
|
|
2020-09-16 20:01:50 +02:00
|
|
|
|
lRes := RESTClient.Accept(TMVCMediaType.TEXT_HTML).Get('/static/');
|
|
|
|
|
Assert.areEqual(200, lRes.StatusCode, '/static/');
|
2020-09-14 15:52:50 +02:00
|
|
|
|
|
2020-09-18 01:06:24 +02:00
|
|
|
|
lRes := RESTClient.HandleRedirects(False).Accept(TMVCMediaType.TEXT_HTML).Get('/static/folder1');
|
2020-09-16 20:01:50 +02:00
|
|
|
|
Assert.areEqual(301, lRes.StatusCode, '/static/folder1');
|
2020-09-14 15:52:50 +02:00
|
|
|
|
|
2020-09-16 20:01:50 +02:00
|
|
|
|
lRes := RESTClient.Accept(TMVCMediaType.TEXT_HTML).Get('/static/folder1/');
|
|
|
|
|
Assert.areEqual(200, lRes.StatusCode, '/static/folder1/');
|
2020-09-14 15:52:50 +02:00
|
|
|
|
|
2020-09-16 20:01:50 +02:00
|
|
|
|
lRes := RESTClient.Accept(TMVCMediaType.TEXT_HTML).Get('/static/folder1.html');
|
|
|
|
|
Assert.areEqual(200, lRes.StatusCode, '/static/folder1.html');
|
|
|
|
|
Assert.areEqual('This is a TEXT file', lRes.Content, '/static/folder1.html');
|
2020-09-14 15:52:50 +02:00
|
|
|
|
end;
|
|
|
|
|
|
2019-03-05 20:55:37 +01:00
|
|
|
|
procedure TServerTest.TestGetImagePng;
|
|
|
|
|
var
|
2020-09-12 03:24:38 +02:00
|
|
|
|
c1: IMVCRESTClient;
|
|
|
|
|
lRes: IMVCRESTResponse;
|
2019-03-05 20:55:37 +01:00
|
|
|
|
begin
|
2020-09-12 03:24:38 +02:00
|
|
|
|
c1 := TMVCRESTClient.New.BaseURL(TEST_SERVER_ADDRESS, 9999);
|
|
|
|
|
// c1.Accept(TMVCMediaType.IMAGE_PNG);
|
|
|
|
|
lRes := c1.Get('/image/png');
|
|
|
|
|
Assert.areEqual(200, lRes.StatusCode);
|
|
|
|
|
Assert.areEqual('image/png', lRes.ContentType);
|
|
|
|
|
Assert.areEqual(249, Integer(lRes.ContentLength));
|
2019-03-05 20:55:37 +01:00
|
|
|
|
end;
|
|
|
|
|
|
2016-04-03 22:35:27 +02:00
|
|
|
|
procedure TServerTest.TestInvalidateSession;
|
|
|
|
|
var
|
2020-09-12 03:24:38 +02:00
|
|
|
|
c1: IMVCRESTClient;
|
|
|
|
|
res: IMVCRESTResponse;
|
2016-04-03 22:35:27 +02:00
|
|
|
|
begin
|
2020-09-12 03:24:38 +02:00
|
|
|
|
c1 := TMVCRESTClient.New.BaseURL(TEST_SERVER_ADDRESS, 9999);
|
|
|
|
|
c1.Accept(TMVCMediaType.APPLICATION_JSON);
|
|
|
|
|
c1.AddPathParam('param1', 'daniele teti').Post('/session/($param1)'); // imposto un valore in sessione
|
|
|
|
|
res := c1.Get('/session'); // rileggo il valore dalla sessione
|
|
|
|
|
Assert.areEqual('daniele teti', res.Content);
|
2020-09-24 20:03:11 +02:00
|
|
|
|
c1.SessionId('');
|
2020-09-12 03:24:38 +02:00
|
|
|
|
res := c1.Get('/session'); // rileggo il valore dalla sessione
|
|
|
|
|
Assert.areEqual('', res.Content);
|
2016-04-03 22:35:27 +02:00
|
|
|
|
end;
|
|
|
|
|
|
2020-09-08 15:25:44 +02:00
|
|
|
|
procedure TServerTest.TestIssue406;
|
|
|
|
|
var
|
2020-09-12 03:24:38 +02:00
|
|
|
|
r: IMVCRESTResponse;
|
2020-09-08 15:25:44 +02:00
|
|
|
|
begin
|
2020-09-12 03:24:38 +02:00
|
|
|
|
r := RESTClient.Accept(TMVCMediaType.APPLICATION_JSON).Get('/issues/406');
|
|
|
|
|
Assert.areEqual(422, r.StatusCode);
|
|
|
|
|
Assert.areEqual('{"message":"The Message"}', r.Content, r.Content);
|
2020-09-08 15:25:44 +02:00
|
|
|
|
end;
|
|
|
|
|
|
2015-12-22 12:29:25 +01:00
|
|
|
|
procedure TServerTest.TestMiddlewareHandler;
|
|
|
|
|
var
|
2020-09-12 03:24:38 +02:00
|
|
|
|
r: IMVCRESTResponse;
|
2015-12-22 12:29:25 +01:00
|
|
|
|
begin
|
2020-09-12 03:24:38 +02:00
|
|
|
|
r := RESTClient.Accept(TMVCMediaType.APPLICATION_JSON).Get('/handledbymiddleware');
|
|
|
|
|
Assert.areEqual('This is a middleware response', r.Content);
|
|
|
|
|
Assert.areEqual<Integer>(HTTP_STATUS.OK, r.StatusCode);
|
2015-12-22 12:29:25 +01:00
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TServerTest.TestMiddlewareSpeedMiddleware;
|
|
|
|
|
var
|
2020-09-12 03:24:38 +02:00
|
|
|
|
r: IMVCRESTResponse;
|
2015-12-22 12:29:25 +01:00
|
|
|
|
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)
|
2020-09-12 03:24:38 +02:00
|
|
|
|
.Post('/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;
|
|
|
|
|
|
2020-04-21 17:04:04 +02:00
|
|
|
|
procedure TServerTest.TestObjectDict;
|
|
|
|
|
var
|
2020-09-12 03:24:38 +02:00
|
|
|
|
lRes: IMVCRESTResponse;
|
2020-04-21 17:04:04 +02:00
|
|
|
|
lJSON: TJSONObject;
|
|
|
|
|
begin
|
2020-09-12 03:24:38 +02:00
|
|
|
|
lRes := RESTClient.Get('/objectdict');
|
|
|
|
|
Assert.areEqual<Integer>(HTTP_STATUS.OK, lRes.StatusCode, lRes.Content);
|
|
|
|
|
lJSON := StrToJSONObject(lRes.Content);
|
2020-04-21 17:04:04 +02:00
|
|
|
|
try
|
|
|
|
|
Assert.isTrue(lJSON.Contains('ncCamelCase_Single'), lJSON.ToJSON());
|
|
|
|
|
Assert.isTrue(lJSON.Contains('ncLowerCase_Single'), lJSON.ToJSON());
|
|
|
|
|
Assert.isTrue(lJSON.Contains('ncPascalCase_Single'), lJSON.ToJSON());
|
|
|
|
|
Assert.isTrue(lJSON.Contains('ncUpperCase_Single'), lJSON.ToJSON());
|
|
|
|
|
|
|
|
|
|
Assert.isTrue(lJSON.Contains('ncCamelCase_List'), lJSON.ToJSON());
|
|
|
|
|
Assert.isTrue(lJSON.Contains('ncLowerCase_List'), lJSON.ToJSON());
|
|
|
|
|
Assert.isTrue(lJSON.Contains('ncPascalCase_List'), lJSON.ToJSON());
|
|
|
|
|
Assert.isTrue(lJSON.Contains('ncUpperCase_List'), lJSON.ToJSON());
|
|
|
|
|
|
|
|
|
|
Assert.areEqual(jdtObject, lJSON.Types['ncCamelCase_Single']);
|
|
|
|
|
Assert.areEqual(jdtObject, lJSON.Types['ncLowerCase_Single']);
|
|
|
|
|
Assert.areEqual(jdtObject, lJSON.Types['ncPascalCase_Single']);
|
|
|
|
|
Assert.areEqual(jdtObject, lJSON.Types['ncUpperCase_Single']);
|
|
|
|
|
|
|
|
|
|
Assert.isTrue(lJSON.O['ncCamelCase_Single'].Contains('custNo'), lJSON.O['ncCamelCase_Single'].ToJSON());
|
|
|
|
|
Assert.isTrue(lJSON.O['ncLowerCase_Single'].Contains('cust_no'), lJSON.O['ncLowerCase_Single'].ToJSON());
|
|
|
|
|
Assert.isTrue(lJSON.O['ncPascalCase_Single'].Contains('CustNo'), lJSON.O['ncPascalCase_Single'].ToJSON());
|
|
|
|
|
Assert.isTrue(lJSON.O['ncUpperCase_Single'].Contains('CUST_NO'), lJSON.O['ncUpperCase_Single'].ToJSON());
|
|
|
|
|
|
|
|
|
|
Assert.areEqual(jdtArray, lJSON.Types['ncCamelCase_List']);
|
|
|
|
|
Assert.areEqual(jdtArray, lJSON.Types['ncLowerCase_List']);
|
|
|
|
|
Assert.areEqual(jdtArray, lJSON.Types['ncPascalCase_List']);
|
|
|
|
|
Assert.areEqual(jdtArray, lJSON.Types['ncUpperCase_List']);
|
|
|
|
|
|
|
|
|
|
Assert.isTrue(lJSON.A['ncCamelCase_List'][0].ObjectValue.Contains('custNo'),
|
|
|
|
|
lJSON.A['ncCamelCase_List'][0].ObjectValue.ToJSON());
|
|
|
|
|
Assert.isTrue(lJSON.A['ncLowerCase_List'][0].ObjectValue.Contains('cust_no'),
|
|
|
|
|
lJSON.A['ncLowerCase_List'][0].ObjectValue.ToJSON());
|
|
|
|
|
Assert.isTrue(lJSON.A['ncPascalCase_List'][0].ObjectValue.Contains('CustNo'),
|
|
|
|
|
lJSON.A['ncPascalCase_List'][0].ObjectValue.ToJSON());
|
|
|
|
|
Assert.isTrue(lJSON.A['ncUpperCase_List'][0].ObjectValue.Contains('CUST_NO'),
|
|
|
|
|
lJSON.A['ncUpperCase_List'][0].ObjectValue.ToJSON());
|
|
|
|
|
|
|
|
|
|
finally
|
|
|
|
|
lJSON.Free;
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
|
2015-12-22 12:29:25 +01:00
|
|
|
|
// procedure TServerTest.TestPATCHWithParamsAndJSONBody;
|
|
|
|
|
// var
|
2020-09-12 03:24:38 +02:00
|
|
|
|
// r: IMVCRESTResponse;
|
2015-12-22 12:29:25 +01:00
|
|
|
|
// 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
|
2020-09-12 03:24:38 +02:00
|
|
|
|
lRes: IMVCRESTResponse;
|
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
|
2020-09-12 03:24:38 +02:00
|
|
|
|
lRes := RESTClient.Post('/customers/list', lSer.SerializeCollection(LCustomers));
|
|
|
|
|
Assert.areEqual<Integer>(HTTP_STATUS.OK, lRes.StatusCode);
|
2015-12-22 12:29:25 +01:00
|
|
|
|
finally
|
|
|
|
|
LCustomers.Free;
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TServerTest.TestPOSTWithObjectJSONBody;
|
|
|
|
|
var
|
2020-09-12 03:24:38 +02:00
|
|
|
|
r: IMVCRESTResponse;
|
2015-12-22 12:29:25 +01:00
|
|
|
|
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)
|
2020-09-12 03:24:38 +02:00
|
|
|
|
.Post('/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
|
2020-09-12 03:24:38 +02:00
|
|
|
|
GetDefaultSerializer.DeserializeObject(r.Content, P);
|
2017-04-26 14:39:18 +02:00
|
|
|
|
// 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
|
2020-09-12 03:24:38 +02:00
|
|
|
|
r: IMVCRESTResponse;
|
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');
|
2020-09-12 03:24:38 +02:00
|
|
|
|
r := RESTClient
|
|
|
|
|
.AddPathParam('par1', 1)
|
|
|
|
|
.AddPathParam('par2', 2)
|
|
|
|
|
.AddPathParam('par3', 3)
|
|
|
|
|
.Post('/echo/($par1)/($par2)/($par3)', TSystemJSON.JSONValueToString(JSON));
|
|
|
|
|
JSON := TSystemJSON.StringAsJSONObject(r.Content);
|
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
|
2020-09-12 03:24:38 +02:00
|
|
|
|
res: IMVCRESTResponse;
|
2015-12-22 12:29:25 +01:00
|
|
|
|
begin
|
2016-04-03 22:35:27 +02:00
|
|
|
|
res := RESTClient.Accept('text/plain')
|
|
|
|
|
// action is waiting for a accept: application/json
|
2020-09-12 03:24:38 +02:00
|
|
|
|
.Post('/testconsumes', TSystemJSON.JSONValueToString(TJSONString.Create('Hello World')), 'application/json');
|
|
|
|
|
Assert.areEqual<Integer>(HTTP_STATUS.NotFound, res.StatusCode);
|
2015-12-22 12:29:25 +01:00
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TServerTest.TestProducesConsumes01;
|
|
|
|
|
var
|
2020-09-12 03:24:38 +02:00
|
|
|
|
res: IMVCRESTResponse;
|
|
|
|
|
lContentType: string;
|
|
|
|
|
lContentCharset: string;
|
2015-12-22 12:29:25 +01:00
|
|
|
|
begin
|
2020-09-12 03:24:38 +02:00
|
|
|
|
res := RESTClient
|
|
|
|
|
.Accept('application/json')
|
|
|
|
|
.Post('/testconsumes', TSystemJSON.JSONValueToString(TJSONString.Create('Hello World')),
|
|
|
|
|
BuildContentType('application/json', 'utf-8'));
|
|
|
|
|
Assert.areEqual<Integer>(HTTP_STATUS.OK, res.StatusCode);
|
|
|
|
|
Assert.areEqual('Hello World', res.Content);
|
|
|
|
|
|
|
|
|
|
SplitContentMediaTypeAndCharset(res.ContentType, lContentType, lContentCharset);
|
|
|
|
|
Assert.AreEqual(lContentType, TMVCMediaType.APPLICATION_JSON, True);
|
|
|
|
|
Assert.AreEqual(lContentCharset, TMVCCharSet.UTF_8, True);
|
2015-12-22 12:29:25 +01:00
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TServerTest.TestProducesConsumes02;
|
|
|
|
|
var
|
2020-09-12 03:24:38 +02:00
|
|
|
|
res: IMVCRESTResponse;
|
|
|
|
|
lContentType: string;
|
|
|
|
|
lContentCharset: string;
|
2015-12-22 12:29:25 +01:00
|
|
|
|
begin
|
2020-09-12 03:24:38 +02:00
|
|
|
|
res := RESTClient
|
|
|
|
|
.Accept('text/plain')
|
|
|
|
|
.Post('/testconsumes', 'Hello World', 'text/plain');
|
2020-08-26 01:07:21 +02:00
|
|
|
|
Assert.areEqual('Hello World', res.Content);
|
2020-09-12 03:24:38 +02:00
|
|
|
|
SplitContentMediaTypeAndCharset(res.ContentType, lContentType, lContentCharset);
|
|
|
|
|
Assert.AreEqual(lContentType, TMVCMediaType.TEXT_PLAIN, True);
|
|
|
|
|
Assert.AreEqual(lContentCharset, TMVCCharSet.UTF_8, True);
|
2015-12-22 12:29:25 +01:00
|
|
|
|
|
2020-09-12 03:24:38 +02:00
|
|
|
|
res := RESTClient
|
|
|
|
|
.Accept('text/plain')
|
|
|
|
|
.Post('/testconsumes', '{"name": "Daniele"}');
|
|
|
|
|
Assert.areEqual<Integer>(HTTP_STATUS.NotFound, res.StatusCode);
|
2015-12-22 12:29:25 +01:00
|
|
|
|
end;
|
|
|
|
|
|
2016-12-07 19:35:36 +01:00
|
|
|
|
procedure TServerTest.TestProducesConsumes03;
|
|
|
|
|
var
|
2020-09-12 03:24:38 +02:00
|
|
|
|
res: IMVCRESTResponse;
|
|
|
|
|
lContentType: string;
|
|
|
|
|
lContentCharset: string;
|
2016-12-07 19:35:36 +01:00
|
|
|
|
begin
|
2020-09-12 03:24:38 +02:00
|
|
|
|
res := RESTClient
|
|
|
|
|
.Accept(TMVCMediaType.TEXT_PLAIN)
|
|
|
|
|
.Post('/testconsumes/textiso8859_1', '<27><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>', BuildContentType(TMVCMediaType.TEXT_PLAIN, TMVCCharSet.ISO88591));
|
|
|
|
|
Assert.areEqual<Integer>(HTTP_STATUS.OK, res.StatusCode);
|
|
|
|
|
// Assert.AreNotEqual('<27><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>', res.Content, 'non iso8859-1 text is rendered ok whan should not');
|
|
|
|
|
SplitContentMediaTypeAndCharset(res.ContentType, lContentType, lContentCharset);
|
|
|
|
|
Assert.AreEqual(lContentType, TMVCMediaType.TEXT_PLAIN);
|
|
|
|
|
Assert.AreEqual(lContentCharset, TMVCCharset.ISO88591);
|
|
|
|
|
|
|
|
|
|
res := RESTClient
|
|
|
|
|
.Accept(TMVCMediaType.TEXT_PLAIN)
|
|
|
|
|
.Post('/testconsumes/textiso8859_1', 'this is an iso8859-1 text',
|
|
|
|
|
BuildContentType(TMVCMediaType.TEXT_PLAIN, TMVCCharSet.ISO88591));
|
2020-08-26 01:07:21 +02:00
|
|
|
|
Assert.areEqual<Integer>(HTTP_STATUS.OK, res.StatusCode);
|
2020-09-12 03:24:38 +02:00
|
|
|
|
SplitContentMediaTypeAndCharset(res.ContentType, lContentType, lContentCharset);
|
|
|
|
|
|
2020-08-26 01:07:21 +02:00
|
|
|
|
Assert.areEqual('this is an iso8859-1 text', res.Content);
|
2020-09-12 03:24:38 +02:00
|
|
|
|
Assert.AreEqual(lContentType, TMVCMediaType.TEXT_PLAIN);
|
|
|
|
|
Assert.AreEqual(lContentCharset, TMVCCharset.ISO88591);
|
2016-12-07 19:35:36 +01:00
|
|
|
|
end;
|
|
|
|
|
|
2015-12-22 12:29:25 +01:00
|
|
|
|
procedure TServerTest.TestPUTWithParamsAndJSONBody;
|
|
|
|
|
var
|
2020-09-12 03:24:38 +02:00
|
|
|
|
r: IMVCRESTResponse;
|
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');
|
2020-09-12 03:24:38 +02:00
|
|
|
|
r := RESTClient
|
|
|
|
|
.AddPathParam('par1', 1)
|
|
|
|
|
.AddPathParam('par2', 2)
|
|
|
|
|
.AddPathParam('par3', 3)
|
|
|
|
|
.Put('/echo/($par1)/($par2)/($par3)', TSystemJSON.JSONValueToString(JSON));
|
2017-04-26 14:39:18 +02:00
|
|
|
|
|
2020-09-12 03:24:38 +02:00
|
|
|
|
JSON := TSystemJSON.StringAsJSONObject(r.Content);
|
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
|
2020-09-12 03:24:38 +02:00
|
|
|
|
r: IMVCRESTResponse;
|
2019-05-10 00:46:03 +02:00
|
|
|
|
JSON: System.JSON.TJSONObject;
|
|
|
|
|
begin
|
|
|
|
|
JSON := System.JSON.TJSONObject.Create;
|
|
|
|
|
JSON.AddPair('client', 'clientdata');
|
2020-09-12 03:24:38 +02:00
|
|
|
|
r := RESTClient
|
|
|
|
|
.AddHeader(TMVCConstants.X_HTTP_Method_Override, 'PUT')
|
|
|
|
|
.AddPathParam('par1', 1)
|
|
|
|
|
.AddPathParam('par2', 2)
|
|
|
|
|
.AddPathParam('par3', 3)
|
|
|
|
|
.Post('/echo/($par1)/($par2)/($par3)', TSystemJSON.JSONValueToString(JSON));
|
|
|
|
|
|
|
|
|
|
JSON := TSystemJSON.StringAsJSONObject(r.Content);
|
2019-05-10 00:46:03 +02:00
|
|
|
|
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;
|
2020-09-12 03:24:38 +02:00
|
|
|
|
r: IMVCRESTResponse;
|
2015-12-22 12:29:25 +01:00
|
|
|
|
begin
|
2020-09-12 03:24:38 +02:00
|
|
|
|
r := RESTClient.Get('/unknownurl/bla/bla');
|
2016-05-13 17:20:08 +02:00
|
|
|
|
|
|
|
|
|
ss := TStringStream.Create;
|
|
|
|
|
try
|
2020-09-12 03:24:38 +02:00
|
|
|
|
r.SaveContentToStream(ss);
|
|
|
|
|
Assert.areEqual(ss.DataString, r.Content,
|
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;
|
|
|
|
|
|
2020-09-12 03:24:38 +02:00
|
|
|
|
Assert.areEqual<Integer>(HTTP_STATUS.NotFound, r.StatusCode, '/unknownurl/bla/bla');
|
2015-12-22 12:29:25 +01:00
|
|
|
|
|
2020-09-12 03:24:38 +02:00
|
|
|
|
r := RESTClient.Get('/req/with/params/');
|
|
|
|
|
Assert.areEqual<Integer>(HTTP_STATUS.NotFound, r.StatusCode, '/req/with/params/');
|
2015-12-22 12:29:25 +01:00
|
|
|
|
|
2020-09-12 03:24:38 +02:00
|
|
|
|
r := RESTClient.Get('/req/with/params');
|
|
|
|
|
Assert.areEqual<Integer>(HTTP_STATUS.NotFound, r.StatusCode, '/req/with/params');
|
2015-12-22 12:29:25 +01:00
|
|
|
|
|
2020-09-12 03:24:38 +02:00
|
|
|
|
r := RESTClient
|
|
|
|
|
.AddPathParam('par1', '1')
|
|
|
|
|
.AddPathParam('par2', '2')
|
|
|
|
|
.AddPathParam('par3', '3')
|
|
|
|
|
.Get('/req/with/params/($par1)/($par2)/($par3)');
|
|
|
|
|
Assert.areEqual<Integer>(HTTP_STATUS.OK, r.StatusCode);
|
2015-12-22 12:29:25 +01:00
|
|
|
|
|
2020-09-12 03:24:38 +02:00
|
|
|
|
lJSON := TSystemJSON.StringAsJSONObject(r.Content);
|
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);
|
2020-09-12 03:24:38 +02:00
|
|
|
|
finally
|
|
|
|
|
lJSON.Free;
|
|
|
|
|
end;
|
2015-12-22 12:29:25 +01:00
|
|
|
|
|
2020-09-06 15:38:25 +02:00
|
|
|
|
|
2020-09-12 03:24:38 +02:00
|
|
|
|
r := RESTClient
|
|
|
|
|
.AddPathParam('par1', 1)
|
|
|
|
|
.AddPathParam('par2', 2)
|
|
|
|
|
.AddPathParam('par3', 3)
|
|
|
|
|
.Post('/req/with/params/($par1)/($par2)/($par3)');
|
|
|
|
|
Assert.areEqual<Integer>(HTTP_STATUS.NotFound, r.StatusCode);
|
|
|
|
|
|
|
|
|
|
r := RESTClient
|
|
|
|
|
.AddPathParam('par1', 1)
|
|
|
|
|
.AddPathParam('par2', 2)
|
|
|
|
|
.AddPathParam('par3', 3)
|
|
|
|
|
.Put('/req/with/params/($par1)/($par2)/($par3)');
|
|
|
|
|
Assert.areEqual<Integer>(HTTP_STATUS.NotFound, r.StatusCode);
|
|
|
|
|
|
|
|
|
|
r := RESTClient
|
|
|
|
|
.AddPathParam('par1', 1)
|
|
|
|
|
.AddPathParam('par2', 2)
|
|
|
|
|
.AddPathParam('par3', 3)
|
|
|
|
|
.Delete('/req/with/params/($par1)/($par2)/($par3)');
|
|
|
|
|
Assert.areEqual<Integer>(HTTP_STATUS.OK, r.StatusCode);
|
|
|
|
|
lJSON := TSystemJSON.StringAsJSONObject(r.Content);
|
|
|
|
|
try
|
|
|
|
|
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('DELETE', lJSON.Get('method').JsonValue.Value);
|
2017-04-26 14:39:18 +02:00
|
|
|
|
finally
|
|
|
|
|
lJSON.Free;
|
|
|
|
|
end;
|
2020-09-06 15:38:25 +02:00
|
|
|
|
|
2017-11-23 17:29:51 +01:00
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TServerTest.TestReqWithURLMappedParams(const par1, par2, par3: string);
|
|
|
|
|
var
|
2020-09-12 03:24:38 +02:00
|
|
|
|
r: IMVCRESTResponse;
|
2017-11-23 17:29:51 +01:00
|
|
|
|
begin
|
2020-09-12 03:24:38 +02:00
|
|
|
|
r := RESTClient
|
|
|
|
|
.AddPathParam('par1', par1)
|
|
|
|
|
.AddPathParam('par2', par2)
|
|
|
|
|
.AddPathParam('par3', par3)
|
|
|
|
|
.Get('/req/with/params/($par1)/($par2)/($par3)');
|
|
|
|
|
Assert.areEqual<Integer>(HTTP_STATUS.OK, r.StatusCode,
|
2017-11-23 17:29:51 +01:00
|
|
|
|
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
|
2020-09-12 03:24:38 +02:00
|
|
|
|
r: IMVCRESTResponse;
|
2019-09-01 20:35:19 +02:00
|
|
|
|
lJSON: TJDOJsonObject;
|
|
|
|
|
begin
|
2020-09-12 03:24:38 +02:00
|
|
|
|
r := RESTClient.Post('/responses/accepted');
|
|
|
|
|
Assert.areEqual<Integer>(HTTP_STATUS.Accepted, r.StatusCode);
|
|
|
|
|
Assert.isTrue(r.StatusText.Contains('thisisthereason'));
|
|
|
|
|
lJSON := StrToJSONObject(r.Content);
|
2019-09-01 20:35:19 +02:00
|
|
|
|
try
|
|
|
|
|
Assert.areEqual(2, lJSON.O['task'].Count);
|
2020-09-08 15:25:44 +02:00
|
|
|
|
Assert.areEqual('http://pippo.it/1234', lJSON.O['task'].S['href']);
|
|
|
|
|
Assert.areEqual('1234', lJSON.O['task'].S['id']);
|
2019-09-01 20:35:19 +02:00
|
|
|
|
finally
|
|
|
|
|
lJSON.Free;
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TServerTest.TestResponseCreated;
|
|
|
|
|
var
|
2020-09-12 03:24:38 +02:00
|
|
|
|
r: IMVCRESTResponse;
|
2019-09-01 20:35:19 +02:00
|
|
|
|
begin
|
2020-09-12 03:24:38 +02:00
|
|
|
|
r := RESTClient.Post('/responses/created');
|
|
|
|
|
Assert.areEqual<Integer>(HTTP_STATUS.Created, r.StatusCode);
|
|
|
|
|
Assert.isTrue(r.StatusText.Contains('thisisthereason'));
|
|
|
|
|
Assert.IsEmpty(r.Content);
|
2019-09-01 20:35:19 +02:00
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TServerTest.TestResponseNoContent;
|
|
|
|
|
var
|
2020-09-12 03:24:38 +02:00
|
|
|
|
r: IMVCRESTResponse;
|
2019-09-01 20:35:19 +02:00
|
|
|
|
begin
|
2020-09-12 03:24:38 +02:00
|
|
|
|
r := RESTClient.Get('/responses/nocontent');
|
|
|
|
|
Assert.areEqual<Integer>(HTTP_STATUS.NoContent, r.StatusCode);
|
|
|
|
|
Assert.isTrue(r.StatusText.Contains('No Content'));
|
|
|
|
|
Assert.IsEmpty(r.Content);
|
2019-09-01 20:35:19 +02:00
|
|
|
|
end;
|
|
|
|
|
|
2017-04-14 16:43:31 +02:00
|
|
|
|
// procedure TServerTest.TestSerializationType;
|
|
|
|
|
// var
|
2020-09-12 03:24:38 +02:00
|
|
|
|
// LResp: IMVCRESTResponse;
|
2017-04-14 16:43:31 +02:00
|
|
|
|
// LPersonProps, LPersonFlds: TPerson;
|
|
|
|
|
// LObj: TObject;
|
|
|
|
|
// begin
|
2020-09-12 03:24:38 +02:00
|
|
|
|
// LResp := RESTClient.Get('/people', ['1']);
|
2017-04-14 16:43:31 +02:00
|
|
|
|
// LPersonProps := Mapper.JSONObjectToObject<TPerson>(LResp.BodyAsJsonObject);
|
|
|
|
|
// try
|
2020-09-12 03:24:38 +02:00
|
|
|
|
// LResp := RESTClient.Get('/people', ['1', 'asfields']);
|
2017-04-14 16:43:31 +02:00
|
|
|
|
// 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
|
|
|
|
|
2020-02-03 13:19:55 +01:00
|
|
|
|
procedure TServerTest.TestDeserializeNullablesWithNulls;
|
2020-02-03 12:11:42 +01:00
|
|
|
|
var
|
2020-09-12 03:24:38 +02:00
|
|
|
|
lRes: IMVCRESTResponse;
|
2020-02-03 12:11:42 +01:00
|
|
|
|
lSer: TMVCJsonDataObjectsSerializer;
|
|
|
|
|
lNullableTest: TNullablesTest;
|
|
|
|
|
begin
|
2020-02-03 13:19:55 +01:00
|
|
|
|
/// nullables/getsinglewithnulls
|
|
|
|
|
|
2020-09-12 03:24:38 +02:00
|
|
|
|
lRes := RESTClient.Get('/nullables/getsinglewithnulls');
|
2020-02-03 13:19:55 +01:00
|
|
|
|
lSer := TMVCJsonDataObjectsSerializer.Create;
|
|
|
|
|
try
|
|
|
|
|
lNullableTest := TNullablesTest.Create();
|
|
|
|
|
try
|
2020-09-12 03:24:38 +02:00
|
|
|
|
lSer.DeserializeObject(lRes.Content, lNullableTest);
|
2020-02-03 13:19:55 +01:00
|
|
|
|
Assert.isFalse(lNullableTest.f_int2.HasValue);
|
|
|
|
|
Assert.isFalse(lNullableTest.f_int4.HasValue);
|
|
|
|
|
Assert.isFalse(lNullableTest.f_int8.HasValue);
|
|
|
|
|
Assert.isFalse(lNullableTest.f_date.HasValue);
|
|
|
|
|
Assert.isFalse(lNullableTest.f_time.HasValue);
|
|
|
|
|
Assert.isFalse(lNullableTest.f_datetime.HasValue);
|
|
|
|
|
Assert.isFalse(lNullableTest.f_bool.HasValue);
|
|
|
|
|
Assert.isFalse(lNullableTest.f_float4.HasValue);
|
|
|
|
|
Assert.isFalse(lNullableTest.f_float8.HasValue);
|
|
|
|
|
Assert.isFalse(lNullableTest.f_string.HasValue);
|
|
|
|
|
Assert.isFalse(lNullableTest.f_currency.HasValue);
|
|
|
|
|
{ TODO -oDanieleT -cGeneral : Compare streams too }
|
|
|
|
|
// Assert.AreEqual('0123456789', lNullableTest.f_blob.Value, 0);
|
|
|
|
|
finally
|
|
|
|
|
lNullableTest.Free;
|
|
|
|
|
end;
|
|
|
|
|
finally
|
|
|
|
|
lSer.Free;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TServerTest.TestDeserializeNullablesWithValue;
|
|
|
|
|
var
|
2020-09-12 03:24:38 +02:00
|
|
|
|
lRes: IMVCRESTResponse;
|
2020-02-03 13:19:55 +01:00
|
|
|
|
lSer: TMVCJsonDataObjectsSerializer;
|
|
|
|
|
lNullableTest: TNullablesTest;
|
|
|
|
|
begin
|
|
|
|
|
/// nullables/getsinglewithnulls
|
|
|
|
|
|
2020-09-12 03:24:38 +02:00
|
|
|
|
lRes := RESTClient.Get('/nullables/getsingle');
|
2020-02-03 12:11:42 +01:00
|
|
|
|
lSer := TMVCJsonDataObjectsSerializer.Create;
|
|
|
|
|
try
|
|
|
|
|
lNullableTest := TNullablesTest.Create();
|
|
|
|
|
try
|
2020-09-12 03:24:38 +02:00
|
|
|
|
lSer.DeserializeObject(lRes.Content, lNullableTest);
|
2020-02-03 12:11:42 +01:00
|
|
|
|
Assert.areEqual<Int16>(2, lNullableTest.f_int2.Value);
|
|
|
|
|
Assert.areEqual(4, lNullableTest.f_int4.Value);
|
|
|
|
|
Assert.areEqual<Int32>(8, lNullableTest.f_int8.Value);
|
|
|
|
|
Assert.areEqual('2011-11-17', DateToISODate(lNullableTest.f_date.Value));
|
|
|
|
|
Assert.areEqual('12:24:36', TimeToISOTime(lNullableTest.f_time.Value));
|
2020-11-04 19:06:54 +01:00
|
|
|
|
Assert.areEqual('2011-11-17T12:24:36.048+01:00', DateTimeToISOTimeStamp(lNullableTest.f_datetime.Value));
|
2020-02-03 12:11:42 +01:00
|
|
|
|
Assert.areEqual<boolean>(true, lNullableTest.f_bool.Value);
|
|
|
|
|
Assert.areEqual(10 / 4, lNullableTest.f_float4.Value, 0.0000009);
|
|
|
|
|
Assert.areEqual(10 / 8, lNullableTest.f_float8.Value, 0.0000000000009);
|
|
|
|
|
Assert.areEqual('0123456789', lNullableTest.f_string.Value);
|
|
|
|
|
Assert.areEqual(98765.4321, lNullableTest.f_currency.Value, 0);
|
|
|
|
|
{ TODO -oDanieleT -cGeneral : Compare streams too }
|
|
|
|
|
// Assert.AreEqual('0123456789', lNullableTest.f_blob.Value, 0);
|
|
|
|
|
finally
|
|
|
|
|
lNullableTest.Free;
|
|
|
|
|
end;
|
|
|
|
|
finally
|
|
|
|
|
lSer.Free;
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
|
2020-05-14 17:41:20 +02:00
|
|
|
|
procedure TServerTest.TestDirectoryRedirect;
|
|
|
|
|
var
|
2020-09-12 03:24:38 +02:00
|
|
|
|
lRes: IMVCRESTResponse;
|
2020-05-14 17:41:20 +02:00
|
|
|
|
begin
|
2020-09-12 03:24:38 +02:00
|
|
|
|
lRes := RESTClient.Accept(TMVCMediaType.TEXT_HTML).Get('/static/');
|
|
|
|
|
Assert.areEqual(200, lRes.StatusCode, '/static/');
|
2020-05-14 17:41:20 +02:00
|
|
|
|
|
2020-09-18 01:06:24 +02:00
|
|
|
|
lRes := RESTClient.HandleRedirects(False).Accept(TMVCMediaType.TEXT_HTML).Get('/static');
|
2020-09-12 03:24:38 +02:00
|
|
|
|
Assert.areEqual(301, lRes.StatusCode, '/static');
|
2020-05-14 17:41:20 +02:00
|
|
|
|
Assert.areEqual('/static/', lRes.HeaderValue('Location'), 'Wrong redirect');
|
|
|
|
|
end;
|
|
|
|
|
|
2020-02-13 23:33:30 +01:00
|
|
|
|
procedure TServerTest.TestDirectoryTraversal1;
|
|
|
|
|
var
|
2020-09-12 03:24:38 +02:00
|
|
|
|
lRes: IMVCRESTResponse;
|
2020-02-13 23:33:30 +01:00
|
|
|
|
I: Integer;
|
2020-04-08 13:00:32 +02:00
|
|
|
|
lUrl: string;
|
2020-02-13 23:33:30 +01:00
|
|
|
|
begin
|
2020-09-16 20:01:50 +02:00
|
|
|
|
lRes := RESTClient.Accept(TMVCMediaType.TEXT_HTML).Get('/static/index.html');
|
|
|
|
|
Assert.areEqual(200, lRes.StatusCode);
|
2020-02-13 23:33:30 +01:00
|
|
|
|
|
2020-09-16 20:01:50 +02:00
|
|
|
|
lRes := RESTClient.Accept(TMVCMediaType.TEXT_HTML).Get('/static/..\donotdeleteme.txt');
|
|
|
|
|
Assert.areEqual(404, lRes.StatusCode);
|
2020-02-13 23:33:30 +01:00
|
|
|
|
|
|
|
|
|
lUrl := 'Windows\win.ini';
|
|
|
|
|
for I := 1 to 20 do
|
|
|
|
|
begin
|
|
|
|
|
lUrl := '..\' + lUrl;
|
2020-09-16 20:01:50 +02:00
|
|
|
|
lRes := RESTClient.Accept(TMVCMediaType.TEXT_HTML).Get('/static/' + lUrl);
|
|
|
|
|
Assert.areEqual(404, lRes.StatusCode, 'Fail with: ' + '/static/' + lUrl);
|
2020-02-13 23:33:30 +01:00
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
|
2020-05-02 16:39:32 +02:00
|
|
|
|
procedure TServerTest.TestDirectoryTraversal2;
|
|
|
|
|
var
|
2020-09-12 03:24:38 +02:00
|
|
|
|
lRes: IMVCRESTResponse;
|
2020-05-02 16:39:32 +02:00
|
|
|
|
I: Integer;
|
|
|
|
|
lUrl: string;
|
|
|
|
|
begin
|
2020-09-12 03:24:38 +02:00
|
|
|
|
lRes := RESTClient.Accept(TMVCMediaType.TEXT_HTML).Get('/static/..\..\donotdeleteme.txt');
|
|
|
|
|
Assert.areEqual(404, lRes.StatusCode);
|
2020-05-02 16:39:32 +02:00
|
|
|
|
|
2020-09-12 03:24:38 +02:00
|
|
|
|
lRes := RESTClient.Accept(TMVCMediaType.TEXT_HTML).Get('/static/../../donotdeleteme.txt');
|
|
|
|
|
Assert.areEqual(404, lRes.StatusCode);
|
2020-05-02 16:39:32 +02:00
|
|
|
|
|
|
|
|
|
lUrl := 'Windows\win.ini';
|
|
|
|
|
for I := 1 to 30 do
|
|
|
|
|
begin
|
|
|
|
|
lUrl := '..\' + lUrl;
|
2020-09-16 20:01:50 +02:00
|
|
|
|
lRes := RESTClient.Accept(TMVCMediaType.TEXT_HTML).Get('/static/' + lUrl);
|
|
|
|
|
Assert.areEqual(404, lRes.StatusCode, 'Fail with: ' + '/static/' + lUrl);
|
2020-05-02 16:39:32 +02:00
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
|
2020-02-03 12:11:42 +01:00
|
|
|
|
procedure TServerTest.TestSerializeAndDeserializeNullables;
|
|
|
|
|
var
|
2020-09-12 03:24:38 +02:00
|
|
|
|
lRes: IMVCRESTResponse;
|
2020-02-03 12:11:42 +01:00
|
|
|
|
lSer: TMVCJsonDataObjectsSerializer;
|
|
|
|
|
lNullableTest: TNullablesTest;
|
|
|
|
|
begin
|
2020-09-12 03:24:38 +02:00
|
|
|
|
lRes := RESTClient.Get('/nullables/getsingle');
|
2020-02-03 12:11:42 +01:00
|
|
|
|
lSer := TMVCJsonDataObjectsSerializer.Create;
|
|
|
|
|
try
|
|
|
|
|
lNullableTest := TNullablesTest.Create();
|
|
|
|
|
try
|
2020-09-12 03:24:38 +02:00
|
|
|
|
lSer.DeserializeObject(lRes.Content, lNullableTest);
|
|
|
|
|
lRes := RESTClient.Post('/nullables/pingpong', lSer.SerializeObject(lNullableTest));
|
2020-02-03 12:11:42 +01:00
|
|
|
|
finally
|
|
|
|
|
lNullableTest.Free;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
lNullableTest := TNullablesTest.Create();
|
|
|
|
|
try
|
2020-09-12 03:24:38 +02:00
|
|
|
|
lSer.DeserializeObject(lRes.Content, lNullableTest);
|
2020-02-03 12:11:42 +01:00
|
|
|
|
Assert.areEqual<Int16>(2, lNullableTest.f_int2.Value);
|
|
|
|
|
Assert.areEqual(4, lNullableTest.f_int4.Value);
|
|
|
|
|
Assert.areEqual<Int32>(8, lNullableTest.f_int8.Value);
|
|
|
|
|
Assert.areEqual('2011-11-17', DateToISODate(lNullableTest.f_date.Value));
|
|
|
|
|
Assert.areEqual('12:24:36', TimeToISOTime(lNullableTest.f_time.Value));
|
2020-11-04 19:06:54 +01:00
|
|
|
|
Assert.areEqual('2011-11-17T12:24:36.048+01:00', DateTimeToISOTimeStamp(lNullableTest.f_datetime.Value));
|
2020-02-03 12:11:42 +01:00
|
|
|
|
Assert.areEqual<boolean>(true, lNullableTest.f_bool.Value);
|
|
|
|
|
Assert.areEqual(10 / 4, lNullableTest.f_float4.Value, 0.0000009);
|
|
|
|
|
Assert.areEqual(10 / 8, lNullableTest.f_float8.Value, 0.0000000000009);
|
|
|
|
|
Assert.areEqual('0123456789', lNullableTest.f_string.Value);
|
|
|
|
|
Assert.areEqual(98765.4321, lNullableTest.f_currency.Value, 0);
|
|
|
|
|
{ TODO -oDanieleT -cGeneral : Compare streams too }
|
|
|
|
|
// Assert.AreEqual('0123456789', lNullableTest.f_blob.Value, 0);
|
|
|
|
|
finally
|
|
|
|
|
lNullableTest.Free;
|
|
|
|
|
end;
|
|
|
|
|
finally
|
|
|
|
|
lSer.Free;
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
|
2020-04-18 23:32:24 +02:00
|
|
|
|
procedure TServerTest.TestSerializeAndDeserializeNullables_ISSUE_362;
|
|
|
|
|
const
|
|
|
|
|
JSON1: string = '{"f_int2":2,"f_int4":4,"f_int8":8,"f_string":"0123456789","f_bool":true, ' +
|
|
|
|
|
'"f_date":"2011-11-17","f_time":"12:24:36","f_datetime":"2011-11-17T12:24:36.048Z",' +
|
|
|
|
|
'"f_float4":2.5,"f_float8":1.25,"f_currency":98765.4321,"f_blob":"0123456789"}';
|
|
|
|
|
JSON2: string = '{"f_int2":2,"f_int4":4,"f_int8":8,"f_string":"0123456789","f_bool":true, ' +
|
|
|
|
|
'"f_date":"2011-11-17","f_time":"12:24:36","f_datetime":"2011-11-17T12:24:36.048Z",' +
|
|
|
|
|
'"f_float4":2,"f_float8":3,"f_currency":4,"f_blob":"0123456789"}';
|
|
|
|
|
var
|
|
|
|
|
lSer: TMVCJsonDataObjectsSerializer;
|
|
|
|
|
lNullableTest: TNullablesTest;
|
|
|
|
|
begin
|
|
|
|
|
lSer := TMVCJsonDataObjectsSerializer.Create;
|
|
|
|
|
try
|
|
|
|
|
lNullableTest := TNullablesTest.Create();
|
|
|
|
|
try
|
|
|
|
|
lSer.DeserializeObject(JSON1, lNullableTest);
|
|
|
|
|
finally
|
|
|
|
|
lNullableTest.Free;
|
|
|
|
|
end;
|
|
|
|
|
lNullableTest := TNullablesTest.Create();
|
|
|
|
|
try
|
2020-09-06 15:38:25 +02:00
|
|
|
|
{ in this case nullable floats type actually contains integers... }
|
2020-04-18 23:32:24 +02:00
|
|
|
|
lSer.DeserializeObject(JSON2, lNullableTest);
|
|
|
|
|
finally
|
|
|
|
|
lNullableTest.Free;
|
|
|
|
|
end;
|
|
|
|
|
Assert.Pass();
|
|
|
|
|
finally
|
|
|
|
|
lSer.Free;
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TServerTest.TestSerializeAndDeserializeNullables_Passing_Integers_InsteadOf_Floats;
|
|
|
|
|
const
|
|
|
|
|
JSON1: string = '{"f_int2":2,"f_int4":4,"f_int8":8,"f_string":"0123456789","f_bool":true, ' +
|
|
|
|
|
'"f_date":"2011-11-17","f_time":"12:24:36","f_datetime":"2011-11-17T12:24:36.048Z",' +
|
|
|
|
|
'"f_float4_not_null":1234,"f_float8_not_null":2345, ' +
|
|
|
|
|
'"f_float4":2.5,"f_float8":1.25,"f_currency":98765.4321,"f_blob":"0123456789"}';
|
|
|
|
|
var
|
|
|
|
|
lSer: TMVCJsonDataObjectsSerializer;
|
|
|
|
|
lNullableTest: TNullablesTest;
|
|
|
|
|
begin
|
|
|
|
|
lSer := TMVCJsonDataObjectsSerializer.Create;
|
|
|
|
|
try
|
|
|
|
|
lNullableTest := TNullablesTest.Create();
|
|
|
|
|
try
|
2020-09-06 15:38:25 +02:00
|
|
|
|
{ in this case not nullable floats type actually contains integers... }
|
2020-04-18 23:32:24 +02:00
|
|
|
|
lSer.DeserializeObject(JSON1, lNullableTest);
|
|
|
|
|
Assert.areEqual(1234, lNullableTest.f_float4_not_null, 0.0001);
|
|
|
|
|
Assert.areEqual(2345, lNullableTest.f_float8_not_null, 0.0001);
|
|
|
|
|
finally
|
|
|
|
|
lNullableTest.Free;
|
|
|
|
|
end;
|
|
|
|
|
finally
|
|
|
|
|
lSer.Free;
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
|
2015-12-22 12:29:25 +01:00
|
|
|
|
procedure TServerTest.TestSession;
|
|
|
|
|
var
|
2020-09-12 03:24:38 +02:00
|
|
|
|
c1: IMVCRESTClient;
|
|
|
|
|
res: IMVCRESTResponse;
|
2020-09-08 15:25:44 +02:00
|
|
|
|
S: string;
|
2015-12-22 12:29:25 +01:00
|
|
|
|
begin
|
2020-09-12 03:24:38 +02:00
|
|
|
|
c1 := TMVCRESTClient.New.BaseURL(TEST_SERVER_ADDRESS, 9999);
|
|
|
|
|
c1.Accept(TMVCMediaType.APPLICATION_JSON);
|
|
|
|
|
res := c1.Post('/session/daniele teti'); // imposto un valore in sessione
|
|
|
|
|
S := res.HeaderValue('Set-Cookie');
|
|
|
|
|
Assert.isFalse(S.Contains('Expires'), 'Session cookie contains "expires" attribute');
|
|
|
|
|
res := c1.Get('/session'); // rileggo il valore dalla sessione
|
|
|
|
|
Assert.areEqual('daniele teti', res.Content);
|
|
|
|
|
c1.Accept(TMVCMediaType.TEXT_PLAIN);
|
|
|
|
|
res := c1.Get('/session');
|
|
|
|
|
// rileggo il valore dalla sessione
|
|
|
|
|
Assert.areEqual('daniele teti', res.Content);
|
|
|
|
|
|
|
|
|
|
// aggiungo altri cookies
|
|
|
|
|
res := c1.Get('/lotofcookies'); // rileggo il valore dalla sessione
|
|
|
|
|
Assert.areEqual<Integer>(HTTP_STATUS.OK, res.StatusCode);
|
|
|
|
|
c1.Accept(TMVCMediaType.TEXT_PLAIN);
|
|
|
|
|
res := c1.Get('/session'); // rileggo il valore dalla sessione
|
|
|
|
|
Assert.areEqual('daniele teti', res.Content);
|
2015-12-22 12:29:25 +01:00
|
|
|
|
end;
|
|
|
|
|
|
2016-06-28 13:42:14 +02:00
|
|
|
|
procedure TServerTest.TestSessionWithLogin;
|
|
|
|
|
begin
|
|
|
|
|
DoLoginWith('daniele');
|
|
|
|
|
DoLogout;
|
|
|
|
|
end;
|
|
|
|
|
|
2020-05-02 16:39:32 +02:00
|
|
|
|
procedure TServerTest.TestSPASupport;
|
|
|
|
|
var
|
2020-09-12 03:24:38 +02:00
|
|
|
|
lRes: IMVCRESTResponse;
|
2020-05-02 16:39:32 +02:00
|
|
|
|
I: Integer;
|
|
|
|
|
lUrl: string;
|
|
|
|
|
begin
|
2020-09-12 03:24:38 +02:00
|
|
|
|
lRes := RESTClient.Accept(TMVCMediaType.TEXT_HTML).Get('/spa/index.html');
|
|
|
|
|
Assert.areEqual(200, lRes.StatusCode);
|
|
|
|
|
Assert.Contains(lRes.Content, 'This is a TEXT file');
|
2020-05-02 16:39:32 +02:00
|
|
|
|
|
2020-09-12 03:24:38 +02:00
|
|
|
|
lRes := RESTClient.Accept(TMVCMediaType.TEXT_HTML).Get('/spa/');
|
|
|
|
|
Assert.areEqual(200, lRes.StatusCode, '/static/');
|
|
|
|
|
Assert.Contains(lRes.Content, 'This is a TEXT file');
|
2020-05-02 16:39:32 +02:00
|
|
|
|
|
2020-09-12 03:24:38 +02:00
|
|
|
|
lRes := RESTClient.Accept(TMVCMediaType.TEXT_HTML).Get('/spa/pippo/pluto/paperino');
|
|
|
|
|
Assert.areEqual(200, lRes.StatusCode, '/spa/pippo/pluto/paperino');
|
|
|
|
|
Assert.Contains(lRes.Content, 'This is a TEXT file');
|
2020-05-02 16:39:32 +02:00
|
|
|
|
|
|
|
|
|
lUrl := 'Windows\win.ini';
|
|
|
|
|
for I := 1 to 30 do
|
|
|
|
|
begin
|
2020-09-16 15:56:14 +02:00
|
|
|
|
{ directory traversal attacks receive always 404 }
|
2020-05-02 16:39:32 +02:00
|
|
|
|
lUrl := '..\' + lUrl;
|
2020-09-22 00:31:26 +02:00
|
|
|
|
lRes := RESTClient.Accept(TMVCMediaType.TEXT_HTML).Get('/spa/' + lUrl);
|
|
|
|
|
Assert.areEqual(404, lRes.StatusCode);
|
|
|
|
|
Assert.Contains(lRes.Content, '[EMVCException] Not Found', true);
|
|
|
|
|
Assert.Contains(lRes.Content, '<p>HTTP 404</p>', true);
|
2020-05-02 16:39:32 +02:00
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
|
2018-10-31 01:07:23 +01:00
|
|
|
|
procedure TServerTest.TestStringDictionary;
|
|
|
|
|
var
|
2020-09-12 03:24:38 +02:00
|
|
|
|
lRes: IMVCRESTResponse;
|
2018-10-31 01:07:23 +01:00
|
|
|
|
lSer: TMVCJsonDataObjectsSerializer;
|
|
|
|
|
lDict: TMVCStringDictionary;
|
|
|
|
|
begin
|
2020-09-12 03:24:38 +02:00
|
|
|
|
lRes := RESTClient.Post('/stringdictionary', '{"prop1":"value1","prop2":"value2"}');
|
|
|
|
|
Assert.areEqual(200, lRes.StatusCode);
|
2018-10-31 01:07:23 +01:00
|
|
|
|
lSer := TMVCJsonDataObjectsSerializer.Create;
|
|
|
|
|
try
|
|
|
|
|
lDict := TMVCStringDictionary.Create;
|
|
|
|
|
try
|
2020-09-12 03:24:38 +02:00
|
|
|
|
lSer.DeserializeObject(lRes.Content, 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
|
2020-09-12 03:24:38 +02:00
|
|
|
|
res: IMVCRESTResponse;
|
2018-11-24 16:56:21 +01:00
|
|
|
|
lJObj: System.JSON.TJSONObject;
|
2016-06-22 17:49:16 +02:00
|
|
|
|
begin
|
2020-09-12 03:24:38 +02:00
|
|
|
|
// ----------------------'/typed/all/($ParString)/($ParInteger)/($ParInt64)/($ParSingle)/($ParDouble)/($ParExtended)')');
|
|
|
|
|
res := RESTClient.Get('/typed/all/mystring/1234/12345678/12.3/1234.5678/1234.5678');
|
|
|
|
|
Assert.isTrue(res.StatusCode = HTTP_STATUS.OK, 'Cannot route');
|
|
|
|
|
lJObj := TSystemJSON.StringAsJSONObject(res.Content);
|
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
|
2020-09-12 03:24:38 +02:00
|
|
|
|
res: IMVCRESTResponse;
|
2016-06-22 17:49:16 +02:00
|
|
|
|
begin
|
2020-09-12 03:24:38 +02:00
|
|
|
|
res := RESTClient.Get('/typed/booleans/true/false/1/0');
|
|
|
|
|
Assert.isTrue(res.StatusCode = HTTP_STATUS.OK, 'Cannot route');
|
|
|
|
|
Assert.areEqual('true.false.true.false', res.Content.ToLower);
|
2016-06-22 17:49:16 +02:00
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TServerTest.TestTypedDouble1;
|
|
|
|
|
var
|
2020-09-12 03:24:38 +02:00
|
|
|
|
res: IMVCRESTResponse;
|
2016-06-22 17:49:16 +02:00
|
|
|
|
begin
|
2020-09-12 03:24:38 +02:00
|
|
|
|
res := RESTClient.Get('/typed/double1/1234.5678');
|
|
|
|
|
Assert.isTrue(res.StatusCode = HTTP_STATUS.OK, 'Cannot route');
|
|
|
|
|
Assert.areEqual('1234.5678 modified from server', res.Content);
|
2016-06-22 17:49:16 +02:00
|
|
|
|
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TServerTest.TestTypedExtended1;
|
|
|
|
|
var
|
2020-09-12 03:24:38 +02:00
|
|
|
|
res: IMVCRESTResponse;
|
2016-06-22 17:49:16 +02:00
|
|
|
|
begin
|
2020-09-12 03:24:38 +02:00
|
|
|
|
res := RESTClient.Get('/typed/extended1/1234.5678');
|
|
|
|
|
Assert.isTrue(res.StatusCode = HTTP_STATUS.OK, 'Cannot route');
|
|
|
|
|
Assert.areEqual('1234.5678 modified from server', res.Content);
|
2016-06-22 17:49:16 +02:00
|
|
|
|
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TServerTest.TestTypedInt641;
|
|
|
|
|
var
|
2020-09-12 03:24:38 +02:00
|
|
|
|
res: IMVCRESTResponse;
|
2016-06-22 17:49:16 +02:00
|
|
|
|
begin
|
2020-09-12 03:24:38 +02:00
|
|
|
|
res := RESTClient.Get('/typed/int641/12345678');
|
|
|
|
|
Assert.isTrue(res.StatusCode = HTTP_STATUS.OK, 'Cannot route');
|
|
|
|
|
Assert.areEqual('12345678 modified from server', res.Content);
|
2016-06-22 17:49:16 +02:00
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TServerTest.TestTypedInteger1;
|
|
|
|
|
var
|
2020-09-12 03:24:38 +02:00
|
|
|
|
res: IMVCRESTResponse;
|
2016-06-22 17:49:16 +02:00
|
|
|
|
begin
|
2020-09-12 03:24:38 +02:00
|
|
|
|
res := RESTClient.Get('/typed/integer1/1234');
|
|
|
|
|
Assert.isTrue(res.StatusCode = HTTP_STATUS.OK, 'Cannot route');
|
|
|
|
|
Assert.areEqual('1234 modified from server', res.Content);
|
2016-06-22 17:49:16 +02:00
|
|
|
|
end;
|
|
|
|
|
|
2020-04-08 13:00:32 +02:00
|
|
|
|
procedure TServerTest.TestTypedIntegerWrongParam1;
|
|
|
|
|
var
|
2020-09-12 03:24:38 +02:00
|
|
|
|
res: IMVCRESTResponse;
|
2020-04-08 13:00:32 +02:00
|
|
|
|
begin
|
2020-09-12 03:24:38 +02:00
|
|
|
|
res := RESTClient.Get('/typed/integer1/boom');
|
|
|
|
|
Assert.isTrue(res.StatusCode = HTTP_STATUS.BadRequest, 'Cannot route');
|
|
|
|
|
Assert.Contains(res.Content, 'EConvertError');
|
|
|
|
|
Assert.Contains(res.Content, '''boom'' is not a valid');
|
2020-04-08 13:00:32 +02:00
|
|
|
|
end;
|
|
|
|
|
|
2016-06-22 17:49:16 +02:00
|
|
|
|
procedure TServerTest.TestTypedSingle1;
|
|
|
|
|
var
|
2020-09-12 03:24:38 +02:00
|
|
|
|
res: IMVCRESTResponse;
|
2016-06-22 17:49:16 +02:00
|
|
|
|
begin
|
2020-09-12 03:24:38 +02:00
|
|
|
|
res := RESTClient.Get('/typed/single1/1234.5');
|
|
|
|
|
Assert.isTrue(res.StatusCode = HTTP_STATUS.OK, 'Cannot route');
|
|
|
|
|
Assert.areEqual('1234.5 modified from server', res.Content);
|
2016-06-22 17:49:16 +02:00
|
|
|
|
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TServerTest.TestTypedString1;
|
|
|
|
|
var
|
2020-09-12 03:24:38 +02:00
|
|
|
|
res: IMVCRESTResponse;
|
2020-04-08 13:00:32 +02:00
|
|
|
|
lValues: array [0 .. 7] of string;
|
2020-09-08 15:25:44 +02:00
|
|
|
|
S: string;
|
2016-06-22 17:49:16 +02:00
|
|
|
|
begin
|
2020-03-04 19:56:01 +01:00
|
|
|
|
lValues[0] := 'daniele';
|
|
|
|
|
lValues[1] := 'dan''iele';
|
|
|
|
|
lValues[2] := '"daniele"';
|
|
|
|
|
lValues[3] := '"daniele teti"';
|
|
|
|
|
lValues[4] := '"daniele" "teti"';
|
|
|
|
|
lValues[5] := '"daniele" "teti"!';
|
2020-03-08 17:54:10 +01:00
|
|
|
|
lValues[6] := ' _\"daniele" "teti"!_ ';
|
2020-03-04 19:56:01 +01:00
|
|
|
|
|
2020-09-08 15:25:44 +02:00
|
|
|
|
for S in lValues do
|
2020-03-04 19:56:01 +01:00
|
|
|
|
begin
|
2020-09-16 20:01:50 +02:00
|
|
|
|
res := RESTClient.AddPathParam('TypedString', S).Get('/typed/string1/{TypedString}');
|
|
|
|
|
Assert.areEqual(HTTP_STATUS.OK, res.StatusCode, 'Cannot route when param is [' + S + ']');
|
|
|
|
|
Assert.areEqual('*' + S + '*', res.Content);
|
2020-03-04 19:56:01 +01:00
|
|
|
|
end;
|
|
|
|
|
|
2020-09-16 20:01:50 +02:00
|
|
|
|
// res := RESTClient.doGET('/typed/string1/daniele', []);
|
|
|
|
|
// Assert.isTrue(res.ResponseCode = HTTP_STATUS.OK, 'Cannot route');
|
|
|
|
|
// Assert.areEqual('daniele modified from server', res.BodyAsString);
|
2020-04-08 13:00:32 +02:00
|
|
|
|
//
|
2020-09-16 20:01:50 +02:00
|
|
|
|
// res := RESTClient.doGET('/typed/string1/dan''iele', []);
|
|
|
|
|
// Assert.isTrue(res.ResponseCode = HTTP_STATUS.OK, 'Cannot route');
|
|
|
|
|
// Assert.areEqual('dan''iele modified from server', res.BodyAsString);
|
2020-04-08 13:00:32 +02:00
|
|
|
|
//
|
2020-09-16 20:01:50 +02:00
|
|
|
|
// res := RESTClient.doGET('/typed/string1/"the value"', []);
|
|
|
|
|
// Assert.isTrue(res.ResponseCode = HTTP_STATUS.OK, 'Cannot route');
|
|
|
|
|
// Assert.areEqual('"the value" modified from server', res.BodyAsString);
|
2020-04-08 13:00:32 +02:00
|
|
|
|
//
|
2020-09-16 20:01:50 +02:00
|
|
|
|
// res := RESTClient.doGET('/typed/string1/"the:value"', []);
|
|
|
|
|
// Assert.isTrue(res.ResponseCode = HTTP_STATUS.OK, 'Cannot route');
|
|
|
|
|
// Assert.areEqual('"the value" modified from server', res.BodyAsString);
|
2020-04-08 13:00:32 +02:00
|
|
|
|
//
|
2020-09-16 20:01:50 +02:00
|
|
|
|
// res := RESTClient.doGET('/typed/string1/"the:value!"', []);
|
|
|
|
|
// Assert.isTrue(res.ResponseCode = HTTP_STATUS.OK, 'Cannot route');
|
|
|
|
|
// Assert.areEqual('"the value" modified from server', res.BodyAsString);
|
2020-04-08 13:00:32 +02:00
|
|
|
|
//
|
2020-09-16 20:01:50 +02:00
|
|
|
|
// res := RESTClient.doGET('/typed/string1/"the:value!?"', []);
|
|
|
|
|
// Assert.isTrue(res.ResponseCode = HTTP_STATUS.OK, 'Cannot route');
|
|
|
|
|
// Assert.areEqual('"the value" modified from server', res.BodyAsString);
|
2020-03-04 19:56:01 +01:00
|
|
|
|
|
2016-06-22 17:49:16 +02:00
|
|
|
|
end;
|
|
|
|
|
|
2020-04-09 16:04:45 +02:00
|
|
|
|
procedure TServerTest.TestTypedTGuid1;
|
|
|
|
|
var
|
2020-09-12 03:24:38 +02:00
|
|
|
|
res: IMVCRESTResponse;
|
2020-04-09 16:04:45 +02:00
|
|
|
|
begin
|
2020-09-12 03:24:38 +02:00
|
|
|
|
res := RESTClient.Get('/typed/tguid1/{161BEA56-480B-40A8-AF0E-7FDF6B08E121}');
|
|
|
|
|
Assert.isTrue(res.StatusCode = HTTP_STATUS.OK, 'Cannot route');
|
|
|
|
|
Assert.areEqual('{161BEA56-480B-40A8-AF0E-7FDF6B08E121} modified from server', res.Content);
|
2020-04-09 16:04:45 +02:00
|
|
|
|
|
2020-09-12 03:24:38 +02:00
|
|
|
|
res := RESTClient.Get('/typed/tguid1/161BEA56-480B-40A8-AF0E-7FDF6B08E121');
|
|
|
|
|
Assert.isTrue(res.StatusCode = HTTP_STATUS.OK, 'Cannot route');
|
|
|
|
|
Assert.areEqual('{161BEA56-480B-40A8-AF0E-7FDF6B08E121} modified from server', res.Content);
|
2020-04-09 16:04:45 +02:00
|
|
|
|
|
2020-09-12 03:24:38 +02:00
|
|
|
|
res := RESTClient.Get('/typed/tguid1/161BEA56480B40A8AF0E7FDF6B08E121');
|
|
|
|
|
Assert.isTrue(res.StatusCode = HTTP_STATUS.OK, 'Cannot route');
|
|
|
|
|
Assert.areEqual('{161BEA56-480B-40A8-AF0E-7FDF6B08E121} modified from server', res.Content);
|
2020-04-09 16:04:45 +02:00
|
|
|
|
end;
|
|
|
|
|
|
2020-04-13 16:22:15 +02:00
|
|
|
|
procedure TServerTest.TestViewDataViewDataSet;
|
|
|
|
|
var
|
2020-09-12 03:24:38 +02:00
|
|
|
|
lRes: IMVCRESTResponse;
|
2020-04-13 16:22:15 +02:00
|
|
|
|
begin
|
2020-09-12 03:24:38 +02:00
|
|
|
|
lRes := RESTClient.Accept(TMVCMediaType.TEXT_PLAIN).Get('/website/list');
|
|
|
|
|
Assert.areEqual(HTTP_STATUS.OK, lRes.StatusCode, lRes.Content);
|
2020-04-18 23:32:24 +02:00
|
|
|
|
var
|
2020-09-12 03:24:38 +02:00
|
|
|
|
lLines := lRes.Content.Split([sLineBreak]);
|
2020-05-27 11:28:22 +02:00
|
|
|
|
var
|
|
|
|
|
lCount: Integer := 1001;
|
2020-04-13 16:22:15 +02:00
|
|
|
|
for var lLine in lLines do
|
|
|
|
|
begin
|
2020-04-18 23:32:24 +02:00
|
|
|
|
var
|
|
|
|
|
lLinePieces := lLine.Split(['|']);
|
2020-04-13 16:22:15 +02:00
|
|
|
|
if Length(lLinePieces) = 1 then
|
|
|
|
|
begin
|
|
|
|
|
lCount := 1001;
|
|
|
|
|
Continue;
|
|
|
|
|
end;
|
2020-04-18 23:32:24 +02:00
|
|
|
|
Assert.areEqual(9, Length(lLinePieces));
|
|
|
|
|
Assert.areEqual(lCount, lLinePieces[0].ToInteger);
|
2020-04-13 16:22:15 +02:00
|
|
|
|
Inc(lCount);
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
|
2018-10-31 01:07:23 +01:00
|
|
|
|
procedure TServerTest.TestWrongJSONBody;
|
|
|
|
|
var
|
2020-09-12 03:24:38 +02:00
|
|
|
|
lRes: IMVCRESTResponse;
|
2018-10-31 01:07:23 +01:00
|
|
|
|
begin
|
2020-09-12 03:24:38 +02:00
|
|
|
|
lRes := RESTClient.Post('/stringdictionary', '{"prop1","value1"}');
|
|
|
|
|
Assert.areEqual(HTTP_STATUS.BadRequest, lRes.StatusCode);
|
2018-10-31 01:07:23 +01:00
|
|
|
|
end;
|
|
|
|
|
|
2016-06-22 17:49:16 +02:00
|
|
|
|
procedure TServerTest.TestTypedDateTimeTypes;
|
|
|
|
|
var
|
2020-09-12 03:24:38 +02:00
|
|
|
|
res: IMVCRESTResponse;
|
2016-06-22 17:49:16 +02:00
|
|
|
|
begin
|
|
|
|
|
// TDate, wrong and correct
|
2020-09-12 03:24:38 +02:00
|
|
|
|
res := RESTClient.Get('/typed/tdate1/20161012');
|
|
|
|
|
Assert.areEqual<Integer>(HTTP_STATUS.BadRequest, res.StatusCode, 'wrong TDate');
|
2016-06-22 17:49:16 +02:00
|
|
|
|
|
2020-09-12 03:24:38 +02:00
|
|
|
|
res := RESTClient.Get('/typed/tdate1/2016-10-12');
|
|
|
|
|
Assert.areEqual<Integer>(HTTP_STATUS.OK, res.StatusCode);
|
|
|
|
|
Assert.areEqual('2016-10-12 modified from server', res.Content);
|
2016-06-22 17:49:16 +02:00
|
|
|
|
|
|
|
|
|
// TDateTime, wrong and correct
|
2020-09-12 03:24:38 +02:00
|
|
|
|
res := RESTClient.Get('/typed/tdatetime1/20161');
|
|
|
|
|
Assert.areEqual<Integer>(HTTP_STATUS.BadRequest, res.StatusCode, 'wrong TDateTime (1)');
|
2017-08-29 11:54:22 +02:00
|
|
|
|
|
2017-09-08 16:59:02 +02:00
|
|
|
|
// Wrong
|
2020-09-12 03:24:38 +02:00
|
|
|
|
res := RESTClient.Get('/typed/tdatetime1/20161012121212');
|
|
|
|
|
Assert.areEqual<Integer>(HTTP_STATUS.BadRequest, res.StatusCode, 'wrong TDateTime (2)');
|
2016-06-22 17:49:16 +02:00
|
|
|
|
|
2017-09-08 16:59:02 +02:00
|
|
|
|
// Correct without 'T'
|
2020-11-10 23:13:02 +01:00
|
|
|
|
res := RESTClient.Get('/typed/tdatetime1/2016-10-12 12:12:12');
|
|
|
|
|
Assert.areEqual<Integer>(HTTP_STATUS.OK, res.StatusCode, 'wrong TDateTime (3)');
|
|
|
|
|
Assert.areEqual('2016-10-12T12:12:12.000+02:00 modified from server', res.Content);
|
2017-08-29 11:54:22 +02:00
|
|
|
|
|
2020-11-04 19:06:54 +01:00
|
|
|
|
// Correct in extended form with zero UTC offset
|
2020-11-10 23:13:02 +01:00
|
|
|
|
res := RESTClient.Get('/typed/tdatetime1/2016-10-12T12:12:12Z');
|
|
|
|
|
Assert.areEqual<Integer>(HTTP_STATUS.OK, res.StatusCode);
|
|
|
|
|
Assert.areEqual('2016-10-12T14:12:12.000+02:00 modified from server', res.Content);
|
2017-08-29 11:54:22 +02:00
|
|
|
|
|
2020-11-04 19:06:54 +01:00
|
|
|
|
// Correct in extended form without timezone
|
2020-11-10 23:13:02 +01:00
|
|
|
|
res := RESTClient.Get('/typed/tdatetime1/2016-10-12T12:12:12');
|
|
|
|
|
Assert.areEqual<Integer>(HTTP_STATUS.OK, res.StatusCode);
|
|
|
|
|
Assert.areEqual('2016-10-12T12:12:12.000+02:00 modified from server', res.Content);
|
2017-08-29 11:54:22 +02:00
|
|
|
|
|
2017-09-08 16:59:02 +02:00
|
|
|
|
// Correct in extended form with timezone
|
2020-11-10 23:13:02 +01:00
|
|
|
|
res := RESTClient.Get('/typed/tdatetime1/2016-10-12T12:12:12.000Z');
|
|
|
|
|
Assert.areEqual<Integer>(HTTP_STATUS.OK, res.StatusCode);
|
|
|
|
|
Assert.areEqual('2016-10-12T14:12:12.000+02:00 modified from server', res.Content);
|
2016-06-22 17:49:16 +02:00
|
|
|
|
|
|
|
|
|
// TTime, wrong and correct
|
2020-09-12 03:24:38 +02:00
|
|
|
|
res := RESTClient.Get('/typed/ttime1/121212');
|
|
|
|
|
Assert.areEqual<Integer>(HTTP_STATUS.BadRequest, res.StatusCode, 'wrong TTime');
|
2016-06-22 17:49:16 +02:00
|
|
|
|
|
2020-09-12 03:24:38 +02:00
|
|
|
|
res := RESTClient.Get('/typed/ttime1/12:12:12');
|
|
|
|
|
Assert.areEqual<Integer>(HTTP_STATUS.OK, res.StatusCode);
|
|
|
|
|
Assert.areEqual('12:12:12 modified from server', res.Content);
|
2016-06-22 17:49:16 +02:00
|
|
|
|
|
|
|
|
|
end;
|
|
|
|
|
|
2020-11-04 19:06:54 +01:00
|
|
|
|
procedure TServerTest.TestTypedDateTimeTypes_UTC;
|
|
|
|
|
var
|
2020-11-10 23:13:02 +01:00
|
|
|
|
res: IMVCRESTResponse;
|
2020-11-04 19:06:54 +01:00
|
|
|
|
begin
|
|
|
|
|
// If no UTC zone is defined, server must assume its local time zone
|
2020-11-10 23:13:02 +01:00
|
|
|
|
res := RESTClient.Get('/typed/tdatetime1/2016-10-12 12:12:12');
|
|
|
|
|
Assert.areEqual<Integer>(HTTP_STATUS.OK, res.StatusCode, 'wrong TDateTime (3)');
|
|
|
|
|
Assert.areEqual('2016-10-12T12:12:12.000+02:00 modified from server', res.Content);
|
2020-11-04 19:06:54 +01:00
|
|
|
|
|
|
|
|
|
// With UTC zero offset, server must return time expressed as its local UTC
|
2020-11-10 23:13:02 +01:00
|
|
|
|
res := RESTClient.Get('/typed/tdatetime1/2016-10-12T12:12:12Z');
|
|
|
|
|
Assert.areEqual<Integer>(HTTP_STATUS.OK, res.StatusCode);
|
|
|
|
|
Assert.areEqual('2016-10-12T14:12:12.000+02:00 modified from server', res.Content);
|
2020-11-04 19:06:54 +01:00
|
|
|
|
|
|
|
|
|
// Correct in extended form with timezone
|
2020-11-10 23:13:02 +01:00
|
|
|
|
res := RESTClient.Get('/typed/tdatetime1/2016-10-12T12:12:12.000Z');
|
|
|
|
|
Assert.areEqual<Integer>(HTTP_STATUS.OK, res.StatusCode);
|
|
|
|
|
Assert.areEqual('2016-10-12T14:12:12.000+02:00 modified from server', res.Content);
|
2020-11-04 19:06:54 +01:00
|
|
|
|
end;
|
|
|
|
|
|
2015-12-22 12:29:25 +01:00
|
|
|
|
procedure TBaseServerTest.DoLoginWith(UserName: string);
|
|
|
|
|
var
|
2020-09-12 03:24:38 +02:00
|
|
|
|
res: IMVCRESTResponse;
|
2015-12-22 12:29:25 +01:00
|
|
|
|
begin
|
2020-09-12 03:24:38 +02:00
|
|
|
|
res := RESTClient.AddPathParam('username', UserName).Get('/login/{username}');
|
|
|
|
|
Assert.isTrue(res.StatusCode = HTTP_STATUS.OK, 'Login Failed');
|
2015-12-22 12:29:25 +01:00
|
|
|
|
end;
|
|
|
|
|
|
2017-09-26 01:02:09 +02:00
|
|
|
|
{ TJSONRPCServerTest }
|
|
|
|
|
|
2020-05-27 11:28:22 +02:00
|
|
|
|
procedure TJSONRPCServerTest.Setup;
|
2018-05-16 19:46:29 +02:00
|
|
|
|
begin
|
2020-05-27 11:28:22 +02:00
|
|
|
|
FExecutor := TMVCJSONRPCExecutor.Create('http://' + TEST_SERVER_ADDRESS + ':9999/jsonrpc', false);
|
|
|
|
|
FExecutor2 := TMVCJSONRPCExecutor.Create('http://' + TEST_SERVER_ADDRESS + ':9999/jsonrpcclass', false);
|
2020-08-05 09:50:06 +02:00
|
|
|
|
FExecutor3 := TMVCJSONRPCExecutor.Create('http://' + TEST_SERVER_ADDRESS + ':9999/jsonrpcclass1', false);
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TJSONRPCServerTest.TestHooks;
|
|
|
|
|
begin
|
|
|
|
|
var lRequest1: IJSONRPCRequest := TJSONRPCRequest.Create(1234, 'request1');
|
2020-09-08 15:25:44 +02:00
|
|
|
|
var
|
|
|
|
|
lResp := FExecutor3.ExecuteRequest(lRequest1);
|
2020-08-05 09:50:06 +02:00
|
|
|
|
Assert.areEqual('OnBeforeRoutingHook|OnBeforeCallHook|OnAfterCallHook',
|
|
|
|
|
FExecutor3.HTTPResponse.HeaderValue['x-history']);
|
|
|
|
|
end;
|
|
|
|
|
|
2020-08-06 17:40:56 +02:00
|
|
|
|
procedure TJSONRPCServerTest.TestHooksNotif;
|
2020-09-08 15:25:44 +02:00
|
|
|
|
var
|
|
|
|
|
lResp: IJSONRPCResponse;
|
2020-08-06 17:40:56 +02:00
|
|
|
|
begin
|
|
|
|
|
var lNotif: IJSONRPCNotification := TJSONRPCNotification.Create('Notif1');
|
2020-09-08 15:25:44 +02:00
|
|
|
|
lResp := FExecutor3.ExecuteNotification(lNotif);
|
|
|
|
|
Assert.areEqual('OnBeforeRoutingHook|OnBeforeCallHook|OnAfterCallHook',
|
|
|
|
|
FExecutor3.HTTPResponse.HeaderValue['x-history']);
|
|
|
|
|
Assert.isFalse(lResp.IsError);
|
2020-08-06 17:40:56 +02:00
|
|
|
|
Assert.WillRaise(
|
|
|
|
|
procedure
|
|
|
|
|
begin
|
|
|
|
|
lResp.AsJSONString;
|
|
|
|
|
end, EMVCJSONRPCException);
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TJSONRPCServerTest.TestHooksNotifWhenOnAfterCallHookRaisesError;
|
2020-09-08 15:25:44 +02:00
|
|
|
|
var
|
|
|
|
|
lResp: IJSONRPCResponse;
|
2020-08-06 17:40:56 +02:00
|
|
|
|
begin
|
|
|
|
|
var lNotif: IJSONRPCNotification := TJSONRPCNotification.Create('error_OnAfterCallHook');
|
2020-09-08 15:25:44 +02:00
|
|
|
|
lResp := FExecutor3.ExecuteNotification(lNotif);
|
2020-08-06 17:40:56 +02:00
|
|
|
|
Assert.areEqual('', FExecutor3.HTTPResponse.HeaderValue['x-history']);
|
|
|
|
|
Assert.isTrue(lResp.IsError);
|
|
|
|
|
Assert.WillNotRaise(
|
|
|
|
|
procedure
|
|
|
|
|
begin
|
|
|
|
|
lResp.AsJSONString;
|
|
|
|
|
end, EMVCJSONRPCException);
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TJSONRPCServerTest.TestHooksNotifWhenOnBeforeCallHookRaisesError;
|
2020-09-08 15:25:44 +02:00
|
|
|
|
var
|
|
|
|
|
lResp: IJSONRPCResponse;
|
2020-08-06 17:40:56 +02:00
|
|
|
|
begin
|
|
|
|
|
var lNotif: IJSONRPCNotification := TJSONRPCNotification.Create('error_OnBeforeCallHook');
|
2020-09-08 15:25:44 +02:00
|
|
|
|
lResp := FExecutor3.ExecuteNotification(lNotif);
|
2020-08-06 17:40:56 +02:00
|
|
|
|
Assert.areEqual('', FExecutor3.HTTPResponse.HeaderValue['x-history']);
|
|
|
|
|
Assert.isTrue(lResp.IsError);
|
|
|
|
|
Assert.WillNotRaise(
|
|
|
|
|
procedure
|
|
|
|
|
begin
|
|
|
|
|
lResp.AsJSONString;
|
|
|
|
|
end, EMVCJSONRPCException);
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TJSONRPCServerTest.TestHooksNotifWhenOnBeforeRoutingHookRaisesError;
|
2020-09-08 15:25:44 +02:00
|
|
|
|
var
|
|
|
|
|
lResp: IJSONRPCResponse;
|
2020-08-06 17:40:56 +02:00
|
|
|
|
begin
|
|
|
|
|
var lNotif: IJSONRPCNotification := TJSONRPCNotification.Create('error_OnBeforeRoutingHook');
|
2020-09-08 15:25:44 +02:00
|
|
|
|
lResp := FExecutor3.ExecuteNotification(lNotif);
|
2020-08-06 17:40:56 +02:00
|
|
|
|
Assert.areEqual('', FExecutor3.HTTPResponse.HeaderValue['x-history']);
|
|
|
|
|
Assert.isTrue(lResp.IsError);
|
|
|
|
|
Assert.WillNotRaise(
|
|
|
|
|
procedure
|
|
|
|
|
begin
|
|
|
|
|
lResp.AsJSONString;
|
|
|
|
|
end, EMVCJSONRPCException);
|
|
|
|
|
end;
|
|
|
|
|
|
2020-08-05 09:50:06 +02:00
|
|
|
|
procedure TJSONRPCServerTest.TestHooksWhenMethodRaisesError;
|
2020-09-08 15:25:44 +02:00
|
|
|
|
var
|
|
|
|
|
lResp: IJSONRPCResponse;
|
2020-08-05 09:50:06 +02:00
|
|
|
|
begin
|
|
|
|
|
var lRequest1: IJSONRPCRequest := TJSONRPCRequest.Create(1234, 'RequestWithError');
|
2020-09-08 15:25:44 +02:00
|
|
|
|
lResp := FExecutor3.ExecuteRequest(lRequest1);
|
2020-08-05 09:50:06 +02:00
|
|
|
|
Assert.areEqual('OnBeforeRoutingHook|OnBeforeCallHook|OnAfterCallHook|error',
|
|
|
|
|
FExecutor3.HTTPResponse.HeaderValue['x-history']);
|
|
|
|
|
Assert.isTrue(lResp.IsError, 'Method raised error but response is not an error');
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TJSONRPCServerTest.TestHooksWhenOnAfterCallHookRaisesError;
|
|
|
|
|
begin
|
|
|
|
|
var lRequest1: IJSONRPCRequest := TJSONRPCRequest.Create(1234, 'error_OnAfterCallHook');
|
2020-09-08 15:25:44 +02:00
|
|
|
|
var
|
|
|
|
|
lResp := FExecutor3.ExecuteRequest(lRequest1);
|
2020-08-05 09:50:06 +02:00
|
|
|
|
Assert.isTrue(lResp.IsError, lResp.ToString(true));
|
|
|
|
|
Assert.areEqual(lResp.Error.ErrMessage, 'error_OnAfterCallHook');
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TJSONRPCServerTest.TestHooksWhenOnBeforeCallHookRaisesError;
|
|
|
|
|
begin
|
|
|
|
|
var lRequest1: IJSONRPCRequest := TJSONRPCRequest.Create(1234, 'error_OnBeforeCallHook');
|
2020-09-08 15:25:44 +02:00
|
|
|
|
var
|
|
|
|
|
lResp := FExecutor3.ExecuteRequest(lRequest1);
|
2020-08-05 09:50:06 +02:00
|
|
|
|
Assert.isTrue(lResp.IsError, lResp.ToString(true));
|
|
|
|
|
Assert.areEqual(lResp.Error.ErrMessage, 'error_OnBeforeCallHook');
|
|
|
|
|
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TJSONRPCServerTest.TestHooksWhenOnBeforeRoutingHookRaisesError;
|
|
|
|
|
begin
|
|
|
|
|
var lRequest1: IJSONRPCRequest := TJSONRPCRequest.Create(1234, 'error_OnBeforeRoutingHook');
|
2020-09-08 15:25:44 +02:00
|
|
|
|
var
|
|
|
|
|
lResp := FExecutor3.ExecuteRequest(lRequest1);
|
2020-08-05 09:50:06 +02:00
|
|
|
|
Assert.isTrue(lResp.IsError, lResp.ToString(true));
|
|
|
|
|
Assert.areEqual(lResp.Error.ErrMessage, 'error_OnBeforeRoutingHook');
|
2018-05-16 19:46:29 +02:00
|
|
|
|
end;
|
|
|
|
|
|
2020-08-06 17:40:56 +02:00
|
|
|
|
procedure TJSONRPCServerTest.TestNotificationWhichRaisesError;
|
|
|
|
|
var
|
|
|
|
|
lReq: IJSONRPCNotification;
|
|
|
|
|
begin
|
|
|
|
|
lReq := TJSONRPCNotification.Create;
|
|
|
|
|
lReq.Method := 'NotifWithError';
|
2020-09-08 15:25:44 +02:00
|
|
|
|
var
|
|
|
|
|
lResp := FExecutor3.ExecuteNotification(lReq);
|
|
|
|
|
Assert.isTrue(lResp.IsError);
|
2020-08-06 17:40:56 +02:00
|
|
|
|
Assert.Contains(lResp.Error.ErrMessage, 'BOOM NOTIF');
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TJSONRPCServerTest.TestNotificationWithoutParams;
|
|
|
|
|
var
|
|
|
|
|
lReq: IJSONRPCNotification;
|
|
|
|
|
begin
|
|
|
|
|
lReq := TJSONRPCNotification.Create;
|
|
|
|
|
lReq.Method := 'mynotify';
|
|
|
|
|
FExecutor.ExecuteNotification(lReq);
|
|
|
|
|
FExecutor2.ExecuteNotification(lReq);
|
|
|
|
|
Assert.Pass();
|
|
|
|
|
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);
|
2020-07-29 12:35:12 +02:00
|
|
|
|
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);
|
2020-07-29 12:35:12 +02:00
|
|
|
|
lRes := ISOTimeStampToDateTime(lRPCResp.Result.AsString());
|
2019-03-05 20:55:37 +01:00
|
|
|
|
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;
|
|
|
|
|
|
2020-05-02 16:39:32 +02:00
|
|
|
|
procedure TJSONRPCServerTest.TestRequestWithWrongNamedParams;
|
|
|
|
|
var
|
|
|
|
|
lReq: IJSONRPCRequest;
|
|
|
|
|
lRPCResp: IJSONRPCResponse;
|
|
|
|
|
begin
|
|
|
|
|
lReq := TJSONRPCRequest.Create;
|
|
|
|
|
lReq.Method := 'add';
|
|
|
|
|
lReq.Params.AddByName('wrongname1', 3);
|
|
|
|
|
lReq.Params.AddByName('wrongname2', 4);
|
|
|
|
|
lReq.Params.AddByName('wrongname3', 5);
|
|
|
|
|
lReq.RequestID := 1234;
|
|
|
|
|
|
|
|
|
|
lRPCResp := FExecutor.ExecuteRequest(lReq);
|
|
|
|
|
Assert.isTrue(lRPCResp.IsError);
|
2020-07-29 12:35:12 +02:00
|
|
|
|
Assert.Contains(lRPCResp.Error.ErrMessage, 'cannot find parameter', true);
|
2020-05-02 16:39:32 +02:00
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TJSONRPCServerTest.TestRequestWithNamedParams_I_I_I_ret_O;
|
|
|
|
|
var
|
|
|
|
|
lReq: IJSONRPCRequest;
|
|
|
|
|
lRPCResp: IJSONRPCResponse;
|
|
|
|
|
lS: string;
|
|
|
|
|
begin
|
|
|
|
|
lReq := TJSONRPCRequest.Create;
|
|
|
|
|
lReq.Method := 'add';
|
|
|
|
|
lReq.Params.AddByName('value1', 3);
|
|
|
|
|
lReq.Params.AddByName('value2', 4);
|
|
|
|
|
lReq.Params.AddByName('value3', 5);
|
|
|
|
|
lReq.RequestID := 1234;
|
|
|
|
|
|
|
|
|
|
lRPCResp := FExecutor.ExecuteRequest(lReq);
|
|
|
|
|
lS := (lRPCResp.Result.AsObject as TJDOJsonObject).ToJSON();
|
|
|
|
|
Assert.areEqual(12, TJDOJsonObject(lRPCResp.Result.AsObject).I['res']);
|
|
|
|
|
|
|
|
|
|
lRPCResp := FExecutor2.ExecuteRequest(lReq);
|
|
|
|
|
lS := (lRPCResp.Result.AsObject as TJDOJsonObject).ToJSON();
|
|
|
|
|
Assert.areEqual(12, TJDOJsonObject(lRPCResp.Result.AsObject).I['res']);
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TJSONRPCServerTest.TestRequestWithNamedParams_I_I_ret_I;
|
|
|
|
|
var
|
|
|
|
|
lReq: IJSONRPCRequest;
|
|
|
|
|
lResp: IJSONRPCResponse;
|
|
|
|
|
begin
|
|
|
|
|
lReq := TJSONRPCRequest.Create;
|
|
|
|
|
lReq.RequestID := 1234;
|
|
|
|
|
lReq.Method := 'subtract';
|
|
|
|
|
lReq.Params.AddByName('Value1', 18);
|
|
|
|
|
lReq.Params.AddByName('Value2', 8);
|
|
|
|
|
|
|
|
|
|
lResp := FExecutor.ExecuteRequest(lReq);
|
|
|
|
|
Assert.areEqual(10, lResp.Result.AsInteger);
|
|
|
|
|
Assert.areEqual(1234, lResp.RequestID.AsInteger);
|
|
|
|
|
|
|
|
|
|
lResp := FExecutor2.ExecuteRequest(lReq);
|
|
|
|
|
Assert.areEqual(10, lResp.Result.AsInteger);
|
|
|
|
|
Assert.areEqual(1234, lResp.RequestID.AsInteger);
|
|
|
|
|
end;
|
|
|
|
|
|
2017-09-26 01:02:09 +02:00
|
|
|
|
procedure TJSONRPCServerTest.TestRequestWithoutParams;
|
|
|
|
|
var
|
2020-08-06 17:40:56 +02:00
|
|
|
|
lReq: IJSONRPCRequest;
|
|
|
|
|
lResp: IJSONRPCResponse;
|
2017-09-26 01:02:09 +02:00
|
|
|
|
begin
|
2020-08-06 17:40:56 +02:00
|
|
|
|
lReq := TJSONRPCRequest.Create;
|
|
|
|
|
lReq.Method := 'MyRequest';
|
|
|
|
|
lReq.RequestID := 1234;
|
|
|
|
|
lResp := FExecutor.ExecuteRequest(lReq);
|
|
|
|
|
Assert.isFalse(lResp.IsError);
|
|
|
|
|
Assert.isTrue(lResp.Result.AsBoolean);
|
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);
|
2020-05-02 16:39:32 +02:00
|
|
|
|
|
2019-01-08 12:48:27 +01:00
|
|
|
|
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;
|
|
|
|
|
|
2020-05-02 16:39:32 +02:00
|
|
|
|
procedure TJSONRPCServerTest.TestRequest_NamedParams_S_I_ret_S;
|
|
|
|
|
var
|
|
|
|
|
lReq: IJSONRPCRequest;
|
|
|
|
|
lRPCResp: IJSONRPCResponse;
|
|
|
|
|
begin
|
|
|
|
|
lReq := TJSONRPCRequest.Create;
|
|
|
|
|
lReq.Method := 'MultiplyString';
|
|
|
|
|
lReq.Params.AddByName('aString', 'Daniele');
|
|
|
|
|
lReq.Params.AddByName('Multiplier', 4);
|
|
|
|
|
lReq.RequestID := 1234;
|
|
|
|
|
lRPCResp := FExecutor.ExecuteRequest(lReq);
|
|
|
|
|
Assert.isFalse(lRPCResp.IsError);
|
|
|
|
|
Assert.areEqual('DanieleDanieleDanieleDaniele', lRPCResp.Result.AsString);
|
|
|
|
|
|
|
|
|
|
lRPCResp := FExecutor2.ExecuteRequest(lReq);
|
|
|
|
|
Assert.areEqual('DanieleDanieleDanieleDaniele', lRPCResp.Result.AsString);
|
|
|
|
|
end;
|
|
|
|
|
|
2017-09-28 00:14:34 +02:00
|
|
|
|
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.
|