2015-12-22 12:29:25 +01:00
|
|
|
|
unit MVCFramework.Tests.RESTClient;
|
|
|
|
|
|
|
|
|
|
interface
|
|
|
|
|
|
|
|
|
|
uses
|
|
|
|
|
TestFramework,
|
|
|
|
|
System.Classes,
|
|
|
|
|
System.SysUtils,
|
|
|
|
|
System.Generics.Collections,
|
2017-04-26 14:39:18 +02:00
|
|
|
|
// ObjectsMappers,
|
2015-12-22 12:29:25 +01:00
|
|
|
|
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,
|
2015-12-22 12:29:25 +01:00
|
|
|
|
MVCFramework.Tests.AppController;
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
TTestRESTClient = class(TTestCase)
|
|
|
|
|
strict private
|
2016-06-16 22:13:35 +02:00
|
|
|
|
FServerListener: IMVCListener;
|
2015-12-22 12:29:25 +01:00
|
|
|
|
FRESTClient: TRESTClient;
|
|
|
|
|
FRESTAdapter: TRESTAdapter<IAppResource>;
|
|
|
|
|
FAppResource: IAppResource;
|
|
|
|
|
protected
|
|
|
|
|
procedure SetUp; override;
|
|
|
|
|
procedure TearDown; override;
|
|
|
|
|
published
|
|
|
|
|
procedure TestCreateAndDestroy();
|
|
|
|
|
procedure TestInformation();
|
|
|
|
|
|
|
|
|
|
procedure TestHelloWorld();
|
|
|
|
|
procedure TestGetUser();
|
|
|
|
|
procedure TestPostUser();
|
|
|
|
|
procedure TestPostUsers();
|
|
|
|
|
procedure TestGetUsers();
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
implementation
|
|
|
|
|
|
|
|
|
|
uses
|
2017-04-26 14:39:18 +02:00
|
|
|
|
MVCFramework.Tests.WebModule1, MVCFramework.RESTClient.SystemJSONUtils,
|
|
|
|
|
MVCFramework.TypesAliases;
|
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
|
|
|
|
|
|
|
|
|
FRESTClient := TRESTClient.Create('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;
|
|
|
|
|
FRESTClient.Free;
|
2015-12-22 12:29:25 +01:00
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TTestRESTClient.TestCreateAndDestroy;
|
|
|
|
|
var
|
2016-06-16 22:13:35 +02:00
|
|
|
|
LClient: TRESTClient;
|
2015-12-22 12:29:25 +01:00
|
|
|
|
begin
|
2016-06-16 22:13:35 +02:00
|
|
|
|
LClient := TRESTClient.Create('', 80, nil);
|
|
|
|
|
CheckTrue(LClient <> nil);
|
|
|
|
|
FreeAndNil(LClient);
|
|
|
|
|
CheckTrue(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;
|
|
|
|
|
LResp: IRESTResponse;
|
2017-04-26 14:39:18 +02:00
|
|
|
|
lJObj: TJSONObject;
|
2015-12-22 12:29:25 +01:00
|
|
|
|
begin
|
|
|
|
|
FRESTClient.Resource('/user').Params([]);
|
2016-06-16 22:13:35 +02:00
|
|
|
|
FRESTClient.Authentication('dmvc', '123');
|
2015-12-22 12:29:25 +01:00
|
|
|
|
|
|
|
|
|
// String
|
2016-06-16 22:13:35 +02:00
|
|
|
|
LResp := FRESTClient.doGET;
|
2015-12-22 12:29:25 +01:00
|
|
|
|
CheckTrue(
|
2016-06-16 22:13:35 +02:00
|
|
|
|
('{"Cod":1,"Name":"Ezequiel","Pass":"123"}' = LResp.BodyAsString) and
|
|
|
|
|
(LResp.ResponseCode = 200)
|
2015-12-22 12:29:25 +01:00
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// Object
|
2017-04-26 14:39:18 +02:00
|
|
|
|
// lJObj := TSystemJSON.BodyAsJSONObject(FRESTClient.doGET);
|
|
|
|
|
|
|
|
|
|
LUser := TAppUser.Create; // TSystemJSON.BodyAsJSONObject(FRESTClient.doGET).BodyAsJSONObject.AsObject<TAppUser>();
|
2015-12-22 12:29:25 +01:00
|
|
|
|
try
|
2017-04-26 14:39:18 +02:00
|
|
|
|
GetDefaultSerializer.DeserializeObject(FRESTClient.doGET.BodyAsString, LUser);
|
2016-06-16 22:13:35 +02:00
|
|
|
|
CheckTrue((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
|
2016-06-16 22:13:35 +02:00
|
|
|
|
CheckTrue((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
|
|
|
|
|
FRESTClient.Resource('/users').Params([]);
|
2016-06-16 22:13:35 +02:00
|
|
|
|
FRESTClient.Authentication('dmvc', '123');
|
2015-12-22 12:29:25 +01:00
|
|
|
|
|
2017-04-26 14:39:18 +02:00
|
|
|
|
lBody := FRESTClient.doGET.BodyAsString;
|
2015-12-22 12:29:25 +01:00
|
|
|
|
// String
|
|
|
|
|
CheckEqualsString('[{"Cod":0,"Name":"Ezequiel 0","Pass":"0"},{"Cod":1,"Name":"Ezequiel 1","Pass":"1"},' +
|
|
|
|
|
'{"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;
|
|
|
|
|
CheckTrue(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;
|
|
|
|
|
CheckTrue(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
|
|
|
|
|
FRESTClient.Resource('/hello').Params([]);
|
2016-06-16 22:13:35 +02:00
|
|
|
|
FRESTClient.Authentication('dmvc', '123');
|
2015-12-22 12:29:25 +01:00
|
|
|
|
|
|
|
|
|
// String
|
2017-03-20 21:42:28 +01:00
|
|
|
|
CheckEqualsString('Hello World called with GET', FRESTClient.doGET.BodyAsString);
|
2015-12-22 12:29:25 +01:00
|
|
|
|
|
|
|
|
|
// Adapter
|
2017-03-20 21:42:28 +01:00
|
|
|
|
CheckEqualsString('Hello World called with GET', FAppResource.HelloWorld);
|
2015-12-22 12:29:25 +01:00
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TTestRESTClient.TestInformation;
|
|
|
|
|
var
|
2016-06-16 22:13:35 +02:00
|
|
|
|
LClient: TRESTClient;
|
2015-12-22 12:29:25 +01:00
|
|
|
|
begin
|
2016-06-16 22:13:35 +02:00
|
|
|
|
LClient := TRESTClient.Create('', 80, nil);
|
|
|
|
|
LClient
|
2015-12-22 12:29:25 +01:00
|
|
|
|
.ReadTimeOut(100)
|
|
|
|
|
.ConnectionTimeOut(100)
|
|
|
|
|
.Authentication('dmvc', 'dmvc', True)
|
|
|
|
|
.Accept(TMVCMediaType.APPLICATION_JSON)
|
|
|
|
|
.AcceptCharSet(TMVCCharset.UTF_8)
|
|
|
|
|
.ContentType(TMVCMediaType.APPLICATION_JSON)
|
|
|
|
|
.ContentCharSet(TMVCCharset.UTF_8)
|
|
|
|
|
.ContentEncoding(TMVCCharset.UTF_8)
|
|
|
|
|
.SSL
|
|
|
|
|
.Compression;
|
|
|
|
|
|
2016-06-16 22:13:35 +02:00
|
|
|
|
CheckTrue(LClient.ReadTimeOut = 100);
|
|
|
|
|
CheckTrue(LClient.ConnectionTimeOut = 100);
|
|
|
|
|
CheckTrue(LClient.Username = 'dmvc');
|
|
|
|
|
CheckTrue(LClient.Password = 'dmvc');
|
|
|
|
|
CheckTrue(LClient.UseBasicAuthentication);
|
|
|
|
|
CheckTrue(LClient.Accept = 'application/json;charset=UTF-8');
|
|
|
|
|
CheckTrue(LClient.ContentType = 'application/json;charset=UTF-8');
|
|
|
|
|
CheckTrue(LClient.ContentEncoding = 'UTF-8');
|
|
|
|
|
CheckTrue(LClient.HasSSL);
|
|
|
|
|
CheckTrue(LClient.HasCompression);
|
|
|
|
|
|
|
|
|
|
CheckTrue(LClient.RawBody <> nil);
|
|
|
|
|
CheckTrue(LClient.MultiPartFormData <> nil);
|
|
|
|
|
CheckTrue(LClient.BodyParams <> nil);
|
|
|
|
|
CheckTrue(LClient.RequestHeaders <> nil);
|
|
|
|
|
CheckTrue(LClient.QueryStringParams <> nil);
|
|
|
|
|
|
|
|
|
|
FreeAndNil(LClient);
|
2015-12-22 12:29:25 +01:00
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TTestRESTClient.TestPostUser;
|
|
|
|
|
var
|
2016-06-16 22:13:35 +02:00
|
|
|
|
LUser: TAppUser;
|
|
|
|
|
LResp: IRESTResponse;
|
2015-12-22 12:29:25 +01:00
|
|
|
|
begin
|
|
|
|
|
FRESTClient.Resource('/user/save').Params([]);
|
2016-06-16 22:13:35 +02:00
|
|
|
|
FRESTClient.Authentication('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';
|
|
|
|
|
LResp := FRESTClient.doPOST<TAppUser>(LUser);
|
2017-03-20 21:42:28 +01:00
|
|
|
|
CheckTrue(('Sucess!' = LResp.BodyAsString) and (LResp.ResponseCode = 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>;
|
|
|
|
|
LResp: IRESTResponse;
|
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
|
|
|
|
|
FRESTClient.Resource('/users/save').Params([]);
|
2016-06-16 22:13:35 +02:00
|
|
|
|
FRESTClient.Authentication('dmvc', '123');
|
2015-12-22 12:29:25 +01:00
|
|
|
|
FRESTClient.Accept('application/json;charset=utf-8');
|
|
|
|
|
FRESTClient.ContentType('application/json;charset=utf-8');
|
|
|
|
|
|
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
|
|
|
|
LResp := FRESTClient.doPOST<TAppUser>(LUsers);
|
2017-03-20 21:42:28 +01:00
|
|
|
|
CheckTrue(('Sucess!' = LResp.BodyAsString) and (LResp.ResponseCode = 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
|
|
|
|
|
|
|
|
|
|
RegisterTest(TTestRESTClient.Suite);
|
|
|
|
|
|
|
|
|
|
end.
|