File: testinsightserver.pas

package info (click to toggle)
lazarus 4.0%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 275,760 kB
  • sloc: pascal: 2,341,904; xml: 509,420; makefile: 348,726; cpp: 93,608; sh: 3,387; java: 609; perl: 297; sql: 222; ansic: 137
file content (803 lines) | stat: -rw-r--r-- 21,185 bytes parent folder | download
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
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
unit TestInsightServer;

{$mode ObjFPC}{$H+}

interface

uses
  Classes, SysUtils, types, httpdefs, syncobjs, fphttpserver, fpJSON, testinsightprotocol;

{$IF DECLARED(TCORSSUPPORT)}
{$DEFINE USECORS}
{$ENDIF}


Type
  TTestItem = Class;

  { TTestItem }

  TTestItem = Class
  Public
    TestParent : TTestItem;
    TestName : String;
    Tests : Array of TTestItem;
    TestResult : TTestInsightResult;
    Constructor Create(aParent : TTestItem; const aTestName : String);
    Destructor Destroy; override;
    Procedure FromJSON(aJSON : TJSONObject);
    Function TestPath : String;
    Function CountTestCases : Integer;
    Function IsTestSuite : Boolean;
    Function IsTestCase : Boolean;
  end;

   (*
     // Incoming
     {
       "name" : null
       "suite" : {
         "name1" : null,
         "name2" : null
       }
     }
     // Internal

      {
        "name" : { "$Result" : { }, "$Path" : "name" }
        "suite" : {
          "$path'
          name1 : { $Result : {} , "$Path" : "suite.name1" }
          name1 : { $Result : {} , "$Path" : "suite.name2"}
        }
      }
   *)


   { TTestInsightServer }

   // Object Receiver frees received object.
   TTestNamesEvent = Procedure(Sender : TObject; Tests : TTestItem) of object;
   TSelectedTestsEvent = Procedure(Sender : TObject; var Tests: TStringDynArray) of object;
   TTestResultEvent = Procedure(Sender : TObject; aResult : TTestInsightResultArray) of object;
   TTestsStartedEvent = Procedure(Sender : TObject; aCount : Integer) of object;
   TTestsOptionsEvent = Procedure(Sender : TObject; aOptions : TTestInsightOptions) of object;
   TInsightMessageType = (imtInfo,imtError);
   TTestInsightLogEvent = Procedure(Sender : TObject; const aType : TInsightMessageType; const aMessage : String) of object;

   TTestInsightServer = class(TComponent)
   private
     FBasePath: String;
     FOnClearTests: TNotifyEvent;
     FOnLog: TTestInsightLogEvent;
     FOnTestsFinished: TNotifyEvent;
     FOnTestsStarted: TTestsStartedEvent;
     FServer : TFPHttpServer;
     FOnSelectedTests: TSelectedTestsEvent;
     FOnSetTestNames: TTestNamesEvent;
     FOnTestResult: TtestResultEvent;
     FSelectedTests : String;
     FTestInsightResultClass: TTestInsightResultClass;
     FTestSuite : TTestItem;
     FTestCount : Integer;
     FInsightOptions : TTestInsightOptions;
     FOnGetOptions: TTestsOptionsEvent;
     FThread: TThread;
     FServerPort : Word;
     FServerActive : Boolean;
{$IFDEF USECORS}
     FCorsSupport : TCORSSupport;
{$ENDIF}
     procedure CreateServer;
     function ExtractResults(anArray: TJSONArray): TTestInsightResultArray;
     procedure FreeResults(Results: TTestInsightResultArray);
     function GetPort: Word;
     procedure HandleStartThreadTerminate(Sender: TObject);
     procedure SetBasePath(AValue: String);
{$IFDEF USECORS}
     procedure SetCorsSupport(AValue: TCORSSupport);
{$ENDIF}
     procedure SetPort(AValue: Word);
   Protected
     Procedure DoLog(const aType : TInsightMessageType; const aMessage : String);
     Procedure DoLog(const aType : TInsightMessageType; const Fmt : String; Args : Array of const);
     // Override if you want to create a descendent.
     function CreateTestInsightOptions: TTestInsightOptions; virtual;
     // these are called in the main thread
     procedure DoGetSelectedTests; virtual;
     procedure DoSetTestNames; virtual;
     procedure DoTestsStarted; virtual;
     procedure DoTestsFinished; virtual;
     procedure DoClearTests; virtual;
     procedure DoOptions; virtual;
     procedure Send400(aResponse: TFPHTTPConnectionResponse; aText: String);
     Procedure Send404(aResponse : TFPHTTPConnectionResponse);
     Procedure Send405(aResponse : TFPHTTPConnectionResponse);
     Procedure Send200(aResponse : TFPHTTPConnectionResponse);
     procedure SetTestList(ARequest: TFPHTTPConnectionRequest; aResponse: TFPHTTPConnectionResponse);
     procedure DoTestResults(ARequest: TFPHTTPConnectionRequest;  aResponse: TFPHTTPConnectionResponse);
     procedure ClearTestList(aResponse: TFPHTTPConnectionResponse); virtual;
     procedure DoFinishTests(aResponse: TFPHTTPConnectionResponse); virtual;
     procedure DoStartTests(aResponse: TFPHTTPConnectionResponse; aCount: Integer); virtual;
     procedure SendSelectedTestList(aResponse: TFPHTTPConnectionResponse); virtual;
     procedure DoGetOptions(aResponse: TFPHTTPConnectionResponse); virtual;
     procedure DoRequest(Sender: TObject;
       var ARequest: TFPHTTPConnectionRequest;
       var AResponse: TFPHTTPConnectionResponse);
   Public
     constructor create(aOwner : TComponent); override;
     destructor destroy; override;
     procedure StartServer;
     procedure StopServer;
     Property Active : Boolean Read FServerActive;
     // TestResultClass
     Property TestInsightResultClass : TTestInsightResultClass Read FTestInsightResultClass Write FTestInsightResultClass;
     // Options sent to the client.
     property InsightOptions : TTestInsightOptions Read FInsightOptions;
     // Port on which to listen
     property Port: Word Read GetPort Write SetPort;
     // First part of URL. By default: /tests
     Property BasePath : String Read FBasePath Write SetBasePath;
     // CORS Support ?
{$IFDEF USECORS}
     Property CorsSupport : TCORSSupport Read FCorsSupport Write SetCorsSupport;
{$ENDIF}
     // Set the list of tests. Event handler must free JSON object.
     Property OnSetTestNames : TTestNamesEvent Read FOnSetTestNames Write FOnSetTestNames;
     // Get the list of selected tests. The server will free the received object.
     Property OnGetSelectedTests : TSelectedTestsEvent Read FOnSelectedTests Write FOnSelectedTests;
     // Results of run tests. Event handler must free result array.
     Property OnTestResult : TTestResultEvent Read FOnTestResult Write FOnTestResult;
     // Tests started.
     Property OnTestsStarted : TTestsStartedEvent  Read FOnTestsStarted Write FOnTestsStarted;
     // Tests finished.
     Property OnTestsFinished : TNotifyEvent  Read FOnTestsFinished Write FOnTestsFinished;
     // Clear tests.
     Property OnClearTests : TNotifyEvent  Read FOnClearTests Write FOnClearTests;
     // Last change to set options before sending to the client. If not, Options are sent as-is.
     Property OnGetOptions : TTestsOptionsEvent Read FOnGetOptions Write FOnGetOptions;
     // Diagnostic messages
     Property OnLog : TTestInsightLogEvent Read FOnLog Write FOnLog;
   end;


