2
2
{ }
3
3
{ Delphi-WebMocks }
4
4
{ }
5
- { Copyright (c) 2019 Richard Hatherall }
5
+ { Copyright (c) 2019-2021 Richard Hatherall }
6
6
{ }
7
7
8
8
{ https://appercept.com }
@@ -29,17 +29,20 @@ interface
29
29
30
30
uses
31
31
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;
34
37
35
38
type
36
39
TWebMockAssertion = class (TObject)
37
40
private
38
- FMatcher: TWebMockHTTPRequestMatcher ;
39
- FHistory: TList<IWebMockHTTPRequest> ;
41
+ FMatcher: IWebMockHTTPRequestMatcher ;
42
+ FHistory: IInterfaceList ;
40
43
function MatchesHistory : Boolean;
41
44
public
42
- constructor Create(AHistory: TList<IWebMockHTTPRequest> );
45
+ constructor Create(const AHistory: IInterfaceList );
43
46
function Delete (const AURI: string): TWebMockAssertion;
44
47
function Get (const AURI: string): TWebMockAssertion;
45
48
function Patch (const AURI: string): TWebMockAssertion;
@@ -52,10 +55,18 @@ TWebMockAssertion = class(TObject)
52
55
function WithHeader (const AName, AValue: string): TWebMockAssertion; overload;
53
56
function WithHeader (const AName: string; const APattern: TRegEx): TWebMockAssertion; overload;
54
57
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;
55
66
procedure WasRequested ;
56
67
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;
59
70
end ;
60
71
61
72
implementation
@@ -64,9 +75,11 @@ implementation
64
75
65
76
uses
66
77
System.SysUtils,
67
- WebMock.StringRegExMatcher, WebMock.StringWildcardMatcher;
78
+ WebMock.FormDataMatcher,
79
+ WebMock.StringRegExMatcher,
80
+ WebMock.StringWildcardMatcher;
68
81
69
- constructor TWebMockAssertion.Create(AHistory: TList<IWebMockHTTPRequest> );
82
+ constructor TWebMockAssertion.Create(const AHistory: IInterfaceList );
70
83
begin
71
84
inherited Create;
72
85
FHistory := AHistory;
@@ -84,12 +97,15 @@ function TWebMockAssertion.Get(const AURI: string): TWebMockAssertion;
84
97
85
98
function TWebMockAssertion.MatchesHistory : Boolean;
86
99
var
100
+ I: Integer;
87
101
LRequest: IWebMockHTTPRequest;
88
102
begin
89
103
Result := False;
90
104
91
- for LRequest in History do
105
+ for I := 0 to History.Count - 1 do
92
106
begin
107
+ LRequest := History[I] as IWebMockHTTPRequest;
108
+
93
109
if Matcher.IsMatch(LRequest) then
94
110
begin
95
111
Result := True;
@@ -130,51 +146,69 @@ function TWebMockAssertion.Request(const AMethod, AURI: string): TWebMockAsserti
130
146
131
147
procedure TWebMockAssertion.WasNotRequested ;
132
148
begin
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 ;
137
157
end ;
138
158
139
159
procedure TWebMockAssertion.WasRequested ;
140
160
begin
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 ;
145
169
end ;
146
170
147
171
function TWebMockAssertion.WithBody (const APattern: TRegEx): TWebMockAssertion;
148
172
begin
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);
150
190
151
191
Result := Self;
152
192
end ;
153
193
154
194
function TWebMockAssertion.WithHeader (const AName,
155
195
AValue: string): TWebMockAssertion;
156
196
begin
157
- Matcher.Headers.AddOrSetValue(
158
- AName,
159
- TWebMockStringWildcardMatcher.Create(AValue)
160
- );
197
+ Matcher.Builder.WithHeader(AName, AValue);
161
198
162
199
Result := Self;
163
200
end ;
164
201
165
202
function TWebMockAssertion.WithBody (const AContent: string): TWebMockAssertion;
166
203
begin
167
- Matcher.Body := TWebMockStringWildcardMatcher.Create (AContent);
204
+ Matcher.Builder.WithBody (AContent);
168
205
169
206
Result := Self;
170
207
end ;
171
208
172
209
function TWebMockAssertion.WithHeader (const AName: string; const APattern: TRegEx): TWebMockAssertion;
173
210
begin
174
- Matcher.Headers.AddOrSetValue(
175
- AName,
176
- TWebMockStringRegExMatcher.Create(APattern)
177
- );
211
+ Matcher.Builder.WithHeader(AName, APattern);
178
212
179
213
Result := Self;
180
214
end ;
@@ -190,4 +224,52 @@ function TWebMockAssertion.WithHeaders(
190
224
Result := Self;
191
225
end ;
192
226
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
+
193
275
end .
0 commit comments