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
|
unit TestInsightClient;
{$mode ObjFPC}{$H+}
interface
uses
Classes, SysUtils, types, fphttpclient, fpjson, contnrs, inifiles,
TestInsightProtocol;
Type
{ TAbstractTestInsightClient }
TAbstractTestInsightClient = class (TObject)
private
FOptions : TTestInsightOptions;
FPendingResultCount: Integer;
FResults : TFPObjectList;
FBaseURL : String;
procedure SetOptions(const value: TTestInsightOptions);
protected
function GetHasError: Boolean; virtual; abstract;
function GetLastErrorMessage: String; virtual; abstract;
function JSONToTests(const aJSON: string): TStringDynArray;
// URL is Relative to base URL
procedure ServerPost(aURL: string; const aContent: string = ''); virtual; abstract;
Function ServerGet(aURL: string) : string; virtual; abstract;
Procedure ServerDelete(aURL: string) ; virtual; abstract;
Function ConcatURL(aURL : String) : String; virtual;
public
constructor Create(const aBaseURL : String); virtual;
destructor Destroy; override;
procedure LoadConfig(const aConfigFileName : String; aSection : String = '');
procedure LoadConfig(aIni : TCustomIniFile; aSection : String);
procedure GetServerOptions;
// The client will free the result.
procedure PostResult(const testResult: TTestInsightResult; forceSend: Boolean);
// The client will free the results.
procedure PostResults(const testResults: array of TTestInsightResult; forceSend: Boolean);
procedure StartedTesting(const totalCount: Integer);
procedure FinishedTesting;
procedure ClearTests;
Procedure SetTestNames(aJSON : TJSONObject);
function GetTests: TStringDynArray;
property Options: TTestInsightOptions read FOptions write SetOptions;
Property HasError : Boolean Read GetHasError;
Property LastErrorMessage : String Read GetLastErrorMessage;
Property BaseURL : String Read FBaseURL;
Property PendingResultCount : Integer Read FPendingResultCount;
end;
{ TTestInsightHTTPClient }
TTestInsightHTTPClient = class(TAbstractTestInsightClient)
private
FHttp: TFPHTTPClient;
FLastError : String;
FInError : Boolean;
Procedure SetError(E : Exception);
protected
function GetHasError: Boolean; override;
function GetLastErrorMessage: string; override;
procedure ServerPost(aURL: string; const aContent: string = ''); override;
Function ServerGet(aURL: string) : string; override;
Procedure ServerDelete(aURL: string); override;
public
Constructor Create(Const aBaseURL : String); override;
Destructor Destroy; override;
end;
implementation
{ TTestInsightHTTPClient }
procedure TTestInsightHTTPClient.ServerPost(aURL: string; const aContent: string = '');
Var
Body,Res : TStringStream;
begin
Body:=Nil;
Res:=TStringStream.Create('');
try
// Writeln('Sending content to; ',ConcatURL(aURL),':');
// Writeln(aContent);
if aContent<>'' then
Body:=TStringStream.Create(aContent);
FHTTP.RequestBody:=Body;
try
FHTTP.AddHeader('Content-Type','application/json');
FHTTP.Post(ConcatURL(aURL),Res);
SetError(Nil);
except
on E : Exception do
SetError(E);
end;
finally
FHTTP.RequestBody:=Nil;
Res.Free;
Body.Free;
end;
end;
function TTestInsightHTTPClient.ServerGet(aURL: string): string;
Var
Res : TStringStream;
begin
Res:=TStringStream.Create('');
try
try
// Writeln('Get URL ',ConcatURL(aURL),':');
FHTTP.Get(ConcatURL(aURL),Res);
Result:=Res.DataString;
// Writeln(Result);
SetError(Nil);
except
on E : Exception do
SetError(E);
end;
finally
Res.Free;
end;
end;
procedure TTestInsightHTTPClient.ServerDelete(aURL: string);
begin
try
FHTTP.Delete(ConcatURL(aURL));
SetError(Nil);
except
on E : Exception do
SetError(E);
end;
end;
constructor TTestInsightHTTPClient.Create(const aBaseURL: String);
begin
inherited Create(aBaseURL);
FHTTP:=TFPHTTPClient.Create(Nil);
// FHTTP.ConnectTimeout:=100;
// FHTTP.IOTimeout:=1000;
end;
destructor TTestInsightHTTPClient.Destroy;
begin
FreeAndNil(FHTTP);
inherited Destroy;
end;
procedure TTestInsightHTTPClient.SetError(E: Exception);
begin
FInError:=Assigned(E);
if Assigned(E) then
FLastError:=E.ClassName+': '+E.Message
else
FLastError:='';
end;
function TTestInsightHTTPClient.GetHasError: Boolean;
begin
Result:=FInError;
end;
function TTestInsightHTTPClient.GetLastErrorMessage: string;
begin
Result:=FLastError;
end;
{ TAbstractTestInsightClient }
procedure TAbstractTestInsightClient.SetOptions(const value: TTestInsightOptions
);
begin
FOptions.Assign(Value);
end;
function TAbstractTestInsightClient.JSONToTests(const aJSON: string): TStringDynArray;
Var
D : TJSONData;
A : TJSONArray;
I : Integer;
begin
Result:=[];
D:=GetJSON(aJSON);
try
if D=Nil then exit;
if D.JSONType=jtArray then
A:=D as TJSONArray
else if (D.Count=1) and (D.Items[0].JSONType=jtArray) then
A:=D.Items[0] as TJSONArray
else
A:=nil;
if A<>Nil then
begin
SetLength(Result,a.Count);
For I:=0 to Length(Result)-1 do
Result[i]:=A.Strings[i];
end;
finally
D.Free;
end;
end;
function TAbstractTestInsightClient.ConcatURL(aURL: String): String;
begin
Result:=fBaseURL;
if (Result<>'') and (aURL<>'') and (Result[Length(Result)]<>'/') then
Result:=Result+'/';
Result:=Result+aURL;
end;
constructor TAbstractTestInsightClient.Create(const aBaseURL: String);
begin
FBaseURL:=aBaseURL;
FOptions:=TTestInsightOptions.Create;
FResults:=TFPObjectList.Create(False);
end;
destructor TAbstractTestInsightClient.Destroy;
begin
FResults.Clear;
FreeAndNil(FResults);
FreeAndNil(FOptions);
inherited Destroy;
end;
procedure TAbstractTestInsightClient.LoadConfig(const aConfigFileName: String; aSection : String = '');
Var
aIni: TCustomIniFile;
begin
aIni:=TMemIniFile.Create(aConfigFileName);
try
if aSection='' then
aSection:=SConfig;
LoadConfig(aIni,aSection);
finally
aIni.Free;
end;
end;
procedure TAbstractTestInsightClient.LoadConfig(aIni: TCustomIniFile;aSection : String);
begin
FBaseURL:=aIni.ReadString(aSection,KeyBaseURL,BaseURL);
Options.ShowProgress:=aIni.ReadBool(aSection,keyShowProgress,Self.Options.ShowProgress);
Options.ExecuteTests:=aIni.ReadBool(aSection,keyExecuteTests,Self.Options.ExecuteTests);
Options.TestSuite:=aIni.ReadString(aSection,keySuite,Self.Options.TestSuite);
end;
procedure TAbstractTestInsightClient.GetServerOptions;
begin
Options.FromJSON(ServerGet(pathOptions))
end;
procedure TAbstractTestInsightClient.PostResult(
const testResult: TTestInsightResult; forceSend: Boolean);
begin
PostResults([testResult],forceSend);
end;
procedure TAbstractTestInsightClient.PostResults(
const testResults: array of TTestInsightResult; forceSend: Boolean);
Var
Res : TTestInsightResult;
J : TJSONArray;
O : TJSONOBject;
begin
if ForceSend or (Options.ShowProgress and Options.ExecuteTests) then
begin
J:=TJSONArray.Create;
try
For Res in testResults do
begin
O:=TJSONObject.Create;
J.Add(O);
Res.ToJSON(O);
Res.Free;
end;
ServerPost(pathResults,J.AsJSON);
finally
J.Free;
end;
end
else
For Res in TestResults do
FResults.Add(res);
end;
procedure TAbstractTestInsightClient.StartedTesting(const totalCount: Integer);
begin
ServerPost(Format('%s?%s=%d', [pathStarted,qryTotalCount,Totalcount]),'');
end;
procedure TAbstractTestInsightClient.FinishedTesting;
Var
A : Array of TTestInsightResult;
Len,I : Integer;
begin
A:=[];
Len:=FResults.Count;
if (Len>0) then
begin
Setlength(A,Len);
For I:=0 to Len-1 do
A[I]:=TTestInsightResult(FResults[i]);
try
PostResults(A,True);
finally
FResults.Clear;
end;
end;
ServerPost(pathFinished,'');
end;
procedure TAbstractTestInsightClient.ClearTests;
begin
ServerDelete(pathResults);
end;
procedure TAbstractTestInsightClient.SetTestNames(aJSON : TJSONObject);
begin
ServerPost('',aJSON.AsJSON);
end;
function TAbstractTestInsightClient.GetTests: TStringDynArray;
begin
Result:=JSONToTests(ServerGet(''));
end;
end.
|