22{ }
33{ Delphi-WebMocks }
44{ }
5- { Copyright (c) 2019 Richard Hatherall }
5+ { Copyright (c) 2019-2021 Richard Hatherall }
66{ }
7788{ https://appercept.com }
@@ -29,17 +29,20 @@ interface
2929
3030uses
3131 DUnitX.TestFramework,
32- System.Classes, System.Generics.Collections, System.RegularExpressions,
33- WebMock.HTTP.Messages, WebMock.HTTP.RequestMatcher;
32+ System.Classes,
33+ System.RegularExpressions,
34+ System.Rtti,
35+ WebMock.HTTP.Messages,
36+ WebMock.HTTP.RequestMatcher;
3437
3538type
3639 TWebMockAssertion = class (TObject)
3740 private
38- FMatcher: TWebMockHTTPRequestMatcher ;
39- FHistory: TList<IWebMockHTTPRequest> ;
41+ FMatcher: IWebMockHTTPRequestMatcher ;
42+ FHistory: IInterfaceList ;
4043 function MatchesHistory : Boolean;
4144 public
42- constructor Create(AHistory: TList<IWebMockHTTPRequest> );
45+ constructor Create(const AHistory: IInterfaceList );
4346 function Delete (const AURI: string): TWebMockAssertion;
4447 function Get (const AURI: string): TWebMockAssertion;
4548 function Patch (const AURI: string): TWebMockAssertion;
@@ -52,10 +55,18 @@ TWebMockAssertion = class(TObject)
5255 function WithHeader (const AName, AValue: string): TWebMockAssertion; overload;
5356 function WithHeader (const AName: string; const APattern: TRegEx): TWebMockAssertion; overload;
5457 function WithHeaders (const AHeaders: TStringList): TWebMockAssertion;
58+ function WithQueryParam (const AName, AValue: string): TWebMockAssertion; overload;
59+ function WithQueryParam (const AName: string; const APattern: TRegEx): TWebMockAssertion; overload;
60+ function WithFormData (const AName, AValue: string): TWebMockAssertion; overload;
61+ function WithFormData (const AName: string; const APattern: TRegEx): TWebMockAssertion; overload;
62+ function WithJSON (const APath: string; AValue: Boolean): TWebMockAssertion; overload;
63+ function WithJSON (const APath: string; AValue: Float64): TWebMockAssertion; overload;
64+ function WithJSON (const APath: string; AValue: Integer): TWebMockAssertion; overload;
65+ function WithJSON (const APath: string; AValue: string): TWebMockAssertion; overload;
5566 procedure WasRequested ;
5667 procedure WasNotRequested ;
57- property History: TList<IWebMockHTTPRequest> read FHistory;
58- property Matcher: TWebMockHTTPRequestMatcher read FMatcher;
68+ property History: IInterfaceList read FHistory;
69+ property Matcher: IWebMockHTTPRequestMatcher read FMatcher;
5970 end ;
6071
6172implementation
@@ -64,9 +75,11 @@ implementation
6475
6576uses
6677 System.SysUtils,
67- WebMock.StringRegExMatcher, WebMock.StringWildcardMatcher;
78+ WebMock.FormDataMatcher,
79+ WebMock.StringRegExMatcher,
80+ WebMock.StringWildcardMatcher;
6881
69- constructor TWebMockAssertion.Create(AHistory: TList<IWebMockHTTPRequest> );
82+ constructor TWebMockAssertion.Create(const AHistory: IInterfaceList );
7083begin
7184 inherited Create;
7285 FHistory := AHistory;
@@ -84,12 +97,15 @@ function TWebMockAssertion.Get(const AURI: string): TWebMockAssertion;
8497
8598function TWebMockAssertion.MatchesHistory : Boolean;
8699var
100+ I: Integer;
87101 LRequest: IWebMockHTTPRequest;
88102begin
89103 Result := False;
90104
91- for LRequest in History do
105+ for I := 0 to History.Count - 1 do
92106 begin
107+ LRequest := History[I] as IWebMockHTTPRequest;
108+
93109 if Matcher.IsMatch(LRequest) then
94110 begin
95111 Result := True;
@@ -130,51 +146,69 @@ function TWebMockAssertion.Request(const AMethod, AURI: string): TWebMockAsserti
130146
131147procedure TWebMockAssertion.WasNotRequested ;
132148begin
133- if MatchesHistory then
134- Assert.Fail(Format(' Found request matching %s' , [Matcher.ToString]))
135- else
136- Assert.Pass(Format(' Did not find request matching %s' , [Matcher.ToString]));
149+ try
150+ if MatchesHistory then
151+ Assert.Fail(Format(' Found request matching %s' , [Matcher.ToString]))
152+ else
153+ Assert.Pass(Format(' Did not find request matching %s' , [Matcher.ToString]));
154+ finally
155+ Free;
156+ end ;
137157end ;
138158
139159procedure TWebMockAssertion.WasRequested ;
140160begin
141- if MatchesHistory then
142- Assert.Pass(Format(' Found request matching %s' , [Matcher.ToString]))
143- else
144- Assert.Fail(Format(' Expected to find request matching %s' , [Matcher.ToString]));
161+ try
162+ if MatchesHistory then
163+ Assert.Pass(Format(' Found request matching %s' , [Matcher.ToString]))
164+ else
165+ Assert.Fail(Format(' Expected to find request matching %s' , [Matcher.ToString]));
166+ finally
167+ Free;
168+ end ;
145169end ;
146170
147171function TWebMockAssertion.WithBody (const APattern: TRegEx): TWebMockAssertion;
148172begin
149- Matcher.Body := TWebMockStringRegExMatcher.Create(APattern);
173+ Matcher.Builder.WithBody(APattern);
174+
175+ Result := Self;
176+ end ;
177+
178+ function TWebMockAssertion.WithFormData (const AName: string;
179+ const APattern: TRegEx): TWebMockAssertion;
180+ begin
181+ Matcher.Builder.WithFormData(AName, APattern);
182+
183+ Result := Self;
184+ end ;
185+
186+ function TWebMockAssertion.WithFormData (const AName,
187+ AValue: string): TWebMockAssertion;
188+ begin
189+ Matcher.Builder.WithFormData(AName, AValue);
150190
151191 Result := Self;
152192end ;
153193
154194function TWebMockAssertion.WithHeader (const AName,
155195 AValue: string): TWebMockAssertion;
156196begin
157- Matcher.Headers.AddOrSetValue(
158- AName,
159- TWebMockStringWildcardMatcher.Create(AValue)
160- );
197+ Matcher.Builder.WithHeader(AName, AValue);
161198
162199 Result := Self;
163200end ;
164201
165202function TWebMockAssertion.WithBody (const AContent: string): TWebMockAssertion;
166203begin
167- Matcher.Body := TWebMockStringWildcardMatcher.Create (AContent);
204+ Matcher.Builder.WithBody (AContent);
168205
169206 Result := Self;
170207end ;
171208
172209function TWebMockAssertion.WithHeader (const AName: string; const APattern: TRegEx): TWebMockAssertion;
173210begin
174- Matcher.Headers.AddOrSetValue(
175- AName,
176- TWebMockStringRegExMatcher.Create(APattern)
177- );
211+ Matcher.Builder.WithHeader(AName, APattern);
178212
179213 Result := Self;
180214end ;
@@ -190,4 +224,52 @@ function TWebMockAssertion.WithHeaders(
190224 Result := Self;
191225end ;
192226
227+ function TWebMockAssertion.WithJSON (const APath: string;
228+ AValue: Boolean): TWebMockAssertion;
229+ begin
230+ Matcher.Builder.WithJSON(APath, AValue);
231+
232+ Result := Self;
233+ end ;
234+
235+ function TWebMockAssertion.WithJSON (const APath: string;
236+ AValue: Float64): TWebMockAssertion;
237+ begin
238+ Matcher.Builder.WithJSON(APath, AValue);
239+
240+ Result := Self;
241+ end ;
242+
243+ function TWebMockAssertion.WithJSON (const APath: string;
244+ AValue: Integer): TWebMockAssertion;
245+ begin
246+ Matcher.Builder.WithJSON(APath, AValue);
247+
248+ Result := Self;
249+ end ;
250+
251+ function TWebMockAssertion.WithJSON (const APath: string;
252+ AValue: string): TWebMockAssertion;
253+ begin
254+ Matcher.Builder.WithJSON(APath, AValue);
255+
256+ Result := Self;
257+ end ;
258+
259+ function TWebMockAssertion.WithQueryParam (const AName: string;
260+ const APattern: TRegEx): TWebMockAssertion;
261+ begin
262+ Matcher.Builder.WithQueryParam(AName, APattern);
263+
264+ Result := Self;
265+ end ;
266+
267+ function TWebMockAssertion.WithQueryParam (const AName,
268+ AValue: string): TWebMockAssertion;
269+ begin
270+ Matcher.Builder.WithQueryParam(AName, AValue);
271+
272+ Result := Self;
273+ end ;
274+
193275end .
0 commit comments