-
-
Notifications
You must be signed in to change notification settings - Fork 132
/
test.soa.network.pas
589 lines (527 loc) · 17.4 KB
/
test.soa.network.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
/// regression tests for RESTful SOA over Http or WebSockets
// - this unit is a part of the Open Source Synopse mORMot framework 2,
// licensed under a MPL/GPL/LGPL three license - see LICENSE.md
unit test.soa.network;
interface
{$I ..\src\mormot.defines.inc}
uses
sysutils,
contnrs,
classes,
mormot.core.base,
mormot.core.os,
mormot.core.text,
mormot.core.buffers,
mormot.core.unicode,
mormot.core.datetime,
mormot.core.rtti,
mormot.crypt.core,
mormot.core.data,
mormot.core.variants,
mormot.core.json,
mormot.core.log,
mormot.core.perf,
mormot.core.search,
mormot.core.mustache,
mormot.core.test,
mormot.core.interfaces,
mormot.crypt.jwt,
mormot.net.client,
mormot.net.server,
mormot.net.relay,
mormot.net.http,
mormot.net.ws.core,
mormot.net.ws.client,
mormot.net.ws.server,
mormot.db.core,
mormot.db.nosql.bson,
mormot.orm.core,
mormot.orm.rest,
mormot.orm.storage,
mormot.orm.sqlite3,
mormot.orm.client,
mormot.orm.server,
mormot.soa.core,
mormot.soa.server,
mormot.rest.core,
mormot.rest.client,
mormot.rest.server,
mormot.rest.memserver,
mormot.rest.sqlite3,
mormot.rest.http.client,
mormot.rest.http.server,
mormot.db.raw.sqlite3,
mormot.db.raw.sqlite3.static,
test.core.data,
test.core.base,
test.orm.core;
type
/// SOA callback definition as expected by TTestBidirectionalRemoteConnection
IBidirCallback = interface(IInvokable)
['{5C5818CC-FFBA-445C-82C1-39F45B84520C}']
procedure AsyncEvent(a: integer);
function Value: Integer;
end;
/// SOA service definition as expected by TTestBidirectionalRemoteConnection
IBidirService = interface(IInvokable)
['{0984A2DA-FD1F-49D6-ACFE-4D45CF08CA1B}']
function TestRest(a, b: integer; out c: RawUtf8): variant;
function TestRestCustom(a: integer): TServiceCustomAnswer;
function TestRestStatus(a: integer): TServiceCustomStatus;
function TestCallback(d: Integer; const callback: IBidirCallback): boolean;
procedure LaunchAsyncCallback(a: integer);
procedure RemoveCallback;
end;
TBidirServer = class(TInterfacedObject, IBidirService)
protected
fCallback: IBidirCallback;
// IBidirService implementation methods
function TestRest(a, b: integer; out c: RawUtf8): variant;
function TestRestCustom(a: integer): TServiceCustomAnswer;
function TestRestStatus(a: integer): TServiceCustomStatus;
function TestCallback(d: Integer; const callback: IBidirCallback): boolean;
procedure LaunchAsyncCallback(a: integer);
procedure RemoveCallback;
public
function LaunchSynchCallback: integer;
end;
/// a test case for all bidirectional remote access, e.g. WebSockets
TTestBidirectionalRemoteConnection = class(TSynTestCase)
protected
fHttpServer: TRestHttpServer;
fServer: TRestServerFullMemory;
fBidirServer: TBidirServer;
fPublicRelayClientsPort, fPublicRelayPort: RawUtf8;
fPublicRelay: TPublicRelay;
fPrivateRelay: TPrivateRelay;
procedure CleanUp; override;
function NewClient(const port: RawUtf8): TRestHttpClientWebsockets;
procedure WebsocketsLowLevel(protocol: TWebSocketProtocol;
opcode: TWebSocketFrameOpCode; const name: RawUtf8);
procedure TestRest(Rest: TRest);
procedure TestCallback(Rest: TRest; TryReconnect: boolean);
procedure SoaCallbackViaWebsockets(Ajax, Relay: boolean);
public
property HttpServer: TRestHttpServer
read fHttpServer;
published
/// low-level test of our 'synopsejson' and 'synopsebinary' protocols
procedure WebsocketsProtocols;
/// launch the WebSockets-ready HTTP server
procedure RunHttpServer;
/// test the callback mechanism via interface-based services on server side
procedure SoaCallbackOnServerSide;
/// test callbacks via interface-based services over JSON WebSockets
procedure SoaCallbackViaJsonWebsockets;
/// test callbacks via interface-based services over binary WebSockets
procedure SoaCallbackViaBinaryWebsockets;
/// initialize SynProtoRelay tunnelling
procedure RelayStart;
/// test SynProtoRelay tunnelling over JSON WebSockets
procedure RelaySoaCallbackViaJsonWebsockets;
/// verify ability to reconect from Private Relay to Public Relay
procedure RelayConnectionRecreate;
/// test SynProtoRelay tunnelling over binary WebSockets
procedure RelaySoaCallbackViaBinaryWebsockets;
/// finalize SynProtoRelay tunnelling
procedure RelayShutdown;
/// test Master/Slave replication using TRecordVersion field over WebSockets
procedure _TRecordVersion;
end;
implementation
uses
test.orm.sqlite3;
type
TBidirCallbackInterfacedObject = class(TInterfacedObject, IBidirCallback)
protected
fValue: Integer;
public
function Value: Integer;
procedure AsyncEvent(a: integer);
end;
TBidirCallback = class(TInterfacedCallback, IBidirCallback)
protected
fValue: Integer;
public
function Value: Integer;
procedure AsyncEvent(a: integer);
end;
{ TBidirServer }
function TBidirServer.TestRest(a, b: integer; out c: RawUtf8): variant;
begin
c := Int32ToUtf8(a + b);
result := _ObjFast(['a', a, 'b', b, 'c', c]);
end;
function TBidirServer.TestRestCustom(a: integer): TServiceCustomAnswer;
begin
result.Header := BINARY_CONTENT_TYPE_HEADER;
result.Content := Int32ToUtf8(a) + #0#1;
result.Status := HTTP_SUCCESS;
end;
function TBidirServer.TestRestStatus(a: integer): TServiceCustomStatus;
begin
result := a;
end;
function TBidirServer.TestCallback(d: Integer;
const callback: IBidirCallback): boolean;
begin
fCallback := callback;
result := d <> 0;
end;
procedure TBidirServer.LaunchAsyncCallback(a: integer);
begin
if Assigned(fCallback) then
fCallback.AsyncEvent(a);
end;
function TBidirServer.LaunchSynchCallback: integer;
begin
if Assigned(fCallback) then
result := fCallback.Value
else
result := 0;
end;
procedure TBidirServer.RemoveCallback;
begin
fCallback := nil;
end;
{ TBidirCallbackInterfacedObject }
procedure TBidirCallbackInterfacedObject.AsyncEvent(a: integer);
begin
inc(fValue, a);
end;
function TBidirCallbackInterfacedObject.Value: integer;
begin
result := fValue;
end;
{ TBidirCallback }
procedure TBidirCallback.AsyncEvent(a: integer);
begin
inc(fValue, a);
end;
function TBidirCallback.Value: integer;
begin
result := fValue;
end;
{ TTestBidirectionalRemoteConnection }
const
WEBSOCKETS_KEY = 'key';
procedure TTestBidirectionalRemoteConnection.WebsocketsProtocols;
var
Settings: TWebSocketProcessSettings;
begin
Settings.SetDefaults;
WebsocketsLowLevel(
TWebSocketProtocolJson.Create(''), focText, '');
WebsocketsLowLevel(
TWebSocketProtocolBinary.Create('', false, '', @Settings, []),
focBinary, '');
WebsocketsLowLevel(
TWebSocketProtocolBinary.Create('', false, 'pass', @Settings, []),
focBinary, ' encrypted');
WebsocketsLowLevel(
TWebSocketProtocolBinary.Create('', false, '', @Settings, [pboSynLzCompress]),
focBinary, ' compressed');
WebsocketsLowLevel(
TWebSocketProtocolBinary.Create('', false, 'pass', @Settings, [pboSynLzCompress]),
focBinary, ' encrypted compressed');
end;
type // to access protected low-level frame methods
TWebSocketProtocolRestHook = class(TWebSocketProtocolRest);
procedure TTestBidirectionalRemoteConnection.WebsocketsLowLevel(
protocol: TWebSocketProtocol; opcode: TWebSocketFrameOpCode;
const name: RawUtf8);
procedure TestOne(const content, contentType: RawByteString);
var
C1, C2: THttpServerRequest;
P2: TWebSocketProtocol;
frame: TWebSocketFrame;
head: RawUtf8;
noAnswer1, noAnswer2: boolean;
i: integer;
begin
C1 := THttpServerRequest.Create(nil, 0, nil, 0, [], nil);
C2 := THttpServerRequest.Create(nil, 0, nil, 0, [], nil);
P2 := protocol.Clone('');
try
for i := 1 to 100 do
begin
C1.PrepareDirect('url', 'POST', 'headers', content, contentType, '');
noAnswer1 := opcode = focBinary;
noAnswer2 := not noAnswer1;
TWebSocketProtocolRestHook(protocol).InputToFrame(C1, noAnswer1, frame, head);
check(frame.opcode = opcode);
TWebSocketProtocolRestHook(P2).FrameToInput(frame, noAnswer2, C2);
check(noAnswer1 = noAnswer2);
check(C2.URL = 'url');
check(C2.Method = 'POST');
check(IsPost(C2.Method));
check(C2.InHeaders = 'headers');
check(C2.InContentType = contentType);
check(C2.InContent = content);
C1.OutContent := content;
C1.OutContentType := contentType;
C1.OutCustomHeaders := 'outheaders';
frame.opcode := focContinuation;
head := 'answer';
TWebSocketProtocolRestHook(protocol).OutputToFrame(C1, 200, head, frame);
check(frame.opcode = opcode);
check(TWebSocketProtocolRestHook(P2).FrameToOutput(frame, C2) = 200);
check(C2.OutContent = content);
check(C2.OutContentType = contentType);
check(C2.OutCustomHeaders = 'outheaders');
end;
finally
P2.Free;
C2.Free;
C1.Free;
end;
end;
var
timer: TPrecisionTimer;
begin
timer.Start;
try
TestOne('content', TEXT_CONTENT_TYPE);
TestOne('{"content":1234}', JSON_CONTENT_TYPE);
TestOne('"content"', JSON_CONTENT_TYPE);
TestOne('["json",2]', JSON_CONTENT_TYPE);
TestOne('binary'#0'data', BINARY_CONTENT_TYPE);
// NotifyTestSpeed('%%', [protocol.Name, name], 100 * 5, 0, @timer);
finally
protocol.Free;
end;
end;
procedure TTestBidirectionalRemoteConnection.RunHttpServer;
var
port: integer;
begin
WebSocketLog := TSynLog; // for very detailed log
TInterfaceFactory.RegisterInterfaces([TypeInfo(IBidirService), TypeInfo(IBidirCallback)]);
// sicClientDriven services expect authentication for sessions
fServer := TRestServerFullMemory.CreateWithOwnModel([], {withauth=}true);
fServer.Server.CreateMissingTables;
fBidirServer := TBidirServer.Create;
check(fServer.ServiceDefine(fBidirServer, [IBidirService]) <> nil);
fHttpServer := TRestHttpServer.Create(
HTTP_DEFAULTPORT, [], '+', WEBSOCKETS_DEFAULT_MODE);
check(fHttpServer.AddServer(fServer));
fHttpServer.WebSocketsEnable(fServer, WEBSOCKETS_KEY, true)^.SetFullLog;
//(fHttpServer.HttpServer as TWebSocketServer).HeartbeatDelay := 5000;
port := Utf8ToInteger(HTTP_DEFAULTPORT);
fPublicRelayClientsPort := ToUtf8(port + 1);
fPublicRelayPort := ToUtf8(port + 2);
end;
procedure TTestBidirectionalRemoteConnection.TestRest(Rest: TRest);
var
I: IBidirService;
a, b: integer;
c: RawUtf8;
v: variant;
res: TServiceCustomAnswer;
begin
Rest.Services.Resolve(IBidirService, I);
if CheckFailed(Assigned(I), 'Rest IBidirService') then
exit;
for a := -10 to 10 do
for b := -10 to 10 do
begin
v := I.TestRest(a, b, c);
CheckEqual(GetInteger(pointer(c)), a + b, 'TestRest');
if CheckFailed(DocVariantType.IsOfType(v)) then
continue;
check(v.a = a);
check(v.b = b);
check(v.c = c);
end;
for a := -10 to 10 do
begin
res := I.TestRestCustom(a);
check(res.Status = HTTP_SUCCESS, 'TestRestCustom');
check(GetInteger(pointer(res.Content)) = a);
check(res.Content[Length(res.Content)] = #1);
end;
for a := 0 to 10 do
begin
b := a * 35 + 200;
check(b >= 200, '>= http range');
check(b <= 599, '<= http range');
CheckEqual(I.TestRestStatus(b), b, 'TestRestStatus');
end;
end;
procedure TTestBidirectionalRemoteConnection.TestCallback(
Rest: TRest; TryReconnect: boolean);
var
I: IBidirService;
d: integer;
subscribed: IBidirCallback;
resp: RawUtf8;
procedure WaitUntilNotified;
var
timeout: Int64;
begin
timeout := GetTickCount64 + 5000;
while (subscribed.value <> 6) and
(GetTickCount64 < timeout) do
sleep(1);
check(subscribed.value = 6);
end;
begin
Rest.Services.Resolve(IBidirService, I);
if CheckFailed(Assigned(I), 'Callback IBidirService') then
exit;
subscribed := TBidirCallbackInterfacedObject.Create;
for d := -5 to 6 do
begin
check(I.TestCallback(d, subscribed) = (d <> 0));
I.LaunchAsyncCallback(d);
end;
WaitUntilNotified;
check(fBidirServer.LaunchSynchCallback = 6);
Rest.Services.CallBackUnRegister(subscribed); // manual callback release notify
subscribed := TBidirCallback.Create(Rest, IBidirCallback); // auto notification
for d := -5 to 6 do
begin
check(I.TestCallback(d, subscribed) = (d <> 0));
I.LaunchAsyncCallback(d);
end;
WaitUntilNotified;
subscribed := TBidirCallback.Create(Rest, IBidirCallback);
for d := -5 to 6 do
begin
check(I.TestCallback(d, subscribed) = (d <> 0));
if TryReconnect and
(d and 3 = 0) then
begin
// simulate a broken connection
//TRestHttpClientWebsockets(Rest).WebSockets.Close;
// should reconnect and upgrade the socket, then move the callbacks
CheckEqual(TRestClientUri(Rest).CallBackGet('timestamp', [], resp), 200);
end;
I.LaunchAsyncCallback(d);
I.RemoveCallback;
end;
WaitUntilNotified;
check(fBidirServer.LaunchSynchCallback = 0);
end; // here TBidirCallback.Free will notify Rest.Services.CallBackUnRegister()
procedure TTestBidirectionalRemoteConnection.SoaCallbackOnServerSide;
begin
TestRest(fServer);
TestCallback(fServer, {tryreconnect=}false);
TestRest(fServer);
end;
function TTestBidirectionalRemoteConnection.NewClient(
const port: RawUtf8): TRestHttpClientWebsockets;
begin
result := TRestHttpClientWebsockets.Create(
'127.0.0.1', port, TOrmModel.Create(fServer.Model));
result.Model.Owner := result;
result.WebSockets.Settings.SetFullLog.ClientRestoreCallbacks := true;
end;
procedure TTestBidirectionalRemoteConnection.SoaCallbackViaWebsockets(
Ajax, Relay: boolean);
procedure ServiceDefine(c: TRestHttpClientWebsockets; const msg: string);
begin
check(c.SetUser('User', 'synopse'), 'setuser' + msg);
check(c.ServiceDefine(IBidirService, sicShared) <> nil, 'IBidirService' + msg);
end;
var
c1, c2: TRestHttpClientWebsockets;
port: RawUtf8;
stats: RawUtf8;
begin
if Relay then
port := fPublicRelayClientsPort
else
port := HTTP_DEFAULTPORT;
c1 := NewClient(port);
try
// check plain HTTP REST calls
check(c1.ServerTimestampSynchronize, 'ServerTimestampSynchronize');
ServiceDefine(c1, '1');
TestRest(c1);
// check WebSockets communication
CheckEqual(c1.WebSocketsUpgrade(WEBSOCKETS_KEY, Ajax, [pboSynLzCompress]), '',
'WebSocketsUpgrade1');
TestCallback(c1, {tryreconnect=}false);
c2 := NewClient(port);
try
CheckEqual(c2.WebSocketsUpgrade(WEBSOCKETS_KEY, Ajax, [pboSynLzCompress]), '',
'WebSocketsUpgrade2');
ServiceDefine(c2, '2');
TestCallback(c2, {tryreconnect=}true);
if Relay then
begin
stats := OpenHttpGet('127.0.0.1', fPublicRelayPort, '/stats', '');
check(PosEx('"version"', stats) > 0, 'stats');
end;
TestRest(c1);
TestRest(c2);
finally
c2.Free;
end;
finally
c1.Free;
end;
end;
procedure TTestBidirectionalRemoteConnection.SoaCallbackViaJsonWebsockets;
begin
SoaCallbackViaWebsockets({ajax=}true, {relay=}false);
end;
procedure TTestBidirectionalRemoteConnection.SoaCallbackViaBinaryWebsockets;
begin
SoaCallbackViaWebsockets({ajax=}false, {relay=}false);
end;
procedure TTestBidirectionalRemoteConnection.RelayStart;
const
RELAYKEY = 'aes256secret';
var
stats: RawUtf8;
begin
fPublicRelay := TPublicRelay.Create(nil, fPublicRelayClientsPort,
fPublicRelayPort, RELAYKEY, TJwtHS256.Create('jwtsecret', 100, [], []));
fPrivateRelay := TPrivateRelay.Create(nil, '127.0.0.1', fPublicRelayPort,
RELAYKEY, fPublicRelay.ServerJwt.Compute([]), '127.0.0.1', HTTP_DEFAULTPORT,
'X-Real-IP');
check(not fPrivateRelay.Connected);
check(fPrivateRelay.TryConnect);
checkEqual(OpenHttpGet(
'127.0.0.1', fPublicRelayPort, '/invalid', ''), '', 'wrong URI');
stats := OpenHttpGet('127.0.0.1', fPublicRelayPort, '/stats', '');
check(PosEx('version', stats) > 0, 'stats');
end;
procedure TTestBidirectionalRemoteConnection.RelaySoaCallbackViaJsonWebsockets;
begin
SoaCallbackViaWebsockets({ajax=}true, {relay=}true);
SleepHiRes(10);
end;
procedure TTestBidirectionalRemoteConnection.RelayConnectionRecreate;
begin
check(fPrivateRelay.TryConnect);
end;
procedure TTestBidirectionalRemoteConnection.RelaySoaCallbackViaBinaryWebsockets;
begin
SoaCallbackViaWebsockets({ajax=}false, {relay=}true);
end;
procedure TTestBidirectionalRemoteConnection.RelayShutdown;
var
stats: RawUtf8;
begin
stats := OpenHttpGet('127.0.0.1', fPublicRelayPort, '/stats', '');
check(PosEx('"version"', stats) > 0, 'stats');
fPrivateRelay.Free;
SleepHiRes(100);
stats := OpenHttpGet('127.0.0.1', fPublicRelayPort, '/stats', '');
check(PosEx('"version"', stats) > 0, 'stats');
fPublicRelay.Free;
SleepHiRes(10);
end;
procedure TTestBidirectionalRemoteConnection._TRecordVersion;
begin
TestMasterSlaveRecordVersion(Self, 'ws.db3');
end;
procedure TTestBidirectionalRemoteConnection.CleanUp;
begin
FreeAndNil(fHttpServer);
FreeAndNil(fServer);
end;
end.