2015-12-22 12:29:25 +01:00
|
|
|
unit MVCFramework.Tests.StandaloneServer;
|
2015-06-15 18:57:46 +02:00
|
|
|
|
|
|
|
interface
|
|
|
|
|
|
|
|
uses
|
2017-08-20 02:36:22 +02:00
|
|
|
DUnitX.TestFramework,
|
2015-06-15 18:57:46 +02:00
|
|
|
System.Classes,
|
|
|
|
System.SysUtils,
|
|
|
|
System.Generics.Collections,
|
2016-06-16 22:13:35 +02:00
|
|
|
MVCFramework,
|
2017-01-18 21:53:53 +01:00
|
|
|
MVCFramework.Commons,
|
2016-06-16 22:13:35 +02:00
|
|
|
MVCFramework.Server,
|
|
|
|
MVCFramework.Server.Impl;
|
2015-06-15 18:57:46 +02:00
|
|
|
|
|
|
|
type
|
|
|
|
|
|
|
|
[MVCPath('/')]
|
2016-06-16 22:13:35 +02:00
|
|
|
TTestController = class(TMVCController)
|
2015-06-15 18:57:46 +02:00
|
|
|
public
|
|
|
|
[MVCPath('/hello')]
|
|
|
|
[MVCHTTPMethod([httpGET])]
|
|
|
|
procedure HelloWorld(ctx: TWebContext);
|
|
|
|
end;
|
|
|
|
|
2017-08-20 02:36:22 +02:00
|
|
|
|
|
|
|
[TestFixture]
|
|
|
|
TTestMVCFrameworkServer = class(TObject)
|
2016-06-16 22:13:35 +02:00
|
|
|
private
|
2015-06-15 18:57:46 +02:00
|
|
|
|
|
|
|
protected
|
2017-08-20 02:36:22 +02:00
|
|
|
[SetUp]
|
|
|
|
procedure SetUp;
|
|
|
|
[TearDown]
|
|
|
|
procedure TearDown;
|
2017-08-21 15:31:31 +02:00
|
|
|
public
|
2017-08-20 02:36:22 +02:00
|
|
|
[Test]
|
2016-06-16 22:13:35 +02:00
|
|
|
procedure TestListener;
|
2017-08-20 02:36:22 +02:00
|
|
|
[Test]
|
2016-06-16 22:13:35 +02:00
|
|
|
procedure TestListenerContext;
|
2017-08-20 02:36:22 +02:00
|
|
|
[Test]
|
2016-06-16 22:13:35 +02:00
|
|
|
procedure TestServerListenerAndClient;
|
2015-06-15 18:57:46 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
implementation
|
|
|
|
|
|
|
|
uses
|
2016-11-13 20:59:21 +01:00
|
|
|
MVCFramework.Tests.WebModule2,
|
2015-06-15 18:57:46 +02:00
|
|
|
MVCFramework.RESTClient;
|
|
|
|
|
|
|
|
{ TTestMVCFrameworkServer }
|
|
|
|
|
|
|
|
procedure TTestMVCFrameworkServer.SetUp;
|
|
|
|
begin
|
|
|
|
inherited;
|
|
|
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TTestMVCFrameworkServer.TearDown;
|
|
|
|
begin
|
|
|
|
inherited;
|
|
|
|
|
|
|
|
end;
|
|
|
|
|
2016-06-16 22:13:35 +02:00
|
|
|
procedure TTestMVCFrameworkServer.TestListener;
|
2015-06-15 18:57:46 +02:00
|
|
|
var
|
2016-06-16 22:13:35 +02:00
|
|
|
LListener: IMVCListener;
|
2015-06-15 18:57:46 +02:00
|
|
|
begin
|
2016-06-16 22:13:35 +02:00
|
|
|
LListener := TMVCListener.Create(TMVCListenerProperties.New
|
|
|
|
.SetName('Listener1')
|
|
|
|
.SetPort(5000)
|
|
|
|
.SetMaxConnections(512)
|
|
|
|
.SetWebModuleClass(TestWebModuleClass)
|
|
|
|
);
|
2015-06-15 18:57:46 +02:00
|
|
|
|
2017-08-20 02:36:22 +02:00
|
|
|
Assert.isTrue(Assigned(LListener));
|
2015-06-15 22:51:36 +02:00
|
|
|
|
2016-06-16 22:13:35 +02:00
|
|
|
LListener.Start;
|
2017-08-20 02:36:22 +02:00
|
|
|
Assert.isTrue(LListener.Active);
|
2015-06-15 22:51:36 +02:00
|
|
|
|
2016-06-16 22:13:35 +02:00
|
|
|
LListener.Stop;
|
2017-08-20 02:36:22 +02:00
|
|
|
Assert.isFalse(LListener.Active);
|
2015-06-15 18:57:46 +02:00
|
|
|
end;
|
|
|
|
|
2016-06-16 22:13:35 +02:00
|
|
|
procedure TTestMVCFrameworkServer.TestServerListenerAndClient;
|
2015-06-15 18:57:46 +02:00
|
|
|
var
|
2016-06-16 22:13:35 +02:00
|
|
|
LListener: IMVCListener;
|
|
|
|
LClient: TRESTClient;
|
2015-06-15 18:57:46 +02:00
|
|
|
begin
|
2016-06-16 22:13:35 +02:00
|
|
|
LListener := TMVCListener.Create(TMVCListenerProperties.New
|
|
|
|
.SetName('Listener1')
|
|
|
|
.SetPort(6000)
|
|
|
|
.SetMaxConnections(1024)
|
|
|
|
.SetWebModuleClass(TestWebModuleClass)
|
|
|
|
);
|
2015-06-15 18:57:46 +02:00
|
|
|
|
2017-08-20 02:36:22 +02:00
|
|
|
Assert.isTrue(Assigned(LListener));
|
2015-06-15 22:51:36 +02:00
|
|
|
|
2016-06-16 22:13:35 +02:00
|
|
|
LListener.Start;
|
2017-08-20 02:36:22 +02:00
|
|
|
Assert.isTrue(LListener.Active);
|
2015-06-15 18:57:46 +02:00
|
|
|
|
2016-06-16 22:13:35 +02:00
|
|
|
LClient := TRESTClient.Create('localhost', 6000);
|
2015-06-15 18:57:46 +02:00
|
|
|
try
|
2016-06-16 22:13:35 +02:00
|
|
|
LClient.UserName := 'dmvc';
|
|
|
|
LClient.Password := '123';
|
2017-08-20 02:36:22 +02:00
|
|
|
Assert.AreEqual('Hello World called with GET', LClient.doGET('/hello', []).BodyAsString);
|
2015-06-15 18:57:46 +02:00
|
|
|
finally
|
2016-06-16 22:13:35 +02:00
|
|
|
FreeAndNil(LClient);
|
2015-06-15 18:57:46 +02:00
|
|
|
end;
|
|
|
|
|
2016-06-16 22:13:35 +02:00
|
|
|
LListener.Stop;
|
2017-08-20 02:36:22 +02:00
|
|
|
Assert.isFalse(LListener.Active);
|
2015-06-15 18:57:46 +02:00
|
|
|
end;
|
|
|
|
|
2016-06-16 22:13:35 +02:00
|
|
|
procedure TTestMVCFrameworkServer.TestListenerContext;
|
2015-06-15 18:57:46 +02:00
|
|
|
var
|
2016-06-16 22:13:35 +02:00
|
|
|
LListenerCtx: IMVCListenersContext;
|
2015-06-15 18:57:46 +02:00
|
|
|
begin
|
2016-06-16 22:13:35 +02:00
|
|
|
LListenerCtx := TMVCListenersContext.Create;
|
2015-06-15 18:57:46 +02:00
|
|
|
|
2016-06-16 22:13:35 +02:00
|
|
|
LListenerCtx.Add(TMVCListenerProperties.New
|
|
|
|
.SetName('Listener2')
|
|
|
|
.SetPort(6000)
|
|
|
|
.SetMaxConnections(1024)
|
|
|
|
.SetWebModuleClass(TestWebModuleClass)
|
|
|
|
);
|
2015-06-15 18:57:46 +02:00
|
|
|
|
2016-06-16 22:13:35 +02:00
|
|
|
LListenerCtx.Add(TMVCListenerProperties.New
|
|
|
|
.SetName('Listener3')
|
|
|
|
.SetPort(7000)
|
|
|
|
.SetMaxConnections(1024)
|
|
|
|
.SetWebModuleClass(TestWebModuleClass2)
|
|
|
|
);
|
2015-06-15 18:57:46 +02:00
|
|
|
|
2017-08-20 02:36:22 +02:00
|
|
|
Assert.isTrue(Assigned(LListenerCtx.FindByName('Listener2')));
|
|
|
|
Assert.isTrue(Assigned(LListenerCtx.FindByName('Listener3')));
|
2015-06-15 18:57:46 +02:00
|
|
|
|
2016-06-16 22:13:35 +02:00
|
|
|
LListenerCtx.StartAll;
|
|
|
|
|
2017-08-20 02:36:22 +02:00
|
|
|
Assert.isTrue(LListenerCtx.Count = 2);
|
|
|
|
Assert.isTrue(LListenerCtx.FindByName('Listener2').Active);
|
|
|
|
Assert.isTrue(LListenerCtx.FindByName('Listener3').Active);
|
2016-06-16 22:13:35 +02:00
|
|
|
|
|
|
|
LListenerCtx.StopAll;
|
|
|
|
|
2017-08-20 02:36:22 +02:00
|
|
|
Assert.isFalse(LListenerCtx.FindByName('Listener2').Active);
|
|
|
|
Assert.isFalse(LListenerCtx.FindByName('Listener3').Active);
|
2016-06-16 22:13:35 +02:00
|
|
|
|
|
|
|
LListenerCtx
|
|
|
|
.Remove('Listener2')
|
|
|
|
.Remove('Listener3');
|
|
|
|
|
2017-08-20 02:36:22 +02:00
|
|
|
Assert.isTrue(LListenerCtx.Count = 0);
|
2015-06-15 18:57:46 +02:00
|
|
|
end;
|
|
|
|
|
2016-06-16 22:13:35 +02:00
|
|
|
{ TTestController }
|
2015-06-15 18:57:46 +02:00
|
|
|
|
2016-06-16 22:13:35 +02:00
|
|
|
procedure TTestController.HelloWorld(ctx: TWebContext);
|
2015-06-15 18:57:46 +02:00
|
|
|
begin
|
|
|
|
Render('Hello World called with GET');
|
|
|
|
end;
|
|
|
|
|
|
|
|
initialization
|
|
|
|
|
2017-08-20 02:36:22 +02:00
|
|
|
TDUnitX.RegisterTestFixture(TTestMVCFrameworkServer);
|
2015-06-15 18:57:46 +02:00
|
|
|
|
|
|
|
end.
|