2021-02-04 14:11:33 +01:00
|
|
|
|
// ***************************************************************************
|
|
|
|
|
//
|
|
|
|
|
// Delphi MVC Framework
|
|
|
|
|
//
|
|
|
|
|
// Copyright (c) 2010-2020 Daniele Teti and the DMVCFramework Team
|
|
|
|
|
//
|
|
|
|
|
// 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 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();
|
2020-09-24 01:50:19 +02:00
|
|
|
|
[Test]
|
|
|
|
|
procedure TestUploadFile();
|
|
|
|
|
[Test]
|
|
|
|
|
procedure TestBodyURLEncoded();
|
2020-12-17 00:05:39 +01:00
|
|
|
|
[Test]
|
|
|
|
|
procedure TestRequestHooksProc();
|
2015-12-22 12:29:25 +01:00
|
|
|
|
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,
|
2020-09-24 01:50:19 +02:00
|
|
|
|
System.JSON,
|
|
|
|
|
System.IOUtils,
|
2020-09-24 20:03:11 +02:00
|
|
|
|
JsonDataObjects, System.Hash, Vcl.Graphics;
|
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-24 20:03:11 +02:00
|
|
|
|
FRESTClient := TMVCRESTClient.New
|
|
|
|
|
.BaseURL('localhost', 3000)
|
|
|
|
|
.SetBasicAuthorization('dmvc', '123'); // Set the authorization only once and it will be stored for all requests
|
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;
|
|
|
|
|
|
2020-12-17 00:05:39 +01:00
|
|
|
|
procedure TTestRESTClient.TestRequestHooksProc;
|
|
|
|
|
var
|
|
|
|
|
lResponse: IMVCRESTResponse;
|
|
|
|
|
begin
|
|
|
|
|
lResponse := FRESTClient
|
|
|
|
|
.Resource('/hello')
|
|
|
|
|
.SetBeforeRequestProc(
|
|
|
|
|
procedure(aRequest: IHTTPRequest)
|
|
|
|
|
begin
|
|
|
|
|
Assert.AreEqual('/hello', aRequest.URL.Path);
|
|
|
|
|
end
|
|
|
|
|
)
|
|
|
|
|
.SetRequestCompletedProc(
|
|
|
|
|
procedure (aResponse: IHTTPResponse; var aHandled: Boolean)
|
|
|
|
|
begin
|
|
|
|
|
Assert.IsTrue(aResponse.ContentLength > 0);
|
|
|
|
|
// Set Handled to True to not process TMVCRESTResponse
|
|
|
|
|
aHandled := True;
|
|
|
|
|
end
|
|
|
|
|
)
|
|
|
|
|
.Get;
|
|
|
|
|
Assert.IsNull(lResponse);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
lResponse := FRESTClient
|
|
|
|
|
.Resource('/hello')
|
|
|
|
|
.SetRequestCompletedProc(
|
|
|
|
|
procedure (aResponse: IHTTPResponse; var aHandled: Boolean)
|
|
|
|
|
begin
|
|
|
|
|
Assert.IsTrue(aResponse.ContentLength > 0);
|
|
|
|
|
end
|
|
|
|
|
)
|
|
|
|
|
.SetResponseCompletedProc(
|
|
|
|
|
procedure(aResponse: IMVCRESTResponse)
|
|
|
|
|
begin
|
|
|
|
|
Assert.AreEqual('Hello World called with GET', aResponse.Content);
|
|
|
|
|
end
|
|
|
|
|
)
|
|
|
|
|
.Get;
|
|
|
|
|
end;
|
|
|
|
|
|
2020-09-24 01:50:19 +02:00
|
|
|
|
procedure TTestRESTClient.TestBodyURLEncoded;
|
|
|
|
|
var
|
|
|
|
|
lResp: IMVCRESTResponse;
|
|
|
|
|
lJsonResp: TJDOJsonObject;
|
|
|
|
|
begin
|
|
|
|
|
lResp := FRESTClient
|
2020-09-24 20:03:11 +02:00
|
|
|
|
// .SetBasicAuthorization('dmvc', '123')
|
2020-09-24 01:50:19 +02:00
|
|
|
|
.AddBodyFieldURLEncoded('field1', 'value1')
|
|
|
|
|
.AddBodyFieldURLEncoded('field2', 'Jo<4A>o Ant<6E>nio')
|
|
|
|
|
.AddBodyFieldURLEncoded('field3', 'Special characters: <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>s')
|
|
|
|
|
.Post('/body-url-encoded');
|
|
|
|
|
|
|
|
|
|
Assert.AreEqual(lResp.StatusCode, 200);
|
|
|
|
|
|
|
|
|
|
lJsonResp := TJDOJsonBaseObject.Parse(lResp.Content) as TJDOJsonObject;
|
|
|
|
|
try
|
|
|
|
|
Assert.AreEqual('value1', lJsonResp.S['field1']);
|
|
|
|
|
Assert.AreEqual('Jo<4A>o Ant<6E>nio', lJsonResp.S['field2']);
|
|
|
|
|
Assert.AreEqual('Special characters: <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>s', lJsonResp.S['field3']);
|
|
|
|
|
finally
|
|
|
|
|
lJsonResp.Free;
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
|
2015-12-22 12:29:25 +01:00
|
|
|
|
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');
|
2020-09-24 20:03:11 +02:00
|
|
|
|
// 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-24 20:03:11 +02:00
|
|
|
|
LUser := TAppUser.Create;
|
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');
|
2020-09-24 20:03:11 +02:00
|
|
|
|
// 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');
|
2020-09-24 20:03:11 +02:00
|
|
|
|
// 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
|
2020-09-24 20:03:11 +02:00
|
|
|
|
JWT_TOKEN =
|
|
|
|
|
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.' +
|
|
|
|
|
'eyJzdWIiOiIxMjMiLCJuYW1lIjoiVXNlciIsImlhdCI6MTUxNjIzOTAyMn0.' +
|
|
|
|
|
'EdtbBz7jOucEVf5AV-wD_NwqlJzoCdZKYmMa6p54PVY';
|
2020-09-22 00:31:26 +02:00
|
|
|
|
|
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)
|
2020-09-24 20:03:11 +02:00
|
|
|
|
.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')
|
2020-09-24 20:03:11 +02:00
|
|
|
|
.SetBasicAuthorization('username', 'password');
|
2020-09-22 00:31:26 +02:00
|
|
|
|
|
|
|
|
|
Assert.AreEqual('http://localhost:8080', LClient.BaseURL);
|
|
|
|
|
Assert.AreEqual(100, LClient.ReadTimeOut);
|
|
|
|
|
Assert.AreEqual(100, LClient.ConnectTimeout);
|
|
|
|
|
Assert.AreEqual('application/json', LClient.Accept);
|
2020-09-24 20:03:11 +02:00
|
|
|
|
Assert.AreEqual('UTF-8', LClient.AcceptCharset);
|
2020-09-22 00:31:26 +02:00
|
|
|
|
Assert.AreEqual('gzip,deflate', LClient.AcceptEncoding);
|
|
|
|
|
Assert.AreEqual('DMVCFramework RESTClient', LClient.UserAgent);
|
2020-09-24 20:03:11 +02:00
|
|
|
|
Assert.AreEqual('Basic dXNlcm5hbWU6cGFzc3dvcmQ=', LClient.Authorization);
|
2020-09-22 00:31:26 +02:00
|
|
|
|
|
2020-09-24 20:03:11 +02:00
|
|
|
|
LClient.SetBearerAuthorization(JWT_TOKEN);
|
2020-09-22 00:31:26 +02:00
|
|
|
|
|
2020-09-24 20:03:11 +02:00
|
|
|
|
Assert.AreEqual('Bearer ' + JWT_TOKEN, LClient.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');
|
2020-09-24 20:03:11 +02:00
|
|
|
|
// 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');
|
2020-09-24 20:03:11 +02:00
|
|
|
|
// 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;
|
|
|
|
|
|
2020-09-24 01:50:19 +02:00
|
|
|
|
procedure TTestRESTClient.TestUploadFile;
|
|
|
|
|
const
|
|
|
|
|
TEXT_SAMPLE = 'This is a simple text for testing RESTClient file uploads.';
|
|
|
|
|
var
|
|
|
|
|
lStringFile: TStringStream;
|
|
|
|
|
lFilename: string;
|
|
|
|
|
lResp: IMVCRESTResponse;
|
2020-09-24 20:03:11 +02:00
|
|
|
|
lBitmap: TBitmap;
|
2020-09-24 01:50:19 +02:00
|
|
|
|
begin
|
2020-09-24 20:03:11 +02:00
|
|
|
|
// Text File
|
|
|
|
|
lFilename := ExtractFilePath(ParamStr(0)) + 'text_file_upload_sample.txt';
|
2020-09-24 01:50:19 +02:00
|
|
|
|
if TFile.Exists(lFilename) then
|
|
|
|
|
TFile.Delete(lFilename);
|
|
|
|
|
|
|
|
|
|
lStringFile := TStringStream.Create(TEXT_SAMPLE);
|
|
|
|
|
try
|
2020-09-24 20:03:11 +02:00
|
|
|
|
lStringFile.SaveToFile(lFilename);
|
|
|
|
|
finally
|
|
|
|
|
lStringFile.Free;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
lResp := FRESTClient
|
|
|
|
|
.AddFile(lFilename)
|
|
|
|
|
.Post('/file/upload');
|
|
|
|
|
|
|
|
|
|
Assert.AreEqual(lResp.StatusCode, 200);
|
|
|
|
|
Assert.AreEqual(THashMD5.GetHashStringFromFile(lFilename), lResp.Content);
|
|
|
|
|
|
|
|
|
|
// Image File
|
|
|
|
|
lFilename := ExtractFilePath(ParamStr(0)) + 'bitmap_file_upload_sample.bmp';
|
|
|
|
|
if TFile.Exists(lFilename) then
|
|
|
|
|
TFile.Delete(lFilename);
|
|
|
|
|
|
|
|
|
|
lBitmap := TBitmap.Create;
|
|
|
|
|
try
|
|
|
|
|
lBitmap.SetSize(200, 200);
|
|
|
|
|
lBitmap.Canvas.Brush.Color := clBlue;
|
|
|
|
|
lBitmap.Canvas.FillRect(Rect(0, 0, 200, 200));
|
|
|
|
|
lBitmap.Canvas.Font.Color := clRed;
|
|
|
|
|
lBitmap.Canvas.Font.Style := [fsBold];
|
|
|
|
|
lBitmap.Canvas.TextOut(10, 100, 'DelphiMVCFramework');
|
2020-12-13 00:36:55 +01:00
|
|
|
|
lBitmap.SaveToFile(lFilename);
|
2020-09-24 01:50:19 +02:00
|
|
|
|
finally
|
2020-09-24 20:03:11 +02:00
|
|
|
|
lBitmap.Free;
|
2020-09-24 01:50:19 +02:00
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
lResp := FRESTClient
|
2020-09-24 20:03:11 +02:00
|
|
|
|
.AddFile(lFilename)
|
2020-09-24 01:50:19 +02:00
|
|
|
|
.Post('/file/upload');
|
|
|
|
|
|
|
|
|
|
Assert.AreEqual(lResp.StatusCode, 200);
|
2020-09-24 20:03:11 +02:00
|
|
|
|
Assert.AreEqual(THashMD5.GetHashStringFromFile(lFilename), lResp.Content);
|
2020-09-24 01:50:19 +02:00
|
|
|
|
end;
|
|
|
|
|
|
2015-12-22 12:29:25 +01:00
|
|
|
|
initialization
|
|
|
|
|
|
2017-08-20 02:36:22 +02:00
|
|
|
|
TDUnitX.RegisterTestFixture(TTestRESTClient);
|
2015-12-22 12:29:25 +01:00
|
|
|
|
|
|
|
|
|
end.
|