implementation

Uses ssockets;

Type

  { TStartServerThread }

  TStartServerThread = Class(TThread)
  Private
    FServer : TFPHttpServer;
    FStartErrorClass: String;
    FStartErrorMessage: String;
  Public
    constructor Create(aServer: TFPHttpServer; aOnTerminate: TNotifyEvent);
    Procedure Execute; override;
    Property StartErrorMessage : String Read FStartErrorMessage;
    Property StartErrorClass : String Read FStartErrorClass;
  end;

{ TTestItem }

constructor TTestItem.Create(aParent: TTestItem; const aTestName: String);
begin
  TestParent:=aParent;
  TestName:=aTestName;
end;

destructor TTestItem.Destroy;

Var
  I : Integer;

begin
  For I:=0 to Length(Tests)-1 do
    FreeAndNil(Tests[i]);
  SetLength(Tests,0);
  FreeAndNil(TestResult);
  Inherited;
end;

procedure TTestItem.FromJSON(aJSON: TJSONObject);

Var
  I : Integer;

begin
  SetLength(Tests,aJSON.Count);
  For I:=0 to aJSON.Count-1 do
    begin
    Tests[i]:=TTestItem.Create(Self, aJSON.Names[i]);
    if aJSON.Items[i].JSONType=jtObject then
      Tests[i].FromJSON(TJSONObject(aJSON.Items[i]));
    end;
