delphimvcframework/unittests/DMVCFrameworkTests/MessagingExtensionsTestU.pas

212 lines
7.0 KiB
ObjectPascal
Raw Normal View History

2013-10-30 01:09:09 +01:00
unit MessagingExtensionsTestU;
interface
uses
TestFramework,
MVCFramework.RESTClient,
LiveServerTestU;
type
TMessagingExtensionsTestCase = class(TBaseServerTest)
published
procedure TestSubscribeOnATopic;
procedure TestMultipleSubscribeOnSameTopic;
procedure TestMultipleSubscribeAndUnsubscribe;
procedure TestMultipleSubscribeAndUnsubscribeHARD;
procedure TestSubscribeAndReceive;
end;
implementation
2015-04-01 17:01:23 +02:00
{$WARN SYMBOL_DEPRECATED OFF}
2013-10-30 01:09:09 +01:00
uses
System.SysUtils,
2014-09-05 12:47:40 +02:00
{$IF CompilerVersion < 27}
2013-10-30 01:09:09 +01:00
Data.DBXJSON,
2014-04-16 22:52:25 +02:00
{$ELSE}
System.JSON,
2014-09-05 12:47:40 +02:00
{$ENDIF}
2013-10-30 01:09:09 +01:00
System.Classes,
2015-04-01 17:01:23 +02:00
MVCFramework.Logger, MVCFramework.Commons, Winapi.Windows;
2013-10-30 01:09:09 +01:00
{ TMessagingExtensionsTestCase }
procedure TMessagingExtensionsTestCase.TestMultipleSubscribeAndUnsubscribe;
var
res: IRESTResponse;
2013-11-09 10:05:14 +01:00
x: string;
2013-10-30 01:09:09 +01:00
begin
RESTClient.ReadTimeout := - 1;
2015-04-01 17:01:23 +02:00
DoLoginWith('guest');
RESTClient.doPOST('/messages/clients', ['my-unique-id']);
res := RESTClient.doPOST('/messages/subscriptions/queue/test01', []);
CheckEquals(HTTP_STATUS.OK, res.ResponseCode, res.ResponseText);
res := RESTClient.doGET('/messages/subscriptions', []);
2013-10-30 01:09:09 +01:00
x := Trim(res.BodyAsString);
2015-04-01 17:01:23 +02:00
CheckEquals('/queue/test01', x);
2013-10-30 01:09:09 +01:00
2015-04-01 17:01:23 +02:00
res := RESTClient.doPOST('/messages/subscriptions/queue', ['test010']);
CheckEquals(HTTP_STATUS.OK, res.ResponseCode, res.ResponseText);
// server shoud not return an error
res := RESTClient.doGET('/messages/subscriptions', []);
2013-10-30 01:09:09 +01:00
x := Trim(res.BodyAsString);
2015-04-01 17:01:23 +02:00
CheckEquals('/queue/test01;/queue/test010', x);
2013-10-30 01:09:09 +01:00
2015-04-01 17:01:23 +02:00
res := RESTClient.doDELETE('/messages/subscriptions/queue/test01', []);
CheckEquals(HTTP_STATUS.OK, res.ResponseCode, res.ResponseText);
// server shoud not return an error
res := RESTClient.doGET('/messages/subscriptions', []);
2013-10-30 01:09:09 +01:00
x := Trim(res.BodyAsString);
2015-04-01 17:01:23 +02:00
CheckEquals('/queue/test010', x);
2013-10-30 01:09:09 +01:00
2015-04-01 17:01:23 +02:00
CheckEquals(HTTP_STATUS.OK, res.ResponseCode, res.ResponseText);
2013-11-09 10:05:14 +01:00
// server shod not return an error
2013-10-30 01:09:09 +01:00
DoLogout;
end;
procedure TMessagingExtensionsTestCase.TestMultipleSubscribeAndUnsubscribeHARD;
var
res: IRESTResponse;
2013-11-09 10:05:14 +01:00
x: string;
2013-10-30 01:09:09 +01:00
begin
RESTClient.ReadTimeout := - 1;
2015-04-01 17:01:23 +02:00
DoLoginWith('guest');
RESTClient.doPOST('/messages/clients', ['my-unique-id']);
res := RESTClient.doPOST('/messages/subscriptions/queue', ['test01']);
res := RESTClient.doPOST('/messages/subscriptions/queue', ['test010']);
res := RESTClient.doPOST('/messages/subscriptions/queue', ['test0101']);
res := RESTClient.doPOST('/messages/subscriptions/queue', ['test01010']);
res := RESTClient.doPOST('/messages/subscriptions/queue', ['test010101']);
res := RESTClient.doPOST('/messages/subscriptions/queue', ['test0101010']);
CheckEquals(HTTP_STATUS.OK, res.ResponseCode, res.ResponseText);
2013-11-09 10:05:14 +01:00
// server shod not return an error
2015-04-01 17:01:23 +02:00
res := RESTClient.doGET('/messages/subscriptions', []);
2013-10-30 01:09:09 +01:00
x := Trim(res.BodyAsString);
2015-04-01 17:01:23 +02:00
CheckEquals
('/queue/test01;/queue/test010;/queue/test0101;/queue/test01010;/queue/test010101;/queue/test0101010',
x);
2013-10-30 01:09:09 +01:00
2015-04-01 17:01:23 +02:00
res := RESTClient.doDELETE('/messages/subscriptions/queue', ['test010']);
CheckEquals(HTTP_STATUS.OK, res.ResponseCode, res.ResponseText);
2013-11-09 10:05:14 +01:00
// server shod not return an error
2015-04-01 17:01:23 +02:00
res := RESTClient.doGET('/messages/subscriptions', []);
2013-10-30 01:09:09 +01:00
x := Trim(res.BodyAsString);
2015-04-01 17:01:23 +02:00
CheckEquals
('/queue/test01;/queue/test0101;/queue/test01010;/queue/test010101;/queue/test0101010', x);
2013-10-30 01:09:09 +01:00
DoLogout;
end;
procedure TMessagingExtensionsTestCase.TestMultipleSubscribeOnSameTopic;
var
res: IRESTResponse;
begin
2015-04-01 17:01:23 +02:00
DoLoginWith('guest');
RESTClient.doPOST('/messages/clients', ['my-unique-id']);
res := RESTClient.doPOST('/messages/subscriptions/queue/test01', []);
CheckEquals(HTTP_STATUS.OK, res.ResponseCode, res.ResponseText);
res := RESTClient.doPOST('/messages/subscriptions/queue/test01', []);
CheckEquals(HTTP_STATUS.OK, res.ResponseCode, res.ResponseText);
// server shoud not return an error
2013-10-30 01:09:09 +01:00
DoLogout;
end;
procedure TMessagingExtensionsTestCase.TestSubscribeAndReceive;
var
2013-11-09 10:05:14 +01:00
res: IRESTResponse;
messages: TJSONArray;
sid: string;
2013-10-30 01:09:09 +01:00
RMessageCount: Integer;
2013-11-09 10:05:14 +01:00
I: Integer;
o: TJSONObject;
2015-04-01 17:01:23 +02:00
J: Integer;
LUnique: string;
2013-10-30 01:09:09 +01:00
const
2015-04-01 17:01:23 +02:00
MSG_COUNT = 10;
2013-10-30 01:09:09 +01:00
begin
2015-04-01 17:01:23 +02:00
LUnique := GetTickCount.ToString;
2013-10-30 01:09:09 +01:00
2015-04-01 17:01:23 +02:00
DoLoginWith('guest');
RESTClient.doPOST('/messages/clients', ['my-unique-' + LUnique]);
RESTClient.doPOST('/messages', ['subscriptions', 'queue', 'test01']);
RESTClient.doPOST('/messages', ['subscriptions', 'queue', 'test02']);
RESTClient.ReadTimeout := - 1;
2013-10-30 01:09:09 +01:00
sid := RESTClient.SessionID;
TThread.CreateAnonymousThread(
procedure
var
RESTC: TRESTClient;
I: Integer;
begin
TThread.Sleep(1000);
2015-04-01 17:01:23 +02:00
RESTC := TRESTClient.Create('localhost', 9999);
2013-10-30 01:09:09 +01:00
try
2015-04-01 17:01:23 +02:00
RESTC.doPOST('/messages/clients', ['my-other-unique-' + LUnique]);
2013-10-30 01:09:09 +01:00
RESTC.ReadTimeout := 60 * 1000 * 30;
2015-04-01 17:01:23 +02:00
RESTC.doGET('/login', ['guest']);
2013-10-30 01:09:09 +01:00
for I := 1 to MSG_COUNT do
begin
2015-04-01 17:01:23 +02:00
RESTC.doPOST('/messages/queue/test02', [], TJSONObject.Create(TJSONPair.Create('hello',
TJSONNumber.Create(I))));
RESTC.doPOST('/messages/queue/test01', [], TJSONObject.Create(TJSONPair.Create('hello',
TJSONNumber.Create(I))));
2013-10-30 01:09:09 +01:00
end;
finally
RESTC.Free;
end;
end).Start;
RMessageCount := 0;
2015-04-01 17:01:23 +02:00
for J := 1 to MSG_COUNT * 2 do
2013-10-30 01:09:09 +01:00
begin
2015-04-01 17:01:23 +02:00
res := RESTClient.doGET('/messages', []);
if res.ResponseCode = HTTP_STATUS.OK then
2013-10-30 01:09:09 +01:00
begin
2015-04-01 17:01:23 +02:00
CheckIs(res.BodyAsJsonObject.Get('_timeout').JsonValue, TJSONFalse);
CheckNotNull(res.BodyAsJsonObject.Get('_timestamp'), '_timestamp is not set');
2013-10-30 01:09:09 +01:00
CheckNotNull(res.BodyAsJsonObject.Get('messages'), 'messages is not set');
2015-04-01 17:01:23 +02:00
CheckIs(res.BodyAsJsonObject.Get('messages').JsonValue, TJSONArray,
'Messages is not a TJSONArray');
2013-10-30 01:09:09 +01:00
messages := res.BodyAsJsonObject.Get('messages').JsonValue as TJSONArray;
if messages.Size > 0 then
for I := 0 to messages.Size - 1 do
begin
o := messages.Get(I) as TJSONObject;
2015-04-01 17:01:23 +02:00
logw(o.Get('message').ToString);
2013-10-30 01:09:09 +01:00
end;
RMessageCount := RMessageCount + messages.Size;
end;
2015-04-01 17:01:23 +02:00
if res.ResponseCode = HTTP_STATUS.RequestTimeout then // receive timeout
2013-10-30 01:09:09 +01:00
break;
end;
2015-04-01 17:01:23 +02:00
CheckEquals(MSG_COUNT * 2, RMessageCount, 'message count');
res := RESTClient.doGET('/messages', []);
CheckIs(res.BodyAsJsonObject.Get('_timeout').JsonValue, TJSONTrue);
2013-10-30 01:09:09 +01:00
DoLogout;
end;
procedure TMessagingExtensionsTestCase.TestSubscribeOnATopic;
var
res: IRESTResponse;
begin
2015-04-01 17:01:23 +02:00
DoLoginWith('guest');
RESTClient.doPOST('/messages/clients', ['my-unique-id']);
res := RESTClient.doPOST('/messages/subscriptions/queue/test01', []);
CheckEquals(HTTP_STATUS.OK, res.ResponseCode, res.ResponseText);
res := RESTClient.doDELETE('/messages/subscriptions/queue/test01', []);
CheckEquals(HTTP_STATUS.OK, res.ResponseCode, res.ResponseText);
2013-10-30 01:09:09 +01:00
DoLogout;
end;
initialization
2015-04-01 17:01:23 +02:00
{$IFDEF USE_MESSAGING}
RegisterTest(TMessagingExtensionsTestCase.Suite);
{$ENDIF}
2013-11-09 10:05:14 +01:00
finalization
2013-10-30 01:09:09 +01:00
end.