[+] improved unit test architecture

This commit is contained in:
Daniele Teti 2021-02-10 11:26:14 +01:00
parent 618a087c86
commit 088b8b3240
6 changed files with 3 additions and 245 deletions

View File

@ -2,7 +2,7 @@
//
// Delphi MVC Framework
//
// Copyright (c) 2010-2020 Daniele Teti and the DMVCFramework Team
// Copyright (c) 2010-2021 Daniele Teti and the DMVCFramework Team
//
// https://github.com/danieleteti/delphimvcframework
//

View File

@ -2,7 +2,7 @@
//
// Delphi MVC Framework
//
// Copyright (c) 2010-2020 Daniele Teti and the DMVCFramework Team
// Copyright (c) 2010-2021 Daniele Teti and the DMVCFramework Team
//
// https://github.com/danieleteti/delphimvcframework
//

View File

@ -2,7 +2,7 @@
//
// Delphi MVC Framework
//
// Copyright (c) 2010-2020 Daniele Teti and the DMVCFramework Team
// Copyright (c) 2010-2021 Daniele Teti and the DMVCFramework Team
//
// https://github.com/danieleteti/delphimvcframework
//

View File

@ -1,163 +0,0 @@
unit MVCFramework.Tests.StandaloneServer;
interface
uses
DUnitX.TestFramework,
System.Classes,
System.SysUtils,
System.Generics.Collections,
MVCFramework,
MVCFramework.Commons,
MVCFramework.Server,
MVCFramework.Server.Impl;
type
[MVCPath('/')]
TTestController = class(TMVCController)
public
[MVCPath('/hello')]
[MVCHTTPMethod([httpGET])]
procedure HelloWorld(ctx: TWebContext);
end;
[TestFixture]
TTestMVCFrameworkServer = class(TObject)
private
protected
[SetUp]
procedure SetUp;
[TearDown]
procedure TearDown;
public
[Test]
procedure TestListener;
[Test]
procedure TestListenerContext;
[Test]
procedure TestServerListenerAndClient;
end;
implementation
uses
MVCFramework.Tests.WebModule2,
MVCFramework.RESTClient,
MVCFramework.RESTClient.Intf;
{ TTestMVCFrameworkServer }
procedure TTestMVCFrameworkServer.SetUp;
begin
inherited;
end;
procedure TTestMVCFrameworkServer.TearDown;
begin
inherited;
end;
procedure TTestMVCFrameworkServer.TestListener;
var
LListener: IMVCListener;
begin
LListener := TMVCListener.Create(TMVCListenerProperties.New
.SetName('Listener1')
.SetPort(5000)
.SetMaxConnections(512)
.SetWebModuleClass(TestWebModuleClass)
);
Assert.isTrue(Assigned(LListener));
LListener.Start;
Assert.isTrue(LListener.Active);
LListener.Stop;
Assert.isFalse(LListener.Active);
end;
procedure TTestMVCFrameworkServer.TestServerListenerAndClient;
var
LListener: IMVCListener;
LClient: IMVCRESTClient;
begin
LListener := TMVCListener.Create(TMVCListenerProperties.New
.SetName('Listener1')
.SetPort(6000)
.SetMaxConnections(1024)
.SetWebModuleClass(TestWebModuleClass)
);
Assert.isTrue(Assigned(LListener));
LListener.Start;
Assert.isTrue(LListener.Active);
LClient := TMVCRESTClient.New.BaseURL('localhost', 6000);
LClient.SetBasicAuthorization('dmvc', '123');
Assert.AreEqual('Hello World called with GET', LClient.Get('/hello').Content);
LListener.Stop;
Assert.isFalse(LListener.Active);
end;
procedure TTestMVCFrameworkServer.TestListenerContext;
var
LListenerCtx: IMVCListenersContext;
begin
LListenerCtx := TMVCListenersContext.Create;
LListenerCtx.Add(TMVCListenerProperties.New
.SetName('Listener2')
.SetPort(6000)
.SetMaxConnections(1024)
.SetWebModuleClass(TestWebModuleClass)
);
LListenerCtx.Add(TMVCListenerProperties.New
.SetName('Listener3')
.SetPort(7000)
.SetMaxConnections(1024)
.SetWebModuleClass(TestWebModuleClass2)
);
Assert.isTrue(Assigned(LListenerCtx.FindByName('Listener2')));
Assert.isTrue(Assigned(LListenerCtx.FindByName('Listener3')));
LListenerCtx.StartAll;
Assert.isTrue(LListenerCtx.Count = 2);
Assert.isTrue(LListenerCtx.FindByName('Listener2').Active);
Assert.isTrue(LListenerCtx.FindByName('Listener3').Active);
LListenerCtx.StopAll;
Assert.isFalse(LListenerCtx.FindByName('Listener2').Active);
Assert.isFalse(LListenerCtx.FindByName('Listener3').Active);
LListenerCtx
.Remove('Listener2')
.Remove('Listener3');
Assert.isTrue(LListenerCtx.Count = 0);
end;
{ TTestController }
procedure TTestController.HelloWorld(ctx: TWebContext);
begin
Render('Hello World called with GET');
end;
initialization
TDUnitX.RegisterTestFixture(TTestMVCFrameworkServer);
end.

View File

@ -1,13 +0,0 @@
object TestWebModule2: TTestWebModule2
OldCreateOrder = False
OnCreate = WebModuleCreate
OnDestroy = WebModuleDestroy
Actions = <
item
Default = True
Name = 'DefaultHandler'
PathInfo = '/'
end>
Height = 230
Width = 415
end

View File

@ -1,66 +0,0 @@
unit MVCFramework.Tests.WebModule2;
interface
uses
System.SysUtils,
System.Classes,
System.Generics.Collections,
Web.HTTPApp,
MVCFramework;
type
TTestWebModule2 = class(TWebModule)
procedure WebModuleCreate(Sender: TObject);
procedure WebModuleDestroy(Sender: TObject);
private
FMVCEngine: TMVCEngine;
public
{ Public declarations }
end;
var
TestWebModuleClass: TComponentClass = TTestWebModule2;
TestWebModuleClass2: TComponentClass = TTestWebModule2;
implementation
uses
MVCFramework.Tests.StandaloneServer,
MVCFramework.Middleware.Authentication,
MVCFramework.Server,
MVCFramework.Server.Impl;
{$R *.dfm}
procedure TTestWebModule2.WebModuleCreate(Sender: TObject);
begin
FMVCEngine := TMVCEngine.Create(Self);
// Add With Delegate Constructor Controller
FMVCEngine.AddController(TTestController,
function: TMVCController
begin
Result := TTestController.Create;
end
);
FMVCEngine.AddMiddleware(TMVCBasicAuthenticationMiddleware.Create(
TMVCDefaultAuthenticationHandler.New
.SetOnAuthentication(
procedure(const AUserName, APassword: string;
AUserRoles: TList<string>; var IsValid: Boolean; const ASessionData: TDictionary<String, String>)
begin
IsValid := AUserName.Equals('dmvc') and APassword.Equals('123');
end
)
));
end;
procedure TTestWebModule2.WebModuleDestroy(Sender: TObject);
begin
FMVCEngine.Free;
end;
end.