2015-12-22 12:29:25 +01:00
|
|
|
|
unit MVCFramework.Tests.RESTClient;
|
|
|
|
|
|
|
|
|
|
interface
|
|
|
|
|
|
|
|
|
|
uses
|
2017-08-20 02:36:22 +02:00
|
|
|
|
DUnitX.TestFramework,
|
2015-12-22 12:29:25 +01:00
|
|
|
|
System.Classes,
|
|
|
|
|
System.SysUtils,
|
|
|
|
|
System.Generics.Collections,
|
|
|
|
|
MVCFramework,
|
|
|
|
|
MVCFramework.Server,
|
2016-06-16 22:13:35 +02:00
|
|
|
|
MVCFramework.Server.Impl,
|
2015-12-22 12:29:25 +01:00
|
|
|
|
MVCFramework.RESTClient,
|
|
|
|
|
MVCFramework.RESTAdapter,
|
|
|
|
|
MVCFramework.Commons,
|
2017-04-26 14:39:18 +02:00
|
|
|
|
MVCFramework.Serializer.Defaults,
|
|
|
|
|
MVCFramework.Serializer.Commons,
|
2020-09-22 00:31:26 +02:00
|
|
|
|
MVCFramework.Tests.AppController,
|
|
|
|
|
MVCFramework.RESTClient.Intf;
|
2015-12-22 12:29:25 +01:00
|
|
|
|
|
|
|
|
|
type
|
|
|
|
|
|
|
|
|
|
IAppResource = interface(IInvokable)
|
|
|
|
|
['{D139CD79-CFE5-49E3-8CFB-27686621911B}']
|
|
|
|
|
|
|
|
|
|
[RESTResource(TMVCHTTPMethodType.httpGET, '/hello')]
|
|
|
|
|
function HelloWorld(): string;
|
|
|
|
|
|
|
|
|
|
[RESTResource(TMVCHTTPMethodType.httpGET, '/user')]
|
|
|
|
|
function GetUser(): TAppUser;
|
|
|
|
|
|
|
|
|
|
[RESTResource(TMVCHTTPMethodType.httpPOST, '/user/save')]
|
|
|
|
|
procedure PostUser([Body] pBody: TAppUser);
|
|
|
|
|
|
|
|
|
|
[RESTResource(TMVCHTTPMethodType.httpGET, '/users')]
|
2017-04-26 14:39:18 +02:00
|
|
|
|
[MVCListOf(TAppUser)]
|
2015-12-22 12:29:25 +01:00
|
|
|
|
function GetUsers(): TObjectList<TAppUser>;
|
|
|
|
|
|
|
|
|
|
[RESTResource(TMVCHTTPMethodType.httpPOST, '/users/save')]
|
2017-04-26 14:39:18 +02:00
|
|
|
|
[MVCListOf(TAppUser)]
|
2015-12-22 12:29:25 +01:00
|
|
|
|
procedure PostUsers([Body] pBody: TObjectList<TAppUser>);
|
|
|
|
|
end;
|
|
|
|
|
|
2017-08-20 02:36:22 +02:00
|
|
|
|
[TestFixture]
|
|
|
|
|
TTestRESTClient = class(TObject)
|
2015-12-22 12:29:25 +01:00
|
|
|
|
strict private
|
2016-06-16 22:13:35 +02:00
|
|
|
|
FServerListener: IMVCListener;
|
2020-09-22 00:31:26 +02:00
|
|
|
|
FRESTClient: IMVCRESTClient;
|
2015-12-22 12:29:25 +01:00
|
|
|
|
FRESTAdapter: TRESTAdapter<IAppResource>;
|
|
|
|
|
FAppResource: IAppResource;
|
2017-08-21 15:31:31 +02:00
|
|
|
|
public
|
2020-05-25 19:34:14 +02:00
|
|
|
|
[Setup]
|
2017-08-20 02:36:22 +02:00
|
|
|
|
procedure SetUp;
|
|
|
|
|
[TearDown]
|
|
|
|
|
procedure TearDown;
|
|
|
|
|
[Test]
|
2015-12-22 12:29:25 +01:00
|
|
|
|
procedure TestCreateAndDestroy();
|
2017-08-20 02:36:22 +02:00
|
|
|
|
[Test]
|
2015-12-22 12:29:25 +01:00
|
|
|
|
procedure TestInformation();
|
2017-08-20 02:36:22 +02:00
|
|
|
|
[Test]
|
2015-12-22 12:29:25 +01:00
|
|
|
|
procedure TestHelloWorld();
|
2017-08-20 02:36:22 +02:00
|
|
|
|
[Test]
|
2015-12-22 12:29:25 +01:00
|
|
|
|
procedure TestGetUser();
|
2017-08-20 02:36:22 +02:00
|
|
|
|
[Test]
|
2015-12-22 12:29:25 +01:00
|
|
|
|
procedure TestPostUser();
|
2017-08-20 02:36:22 +02:00
|
|
|
|
[Test]
|
2015-12-22 12:29:25 +01:00
|
|
|
|
procedure TestPostUsers();
|
2017-08-20 02:36:22 +02:00
|
|
|
|
[Test]
|
2015-12-22 12:29:25 +01:00
|
|
|
|
procedure TestGetUsers();
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
implementation
|
|
|
|
|
|
|
|
|
|
uses
|
2017-04-29 23:56:56 +02:00
|
|
|
|
MVCFramework.Tests.WebModule1,
|
2020-05-25 19:34:14 +02:00
|
|
|
|
LiveServerTestU,
|
2017-04-29 23:56:56 +02:00
|
|
|
|
MVCFramework.SystemJSONUtils,
|
2018-11-24 16:56:21 +01:00
|
|
|
|
System.JSON;
|
2015-12-22 12:29:25 +01:00
|
|
|
|
|
|
|
|
|
{ TTestRESTClient }
|
|
|
|
|
|
|
|
|
|
procedure TTestRESTClient.SetUp;
|
|
|
|
|
begin
|
|
|
|
|
inherited;
|
2016-06-16 22:13:35 +02:00
|
|
|
|
FServerListener := TMVCListener.Create(TMVCListenerProperties.New
|
|
|
|
|
.SetName('AppServer')
|
|
|
|
|
.SetPort(3000)
|
|
|
|
|
.SetMaxConnections(1024)
|
|
|
|
|
.SetWebModuleClass(TestWebModuleClass)
|
|
|
|
|
);
|
|
|
|
|
FServerListener.Start;
|
2015-12-22 12:29:25 +01:00
|
|
|
|
|
2020-09-22 00:31:26 +02:00
|
|
|
|
FRESTClient := TMVCRESTClient.New.BaseURL('localhost', 3000);
|
2016-06-16 22:13:35 +02:00
|
|
|
|
|
2015-12-22 12:29:25 +01:00
|
|
|
|
FRESTAdapter := TRESTAdapter<IAppResource>.Create;
|
|
|
|
|
FRESTAdapter.Build(FRESTClient);
|
2016-06-16 22:13:35 +02:00
|
|
|
|
|
2015-12-22 12:29:25 +01:00
|
|
|
|
FAppResource := FRESTAdapter.ResourcesService;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TTestRESTClient.TearDown;
|
|
|
|
|
begin
|
|
|
|
|
inherited;
|
2016-06-16 22:13:35 +02:00
|
|
|
|
FServerListener.Stop;
|
2020-09-22 00:31:26 +02:00
|
|
|
|
FRESTClient := nil;
|
2015-12-22 12:29:25 +01:00
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TTestRESTClient.TestCreateAndDestroy;
|
|
|
|
|
var
|
2020-09-22 00:31:26 +02:00
|
|
|
|
LClient: IMVCRESTClient;
|
2015-12-22 12:29:25 +01:00
|
|
|
|
begin
|
2020-09-22 00:31:26 +02:00
|
|
|
|
LClient := TMVCRESTClient.New.BaseURL('', 80);
|
2017-08-20 02:36:22 +02:00
|
|
|
|
Assert.IsTrue(LClient <> nil);
|
2015-12-22 12:29:25 +01:00
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TTestRESTClient.TestGetUser;
|
|
|
|
|
var
|
2016-06-16 22:13:35 +02:00
|
|
|
|
LUser: TAppUser;
|
2020-09-22 00:31:26 +02:00
|
|
|
|
LResp: IMVCRESTResponse;
|
2015-12-22 12:29:25 +01:00
|
|
|
|
begin
|
2020-09-22 00:31:26 +02:00
|
|
|
|
FRESTClient.Resource('/user');
|
|
|
|
|
FRESTClient.SetBasicAuthorization('dmvc', '123');
|
2015-12-22 12:29:25 +01:00
|
|
|
|
|
|
|
|
|
// String
|
2020-09-22 00:31:26 +02:00
|
|
|
|
LResp := FRESTClient.Get;
|
2017-08-20 02:36:22 +02:00
|
|
|
|
Assert.IsTrue(
|
2020-09-22 00:31:26 +02:00
|
|
|
|
('{"Cod":1,"Name":"Ezequiel","Pass":"123"}' = LResp.Content) and
|
|
|
|
|
(LResp.StatusCode = 200)
|
2015-12-22 12:29:25 +01:00
|
|
|
|
);
|
|
|
|
|
|
2020-09-06 15:38:25 +02:00
|
|
|
|
// Object
|
|
|
|
|
// lJObj := TSystemJSON.BodyAsJSONObject(FRESTClient.doGET);
|
2017-04-26 14:39:18 +02:00
|
|
|
|
|
2020-09-06 15:38:25 +02:00
|
|
|
|
LUser := TAppUser.Create; // TSystemJSON.BodyAsJSONObject(FRESTClient.doGET).BodyAsJSONObject.AsObject<TAppUser>();
|
2015-12-22 12:29:25 +01:00
|
|
|
|
try
|
2020-09-22 00:31:26 +02:00
|
|
|
|
GetDefaultSerializer.DeserializeObject(FRESTClient.Get.Content, LUser);
|
2017-08-20 02:36:22 +02:00
|
|
|
|
Assert.IsTrue((LUser <> nil) and (LUser.Cod > 0));
|
2015-12-22 12:29:25 +01:00
|
|
|
|
finally
|
2016-06-16 22:13:35 +02:00
|
|
|
|
FreeAndNil(LUser);
|
2015-12-22 12:29:25 +01:00
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
// Adapter
|
2016-06-16 22:13:35 +02:00
|
|
|
|
LUser := FAppResource.GetUser;
|
2015-12-22 12:29:25 +01:00
|
|
|
|
try
|
2017-08-20 02:36:22 +02:00
|
|
|
|
Assert.IsTrue((LUser <> nil) and (LUser.Cod > 0));
|
2015-12-22 12:29:25 +01:00
|
|
|
|
finally
|
2016-06-16 22:13:35 +02:00
|
|
|
|
FreeAndNil(LUser);
|
2015-12-22 12:29:25 +01:00
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TTestRESTClient.TestGetUsers;
|
|
|
|
|
var
|
2016-06-16 22:13:35 +02:00
|
|
|
|
LUsers: TObjectList<TAppUser>;
|
2017-04-26 14:39:18 +02:00
|
|
|
|
lBody: string;
|
2015-12-22 12:29:25 +01:00
|
|
|
|
begin
|
2020-09-22 00:31:26 +02:00
|
|
|
|
FRESTClient.Resource('/users');
|
|
|
|
|
FRESTClient.SetBasicAuthorization('dmvc', '123');
|
2015-12-22 12:29:25 +01:00
|
|
|
|
|
2020-09-22 00:31:26 +02:00
|
|
|
|
lBody := FRESTClient.Get.Content;
|
2015-12-22 12:29:25 +01:00
|
|
|
|
// String
|
2017-08-20 02:36:22 +02:00
|
|
|
|
Assert.AreEqual('[{"Cod":0,"Name":"Ezequiel 0","Pass":"0"},{"Cod":1,"Name":"Ezequiel 1","Pass":"1"},' +
|
2015-12-22 12:29:25 +01:00
|
|
|
|
'{"Cod":2,"Name":"Ezequiel 2","Pass":"2"},{"Cod":3,"Name":"Ezequiel 3","Pass":"3"},{"Cod":4,"Name":"Ezequiel 4","Pass":"4"},' +
|
|
|
|
|
'{"Cod":5,"Name":"Ezequiel 5","Pass":"5"},{"Cod":6,"Name":"Ezequiel 6","Pass":"6"},{"Cod":7,"Name":"Ezequiel 7","Pass":"7"},' +
|
|
|
|
|
'{"Cod":8,"Name":"Ezequiel 8","Pass":"8"},{"Cod":9,"Name":"Ezequiel 9","Pass":"9"},{"Cod":10,"Name":"Ezequiel 10","Pass":"10"}]',
|
2017-04-26 14:39:18 +02:00
|
|
|
|
lBody);
|
2015-12-22 12:29:25 +01:00
|
|
|
|
|
|
|
|
|
// Objects
|
2017-04-26 14:39:18 +02:00
|
|
|
|
LUsers := TObjectList<TAppUser>.Create(True);
|
2015-12-22 12:29:25 +01:00
|
|
|
|
try
|
2017-04-26 14:39:18 +02:00
|
|
|
|
GetDefaultSerializer.DeserializeCollection(lBody, lUsers, TAppUser); // BodyAsJSONArray.AsObjectList<TAppUser>;
|
2016-06-16 22:13:35 +02:00
|
|
|
|
LUsers.OwnsObjects := True;
|
2017-08-20 02:36:22 +02:00
|
|
|
|
Assert.IsTrue(LUsers.Count > 0);
|
2015-12-22 12:29:25 +01:00
|
|
|
|
finally
|
2016-06-16 22:13:35 +02:00
|
|
|
|
FreeAndNil(LUsers);
|
2015-12-22 12:29:25 +01:00
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
// Adapter
|
2016-06-16 22:13:35 +02:00
|
|
|
|
LUsers := FAppResource.GetUsers;
|
2015-12-22 12:29:25 +01:00
|
|
|
|
try
|
2016-06-16 22:13:35 +02:00
|
|
|
|
LUsers.OwnsObjects := True;
|
2017-08-20 02:36:22 +02:00
|
|
|
|
Assert.IsTrue(LUsers.Count > 0);
|
2015-12-22 12:29:25 +01:00
|
|
|
|
finally
|
2016-06-16 22:13:35 +02:00
|
|
|
|
FreeAndNil(LUsers);
|
2015-12-22 12:29:25 +01:00
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TTestRESTClient.TestHelloWorld;
|
|
|
|
|
begin
|
2020-09-22 00:31:26 +02:00
|
|
|
|
FRESTClient.Resource('/hello');
|
|
|
|
|
FRESTClient.SetBasicAuthorization('dmvc', '123');
|
2015-12-22 12:29:25 +01:00
|
|
|
|
|
|
|
|
|
// String
|
2020-09-22 00:31:26 +02:00
|
|
|
|
Assert.AreEqual('Hello World called with GET', FRESTClient.Get.Content);
|
2015-12-22 12:29:25 +01:00
|
|
|
|
|
|
|
|
|
// Adapter
|
2017-08-20 02:36:22 +02:00
|
|
|
|
Assert.AreEqual('Hello World called with GET', FAppResource.HelloWorld);
|
2015-12-22 12:29:25 +01:00
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TTestRESTClient.TestInformation;
|
2020-09-22 00:31:26 +02:00
|
|
|
|
const
|
|
|
|
|
JWT_TOKEN =
|
|
|
|
|
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.'+
|
|
|
|
|
'eyJzdWIiOiIxMjMiLCJuYW1lIjoiVXNlciIsImlhdCI6MTUxNjIzOTAyMn0.'+
|
|
|
|
|
'EdtbBz7jOucEVf5AV-wD_NwqlJzoCdZKYmMa6p54PVY';
|
|
|
|
|
|
2015-12-22 12:29:25 +01:00
|
|
|
|
var
|
2020-09-22 00:31:26 +02:00
|
|
|
|
LClient: IMVCRESTClient;
|
2015-12-22 12:29:25 +01:00
|
|
|
|
begin
|
2020-09-22 00:31:26 +02:00
|
|
|
|
LClient := TMVCRESTClient.New.BaseURL('localhost', 8080);
|
2016-06-16 22:13:35 +02:00
|
|
|
|
LClient
|
2020-09-06 15:38:25 +02:00
|
|
|
|
.ReadTimeOut(100)
|
2020-09-22 00:31:26 +02:00
|
|
|
|
.ConnectTimeout(100)
|
|
|
|
|
.SetBasicAuthorization('dmvc', 'dmvc')
|
2015-12-22 12:29:25 +01:00
|
|
|
|
.Accept(TMVCMediaType.APPLICATION_JSON)
|
2020-09-06 15:38:25 +02:00
|
|
|
|
.AcceptCharSet(TMVCCharset.UTF_8)
|
2020-09-22 00:31:26 +02:00
|
|
|
|
.AcceptEncoding('gzip,deflate')
|
|
|
|
|
.UserAgent('DMVCFramework RESTClient')
|
|
|
|
|
.SetBasicAuthorization('user', 'password');
|
|
|
|
|
|
|
|
|
|
Assert.AreEqual('http://localhost:8080', LClient.BaseURL);
|
|
|
|
|
Assert.AreEqual(100, LClient.ReadTimeOut);
|
|
|
|
|
Assert.AreEqual(100, LClient.ConnectTimeout);
|
|
|
|
|
Assert.AreEqual('application/json', LClient.Accept);
|
|
|
|
|
Assert.AreEqual('UTF-8', LClient.AcceptCharset );
|
|
|
|
|
Assert.AreEqual('gzip,deflate', LClient.AcceptEncoding);
|
|
|
|
|
Assert.AreEqual('DMVCFramework RESTClient', LClient.UserAgent);
|
|
|
|
|
Assert.AreEqual('Basic dXNlcjpwYXNzb3dvcmQ=', LClient.HeaderValue('Authorization'));
|
|
|
|
|
|
|
|
|
|
LClient.SetBearerAuthorization(JWT_TOKEN);
|
|
|
|
|
|
|
|
|
|
Assert.AreEqual('Bearer ' + JWT_TOKEN, LClient.HeaderValue('Authorization'));
|
2015-12-22 12:29:25 +01:00
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TTestRESTClient.TestPostUser;
|
|
|
|
|
var
|
2016-06-16 22:13:35 +02:00
|
|
|
|
LUser: TAppUser;
|
2020-09-22 00:31:26 +02:00
|
|
|
|
LResp: IMVCRESTResponse;
|
2015-12-22 12:29:25 +01:00
|
|
|
|
begin
|
2020-09-22 00:31:26 +02:00
|
|
|
|
FRESTClient.Resource('/user/save');
|
|
|
|
|
FRESTClient.SetBasicAuthorization('dmvc', '123');
|
2015-12-22 12:29:25 +01:00
|
|
|
|
|
2016-06-16 22:13:35 +02:00
|
|
|
|
LUser := TAppUser.Create;
|
|
|
|
|
LUser.Cod := 1;
|
|
|
|
|
LUser.Name := 'Ezequiel';
|
|
|
|
|
LUser.Pass := '123';
|
2020-09-22 00:31:26 +02:00
|
|
|
|
LResp := FRESTClient.AddBody(LUser).Post;
|
|
|
|
|
Assert.IsTrue(('Success!' = LResp.Content) and (LResp.StatusCode = 200));
|
2015-12-22 12:29:25 +01:00
|
|
|
|
|
|
|
|
|
// Adapter
|
2016-06-16 22:13:35 +02:00
|
|
|
|
LUser := TAppUser.Create;
|
|
|
|
|
LUser.Cod := 1;
|
|
|
|
|
LUser.Name := 'Ezequiel';
|
|
|
|
|
LUser.Pass := '123';
|
|
|
|
|
FAppResource.PostUser(LUser);
|
2015-12-22 12:29:25 +01:00
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TTestRESTClient.TestPostUsers;
|
|
|
|
|
var
|
2016-06-16 22:13:35 +02:00
|
|
|
|
LUsers: TObjectList<TAppUser>;
|
2020-09-22 00:31:26 +02:00
|
|
|
|
LResp: IMVCRESTResponse;
|
2015-12-22 12:29:25 +01:00
|
|
|
|
I: Integer;
|
2016-06-16 22:13:35 +02:00
|
|
|
|
LUser: TAppUser;
|
2015-12-22 12:29:25 +01:00
|
|
|
|
begin
|
2020-09-22 00:31:26 +02:00
|
|
|
|
FRESTClient.Resource('/users/save');
|
|
|
|
|
FRESTClient.SetBasicAuthorization('dmvc', '123');
|
2020-09-06 15:38:25 +02:00
|
|
|
|
FRESTClient.Accept('application/json;charset=utf-8');
|
2015-12-22 12:29:25 +01:00
|
|
|
|
|
2016-06-16 22:13:35 +02:00
|
|
|
|
LUsers := TObjectList<TAppUser>.Create(True);
|
2017-06-22 16:18:58 +02:00
|
|
|
|
try
|
|
|
|
|
for I := 0 to 10 do
|
|
|
|
|
begin
|
|
|
|
|
LUser := TAppUser.Create;
|
|
|
|
|
LUser.Cod := I;
|
|
|
|
|
LUser.Name := 'Ezequiel <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>s ' + IntToStr(I);
|
|
|
|
|
LUser.Pass := IntToStr(I);
|
|
|
|
|
LUsers.Add(LUser);
|
|
|
|
|
end;
|
2020-09-22 00:31:26 +02:00
|
|
|
|
LResp := FRESTClient.AddBody(LUsers, False).Post;
|
2017-06-22 16:18:58 +02:00
|
|
|
|
finally
|
|
|
|
|
LUsers.Free;
|
2015-12-22 12:29:25 +01:00
|
|
|
|
end;
|
2020-09-22 00:31:26 +02:00
|
|
|
|
Assert.IsTrue(('Success!' = LResp.Content) and (LResp.StatusCode = 200));
|
2015-12-22 12:29:25 +01:00
|
|
|
|
|
|
|
|
|
// Adapter
|
2016-06-16 22:13:35 +02:00
|
|
|
|
LUsers := TObjectList<TAppUser>.Create(True);
|
2015-12-22 12:29:25 +01:00
|
|
|
|
for I := 0 to 10 do
|
|
|
|
|
begin
|
2016-06-16 22:13:35 +02:00
|
|
|
|
LUser := TAppUser.Create;
|
|
|
|
|
LUser.Cod := I;
|
|
|
|
|
LUser.Name := 'Ezequiel <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>s ' + IntToStr(I);
|
|
|
|
|
LUser.Pass := IntToStr(I);
|
|
|
|
|
LUsers.Add(LUser);
|
2015-12-22 12:29:25 +01:00
|
|
|
|
end;
|
2016-06-16 22:13:35 +02:00
|
|
|
|
FAppResource.PostUsers(LUsers);
|
2015-12-22 12:29:25 +01:00
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
initialization
|
|
|
|
|
|
2017-08-20 02:36:22 +02:00
|
|
|
|
TDUnitX.RegisterTestFixture(TTestRESTClient);
|
2015-12-22 12:29:25 +01:00
|
|
|
|
|
|
|
|
|
end.
|