end;

function TTestItem.TestPath: String;

Var
  P : TTestItem;

begin
  if Self=Nil then exit('');
  Result:=TestName;
  P:=TestParent;
  While (P<>Nil) do
    begin
    if P.TestName<>'' then
      Result:=P.TestName+'.'+Result;
    P:=P.TestParent;
    end;
end;

function TTestItem.CountTestCases: Integer;

Var
  I : integer;

begin
  if Self=Nil then exit(0);
  Result:=0;
  for I:=0 to Length(Tests)-1 do
    Result:=Result+Tests[i].CountTestCases;
  If (Result=0) and (TestName<>'') then
    Inc(Result);
end;

function TTestItem.IsTestSuite: Boolean;
begin
  if Self=Nil then exit(False);
  Result:=Length(Tests)>0;
end;

function TTestItem.IsTestCase: Boolean;
begin
  if Self=Nil then exit(False);
  Result:=Length(Tests)=0;
end;

{ TStartServerThread }

constructor TStartServerThread.Create(aServer: TFPHttpServer; aOnTerminate : TNotifyEvent);
begin
  FServer:=aServer;
  FreeOnTerminate:=True;
  OnTerminate:=aOnTerminate;
  Inherited Create(False);
end;

procedure TStartServerThread.Execute;
begin
  try
    FServer.Active:=true;
  except
    On E : Exception do
      begin
      FStartErrorClass:=E.ClassName;
      FStartErrorMessage:=E.Message;
      end;
  end;
end;

{ TTestInsightServer }

Type

  { TTransferTestResult }

  TTransferTestResult = Class
  Private
    FEvent: TTestResultEvent;
    FSender : TObject;
    FResult : TTestInsightResultArray;
  Public
    constructor create (aEvent: TTestResultEvent; aSender : TObject; aResult : TTestInsightResultArray);
    procedure DoResultEvent;
  end;

constructor TTransferTestResult.create(aEvent: TTestResultEvent; aSender: TObject; aResult: TTestInsightResultArray);
begin
  FEvent:=aEvent;
  FSender:=aSender;
  FResult:=aResult;
end;

procedure TTransferTestResult.DoResultEvent;

begin
  if Assigned(FEvent) then
    FEvent(FSender,FResult);
end;

procedure TTestInsightServer.DoSetTestNames;
begin
  if Assigned(FOnSetTestNames) then
    FOnSetTestNames(Self,FTestSuite);
end;

procedure TTestInsightServer.DoTestsStarted;
begin
  if Assigned(FOnTestsStarted) then
    FOnTestsStarted(Self,Self.FTestCount);
end;

procedure TTestInsightServer.DoTestsFinished;
begin
  if Assigned(FOnTestsFinished) then
    FOnTestsFinished(Self);
end;

procedure TTestInsightServer.DoClearTests;
begin
  if Assigned(FOnClearTests) then
    FOnClearTests(Self);
end;

procedure TTestInsightServer.DoOptions;
begin
  if Assigned(FOnGetOptions) then
    FOnGetOptions(Self,FInsightOptions);
end;


procedure TTestInsightServer.SetTestList(ARequest: TFPHTTPConnectionRequest; aResponse: TFPHTTPConnectionResponse);

Var
  D : TJSONData;

