delphimvcframework/unittests/general/Several/StandaloneServerTestU.pas

142 lines
3.2 KiB
ObjectPascal
Raw Normal View History

2021-02-04 14:11:33 +01:00
unit StandaloneServerTestU;
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]
TTestServerContainer = class(TObject)
protected
[SetUp]
procedure SetUp;
[TearDown]
procedure TearDown;
public
[Test]
procedure TestListener;
[Test]
procedure TestListenerContext;
[Test]
procedure TestServerListenerAndClient;
end;
implementation
uses
MVCFramework.RESTClient,
MVCFramework.RESTClient.Intf,
StandAloneServerWebModuleTest;
{ TTestServerContainer }
procedure TTestServerContainer.SetUp;
begin
inherited;
end;
procedure TTestServerContainer.TearDown;
begin
inherited;
end;
procedure TTestServerContainer.TestListener;
var
lListener: IMVCListener;
begin
lListener := TMVCListener.Create(TMVCListenerProperties.New.SetName('Listener1').SetPort(5000).SetMaxConnections(512)
.SetWebModuleClass(TestWebModuleClass));
2021-02-04 14:11:33 +01:00
Assert.IsTrue(Assigned(lListener));
2021-02-04 14:11:33 +01:00
lListener.Start;
Assert.IsTrue(lListener.Active);
2021-02-04 14:11:33 +01:00
lListener.Stop;
Assert.IsFalse(lListener.Active);
2021-02-04 14:11:33 +01:00
end;
procedure TTestServerContainer.TestServerListenerAndClient;
var
lListener: IMVCListener;
2021-02-04 14:11:33 +01:00
LClient: IMVCRESTClient;
begin
lListener := TMVCListener.Create(TMVCListenerProperties.New.SetName('Listener1').SetPort(6000).SetMaxConnections(1024)
.SetWebModuleClass(TestWebModuleClass));
2021-02-04 14:11:33 +01:00
Assert.IsTrue(Assigned(lListener));
2021-02-04 14:11:33 +01:00
lListener.Start;
Assert.IsTrue(lListener.Active);
2021-02-04 14:11:33 +01:00
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);
2021-02-04 14:11:33 +01:00
end;
procedure TTestServerContainer.TestListenerContext;
var
LListenerCtx: IMVCListenersContext;
begin
LListenerCtx := TMVCListenersContext.Create;
LListenerCtx.Add(TMVCListenerProperties.New.SetName('Listener2').SetPort(6000).SetMaxConnections(1024)
.SetWebModuleClass(TestWebModuleClass));
2021-02-04 14:11:33 +01:00
LListenerCtx.Add(TMVCListenerProperties.New.SetName('Listener3').SetPort(7000).SetMaxConnections(1024)
.SetWebModuleClass(TestWebModuleClass2));
2021-02-04 14:11:33 +01:00
Assert.IsTrue(Assigned(LListenerCtx.FindByName('Listener2')));
Assert.IsTrue(Assigned(LListenerCtx.FindByName('Listener3')));
2021-02-04 14:11:33 +01:00
LListenerCtx.StartAll;
Assert.IsTrue(LListenerCtx.Count = 2);
Assert.IsTrue(LListenerCtx.FindByName('Listener2').Active);
Assert.IsTrue(LListenerCtx.FindByName('Listener3').Active);
2021-02-04 14:11:33 +01:00
LListenerCtx.StopAll;
Assert.IsFalse(LListenerCtx.FindByName('Listener2').Active);
Assert.IsFalse(LListenerCtx.FindByName('Listener3').Active);
2021-02-04 14:11:33 +01:00
LListenerCtx.Remove('Listener2').Remove('Listener3');
2021-02-04 14:11:33 +01:00
Assert.IsTrue(LListenerCtx.Count = 0);
2021-02-04 14:11:33 +01:00
end;
{ TTestController }
procedure TTestController.HelloWorld(ctx: TWebContext);
begin
Render('Hello World called with GET');
end;
initialization
TDUnitX.RegisterTestFixture(TTestServerContainer);
end.