2016-09-18 19:19:23 +02:00
|
|
|
|
// ***************************************************************************
|
|
|
|
|
//
|
|
|
|
|
// Delphi MVC Framework
|
|
|
|
|
//
|
2017-01-05 12:44:34 +01:00
|
|
|
|
// Copyright (c) 2010-2017 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
|
|
|
|
|
TestFramework,
|
|
|
|
|
MVCFramework.RESTClient;
|
|
|
|
|
|
2017-04-14 16:43:31 +02:00
|
|
|
|
const
|
|
|
|
|
|
|
|
|
|
{$IFDEF LINUX_SERVER}
|
|
|
|
|
|
|
|
|
|
TEST_SERVER_ADDRESS = '192.168.3.88';
|
|
|
|
|
|
|
|
|
|
{$ELSE}
|
|
|
|
|
|
|
|
|
|
TEST_SERVER_ADDRESS = '127.0.0.1';
|
|
|
|
|
|
|
|
|
|
{$ENDIF}
|
|
|
|
|
|
2015-12-22 12:29:25 +01:00
|
|
|
|
type
|
|
|
|
|
TBaseServerTest = class(TTestCase)
|
|
|
|
|
protected
|
|
|
|
|
RESTClient: TRESTClient;
|
|
|
|
|
procedure DoLoginWith(UserName: string);
|
|
|
|
|
procedure DoLogout;
|
|
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
procedure SetUp; override;
|
|
|
|
|
procedure TearDown; override;
|
|
|
|
|
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
TServerTest = class(TBaseServerTest)
|
|
|
|
|
published
|
|
|
|
|
procedure TestReqWithParams;
|
|
|
|
|
procedure TestPOSTWithParamsAndJSONBody;
|
|
|
|
|
// procedure TestPATCHWithParamsAndJSONBody;
|
|
|
|
|
procedure TestPOSTWithObjectJSONBody;
|
|
|
|
|
procedure TestPUTWithParamsAndJSONBody;
|
2016-04-24 19:08:21 +02:00
|
|
|
|
procedure TestCookies;
|
2016-06-28 13:42:14 +02:00
|
|
|
|
procedure TestSessionWithLogin;
|
2015-12-22 12:29:25 +01:00
|
|
|
|
procedure TestSession;
|
2016-04-03 22:35:27 +02:00
|
|
|
|
procedure TestInvalidateSession;
|
2015-12-22 12:29:25 +01:00
|
|
|
|
procedure TestAsynchRequestPOST;
|
|
|
|
|
procedure TestAsynchRequestPUT;
|
|
|
|
|
procedure TestAsynchRequestGET;
|
|
|
|
|
procedure TestAsynchRequestDELETE;
|
|
|
|
|
procedure TestEncodingRenderJSONValue;
|
2016-09-29 18:17:12 +02:00
|
|
|
|
procedure TestRenderWrappedList;
|
2016-11-18 18:02:48 +01:00
|
|
|
|
procedure TestRenderStreamAndFreeWithOwnerFalse;
|
|
|
|
|
procedure TestRenderStreamAndFreeWithOwnerTrue;
|
2017-04-14 16:43:31 +02:00
|
|
|
|
// procedure TestSerializationType;
|
2015-12-22 12:29:25 +01:00
|
|
|
|
procedure TestProducesConsumes01;
|
|
|
|
|
procedure TestProducesConsumes02;
|
2016-12-07 19:35:36 +01:00
|
|
|
|
procedure TestProducesConsumes03;
|
2015-12-22 12:29:25 +01:00
|
|
|
|
procedure TestProducesConsumesWithWrongAcceptHeader;
|
|
|
|
|
procedure TestExceptionInMVCAfterCreate;
|
|
|
|
|
procedure TestExceptionInMVCBeforeDestroy;
|
2017-01-05 12:44:34 +01:00
|
|
|
|
procedure TestActionFiltersOnBeforeAction;
|
2015-12-22 12:29:25 +01:00
|
|
|
|
procedure TestMiddlewareSpeedMiddleware;
|
|
|
|
|
procedure TestMiddlewareHandler;
|
|
|
|
|
procedure TestPostAListOfObjects;
|
2016-09-16 23:54:54 +02:00
|
|
|
|
// test authentication/authorization with BasicAuth
|
2016-09-20 11:10:40 +02:00
|
|
|
|
procedure TestBasicAuth01;
|
|
|
|
|
procedure TestBasicAuth02;
|
|
|
|
|
procedure TestBasicAuth03;
|
|
|
|
|
procedure TestBasicAuth04;
|
|
|
|
|
procedure TestBasicAuth05;
|
2016-09-16 23:54:54 +02:00
|
|
|
|
|
|
|
|
|
// test authentication/authorization with CustomAuth
|
|
|
|
|
procedure TestCustomAuthRequestWithoutLogin;
|
|
|
|
|
procedure TestCustomAuthRequestsWithValidLogin;
|
2016-12-28 10:00:07 +01:00
|
|
|
|
procedure TestCustomAuthRequestsWithValidLogin_HTML;
|
2016-09-16 23:54:54 +02:00
|
|
|
|
procedure TestCustomAuthWrongRequestBodies;
|
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
|
|
|
|
|
procedure TestTypedString1;
|
|
|
|
|
procedure TestTypedInteger1;
|
|
|
|
|
procedure TestTypedInt641;
|
|
|
|
|
procedure TestTypedSingle1;
|
|
|
|
|
procedure TestTypedDouble1;
|
|
|
|
|
procedure TestTypedExtended1;
|
|
|
|
|
procedure TestTypedAll;
|
|
|
|
|
procedure TestTypedDateTimeTypes;
|
|
|
|
|
procedure TestTypedBooleans;
|
2015-12-22 12:29:25 +01:00
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
implementation
|
|
|
|
|
|
|
|
|
|
uses
|
2017-04-29 23:56:56 +02:00
|
|
|
|
System.Math,
|
|
|
|
|
MVCFramework.Serializer.Defaults,
|
|
|
|
|
MVCFramework.TypesAliases,
|
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,
|
|
|
|
|
MVCFramework.SystemJSONUtils;
|
2015-12-22 12:29:25 +01:00
|
|
|
|
|
|
|
|
|
{ TServerTest }
|
|
|
|
|
|
|
|
|
|
procedure TBaseServerTest.DoLogout;
|
|
|
|
|
var
|
|
|
|
|
res: IRESTResponse;
|
|
|
|
|
begin
|
|
|
|
|
res := RESTClient.doGET('/logout', []);
|
|
|
|
|
CheckTrue(res.ResponseCode = HTTP_STATUS.OK, 'Logout Failed');
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TBaseServerTest.SetUp;
|
|
|
|
|
begin
|
|
|
|
|
inherited;
|
2017-04-14 16:43:31 +02:00
|
|
|
|
RESTClient := TRESTClient.Create(TEST_SERVER_ADDRESS, 9999);
|
2015-12-22 12:29:25 +01:00
|
|
|
|
RESTClient.ReadTimeout(60 * 1000 * 30);
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TBaseServerTest.TearDown;
|
|
|
|
|
begin
|
|
|
|
|
inherited;
|
|
|
|
|
RESTClient.Free;
|
|
|
|
|
end;
|
|
|
|
|
|
2017-01-05 12:44:34 +01:00
|
|
|
|
procedure TServerTest.TestActionFiltersOnBeforeAction;
|
|
|
|
|
var
|
|
|
|
|
res: IRESTResponse;
|
|
|
|
|
begin
|
|
|
|
|
res := RESTClient.doGET('/actionfilters/beforeaction/alwayscalled', []);
|
|
|
|
|
CheckEquals(HTTP_STATUS.OK, res.ResponseCode);
|
|
|
|
|
|
|
|
|
|
res := RESTClient.doGET('/actionfilters/beforeaction/nevercalled', []);
|
|
|
|
|
CheckEquals(HTTP_STATUS.NotFound, res.ResponseCode);
|
|
|
|
|
end;
|
|
|
|
|
|
2015-12-22 12:29:25 +01:00
|
|
|
|
procedure TServerTest.TestAsynchRequestDELETE;
|
|
|
|
|
var
|
|
|
|
|
evt: TEvent;
|
|
|
|
|
r: TWaitResult;
|
|
|
|
|
OK: boolean;
|
|
|
|
|
begin
|
|
|
|
|
OK := false;
|
|
|
|
|
evt := TEvent.Create;
|
|
|
|
|
try
|
|
|
|
|
RESTClient.Asynch(
|
|
|
|
|
procedure(Response: IRESTResponse)
|
|
|
|
|
begin
|
|
|
|
|
OK := true;
|
|
|
|
|
evt.SetEvent;
|
|
|
|
|
end,
|
|
|
|
|
procedure(E: Exception)
|
|
|
|
|
begin
|
|
|
|
|
OK := false;
|
|
|
|
|
end).doDELETE('/req/with/params', ['1', '2', '3']);
|
|
|
|
|
|
|
|
|
|
// wait for thred finish
|
|
|
|
|
repeat
|
|
|
|
|
r := evt.WaitFor(2000);
|
|
|
|
|
until r = TWaitResult.wrSignaled;
|
|
|
|
|
|
|
|
|
|
CheckEquals(true, OK);
|
|
|
|
|
finally
|
|
|
|
|
evt.Free;
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TServerTest.TestAsynchRequestGET;
|
|
|
|
|
var
|
|
|
|
|
evt: TEvent;
|
|
|
|
|
r: TWaitResult;
|
|
|
|
|
j: TJSONObject;
|
|
|
|
|
begin
|
|
|
|
|
j := nil;
|
|
|
|
|
evt := TEvent.Create;
|
|
|
|
|
try
|
|
|
|
|
RESTClient.Asynch(
|
|
|
|
|
procedure(Response: IRESTResponse)
|
|
|
|
|
begin
|
|
|
|
|
try
|
2017-05-25 16:57:49 +02:00
|
|
|
|
j := TSystemJSON.StringAsJSONObject(Response.BodyAsString);
|
2015-12-22 12:29:25 +01:00
|
|
|
|
except
|
|
|
|
|
// test should not block...never!
|
|
|
|
|
end;
|
|
|
|
|
evt.SetEvent;
|
|
|
|
|
end,
|
|
|
|
|
procedure(E: Exception)
|
|
|
|
|
begin
|
|
|
|
|
end).doGET('/req/with/params', ['1', '2', '3']);
|
|
|
|
|
|
|
|
|
|
// wait for thred finish
|
|
|
|
|
repeat
|
|
|
|
|
r := evt.WaitFor(2000);
|
|
|
|
|
until r = TWaitResult.wrSignaled;
|
|
|
|
|
|
|
|
|
|
CheckTrue(Assigned(j));
|
|
|
|
|
CheckEquals('1', j.Get('par1').JsonValue.Value);
|
|
|
|
|
j.Free;
|
|
|
|
|
finally
|
|
|
|
|
evt.Free;
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TServerTest.TestAsynchRequestPOST;
|
|
|
|
|
var
|
|
|
|
|
evt: TEvent;
|
|
|
|
|
r: TWaitResult;
|
|
|
|
|
j: TJSONObject;
|
|
|
|
|
begin
|
|
|
|
|
j := nil;
|
|
|
|
|
evt := TEvent.Create;
|
|
|
|
|
try
|
2017-04-26 14:39:18 +02:00
|
|
|
|
|
2015-12-22 12:29:25 +01:00
|
|
|
|
RESTClient.Asynch(
|
|
|
|
|
procedure(Response: IRESTResponse)
|
|
|
|
|
begin
|
|
|
|
|
try
|
2017-05-25 16:57:49 +02:00
|
|
|
|
j := TSystemJSON.StringAsJSONObject(Response.BodyAsString);
|
2015-12-22 12:29:25 +01:00
|
|
|
|
except
|
|
|
|
|
// test should not block...never!
|
|
|
|
|
end;
|
|
|
|
|
evt.SetEvent;
|
|
|
|
|
end,
|
|
|
|
|
procedure(E: Exception)
|
|
|
|
|
begin
|
2016-04-03 22:35:27 +02:00
|
|
|
|
end).doPOST('/echo', ['1', '2', '3'],
|
2017-04-26 14:39:18 +02:00
|
|
|
|
TSystemJSON.JSONValueToString(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;
|
|
|
|
|
|
|
|
|
|
CheckTrue(Assigned(j));
|
|
|
|
|
CheckEquals('from server', j.Get('echo').JsonValue.Value);
|
|
|
|
|
j.Free;
|
|
|
|
|
finally
|
|
|
|
|
evt.Free;
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TServerTest.TestAsynchRequestPUT;
|
|
|
|
|
var
|
|
|
|
|
evt: TEvent;
|
|
|
|
|
r: TWaitResult;
|
|
|
|
|
j: TJSONObject;
|
|
|
|
|
begin
|
|
|
|
|
j := nil;
|
|
|
|
|
evt := TEvent.Create;
|
|
|
|
|
try
|
|
|
|
|
RESTClient.Asynch(
|
|
|
|
|
procedure(Response: IRESTResponse)
|
|
|
|
|
begin
|
|
|
|
|
try
|
2017-05-25 16:57:49 +02:00
|
|
|
|
j := TSystemJSON.StringAsJSONObject(Response.BodyAsString);
|
2015-12-22 12:29:25 +01:00
|
|
|
|
except
|
|
|
|
|
// test should not block...never!
|
|
|
|
|
end;
|
|
|
|
|
evt.SetEvent;
|
|
|
|
|
end,
|
|
|
|
|
procedure(E: Exception)
|
|
|
|
|
begin
|
2016-04-03 22:35:27 +02:00
|
|
|
|
end).doPUT('/echo', ['1', '2', '3'],
|
2017-04-26 14:39:18 +02:00
|
|
|
|
TSystemJSON.JSONValueToString(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;
|
|
|
|
|
|
|
|
|
|
CheckTrue(Assigned(j));
|
|
|
|
|
CheckEquals('from server', j.Get('echo').JsonValue.Value);
|
|
|
|
|
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
|
|
|
|
|
LRes: IRESTResponse;
|
|
|
|
|
begin
|
2016-04-03 22:35:27 +02:00
|
|
|
|
RESTClient.Authentication('user1', 'user1');
|
|
|
|
|
CheckEquals('user1', RESTClient.UserName);
|
|
|
|
|
CheckEquals('user1', RESTClient.Password);
|
2015-12-22 12:29:25 +01:00
|
|
|
|
LRes := RESTClient.doGET('/private/role1', []);
|
|
|
|
|
CheckEquals(HTTP_STATUS.OK, LRes.ResponseCode);
|
|
|
|
|
end;
|
|
|
|
|
|
2016-09-20 11:10:40 +02:00
|
|
|
|
procedure TServerTest.TestBasicAuth02;
|
2015-12-22 12:29:25 +01:00
|
|
|
|
var
|
|
|
|
|
LRes: IRESTResponse;
|
|
|
|
|
begin
|
|
|
|
|
RESTClient.UserName := '';
|
|
|
|
|
RESTClient.Password := '';
|
|
|
|
|
RESTClient.UseBasicAuthentication := false;
|
|
|
|
|
LRes := RESTClient.doGET('/private/role1', []);
|
|
|
|
|
CheckEquals(HTTP_STATUS.Unauthorized, LRes.ResponseCode);
|
|
|
|
|
end;
|
|
|
|
|
|
2016-09-20 11:10:40 +02:00
|
|
|
|
procedure TServerTest.TestBasicAuth03;
|
2015-12-22 12:29:25 +01:00
|
|
|
|
var
|
|
|
|
|
LRes: IRESTResponse;
|
|
|
|
|
begin
|
|
|
|
|
RESTClient.UserName := 'user1';
|
|
|
|
|
RESTClient.Password := 'user1';
|
|
|
|
|
RESTClient.UseBasicAuthentication := true;
|
|
|
|
|
LRes := RESTClient.doGET('/private/role2', []);
|
|
|
|
|
CheckEquals(HTTP_STATUS.Forbidden, LRes.ResponseCode);
|
|
|
|
|
end;
|
|
|
|
|
|
2016-09-20 11:10:40 +02:00
|
|
|
|
procedure TServerTest.TestBasicAuth04;
|
2015-12-22 12:29:25 +01:00
|
|
|
|
var
|
|
|
|
|
LRes: IRESTResponse;
|
|
|
|
|
begin
|
|
|
|
|
RESTClient.UserName := 'user1';
|
|
|
|
|
RESTClient.Password := 'user1';
|
|
|
|
|
RESTClient.UseBasicAuthentication := true;
|
|
|
|
|
LRes := RESTClient.doGET('/private/role1', []);
|
|
|
|
|
CheckEquals(HTTP_STATUS.OK, LRes.ResponseCode);
|
|
|
|
|
LRes := RESTClient.doGET('/people', []);
|
|
|
|
|
CheckEquals(HTTP_STATUS.OK, LRes.ResponseCode);
|
|
|
|
|
end;
|
|
|
|
|
|
2016-09-20 11:10:40 +02:00
|
|
|
|
procedure TServerTest.TestBasicAuth05;
|
2016-04-03 22:35:27 +02:00
|
|
|
|
var
|
|
|
|
|
LRes: IRESTResponse;
|
|
|
|
|
begin
|
|
|
|
|
RESTClient.UserName := 'user1';
|
|
|
|
|
RESTClient.Password := 'user1';
|
|
|
|
|
RESTClient.UseBasicAuthentication := true;
|
|
|
|
|
|
|
|
|
|
// first
|
|
|
|
|
LRes := RESTClient.doGET('/private/role1session?value=danieleteti', []);
|
|
|
|
|
CheckEquals(HTTP_STATUS.OK, LRes.ResponseCode);
|
|
|
|
|
LRes := RESTClient.doGET('/private/role1session', []);
|
|
|
|
|
CheckEquals(HTTP_STATUS.OK, LRes.ResponseCode);
|
2017-03-20 21:42:28 +01:00
|
|
|
|
CheckEquals('danieleteti', LRes.BodyAsString);
|
2016-04-03 22:35:27 +02:00
|
|
|
|
|
|
|
|
|
// second
|
|
|
|
|
LRes := RESTClient.doGET('/private/role1session?value=johndoe', []);
|
|
|
|
|
CheckEquals(HTTP_STATUS.OK, LRes.ResponseCode);
|
|
|
|
|
LRes := RESTClient.doGET('/private/role1session', []);
|
|
|
|
|
CheckEquals(HTTP_STATUS.OK, LRes.ResponseCode);
|
2017-03-20 21:42:28 +01:00
|
|
|
|
CheckEquals('johndoe', LRes.BodyAsString);
|
2016-04-03 22:35:27 +02:00
|
|
|
|
end;
|
|
|
|
|
|
2016-04-24 19:08:21 +02:00
|
|
|
|
procedure TServerTest.TestCookies;
|
|
|
|
|
var
|
|
|
|
|
res: IRESTResponse;
|
|
|
|
|
I: Integer;
|
|
|
|
|
begin
|
|
|
|
|
res := RESTClient.doGET('/lotofcookies', []);
|
|
|
|
|
CheckEquals(HTTP_STATUS.OK, res.ResponseCode);
|
|
|
|
|
CheckEquals(4, res.Cookies.Count, 'Wrong number of cookies');
|
|
|
|
|
for I := 0 to 3 do
|
|
|
|
|
begin
|
|
|
|
|
CheckEquals('usersettings' + IntToStr(I + 1),
|
|
|
|
|
res.Cookies.Cookies[I].CookieName);
|
|
|
|
|
CheckEquals('usersettings' + IntToStr(I + 1) + '-value',
|
|
|
|
|
res.Cookies.Cookies[I].Value);
|
2016-09-29 18:17:12 +02:00
|
|
|
|
CheckEquals('/usersettings' + IntToStr(I + 1), res.Cookies.Cookies[I].Path);
|
2016-04-24 19:08:21 +02:00
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
end;
|
|
|
|
|
|
2016-09-16 23:54:54 +02:00
|
|
|
|
procedure TServerTest.TestCustomAuthRequestWithoutLogin;
|
|
|
|
|
var
|
|
|
|
|
LRes: IRESTResponse;
|
|
|
|
|
begin
|
|
|
|
|
LRes := RESTClient.doGET('/privatecustom/role1', []);
|
|
|
|
|
CheckEquals(HTTP_STATUS.Unauthorized, LRes.ResponseCode);
|
|
|
|
|
CheckEquals('/system/users/logged', LRes.HeaderValue('X-LOGIN-URL'));
|
|
|
|
|
CheckEquals('POST', LRes.HeaderValue('X-LOGIN-METHOD'));
|
|
|
|
|
|
|
|
|
|
LRes := RESTClient.doGET('/privatecustom/role2', []);
|
|
|
|
|
CheckEquals(HTTP_STATUS.Unauthorized, LRes.ResponseCode);
|
|
|
|
|
CheckEquals('/system/users/logged', LRes.HeaderValue('X-LOGIN-URL'));
|
|
|
|
|
CheckEquals('POST', LRes.HeaderValue('X-LOGIN-METHOD'));
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TServerTest.TestCustomAuthRequestsWithValidLogin;
|
|
|
|
|
var
|
|
|
|
|
LRes: IRESTResponse;
|
|
|
|
|
lJSON: TJSONObject;
|
|
|
|
|
lCookieValue: string;
|
|
|
|
|
begin
|
|
|
|
|
lJSON := TJSONObject.Create;
|
|
|
|
|
try
|
|
|
|
|
lJSON.AddPair('username', 'user1');
|
|
|
|
|
lJSON.AddPair('password', 'user1');
|
2017-04-26 14:39:18 +02:00
|
|
|
|
LRes := RESTClient.doPOST('/system/users/logged', [], TSystemJSON.JSONValueToString(lJSON, False));
|
2016-09-18 19:19:23 +02:00
|
|
|
|
CheckEquals('application/json', LRes.ContentType);
|
2016-12-28 10:00:07 +01:00
|
|
|
|
CheckEquals(HTTP_STATUS.OK, LRes.ResponseCode);
|
|
|
|
|
CheckEquals('/system/users/logged', LRes.HeaderValue('X-LOGOUT-URL'));
|
|
|
|
|
CheckEquals('DELETE', LRes.HeaderValue('X-LOGOUT-METHOD'));
|
|
|
|
|
CheckEquals('{"status":"OK"}', LRes.BodyAsString);
|
|
|
|
|
lCookieValue := LRes.Cookies
|
|
|
|
|
[LRes.Cookies.GetCookieIndex(TMVCConstants.SESSION_TOKEN_NAME)].Value;
|
|
|
|
|
CheckNotEquals('', lCookieValue, 'Session cookie not returned after login');
|
|
|
|
|
CheckFalse(lCookieValue.Contains('invalid'),
|
|
|
|
|
'Returned an invalid session token');
|
|
|
|
|
|
|
|
|
|
LRes := RESTClient.doGET('/privatecustom/role2', []);
|
|
|
|
|
CheckEquals(HTTP_STATUS.Forbidden, LRes.ResponseCode,
|
|
|
|
|
'Authorization not respected for not allowed action');
|
|
|
|
|
|
|
|
|
|
LRes := RESTClient.doGET('/privatecustom/role1', []);
|
|
|
|
|
CheckEquals(HTTP_STATUS.OK, LRes.ResponseCode,
|
|
|
|
|
'Authorization not respected for allowed action');
|
|
|
|
|
finally
|
|
|
|
|
lJSON.Free;
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TServerTest.TestCustomAuthRequestsWithValidLogin_HTML;
|
|
|
|
|
var
|
|
|
|
|
LRes: IRESTResponse;
|
|
|
|
|
lJSON: TJSONObject;
|
|
|
|
|
lCookieValue: string;
|
|
|
|
|
begin
|
|
|
|
|
lJSON := TJSONObject.Create;
|
|
|
|
|
try
|
|
|
|
|
lJSON.AddPair('username', 'user1');
|
|
|
|
|
lJSON.AddPair('password', 'user1');
|
2017-04-26 14:39:18 +02:00
|
|
|
|
LRes := RESTClient.Accept('text/html').doPOST('/system/users/logged', [], TSystemJSON.JSONValueToString(lJSON, False));
|
2016-12-28 10:00:07 +01:00
|
|
|
|
CheckEquals('application/json', LRes.ContentType);
|
2016-09-16 23:54:54 +02:00
|
|
|
|
CheckEquals(HTTP_STATUS.OK, LRes.ResponseCode);
|
|
|
|
|
CheckEquals('/system/users/logged', LRes.HeaderValue('X-LOGOUT-URL'));
|
|
|
|
|
CheckEquals('DELETE', LRes.HeaderValue('X-LOGOUT-METHOD'));
|
|
|
|
|
CheckEquals('{"status":"OK"}', LRes.BodyAsString);
|
|
|
|
|
lCookieValue := LRes.Cookies
|
|
|
|
|
[LRes.Cookies.GetCookieIndex(TMVCConstants.SESSION_TOKEN_NAME)].Value;
|
|
|
|
|
CheckNotEquals('', lCookieValue, 'Session cookie not returned after login');
|
2016-09-29 18:17:12 +02:00
|
|
|
|
CheckFalse(lCookieValue.Contains('invalid'),
|
|
|
|
|
'Returned an invalid session token');
|
2016-09-16 23:54:54 +02:00
|
|
|
|
|
|
|
|
|
LRes := RESTClient.doGET('/privatecustom/role2', []);
|
|
|
|
|
CheckEquals(HTTP_STATUS.Forbidden, LRes.ResponseCode,
|
|
|
|
|
'Authorization not respected for not allowed action');
|
|
|
|
|
|
|
|
|
|
LRes := RESTClient.doGET('/privatecustom/role1', []);
|
|
|
|
|
CheckEquals(HTTP_STATUS.OK, LRes.ResponseCode,
|
|
|
|
|
'Authorization not respected for allowed action');
|
|
|
|
|
finally
|
|
|
|
|
lJSON.Free;
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TServerTest.TestCustomAuthWrongRequestBodies;
|
|
|
|
|
var
|
|
|
|
|
LRes: IRESTResponse;
|
|
|
|
|
lJSON: TJSONObject;
|
|
|
|
|
begin
|
|
|
|
|
lJSON := TJSONObject.Create;
|
|
|
|
|
try
|
|
|
|
|
// no request body
|
|
|
|
|
LRes := RESTClient.doPOST('/system/users/logged', []);
|
|
|
|
|
CheckEquals(HTTP_STATUS.BadRequest, LRes.ResponseCode,
|
|
|
|
|
'Empty request body doesn''t return HTTP 400 Bad Request');
|
|
|
|
|
|
|
|
|
|
// wrong request body 1
|
2017-04-26 14:39:18 +02:00
|
|
|
|
LRes := RESTClient.doPOST('/system/users/logged', [], TSystemJSON.JSONValueToString(lJSON, False));
|
2016-09-16 23:54:54 +02:00
|
|
|
|
CheckEquals(HTTP_STATUS.Unauthorized, LRes.ResponseCode,
|
|
|
|
|
'Invalid json doesn''t return HTTP 401 Unauthorized');
|
|
|
|
|
|
|
|
|
|
// wrong request body 2
|
|
|
|
|
lJSON.AddPair('username', '');
|
|
|
|
|
lJSON.AddPair('password', '');
|
2017-04-26 14:39:18 +02:00
|
|
|
|
LRes := RESTClient.doPOST('/system/users/logged', [], TSystemJSON.JSONValueToString(lJSON, False));
|
2016-09-16 23:54:54 +02:00
|
|
|
|
CheckEquals(HTTP_STATUS.Unauthorized, LRes.ResponseCode,
|
|
|
|
|
'Empty username and password doesn''t return HTTP 401 Unauthorized');
|
|
|
|
|
|
|
|
|
|
// wrong username and password 3
|
|
|
|
|
lJSON.RemovePair('username');
|
|
|
|
|
lJSON.RemovePair('password');
|
|
|
|
|
lJSON.AddPair('username', 'notvaliduser');
|
|
|
|
|
lJSON.AddPair('password', 'notvalidpassword');
|
2017-04-26 14:39:18 +02:00
|
|
|
|
LRes := RESTClient.doPOST('/system/users/logged', [], TSystemJSON.JSONValueToString(lJSON, False));
|
2016-09-16 23:54:54 +02:00
|
|
|
|
CheckEquals(HTTP_STATUS.Unauthorized, LRes.ResponseCode,
|
|
|
|
|
'Wrong username and password doesn''t return HTTP 401 Unauthorized');
|
|
|
|
|
finally
|
|
|
|
|
lJSON.Free;
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
|
2016-09-18 19:19:23 +02:00
|
|
|
|
procedure TServerTest.TestCustomAuthLoginLogout;
|
|
|
|
|
var
|
|
|
|
|
LRes: IRESTResponse;
|
|
|
|
|
lJSON: TJSONObject;
|
|
|
|
|
lLogoutUrl: string;
|
|
|
|
|
lValue: string;
|
|
|
|
|
I: Integer;
|
|
|
|
|
lPieces: TArray<string>;
|
2016-09-29 18:17:12 +02:00
|
|
|
|
lPass: boolean;
|
2016-09-16 23:54:54 +02:00
|
|
|
|
begin
|
2016-09-18 19:19:23 +02:00
|
|
|
|
lJSON := TJSONObject.Create;
|
|
|
|
|
try
|
|
|
|
|
lJSON.AddPair('username', 'user1');
|
|
|
|
|
lJSON.AddPair('password', 'user1');
|
2017-04-26 14:39:18 +02:00
|
|
|
|
LRes := RESTClient.doPOST('/system/users/logged', [], TSystemJSON.JSONValueToString(lJSON, False));
|
2016-09-16 23:54:54 +02:00
|
|
|
|
|
2016-09-18 19:19:23 +02:00
|
|
|
|
CheckEquals(HTTP_STATUS.OK, LRes.ResponseCode);
|
|
|
|
|
lLogoutUrl := LRes.HeaderValue('X-LOGOUT-URL');
|
|
|
|
|
|
|
|
|
|
LRes := RESTClient.doDELETE(lLogoutUrl, []);
|
|
|
|
|
lPass := false;
|
|
|
|
|
for I := 0 to LRes.Headers.Count do
|
|
|
|
|
begin
|
|
|
|
|
lValue := LRes.Headers[I];
|
|
|
|
|
if lValue.StartsWith('Set-Cookie') then
|
|
|
|
|
begin
|
|
|
|
|
lPieces := lValue.Split([':']);
|
|
|
|
|
lValue := lPieces[1].Trim;
|
2016-09-29 18:17:12 +02:00
|
|
|
|
if lValue.StartsWith(TMVCConstants.SESSION_TOKEN_NAME) and
|
|
|
|
|
lValue.Contains('invalid') then
|
2016-09-18 19:19:23 +02:00
|
|
|
|
begin
|
|
|
|
|
lPass := true;
|
|
|
|
|
Break;
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
CheckTrue(lPass, 'No session cookie cleanup in the response');
|
|
|
|
|
finally
|
|
|
|
|
lJSON.Free;
|
|
|
|
|
end;
|
2016-09-16 23:54:54 +02:00
|
|
|
|
|
|
|
|
|
end;
|
|
|
|
|
|
2015-12-22 12:29:25 +01:00
|
|
|
|
procedure TServerTest.TestEncodingRenderJSONValue;
|
|
|
|
|
var
|
|
|
|
|
res: IRESTResponse;
|
|
|
|
|
s: string;
|
2017-04-26 14:39:18 +02:00
|
|
|
|
lJSONObj: TJSONObject;
|
2015-12-22 12:29:25 +01:00
|
|
|
|
begin
|
|
|
|
|
res := RESTClient.doGET('/encoding', []);
|
|
|
|
|
|
2017-05-25 16:57:49 +02:00
|
|
|
|
lJSONObj := TSystemJSON.StringAsJSONObject(res.BodyAsString);
|
2017-04-26 14:39:18 +02:00
|
|
|
|
s := lJSONObj.Get('name1').JsonValue.Value;
|
2015-12-22 12:29:25 +01:00
|
|
|
|
CheckEquals('j<>rn', s);
|
2017-04-26 14:39:18 +02:00
|
|
|
|
lJSONObj.Free;
|
2015-12-22 12:29:25 +01:00
|
|
|
|
|
2017-05-25 16:57:49 +02:00
|
|
|
|
lJSONObj := TSystemJSON.StringAsJSONObject(res.BodyAsString);
|
2017-04-26 14:39:18 +02:00
|
|
|
|
s := lJSONObj.Get('name3').JsonValue.Value;
|
2015-12-22 12:29:25 +01:00
|
|
|
|
CheckEquals('<27><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>', s);
|
2017-04-26 14:39:18 +02:00
|
|
|
|
lJSONObj.Free;
|
2015-12-22 12:29:25 +01:00
|
|
|
|
|
2017-05-25 16:57:49 +02:00
|
|
|
|
lJSONObj := TSystemJSON.StringAsJSONObject(res.BodyAsString);
|
2017-04-26 14:39:18 +02:00
|
|
|
|
s := lJSONObj.Get('name2').JsonValue.Value;
|
2015-12-22 12:29:25 +01:00
|
|
|
|
CheckEquals('<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;
|
|
|
|
|
|
2016-11-18 18:02:48 +01:00
|
|
|
|
procedure TServerTest.TestRenderStreamAndFreeWithOwnerFalse;
|
|
|
|
|
var
|
|
|
|
|
LRes: IRESTResponse;
|
|
|
|
|
begin
|
|
|
|
|
LRes := RESTClient.doGET('/renderstreamandfreewithownerfalse', []);
|
|
|
|
|
CheckEquals(200, LRes.ResponseCode);
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TServerTest.TestRenderStreamAndFreeWithOwnerTrue;
|
|
|
|
|
var
|
|
|
|
|
LRes: IRESTResponse;
|
|
|
|
|
begin
|
|
|
|
|
LRes := RESTClient.doGET('/renderstreamandfreewithownertrue', []);
|
|
|
|
|
CheckEquals(200, LRes.ResponseCode);
|
|
|
|
|
end;
|
|
|
|
|
|
2016-09-29 18:17:12 +02:00
|
|
|
|
procedure TServerTest.TestRenderWrappedList;
|
|
|
|
|
var
|
2016-11-18 18:02:48 +01:00
|
|
|
|
LRes: IRESTResponse;
|
2016-09-30 11:44:11 +02:00
|
|
|
|
lJSONArr: TJSONArray;
|
2016-09-29 18:17:12 +02:00
|
|
|
|
I: Integer;
|
2016-09-30 11:44:11 +02:00
|
|
|
|
lJSONObj: TJSONObject;
|
2016-09-29 18:17:12 +02:00
|
|
|
|
begin
|
2016-11-18 18:02:48 +01:00
|
|
|
|
LRes := RESTClient.doGET('/wrappedpeople', []);
|
2016-09-29 18:17:12 +02:00
|
|
|
|
|
2017-05-25 16:57:49 +02:00
|
|
|
|
lJSONArr := TSystemJSON.StringAsJSONArray(LRes.BodyAsString);
|
2017-04-26 14:39:18 +02:00
|
|
|
|
try
|
|
|
|
|
for I := 0 to lJSONArr.Count - 1 do
|
|
|
|
|
begin
|
|
|
|
|
lJSONObj := lJSONArr.Items[I] as TJSONObject;
|
|
|
|
|
CheckFalse(lJSONObj.GetValue<string>('firstname').IsEmpty);
|
|
|
|
|
end;
|
|
|
|
|
finally
|
|
|
|
|
lJSONArr.Free;
|
2016-09-29 18:17:12 +02:00
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
end;
|
|
|
|
|
|
2015-12-22 12:29:25 +01:00
|
|
|
|
procedure TServerTest.TestExceptionInMVCAfterCreate;
|
|
|
|
|
var
|
|
|
|
|
res: IRESTResponse;
|
|
|
|
|
begin
|
|
|
|
|
res := RESTClient.doGET('/exception/aftercreate/nevercalled', []);
|
|
|
|
|
CheckEquals(HTTP_STATUS.InternalServerError, res.ResponseCode);
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TServerTest.TestExceptionInMVCBeforeDestroy;
|
|
|
|
|
var
|
|
|
|
|
res: IRESTResponse;
|
|
|
|
|
begin
|
|
|
|
|
res := RESTClient.doGET('/exception/beforedestroy/nevercalled', []);
|
|
|
|
|
CheckEquals(HTTP_STATUS.InternalServerError, res.ResponseCode);
|
|
|
|
|
end;
|
|
|
|
|
|
2016-04-03 22:35:27 +02:00
|
|
|
|
procedure TServerTest.TestInvalidateSession;
|
|
|
|
|
var
|
|
|
|
|
c1: TRESTClient;
|
|
|
|
|
res: IRESTResponse;
|
|
|
|
|
begin
|
2017-04-14 16:43:31 +02:00
|
|
|
|
c1 := TRESTClient.Create(TEST_SERVER_ADDRESS, 9999);
|
2016-04-03 22:35:27 +02:00
|
|
|
|
try
|
|
|
|
|
c1.Accept(TMVCMediaType.APPLICATION_JSON);
|
|
|
|
|
c1.doPOST('/session', ['daniele teti']); // imposto un valore in sessione
|
|
|
|
|
res := c1.doGET('/session', []); // rileggo il valore dalla sessione
|
2017-03-20 21:42:28 +01:00
|
|
|
|
CheckEquals('daniele teti', res.BodyAsString);
|
2016-04-03 22:35:27 +02:00
|
|
|
|
c1.SessionID := '';
|
|
|
|
|
res := c1.doGET('/session', []); // rileggo il valore dalla sessione
|
2017-03-20 21:42:28 +01:00
|
|
|
|
CheckEquals('', res.BodyAsString);
|
2016-04-03 22:35:27 +02:00
|
|
|
|
finally
|
|
|
|
|
c1.Free;
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
|
2015-12-22 12:29:25 +01:00
|
|
|
|
procedure TServerTest.TestMiddlewareHandler;
|
|
|
|
|
var
|
|
|
|
|
r: IRESTResponse;
|
|
|
|
|
begin
|
2016-04-03 22:35:27 +02:00
|
|
|
|
r := RESTClient.Accept(TMVCMediaType.APPLICATION_JSON)
|
|
|
|
|
.doGET('/handledbymiddleware', []);
|
2015-12-22 12:29:25 +01:00
|
|
|
|
CheckEquals('This is a middleware response', r.BodyAsString);
|
|
|
|
|
CheckEquals(HTTP_STATUS.OK, r.ResponseCode);
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TServerTest.TestMiddlewareSpeedMiddleware;
|
|
|
|
|
var
|
|
|
|
|
r: IRESTResponse;
|
|
|
|
|
P: TPerson;
|
|
|
|
|
begin
|
|
|
|
|
P := TPerson.Create;
|
|
|
|
|
try
|
|
|
|
|
P.FirstName := StringOfChar('*', 1000);
|
|
|
|
|
P.LastName := StringOfChar('*', 1000);
|
|
|
|
|
P.DOB := EncodeDate(1979, 1, 1);
|
|
|
|
|
P.Married := true;
|
2016-04-03 22:35:27 +02:00
|
|
|
|
r := RESTClient.Accept(TMVCMediaType.APPLICATION_JSON)
|
2017-04-26 14:39:18 +02:00
|
|
|
|
.doPOST('/objects', [], GetDefaultSerializer.SerializeObject(P));
|
2015-12-22 12:29:25 +01:00
|
|
|
|
finally
|
|
|
|
|
P.Free;
|
|
|
|
|
end;
|
|
|
|
|
|
2016-01-01 23:00:22 +01:00
|
|
|
|
CheckNotEquals('', r.HeaderValue('request_gen_time'));
|
2015-12-22 12:29:25 +01:00
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
// procedure TServerTest.TestPATCHWithParamsAndJSONBody;
|
|
|
|
|
// var
|
|
|
|
|
// r: IRESTResponse;
|
|
|
|
|
// json: TJSONObject;
|
|
|
|
|
// begin
|
|
|
|
|
// json := TJSONObject.Create;
|
|
|
|
|
// json.AddPair('client', 'clientdata');
|
|
|
|
|
// r := RESTClient.doPATCH('/echo', ['1', '2', '3'], json);
|
|
|
|
|
// CheckEquals('clientdata', r.BodyAsJsonObject.Get('client').JsonValue.Value);
|
|
|
|
|
// CheckEquals('from server', r.BodyAsJsonObject.Get('echo').JsonValue.Value);
|
|
|
|
|
// end;
|
|
|
|
|
|
|
|
|
|
procedure TServerTest.TestPostAListOfObjects;
|
|
|
|
|
var
|
|
|
|
|
LRes: IRESTResponse;
|
|
|
|
|
LCustomers: TObjectList<TCustomer>;
|
|
|
|
|
begin
|
|
|
|
|
LCustomers := TCustomer.GetList;
|
|
|
|
|
try
|
|
|
|
|
LRes := RESTClient.doPOST('/customers/list', [],
|
2017-04-26 14:39:18 +02:00
|
|
|
|
GetDefaultSerializer.SerializeCollection(LCustomers)
|
|
|
|
|
{ Mapper.ObjectListToJSONArray<TCustomer>(LCustomers) }
|
|
|
|
|
);
|
2015-12-22 12:29:25 +01:00
|
|
|
|
CheckEquals(HTTP_STATUS.OK, LRes.ResponseCode);
|
|
|
|
|
finally
|
|
|
|
|
LCustomers.Free;
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TServerTest.TestPOSTWithObjectJSONBody;
|
|
|
|
|
var
|
|
|
|
|
r: IRESTResponse;
|
|
|
|
|
P: TPerson;
|
|
|
|
|
begin
|
|
|
|
|
P := TPerson.Create;
|
|
|
|
|
try
|
|
|
|
|
P.FirstName := 'Daniele';
|
|
|
|
|
P.LastName := '<27><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>';
|
|
|
|
|
P.DOB := EncodeDate(1979, 1, 1);
|
|
|
|
|
P.Married := true;
|
|
|
|
|
try
|
2016-01-01 23:00:22 +01:00
|
|
|
|
r := RESTClient.Accept(TMVCMediaType.APPLICATION_JSON)
|
2017-04-26 14:39:18 +02:00
|
|
|
|
.doPOST('/objects', [],
|
|
|
|
|
GetDefaultSerializer.SerializeObject(P)
|
|
|
|
|
{ Mapper.ObjectToJSONObject(P) }
|
|
|
|
|
);
|
2015-12-22 12:29:25 +01:00
|
|
|
|
except
|
|
|
|
|
Fail('If this test fail, check http://qc.embarcadero.com/wc/qcmain.aspx?d=119779');
|
|
|
|
|
{ WARNING!!! }
|
|
|
|
|
{
|
|
|
|
|
If this test fail, check
|
|
|
|
|
http://qc.embarcadero.com/wc/qcmain.aspx?d=119779
|
|
|
|
|
}
|
|
|
|
|
raise;
|
|
|
|
|
|
|
|
|
|
end;
|
|
|
|
|
finally
|
|
|
|
|
P.Free;
|
|
|
|
|
end;
|
2017-04-26 14:39:18 +02:00
|
|
|
|
|
|
|
|
|
P := TPerson.Create;
|
2015-12-22 12:29:25 +01:00
|
|
|
|
try
|
2017-04-26 14:39:18 +02:00
|
|
|
|
GetDefaultSerializer.DeserializeObject(r.BodyAsString, P);
|
|
|
|
|
// P := Mapper.JSONObjectToObject<TPerson>(r.BodyAsJsonObject);
|
2015-12-22 12:29:25 +01:00
|
|
|
|
CheckEquals('Daniele', P.FirstName);
|
|
|
|
|
CheckEquals('<27><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>', P.LastName);
|
|
|
|
|
CheckEquals(true, P.Married);
|
|
|
|
|
CheckEquals(EncodeDate(1979, 1, 1), P.DOB);
|
|
|
|
|
finally
|
|
|
|
|
P.Free;
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TServerTest.TestPOSTWithParamsAndJSONBody;
|
|
|
|
|
var
|
|
|
|
|
r: IRESTResponse;
|
|
|
|
|
JSON: TJSONObject;
|
|
|
|
|
begin
|
|
|
|
|
JSON := TJSONObject.Create;
|
|
|
|
|
JSON.AddPair('client', 'clientdata');
|
2017-04-26 14:39:18 +02:00
|
|
|
|
r := RESTClient.doPOST('/echo', ['1', '2', '3'], TSystemJSON.JSONValueToString(JSON));
|
2017-05-25 16:57:49 +02:00
|
|
|
|
JSON := TSystemJSON.StringAsJSONObject(r.BodyAsString);
|
2017-04-26 14:39:18 +02:00
|
|
|
|
try
|
|
|
|
|
CheckEquals('clientdata', JSON.Get('client').JsonValue.Value);
|
|
|
|
|
CheckEquals('from server', JSON.Get('echo').JsonValue.Value);
|
|
|
|
|
finally
|
|
|
|
|
JSON.Free;
|
|
|
|
|
end;
|
2015-12-22 12:29:25 +01:00
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TServerTest.TestProducesConsumesWithWrongAcceptHeader;
|
|
|
|
|
var
|
|
|
|
|
res: IRESTResponse;
|
|
|
|
|
begin
|
2016-04-03 22:35:27 +02:00
|
|
|
|
res := RESTClient.Accept('text/plain')
|
|
|
|
|
// action is waiting for a accept: application/json
|
|
|
|
|
.ContentType('application/json').doPOST('/testconsumes', [],
|
2017-04-26 14:39:18 +02:00
|
|
|
|
TSystemJSON.JSONValueToString(TJSONString.Create('Hello World')));
|
2015-12-22 12:29:25 +01:00
|
|
|
|
CheckEquals(HTTP_STATUS.NotFound, res.ResponseCode);
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TServerTest.TestProducesConsumes01;
|
|
|
|
|
var
|
|
|
|
|
res: IRESTResponse;
|
|
|
|
|
begin
|
|
|
|
|
res := RESTClient.Accept('application/json').ContentType('application/json')
|
2016-04-03 22:35:27 +02:00
|
|
|
|
.ContentEncoding('utf-8').doPOST('/testconsumes', [],
|
2017-04-26 14:39:18 +02:00
|
|
|
|
TSystemJSON.JSONValueToString(TJSONString.Create('Hello World')));
|
2015-12-22 12:29:25 +01:00
|
|
|
|
CheckEquals(HTTP_STATUS.OK, res.ResponseCode);
|
2017-03-20 21:42:28 +01:00
|
|
|
|
CheckEquals('Hello World', res.BodyAsString);
|
2016-01-01 23:00:22 +01:00
|
|
|
|
CheckEquals('application/json', res.ContentType);
|
|
|
|
|
CheckEquals('utf-8', res.ContentEncoding);
|
2015-12-22 12:29:25 +01:00
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TServerTest.TestProducesConsumes02;
|
|
|
|
|
var
|
|
|
|
|
res: IRESTResponse;
|
|
|
|
|
begin
|
2016-04-03 22:35:27 +02:00
|
|
|
|
res := RESTClient.Accept('text/plain').ContentType('text/plain')
|
|
|
|
|
.doPOST('/testconsumes', [], 'Hello World');
|
2015-12-22 12:29:25 +01:00
|
|
|
|
CheckEquals('Hello World', res.BodyAsString);
|
2016-01-01 23:00:22 +01:00
|
|
|
|
CheckEquals('text/plain', res.ContentType);
|
|
|
|
|
CheckEquals('UTF-8', res.ContentEncoding);
|
2015-12-22 12:29:25 +01:00
|
|
|
|
|
|
|
|
|
res := RESTClient.Accept('text/plain').ContentType('application/json')
|
|
|
|
|
.doPOST('/testconsumes', [], '{"name": "Daniele"}');
|
|
|
|
|
CheckEquals(HTTP_STATUS.NotFound, res.ResponseCode);
|
|
|
|
|
end;
|
|
|
|
|
|
2016-12-07 19:35:36 +01:00
|
|
|
|
procedure TServerTest.TestProducesConsumes03;
|
|
|
|
|
var
|
|
|
|
|
res: IRESTResponse;
|
|
|
|
|
begin
|
|
|
|
|
res := RESTClient
|
|
|
|
|
.Accept(TMVCMediaType.TEXT_PLAIN)
|
|
|
|
|
.ContentType(TMVCMediaType.TEXT_PLAIN)
|
|
|
|
|
.ContentEncoding('iso8859-1')
|
|
|
|
|
.doPOST('/testconsumes/textiso8859_1', [],
|
|
|
|
|
'<27><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>');
|
|
|
|
|
CheckEquals(HTTP_STATUS.OK, res.ResponseCode);
|
|
|
|
|
CheckEquals('<27><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>', res.BodyAsString);
|
|
|
|
|
CheckEquals(TMVCMediaType.TEXT_PLAIN, res.ContentType);
|
|
|
|
|
CheckEquals('iso8859-1', res.ContentEncoding);
|
|
|
|
|
|
|
|
|
|
end;
|
|
|
|
|
|
2015-12-22 12:29:25 +01:00
|
|
|
|
procedure TServerTest.TestPUTWithParamsAndJSONBody;
|
|
|
|
|
var
|
|
|
|
|
r: IRESTResponse;
|
|
|
|
|
JSON: TJSONObject;
|
|
|
|
|
begin
|
|
|
|
|
JSON := TJSONObject.Create;
|
|
|
|
|
JSON.AddPair('client', 'clientdata');
|
2017-04-26 14:39:18 +02:00
|
|
|
|
r := RESTClient.doPUT('/echo', ['1', '2', '3'], TSystemJSON.JSONValueToString(JSON));
|
|
|
|
|
|
2017-05-25 16:57:49 +02:00
|
|
|
|
JSON := TSystemJSON.StringAsJSONObject(r.BodyAsString);
|
2017-04-26 14:39:18 +02:00
|
|
|
|
try
|
|
|
|
|
CheckEquals('clientdata', JSON.Get('client').JsonValue.Value);
|
|
|
|
|
CheckEquals('from server', JSON.Get('echo').JsonValue.Value);
|
|
|
|
|
finally
|
|
|
|
|
JSON.Free;
|
|
|
|
|
end;
|
2015-12-22 12:29:25 +01:00
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TServerTest.TestReqWithParams;
|
|
|
|
|
var
|
|
|
|
|
r: IRESTResponse;
|
2016-05-13 17:20:08 +02:00
|
|
|
|
ss: TStringStream;
|
2017-04-26 14:39:18 +02:00
|
|
|
|
lJSON: TJSONObject;
|
2015-12-22 12:29:25 +01:00
|
|
|
|
begin
|
|
|
|
|
r := RESTClient.doGET('/unknownurl/bla/bla', []);
|
2016-05-13 17:20:08 +02:00
|
|
|
|
|
|
|
|
|
ss := TStringStream.Create;
|
|
|
|
|
try
|
|
|
|
|
ss.CopyFrom(r.Body, 0);
|
|
|
|
|
CheckEquals(ss.DataString, r.BodyAsString,
|
|
|
|
|
'In case of rotocol error, the body doesn''t contain the same of BodyAsString');
|
|
|
|
|
finally
|
|
|
|
|
ss.Free;
|
|
|
|
|
end;
|
|
|
|
|
|
2015-12-22 12:29:25 +01:00
|
|
|
|
CheckEquals(HTTP_STATUS.NotFound, r.ResponseCode, '/unknownurl/bla/bla');
|
|
|
|
|
|
|
|
|
|
r := RESTClient.doGET('/req/with/params/', []);
|
|
|
|
|
CheckEquals(HTTP_STATUS.NotFound, r.ResponseCode, '/req/with/params/');
|
|
|
|
|
|
|
|
|
|
r := RESTClient.doGET('/req/with/params', []);
|
|
|
|
|
CheckEquals(HTTP_STATUS.NotFound, r.ResponseCode, '/req/with/params');
|
|
|
|
|
|
|
|
|
|
r := RESTClient.doGET('/req/with/params', ['1', '2', '3']);
|
|
|
|
|
CheckEquals(HTTP_STATUS.OK, r.ResponseCode);
|
|
|
|
|
|
2017-05-25 16:57:49 +02:00
|
|
|
|
lJSON := TSystemJSON.StringAsJSONObject(r.BodyAsString);
|
2017-04-26 14:39:18 +02:00
|
|
|
|
try
|
|
|
|
|
CheckEquals('1', lJSON.Get('par1').JsonValue.Value);
|
|
|
|
|
CheckEquals('2', lJSON.Get('par2').JsonValue.Value);
|
|
|
|
|
CheckEquals('3', lJSON.Get('par3').JsonValue.Value);
|
|
|
|
|
CheckEquals('GET', lJSON.Get('method').JsonValue.Value);
|
2015-12-22 12:29:25 +01:00
|
|
|
|
|
2017-04-26 14:39:18 +02:00
|
|
|
|
r := RESTClient.doPOST('/req/with/params', ['1', '2', '3']);
|
|
|
|
|
CheckEquals(HTTP_STATUS.NotFound, r.ResponseCode);
|
2015-12-22 12:29:25 +01:00
|
|
|
|
|
2017-04-26 14:39:18 +02:00
|
|
|
|
r := RESTClient.doPUT('/req/with/params', ['1', '2', '3']);
|
|
|
|
|
CheckEquals(HTTP_STATUS.NotFound, r.ResponseCode);
|
|
|
|
|
|
|
|
|
|
r := RESTClient.doDELETE('/req/with/params', ['1', '2', '3']);
|
|
|
|
|
CheckEquals(HTTP_STATUS.OK, r.ResponseCode);
|
|
|
|
|
CheckEquals('', r.BodyAsString);
|
|
|
|
|
finally
|
|
|
|
|
lJSON.Free;
|
|
|
|
|
end;
|
2015-12-22 12:29:25 +01:00
|
|
|
|
end;
|
|
|
|
|
|
2017-04-14 16:43:31 +02:00
|
|
|
|
// procedure TServerTest.TestSerializationType;
|
|
|
|
|
// var
|
|
|
|
|
// LResp: IRESTResponse;
|
|
|
|
|
// LPersonProps, LPersonFlds: TPerson;
|
|
|
|
|
// LObj: TObject;
|
|
|
|
|
// begin
|
|
|
|
|
// LResp := RESTClient.doGET('/people', ['1']);
|
|
|
|
|
// LPersonProps := Mapper.JSONObjectToObject<TPerson>(LResp.BodyAsJsonObject);
|
|
|
|
|
// try
|
|
|
|
|
// LResp := RESTClient.doGET('/people', ['1', 'asfields']);
|
|
|
|
|
// LObj := Mapper.JSONObjectFieldsToObject(LResp.BodyAsJsonObject);
|
|
|
|
|
// try
|
|
|
|
|
// CheckEquals('BusinessObjectsU.TPerson', LObj.QualifiedClassName);
|
|
|
|
|
// LPersonFlds := TPerson(LObj);
|
|
|
|
|
// CheckTrue(LPersonFlds.Equals(LPersonProps),
|
|
|
|
|
// 'Object tranferred using field serialization is different from the object serialized in the default way');
|
|
|
|
|
// finally
|
|
|
|
|
// LObj.Free;
|
|
|
|
|
// end;
|
|
|
|
|
// finally
|
|
|
|
|
// LPersonProps.Free;
|
|
|
|
|
// end;
|
|
|
|
|
// end;
|
2015-12-22 12:29:25 +01:00
|
|
|
|
|
|
|
|
|
procedure TServerTest.TestSession;
|
|
|
|
|
var
|
|
|
|
|
c1: TRESTClient;
|
|
|
|
|
res: IRESTResponse;
|
|
|
|
|
begin
|
2017-04-14 16:43:31 +02:00
|
|
|
|
c1 := TRESTClient.Create(TEST_SERVER_ADDRESS, 9999);
|
2015-12-22 12:29:25 +01:00
|
|
|
|
try
|
2016-01-01 23:00:22 +01:00
|
|
|
|
c1.Accept(TMVCMediaType.APPLICATION_JSON);
|
2015-12-22 12:29:25 +01:00
|
|
|
|
c1.doPOST('/session', ['daniele teti']); // imposto un valore in sessione
|
|
|
|
|
res := c1.doGET('/session', []); // rileggo il valore dalla sessione
|
2017-03-20 21:42:28 +01:00
|
|
|
|
CheckEquals('daniele teti', res.BodyAsString);
|
2016-01-01 23:00:22 +01:00
|
|
|
|
c1.Accept(TMVCMediaType.TEXT_PLAIN);
|
2015-12-22 12:29:25 +01:00
|
|
|
|
res := c1.doGET('/session', []);
|
|
|
|
|
// rileggo il valore dalla sessione
|
|
|
|
|
CheckEquals('daniele teti', res.BodyAsString);
|
|
|
|
|
|
|
|
|
|
// aggiungo altri cookies
|
|
|
|
|
res := c1.doGET('/lotofcookies', []); // rileggo il valore dalla sessione
|
|
|
|
|
CheckEquals(HTTP_STATUS.OK, res.ResponseCode);
|
2016-01-01 23:00:22 +01:00
|
|
|
|
c1.Accept(TMVCMediaType.TEXT_PLAIN);
|
2015-12-22 12:29:25 +01:00
|
|
|
|
res := c1.doGET('/session', []); // rileggo il valore dalla sessione
|
|
|
|
|
CheckEquals('daniele teti', res.BodyAsString);
|
|
|
|
|
finally
|
|
|
|
|
c1.Free;
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
|
2016-06-28 13:42:14 +02:00
|
|
|
|
procedure TServerTest.TestSessionWithLogin;
|
|
|
|
|
begin
|
|
|
|
|
DoLoginWith('daniele');
|
|
|
|
|
DoLogout;
|
|
|
|
|
end;
|
|
|
|
|
|
2016-06-22 17:49:16 +02:00
|
|
|
|
procedure TServerTest.TestTypedAll;
|
|
|
|
|
var
|
|
|
|
|
res: IRESTResponse;
|
|
|
|
|
lJObj: TJSONObject;
|
|
|
|
|
begin
|
|
|
|
|
// ----------------------'/typed/all/($ParString)/($ParInteger)/($ParInt64)/($ParSingle)/($ParDouble)/($ParExtended)')', []);
|
2016-09-29 18:17:12 +02:00
|
|
|
|
res := RESTClient.doGET
|
|
|
|
|
('/typed/all/mystring/1234/12345678/12.3/1234.5678/1234.5678', []);
|
2016-06-22 17:49:16 +02:00
|
|
|
|
CheckTrue(res.ResponseCode = HTTP_STATUS.OK, 'Cannot route');
|
2017-05-25 16:57:49 +02:00
|
|
|
|
lJObj := TSystemJSON.StringAsJSONObject(res.BodyAsString);
|
2017-04-26 14:39:18 +02:00
|
|
|
|
try
|
|
|
|
|
CheckEquals('mystring', lJObj.GetValue('ParString').Value, 'ParString');
|
|
|
|
|
CheckEquals(1234, TJSONNumber(lJObj.GetValue('ParInteger')).AsInt,
|
|
|
|
|
'ParInteger');
|
|
|
|
|
CheckEquals(12345678, TJSONNumber(lJObj.GetValue('ParInt64')).AsInt64,
|
|
|
|
|
'ParInt64');
|
|
|
|
|
CheckEquals(12.3, RoundTo(TJSONNumber(lJObj.GetValue('ParSingle')).AsDouble,
|
|
|
|
|
-1), 'ParSingle');
|
|
|
|
|
CheckEquals(1234.5678, RoundTo(TJSONNumber(lJObj.GetValue('ParDouble'))
|
|
|
|
|
.AsDouble, -4), 'ParDouble');
|
|
|
|
|
CheckEquals(1234.5678, RoundTo(TJSONNumber(lJObj.GetValue('ParExtended'))
|
|
|
|
|
.AsDouble, -4), 'ParExtended');
|
|
|
|
|
finally
|
|
|
|
|
lJObj.Free;
|
|
|
|
|
end;
|
2016-06-22 17:49:16 +02:00
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TServerTest.TestTypedBooleans;
|
|
|
|
|
var
|
|
|
|
|
res: IRESTResponse;
|
|
|
|
|
begin
|
|
|
|
|
res := RESTClient.doGET('/typed/booleans/true/false/1/0', []);
|
|
|
|
|
CheckTrue(res.ResponseCode = HTTP_STATUS.OK, 'Cannot route');
|
|
|
|
|
CheckEquals('true.false.true.false', res.BodyAsString.ToLower);
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TServerTest.TestTypedDouble1;
|
|
|
|
|
var
|
|
|
|
|
res: IRESTResponse;
|
|
|
|
|
begin
|
|
|
|
|
res := RESTClient.doGET('/typed/double1/1234.5678', []);
|
|
|
|
|
CheckTrue(res.ResponseCode = HTTP_STATUS.OK, 'Cannot route');
|
|
|
|
|
CheckEquals('1234.5678 modified from server', res.BodyAsString);
|
|
|
|
|
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TServerTest.TestTypedExtended1;
|
|
|
|
|
var
|
|
|
|
|
res: IRESTResponse;
|
|
|
|
|
begin
|
|
|
|
|
res := RESTClient.doGET('/typed/extended1/1234.5678', []);
|
|
|
|
|
CheckTrue(res.ResponseCode = HTTP_STATUS.OK, 'Cannot route');
|
|
|
|
|
CheckEquals('1234.5678 modified from server', res.BodyAsString);
|
|
|
|
|
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TServerTest.TestTypedInt641;
|
|
|
|
|
var
|
|
|
|
|
res: IRESTResponse;
|
|
|
|
|
begin
|
|
|
|
|
res := RESTClient.doGET('/typed/int641/12345678', []);
|
|
|
|
|
CheckTrue(res.ResponseCode = HTTP_STATUS.OK, 'Cannot route');
|
|
|
|
|
CheckEquals('12345678 modified from server', res.BodyAsString);
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TServerTest.TestTypedInteger1;
|
|
|
|
|
var
|
|
|
|
|
res: IRESTResponse;
|
|
|
|
|
begin
|
|
|
|
|
res := RESTClient.doGET('/typed/integer1/1234', []);
|
|
|
|
|
CheckTrue(res.ResponseCode = HTTP_STATUS.OK, 'Cannot route');
|
|
|
|
|
CheckEquals('1234 modified from server', res.BodyAsString);
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TServerTest.TestTypedSingle1;
|
|
|
|
|
var
|
|
|
|
|
res: IRESTResponse;
|
|
|
|
|
begin
|
|
|
|
|
res := RESTClient.doGET('/typed/single1/1234.5', []);
|
|
|
|
|
CheckTrue(res.ResponseCode = HTTP_STATUS.OK, 'Cannot route');
|
|
|
|
|
CheckEquals('1234.5 modified from server', res.BodyAsString);
|
|
|
|
|
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TServerTest.TestTypedString1;
|
|
|
|
|
var
|
|
|
|
|
res: IRESTResponse;
|
|
|
|
|
begin
|
|
|
|
|
res := RESTClient.doGET('/typed/string1/daniele', []);
|
|
|
|
|
CheckTrue(res.ResponseCode = HTTP_STATUS.OK, 'Cannot route');
|
|
|
|
|
CheckEquals('daniele modified from server', res.BodyAsString);
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TServerTest.TestTypedDateTimeTypes;
|
|
|
|
|
var
|
|
|
|
|
res: IRESTResponse;
|
|
|
|
|
begin
|
|
|
|
|
// TDate, wrong and correct
|
|
|
|
|
res := RESTClient.doGET('/typed/tdate1/20161012', []);
|
|
|
|
|
CheckEquals(HTTP_STATUS.InternalServerError, res.ResponseCode, 'wrong TDate');
|
|
|
|
|
|
|
|
|
|
res := RESTClient.doGET('/typed/tdate1/2016-10-12', []);
|
|
|
|
|
CheckEquals(HTTP_STATUS.OK, res.ResponseCode);
|
|
|
|
|
CheckEquals('2016-10-12 modified from server', res.BodyAsString);
|
|
|
|
|
|
|
|
|
|
// TDateTime, wrong and correct
|
|
|
|
|
res := RESTClient.doGET('/typed/tdatetime1/20161012121212', []);
|
2016-09-29 18:17:12 +02:00
|
|
|
|
CheckEquals(HTTP_STATUS.InternalServerError, res.ResponseCode,
|
|
|
|
|
'wrong TDateTime');
|
2016-06-22 17:49:16 +02:00
|
|
|
|
|
|
|
|
|
res := RESTClient.doGET('/typed/tdatetime1/2016-10-12 12:12:12', []);
|
|
|
|
|
CheckEquals(HTTP_STATUS.OK, res.ResponseCode);
|
|
|
|
|
CheckEquals('2016-10-12 12:12:12 modified from server', res.BodyAsString);
|
|
|
|
|
|
|
|
|
|
// TTime, wrong and correct
|
|
|
|
|
res := RESTClient.doGET('/typed/ttime1/121212', []);
|
|
|
|
|
CheckEquals(HTTP_STATUS.InternalServerError, res.ResponseCode, 'wrong TTime');
|
|
|
|
|
|
|
|
|
|
res := RESTClient.doGET('/typed/ttime1/12:12:12', []);
|
|
|
|
|
CheckEquals(HTTP_STATUS.OK, res.ResponseCode);
|
|
|
|
|
CheckEquals('12:12:12 modified from server', res.BodyAsString);
|
|
|
|
|
|
|
|
|
|
end;
|
|
|
|
|
|
2015-12-22 12:29:25 +01:00
|
|
|
|
procedure TBaseServerTest.DoLoginWith(UserName: string);
|
|
|
|
|
var
|
|
|
|
|
res: IRESTResponse;
|
|
|
|
|
begin
|
|
|
|
|
res := RESTClient.doGET('/login', [UserName]);
|
|
|
|
|
CheckTrue(res.ResponseCode = HTTP_STATUS.OK, 'Login Failed');
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
initialization
|
|
|
|
|
|
|
|
|
|
RegisterTest(TServerTest.Suite);
|
|
|
|
|
|
|
|
|
|
end.
|