From 7cc7705b298bd09b0e2e7951fb53c31ff5b1402a Mon Sep 17 00:00:00 2001 From: Andreas Schneider Date: Thu, 8 Oct 2015 11:59:41 +0200 Subject: [PATCH] Added status expectation --- README.md | 5 +++++ URestemplateApp.pas | 47 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) diff --git a/README.md b/README.md index df5367a..873fab4 100644 --- a/README.md +++ b/README.md @@ -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 =` +: Checks expectations after the request. + * `Status=`: checks, if the status code matches + If an expectation fails, the application exists with code 5. + `Call ` : This prepares the actual call by providing the URL to be called. Variables in the form of `@` are replaced accordingly. diff --git a/URestemplateApp.pas b/URestemplateApp.pas index d927e04..6fe4ee6 100644 --- a/URestemplateApp.pas +++ b/URestemplateApp.pas @@ -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;