Added status expectation

This commit is contained in:
Andreas Schneider 2015-10-08 11:59:41 +02:00
parent 8e820eb2f3
commit 7cc7705b29
2 changed files with 52 additions and 0 deletions

View File

@ -123,6 +123,11 @@ Once the first none-empty line is unrecognized (no command found), the parser wi
* `isodate` (Date according to ISO8601) * `isodate` (Date according to ISO8601)
* `isotime` (Time according to ISO8601) * `isotime` (Time according to ISO8601)
`Expect <expectation type>=<expecation>`
: Checks expectations after the request.
* `Status=<statuscode>`: checks, if the status code matches
If an expectation fails, the application exists with code 5.
`Call <URL>` `Call <URL>`
: This prepares the actual call by providing the URL to be called. : This prepares the actual call by providing the URL to be called.
Variables in the form of `@<variablename>` are replaced accordingly. Variables in the form of `@<variablename>` are replaced accordingly.

View File

@ -56,6 +56,7 @@ type
FFormFields: TStrings; FFormFields: TStrings;
FFilters: TFilterList; FFilters: TFilterList;
FBeautify: Boolean; FBeautify: Boolean;
FExpectations: TStringList;
FURL: String; FURL: String;
FMethod: String; FMethod: String;
//Main //Main
@ -70,11 +71,13 @@ type
procedure CmdProxy(AData: String); procedure CmdProxy(AData: String);
procedure CmdProxyAuth(AData: String); procedure CmdProxyAuth(AData: String);
procedure CmdGenerate(AData: String); procedure CmdGenerate(AData: String);
procedure CmdExpect(AData: String);
procedure ProcessCall(AURL: String); procedure ProcessCall(AURL: String);
//Helper //Helper
procedure WriteHelp; procedure WriteHelp;
procedure ListTemplates; procedure ListTemplates;
procedure WriteContent; procedure WriteContent;
procedure CheckExpectations;
public public
constructor Create; overload; constructor Create; overload;
destructor Destroy; override; destructor Destroy; override;
@ -273,6 +276,11 @@ begin
Result := True; Result := True;
CmdGenerate(Copy(ALine, 10, Length(ALine))); CmdGenerate(Copy(ALine, 10, Length(ALine)));
end else end else
if AnsiStartsStr('Expect ', ALine) then
begin
Result := True;
CmdExpect(Copy(ALine, 8, Length(ALine)));
end else
if AnsiStartsStr('Call ', ALine) then if AnsiStartsStr('Call ', ALine) then
begin begin
Result := True; Result := True;
@ -422,6 +430,11 @@ begin
end; end;
end; end;
procedure TRestemplateApplication.CmdExpect(AData: String);
begin
FExpectations.Add(AData);
end;
procedure TRestemplateApplication.ProcessCall(AURL: String); procedure TRestemplateApplication.ProcessCall(AURL: String);
var var
s, httpMethod: String; s, httpMethod: String;
@ -524,6 +537,18 @@ begin
response.Free; response.Free;
request.Free; request.Free;
try
CheckExpectations;
except
on e: Exception do
begin
writeln;
writeln('Expections failed:');
writeln(e.Message);
ExitCode := 5;
end;
end;
end; end;
procedure TRestemplateApplication.WriteHelp; procedure TRestemplateApplication.WriteHelp;
@ -620,6 +645,26 @@ begin
highlights.Free; highlights.Free;
end; end;
procedure TRestemplateApplication.CheckExpectations;
var
i: Integer;
expectType, expectation: String;
begin
for i := 0 to FExpectations.Count - 1 do
begin
expectType := LowerCase(FExpectations.Names[i]);
expectation := FExpectations.ValueFromIndex[i];
case expectType of
'status':
if FHttp.ResponseCode <> StrToInt(expectation) then
raise Exception.Create(Format('Status code unexpected! %d <> %s', [FHttp.ResponseCode, expectation]));
else
raise Exception.Create('Invalid expectation: ' + FExpectations[i]);
end;
end;
end;
constructor TRestemplateApplication.Create; constructor TRestemplateApplication.Create;
begin begin
inherited Create(nil); inherited Create(nil);
@ -636,6 +681,7 @@ begin
FRequest.RawHeaders.AddValue('User-Agent', 'Mozilla/4.0 (compatible; restemplate ' + VersionInfo.GetProductVersionString + ')'); FRequest.RawHeaders.AddValue('User-Agent', 'Mozilla/4.0 (compatible; restemplate ' + VersionInfo.GetProductVersionString + ')');
FFilters := TFilterList.Create; FFilters := TFilterList.Create;
FParser := TJTemplateParser.Create; FParser := TJTemplateParser.Create;
FExpectations := TStringList.Create;
end; end;
destructor TRestemplateApplication.Destroy; destructor TRestemplateApplication.Destroy;
@ -647,6 +693,7 @@ begin
//TODO Owned? FRequest.Free; //TODO Owned? FRequest.Free;
FFilters.Free; FFilters.Free;
FParser.Free; FParser.Free;
FExpectations.Free;
inherited Destroy; inherited Destroy;
end; end;