begin
  D:=Nil;
  try
    if SameText(aRequest.ContentType,'application/json') then
      D:=GetJSON(aRequest.Content)
    else if SameText(aRequest.ContentType,'text/text') then
      D:=TestStringsToJSON(aRequest.Content)
    else
      Send400(aResponse,'Invalid content type. Need application/json or text/text');
    if (D<>Nil) and not (D is TJSONObject) then
      begin
      Send400(aResponse,'Invalid JSON, need object');
      FreeAndNil(D);
      end;
  except
    on E : exception do
      begin
      Send400(aResponse,E.Message);
      Raise;
      end;
  end;
  if D=nil then
    Exit;
  // Here we have a valid test list
  Send200(aResponse);
  if Assigned(D) then
    begin
    FTestSuite:=TtestItem.Create(Nil,'');
    FTestSuite.FromJSON(D as TJSONObject);
    if Assigned(OnSetTestNames) then
      TThread.Synchronize(TThread.CurrentThread,@DoSetTestNames)
    else
      FTestSuite.Free;
    D.Free;
    end;
end;

Function TTestInsightServer.ExtractResults(anArray : TJSONArray) : TTestInsightResultArray;

Var
  i,aLen : Integer;
  Res: TTestInsightResult;


begin
  aLen:=0;
  SetLength(Result,anArray.Count);
  For I:=0 to anArray.Count-1 do
    begin
    if anArray.Types[i]=jtObject then
      begin
      Res:=TestInsightResultClass.Create;
      try
        Res.FromJSOn(anArray.Objects[i]);
      except
        on E : Exception do
          begin
          FreeAndNil(res);
          DoLog(imtError,'Error %s extracting test result: %s',[E.ClassName, E.Message]);
          end;
      end;
      if Assigned(Res) then
        begin
        Result[aLen]:=Res;
        Inc(aLen);
        end;
      end;
    end;
end;

procedure TTestInsightServer.DoTestResults(ARequest: TFPHTTPConnectionRequest; aResponse: TFPHTTPConnectionResponse);

Var
  D : TJSONData;
  Results : TTestInsightResultArray;
  Trans : TTransferTestResult;

begin
  Results:=Nil;
  try
    D:=GetJSON(aRequest.Content);
  except
    on E : exception do
      begin
      Send400(aResponse,E.Message);
      Raise;
      end;
  end;
  try
    if D is TJSONArray then
      begin
      Send200(aResponse);
      Results:=ExtractResults(D as TJSONArray);
      end
    else if (D is TJSONObject) and (D.Count=1) and (D.Items[0] is TJSONArray) then
      begin
      Send200(aResponse);
      Results:=ExtractResults(TJSONObject(D).Extract(0) as TJSONArray);
      end
    else
      Send400(aResponse,'Bad JSON message');
  finally
    D.Free;
  end;
  if not (Assigned(Results) and Assigned(OnTestResult)) then
    Exit;
  Trans:=TTransferTestResult.create(OnTestResult,Self,Results);
  try
    TThread.Synchronize(TThread.CurrentThread,@Trans.DoResultEvent);
    FreeResults(Results);
  finally
    Trans.Free;
  end;
end;

procedure TTestInsightServer.FreeResults(Results : TTestInsightResultArray);

Var
  Res : TTestInsightResult;

begin
  For Res in Results do
    Res.Free;
  SetLength(Results,0);
end;


procedure TTestInsightServer.DoGetSelectedTests;

Var
  aTests : TStringDynArray;
  D : TJSONObject;
  A : TJSONArray;
  S : String;

begin
  aTests:=Nil;
  if Assigned(FOnSelectedTests) then
    FOnselectedTests(Self,aTests);
  A:=TJSONArray.Create;
  try
    D:=TJSONObject.Create(['tests',A]);
    For S in aTests do
      A.Add(S);
    FSelectedTests:=D.asJSON;
  Finally
    D.Free;
  end;
end;

procedure TTestInsightServer.SendSelectedTestList(aResponse: TFPHTTPConnectionResponse);
begin
  FSelectedTests:='';
  if Assigned(FOnSelectedTests) then
    TThread.Synchronize(TThread.CurrentThread,@DoGetSelectedTests);
  if FSelectedTests='' then
    FSelectedTests:='{ "tests" : [] }';
  aResponse.Content:=FSelectedTests;
  aResponse.ContentType:='application/json';
  send200(aResponse);
end;

procedure TTestInsightServer.DoGetOptions(aResponse: TFPHTTPConnectionResponse);
begin
  if Assigned(FONGetOptions) then
    TThread.Synchronize(TThread.CurrentThread,@DoOptions);
  aResponse.Content:=FInsightOptions.ToJSON;
  aResponse.ContentType:='application/json';
  send200(aResponse);
end;

