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)
* `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>`
: This prepares the actual call by providing the URL to be called.
Variables in the form of `@<variablename>` are replaced accordingly.

View File

@ -56,6 +56,7 @@ type
FFormFields: TStrings;
FFilters: TFilterList;
FBeautify: Boolean;
FExpectations: TStringList;
FURL: String;
FMethod: String;
//Main
@ -70,11 +71,13 @@ type
procedure CmdProxy(AData: String);
procedure CmdProxyAuth(AData: String);
procedure CmdGenerate(AData: String);
procedure CmdExpect(AData: String);
procedure ProcessCall(AURL: String);
//Helper
procedure WriteHelp;
procedure ListTemplates;
procedure WriteContent;
procedure CheckExpectations;
public
constructor Create; overload;
destructor Destroy; override;
@ -273,6 +276,11 @@ begin
Result := True;
CmdGenerate(Copy(ALine, 10, Length(ALine)));
end else
if AnsiStartsStr('Expect ', ALine) then
begin
Result := True;
CmdExpect(Copy(ALine, 8, Length(ALine)));
end else
if AnsiStartsStr('Call ', ALine) then
begin
Result := True;
@ -422,6 +430,11 @@ begin
end;
end;
procedure TRestemplateApplication.CmdExpect(AData: String);
begin
FExpectations.Add(AData);
end;
procedure TRestemplateApplication.ProcessCall(AURL: String);
var
s, httpMethod: String;
@ -524,6 +537,18 @@ begin
response.Free;
request.Free;
try
CheckExpectations;
except
on e: Exception do
begin
writeln;
writeln('Expections failed:');
writeln(e.Message);
ExitCode := 5;
end;
end;
end;
procedure TRestemplateApplication.WriteHelp;
@ -620,6 +645,26 @@ begin
highlights.Free;
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;
begin
inherited Create(nil);
@ -636,6 +681,7 @@ begin
FRequest.RawHeaders.AddValue('User-Agent', 'Mozilla/4.0 (compatible; restemplate ' + VersionInfo.GetProductVersionString + ')');
FFilters := TFilterList.Create;
FParser := TJTemplateParser.Create;
FExpectations := TStringList.Create;
end;
destructor TRestemplateApplication.Destroy;
@ -647,6 +693,7 @@ begin
//TODO Owned? FRequest.Free;
FFilters.Free;
FParser.Free;
FExpectations.Free;
inherited Destroy;
end;