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
|
2022-08-01 19:11:42 +02:00
|
|
|
lListener := TMVCListener.Create(TMVCListenerProperties.New.SetName('Listener1').SetPort(5000).SetMaxConnections(512)
|
|
|
|
.SetWebModuleClass(TestWebModuleClass));
|
2021-02-04 14:11:33 +01:00
|
|
|
|
2022-08-01 19:11:42 +02:00
|
|
|
Assert.IsTrue(Assigned(lListener));
|
2021-02-04 14:11:33 +01:00
|
|
|
|
2022-08-01 19:11:42 +02:00
|
|
|
lListener.Start;
|
|
|
|
Assert.IsTrue(lListener.Active);
|
2021-02-04 14:11:33 +01:00
|
|
|
|
2022-08-01 19:11:42 +02:00
|
|
|
lListener.Stop;
|
|
|
|
Assert.IsFalse(lListener.Active);
|
2021-02-04 14:11:33 +01:00
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TTestServerContainer.TestServerListenerAndClient;
|
|
|
|
var
|
2022-08-01 19:11:42 +02:00
|
|
|
lListener: IMVCListener;
|
2021-02-04 14:11:33 +01:00
|
|
|
LClient: IMVCRESTClient;
|
|
|
|
begin
|
2022-08-01 19:11:42 +02:00
|
|
|
lListener := TMVCListener.Create(TMVCListenerProperties.New.SetName('Listener1').SetPort(6000).SetMaxConnections(1024)
|
|
|
|
.SetWebModuleClass(TestWebModuleClass));
|
2021-02-04 14:11:33 +01:00
|
|
|
|
2022-08-01 19:11:42 +02:00
|
|
|
Assert.IsTrue(Assigned(lListener));
|
2021-02-04 14:11:33 +01:00
|
|
|
|
2022-08-01 19:11:42 +02: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);
|
|
|
|
|
2022-08-01 19:11:42 +02:00
|
|
|
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;
|
|
|
|
|
2022-08-01 19:11:42 +02:00
|
|
|
LListenerCtx.Add(TMVCListenerProperties.New.SetName('Listener2').SetPort(6000).SetMaxConnections(1024)
|
|
|
|
.SetWebModuleClass(TestWebModuleClass));
|
2021-02-04 14:11:33 +01:00
|
|
|
|
2022-08-01 19:11:42 +02:00
|
|
|
LListenerCtx.Add(TMVCListenerProperties.New.SetName('Listener3').SetPort(7000).SetMaxConnections(1024)
|
|
|
|
.SetWebModuleClass(TestWebModuleClass2));
|
2021-02-04 14:11:33 +01:00
|
|
|
|
2022-08-01 19:11:42 +02:00
|
|
|
Assert.IsTrue(Assigned(LListenerCtx.FindByName('Listener2')));
|
|
|
|
Assert.IsTrue(Assigned(LListenerCtx.FindByName('Listener3')));
|
2021-02-04 14:11:33 +01:00
|
|
|
|
|
|
|
LListenerCtx.StartAll;
|
|
|
|
|
2022-08-01 19:11:42 +02:00
|
|
|
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;
|
|
|
|
|
2022-08-01 19:11:42 +02:00
|
|
|
Assert.IsFalse(LListenerCtx.FindByName('Listener2').Active);
|
|
|
|
Assert.IsFalse(LListenerCtx.FindByName('Listener3').Active);
|
2021-02-04 14:11:33 +01:00
|
|
|
|
2022-08-01 19:11:42 +02:00
|
|
|
LListenerCtx.Remove('Listener2').Remove('Listener3');
|
2021-02-04 14:11:33 +01:00
|
|
|
|
2022-08-01 19:11:42 +02: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.
|