procedure TTestInsightServer.ClearTestList(aResponse: TFPHTTPConnectionResponse);

begin
  Send200(aResponse);
  if Assigned(FOnClearTests) then
    TThread.Synchronize(TThread.CurrentThread,@DoClearTests);
end;

procedure TTestInsightServer.DoStartTests(aResponse: TFPHTTPConnectionResponse; aCount : Integer);

begin
  Send200(aResponse);
  FTestCount:=aCount;
  if Assigned(FOnTestsStarted) then
    TThread.Synchronize(TThread.CurrentThread,@DoTestsStarted);
end;

procedure TTestInsightServer.DoFinishTests(aResponse: TFPHTTPConnectionResponse);

begin
  Send200(aResponse);
  if Assigned(FonTestsFinished) then
    TThread.Synchronize(TThread.CurrentThread,@DoTestsFinished);
end;

procedure TTestInsightServer.DoRequest(Sender: TObject;
  var ARequest: TFPHTTPConnectionRequest;
  var AResponse: TFPHTTPConnectionResponse);

  function CheckMethod(aMethod : String) : Boolean;

  begin
    Result:=SameText(aMethod,aRequest.Method);
    if not Result then
      Send405(aResponse);
  end;

Var
  aPath : String;

begin
  aPath:=aRequest.PathInfo;
  DoLog(imtInfo,'Handling request %s %s',[aRequest.Method,aPath]);
  if not SameText(Copy(aPath,1,Length(BasePath)),BasePath) then
    Send404(aResponse)
  else
    begin
{$IFDEF USECORS}
    if FCorsSupport.HandleRequest(aRequest,aResponse,[hcDetect, hcsend]) then
      exit;
{$ENDIF}
    Delete(aPath,1,Length(BasePath));
    if (aPath='') then // '/tests'
      begin
      if SameText(aRequest.Method,'POST') then
        SetTestList(aRequest,aResponse)
      else if SameText(aRequest.Method,'get') then
        SendSelectedTestList(aResponse)
      else if SameText(aRequest.Method,'delete') then
        ClearTestList(aResponse)
      else
        Send405(aResponse)
      end
    else
      begin
      If aPath[1]='/' then
        delete(aPath,1,1);
      if SameText(aPath,pathStarted) then
        begin
        if CheckMethod('POST') then
          DoStartTests(aResponse,StrToIntDef(aRequest.QueryFields.Values[qryTotalCount],-1));
        end
      else if SameText(aPath,pathFinished) then
        begin
        if CheckMethod('POST') then
          DoFinishTests(aResponse);
        end
      else if SameText(aPath,PathResults) then
        begin
        if CheckMethod('POST') then
          DoTestResults(aRequest,aResponse);
        end
      else if SameText(aPath,PathOptions) then
        begin
        if CheckMethod('GET') then
          DoGetOptions(aResponse);
        end
      else
        Send404(aResponse);
      end;
    end;
end;

function TTestInsightServer.GetPort: Word;
begin
  if Assigned(FServer) then
    Result:=FServer.Port
  else
    Result:=FServerPort;
end;

procedure TTestInsightServer.HandleStartThreadTerminate(Sender: TObject);

var
  SThread : TStartServerThread ;
  ErrClass,ErrMsg : String;

begin
  If Sender<>FThread then exit;
  SThread:=Sender as TStartServerThread;
  ErrClass:=SThread.StartErrorClass;
  ErrMsg:=SThread.StartErrorMessage;
  if ErrClass<>'' then
    DoLog(imtError,'Error %s starting server: %s',[ErrClass,ErrMsg]);
  FThread:=Nil;
  FServerActive:=False;

end;


procedure TTestInsightServer.SetBasePath(AValue: String);

var
  len : integer;

begin
  // Make sure it starts with /
  if (AValue<>'') and (AValue[1]<>'/') then
    AValue:='/'+AValue;
  // Make sure it does not end with /
  Len:=Length(AValue);
  if (Len>1) and (AValue[Len]='/') then
    AValue:=Copy(AValue,1,Len-1)
  else if Len<2 then
    Raise ETestInsight.Create('Basepath cannot be empty');
  if FBasePath=AValue then Exit;
    FBasePath:=AValue;
end;

{$IFDEF USECORS}
procedure TTestInsightServer.SetCorsSupport(AValue: TCORSSupport);
begin
  if FCorsSupport=AValue then Exit;
  FCorsSupport.Assign(AValue);
end;
{$ENDIF}

procedure TTestInsightServer.SetPort(AValue: Word);
begin
  FServer.Port:=aValue;
  FServerPort:=aValue;
end;


procedure TTestInsightServer.DoLog(const aType : TInsightMessageType;const aMessage: String);
begin
  If Assigned(FOnLog) then
    FOnLog(Self,aType,aMessage);
end;

procedure TTestInsightServer.DoLog(const aType : TInsightMessageType;const Fmt: String; Args: array of const);
begin
  DoLog(aType,Format(Fmt,Args));
end;

procedure TTestInsightServer.Send400(aResponse: TFPHTTPConnectionResponse; aText : String);
begin
  aResponse.Code:=400;
  aResponse.CodeText:='400 BAD REQUEST';
  if aText<>'' then
    aResponse.Content:=aText
  else
    aResponse.Content:='The requested URL is invalid';
  aResponse.ContentType:='text/text';
  aResponse.SendResponse;
end;

procedure TTestInsightServer.Send404(aResponse: TFPHTTPConnectionResponse);
begin
  aResponse.Code:=404;
  aResponse.CodeText:='NOT FOUND';
  aResponse.Content:='The requested URL is invalid';
  aResponse.ContentType:='text/text';
  aResponse.SendResponse;
end;

procedure TTestInsightServer.Send405(aResponse: TFPHTTPConnectionResponse);
begin
  aResponse.Code:=405;
  aResponse.CodeText:='METHOD NOT ALLOWED';
  aResponse.Content:='The requested HTTP method is invalid';
  aResponse.ContentType:='text/text';
  aResponse.SendResponse;
end;

procedure TTestInsightServer.Send200(aResponse: TFPHTTPConnectionResponse);
begin
  aResponse.Code:=200;
  aResponse.CodeText:='OK';
  aResponse.ContentType:='application/json';
  aResponse.SendResponse;
end;

constructor TTestInsightServer.create(aOwner: TComponent);
begin
  inherited create(aOwner);
  CreateServer;
  BasePath:=pathTests;
  FTestInsightResultClass:=TTestInsightResult;
  FInsightOptions:=CreateTestInsightOptions;
{$IFDEF USECORS}
  FCorsSupport:=TCORSSupport.Create;
  FCorsSupport.Enabled:=True;
{$ENDIF}
end;


destructor TTestInsightServer.destroy;
begin
  StopServer;
  FreeAndNil(FServer);
  FreeAndNil(FInsightOptions);
{$IFDEF USECORS}
  FreeAndNil(FCorsSupport);
{$ENDIF}
  inherited destroy;
end;

procedure TTestInsightServer.CreateServer;

begin
  FServer:=TFPHttpServer.Create(Self);
  FServer.Threaded:=True;
  if FServerPort>0 then
    FServer.Port:=FServerPort;
end;

function TTestInsightServer.CreateTestInsightOptions: TTestInsightOptions;

begin
  Result:=TTestInsightOptions.Create;
end;

procedure TTestInsightServer.StartServer;
begin
  if FServerActive then
    exit;
  FServer.OnRequest:=@DoRequest;
  FServerActive:=True;
  FThread:=TStartServerThread.Create(FServer,@HandleStartThreadTerminate);
  DoLog(imtInfo,'Starting test insight server on port %d',[Port]);
end;

procedure TTestInsightServer.StopServer;
begin

  if not Assigned(FServer) then
    exit;
  if not FServer.Active then
    exit;
  FServer.OnRequest:=Nil;
  FServerActive:=False;
  DoLog(imtInfo,'Deactivating server');
  FServer.Active:=False;
  DoLog(imtInfo,'Fake request');
  Try
    TInetSocket.Create('localhost',FServer.Port,10,Nil).Free;
  except
    on E  : Exception do
     DoLog(imtError,'Fake request resulted in %s: %s',[E.ClassName,E.Message]);
  end;
  DoLog(imtInfo,'Waiting for server thread to stop');
  If Assigned(FThread) then
    FThread.WaitFor;
  DoLog(imtInfo,'Server thread stopped');
end;



end.