Skip to content

Commit 210bb3b

Browse files
committed
Merge branch 'release/3.1.0'
2 parents 897190c + c822732 commit 210bb3b

6 files changed

+144
-4
lines changed

CHANGELOG.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
# Changelog
22

3+
## [3.1.0](https://github.com/appercept/Delphi-WebMocks/tree/3.1.0) (2021-03-09)
4+
5+
[Full Changelog](https://github.com/appercept/Delphi-WebMocks/compare/3.0.1...3.1.0)
6+
7+
**Implemented enhancements:**
8+
9+
- Add support for matching requests by query params [\#46](https://github.com/appercept/Delphi-WebMocks/pull/46) ([rhatherall](https://github.com/rhatherall))
10+
311
## [3.0.1](https://github.com/appercept/Delphi-WebMocks/tree/3.0.1) (2021-02-08)
412

513
[Full Changelog](https://github.com/appercept/Delphi-WebMocks/compare/3.0.0...3.0.1)
@@ -131,7 +139,6 @@
131139
- Add `URLFor` method [\#12](https://github.com/appercept/Delphi-WebMocks/issues/12)
132140
- Replace usages of Indy HTTP terms with terms used in the RFCs [\#11](https://github.com/appercept/Delphi-WebMocks/issues/11)
133141
- Request history logging [\#10](https://github.com/appercept/Delphi-WebMocks/issues/10)
134-
- Request header matching by regular-expressions [\#5](https://github.com/appercept/Delphi-WebMocks/issues/5)
135142

136143
**Merged pull requests:**
137144

@@ -146,6 +153,7 @@
146153
- Request content matching by regular-expressions [\#9](https://github.com/appercept/Delphi-WebMocks/issues/9)
147154
- Request content matching by value [\#8](https://github.com/appercept/Delphi-WebMocks/issues/8)
148155
- Response headers [\#7](https://github.com/appercept/Delphi-WebMocks/issues/7)
156+
- Request header matching by regular-expressions [\#5](https://github.com/appercept/Delphi-WebMocks/issues/5)
149157
- Request path matching by regular-expressions [\#4](https://github.com/appercept/Delphi-WebMocks/issues/4)
150158
- Delphi 10.3.2 is out [\#2](https://github.com/appercept/Delphi-WebMocks/issues/2)
151159
- Request header matching by value [\#1](https://github.com/appercept/Delphi-WebMocks/issues/1)

README.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ should install version
2020
[2.0.0](https://github.com/appercept/Delphi-WebMocks/releases/tag/2.0.0).
2121

2222
## Installation: GetIt
23-
[WebMocks 2.0](https://getitnow.embarcadero.com/WebMocks-2.0/) is available
23+
[WebMocks 3.0.1](https://getitnow.embarcadero.com/WebMocks-3.0.1/) is available
2424
through Embarcadero's package manager for Delphi
2525
[GetIt](https://getitnow.embarcadero.com/). If you have a recent version of
2626
Delphi including GetIt then this should be the preferred installation method.
@@ -34,10 +34,18 @@ not be found in your test projects.
3434

3535
## Installation: Manual
3636
1. Download and extract the latest version
37-
[3.0.0](https://github.com/appercept/Delphi-WebMocks/archive/3.0.0.zip).
37+
[3.1.0](https://github.com/appercept/Delphi-WebMocks/archive/3.1.0.zip).
3838
2. In "Tools > Options" under the "Language / Delphi / Library" add the
3939
extracted `Source` directory to the "Library path" and "Browsing path".
4040

41+
## Getting Started
42+
If you'd like a gentle introduction to WebMocks for Delphi, there is a series of
43+
articles published on [DEV](https://dev.to) starting with [Testing HTTP clients
44+
in Delphi with DUnitX and WebMocks](https://dev.to/rhatherall/testing-http-clients-in-delphi-with-dunitx-and-webmocks-25ck).
45+
46+
[Delphi-WebMocks-Demos](https://github.com/appercept/Delphi-WebMocks-Demos)
47+
contains a set of demonstrations to accompany the articles.
48+
4149
## Upgrading from versions prior to 2.0.0
4250
Version 2 has dropped the `Delphi.` namespace from all units. Any projects
4351
upgrade to version 2 or later will need to drop the `Delphi.` prefix from any

Source/WebMock.Static.RequestStub.pas

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ TWebMockStaticRequestStub = class(TInterfacedObject, IWebMockRequestStub)
6666
function WithJSON(const APath: string; const AValue: Float64): TWebMockStaticRequestStub; overload;
6767
function WithJSON(const APath: string; const AValue: Integer): TWebMockStaticRequestStub; overload;
6868
function WithJSON(const APath: string; const AValue: string): TWebMockStaticRequestStub; overload;
69+
function WithQueryParam(const AName, AValue: string): TWebMockStaticRequestStub; overload;
70+
function WithQueryParam(const AName: string; const APattern: TRegEx): TWebMockStaticRequestStub; overload;
6971

7072
// IWebMockRequestStub
7173
function IsMatch(ARequest: IWebMockHTTPRequest): Boolean;
@@ -245,6 +247,28 @@ function TWebMockStaticRequestStub.WithJSON(const APath,
245247
Result := Self;
246248
end;
247249

250+
function TWebMockStaticRequestStub.WithQueryParam(const AName: string;
251+
const APattern: TRegEx): TWebMockStaticRequestStub;
252+
begin
253+
Matcher.QueryParams.AddOrSetValue(
254+
AName,
255+
TWebMockStringRegExMatcher.Create(APattern)
256+
);
257+
258+
Result := Self;
259+
end;
260+
261+
function TWebMockStaticRequestStub.WithQueryParam(const AName,
262+
AValue: string): TWebMockStaticRequestStub;
263+
begin
264+
Matcher.QueryParams.AddOrSetValue(
265+
AName,
266+
TWebMockStringWildcardMatcher.Create(AValue)
267+
);
268+
269+
Result := Self;
270+
end;
271+
248272
function TWebMockStaticRequestStub.WithJSON(const APath: string;
249273
const AValue: Boolean): TWebMockStaticRequestStub;
250274
begin

Tests/Features/WebMock.Matching.Tests.pas

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,14 @@ TWebMockMatchingTests = class(TObject)
6262
[TestCase('No Resource by ID', 'GET,^/resource/\d+$,resource/abc')]
6363
procedure RequestWithMethodAndRegExPath_NotMatching_RespondsNotImplemented(const AMatcherMethod, AMatcherURIPattern, ARequestURI: string);
6464
[Test]
65+
procedure Request_MatchingQueryParam_RespondsOK;
66+
[Test]
67+
procedure Request_NotMatchingQueryParam_RespondsNotImplemented;
68+
[Test]
69+
procedure Request_MatchingQueryParamByRegEx_RespondsOK;
70+
[Test]
71+
procedure Request_NotMatchingQueryParamByRegEx_RespondsNotImplemented;
72+
[Test]
6573
procedure Request_MatchingSingleHeader_RespondsOK;
6674
[Test]
6775
procedure Request_NotMatchingSingleHeader_RespondsNotImplemented;
@@ -133,6 +141,26 @@ procedure TWebMockMatchingTests.Request_MatchingMultipleHeaders_RespondsOK;
133141
Assert.AreEqual(200, LResponse.StatusCode);
134142
end;
135143

144+
procedure TWebMockMatchingTests.Request_MatchingQueryParamByRegEx_RespondsOK;
145+
var
146+
LResponse: IHTTPResponse;
147+
begin
148+
WebMock.StubRequest('*', '*').WithQueryParam('Param1', TRegEx.Create('Value\d'));
149+
LResponse := WebClient.Get(WebMock.URLFor('/endpoint?Param1=Value1'));
150+
151+
Assert.AreEqual(200, LResponse.StatusCode);
152+
end;
153+
154+
procedure TWebMockMatchingTests.Request_MatchingQueryParam_RespondsOK;
155+
var
156+
LResponse: IHTTPResponse;
157+
begin
158+
WebMock.StubRequest('*', '*').WithQueryParam('Param1', 'Value1');
159+
LResponse := WebClient.Get(WebMock.URLFor('/endpoint?Param1=Value1'));
160+
161+
Assert.AreEqual(200, LResponse.StatusCode);
162+
end;
163+
136164
procedure TWebMockMatchingTests.Request_MatchingSingleHeaderByRegEx_RespondsOK;
137165
var
138166
LHeaderName: string;
@@ -179,6 +207,26 @@ procedure TWebMockMatchingTests.StubWithMethodAndStringURI_NotMatching_RespondsN
179207
Assert.AreEqual(501, LResponse.StatusCode);
180208
end;
181209

210+
procedure TWebMockMatchingTests.Request_NotMatchingQueryParamByRegEx_RespondsNotImplemented;
211+
var
212+
LResponse: IHTTPResponse;
213+
begin
214+
WebMock.StubRequest('*', '*').WithQueryParam('Param1', TRegEx.Create('Value\d'));
215+
LResponse := WebClient.Get(WebMock.URLFor('/endpoint?Param1=ValueA'));
216+
217+
Assert.AreEqual(501, LResponse.StatusCode);
218+
end;
219+
220+
procedure TWebMockMatchingTests.Request_NotMatchingQueryParam_RespondsNotImplemented;
221+
var
222+
LResponse: IHTTPResponse;
223+
begin
224+
WebMock.StubRequest('*', '*').WithQueryParam('Param1', 'Value1');
225+
LResponse := WebClient.Get(WebMock.URLFor('/endpoint?Param1=Value2'));
226+
227+
Assert.AreEqual(501, LResponse.StatusCode);
228+
end;
229+
182230
procedure TWebMockMatchingTests.Request_NotMatchingSingleHeaderByRegEx_RespondsNotImplemented;
183231
var
184232
LHeaderName: string;

Tests/WebMock.Static.RequestStub.Tests.pas

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,14 @@ TWebMockStaticRequestStubTests = class(TObject)
9898
procedure WithJSON_GivenPathAndString_ReturnsSelf;
9999
[Test]
100100
procedure WithJSON_GivenPathAndString_SetsMatcherForContent;
101+
[Test]
102+
procedure WithQueryParam_GivenString_ReturnsSelf;
103+
[Test]
104+
procedure WithQueryParam_GivenString_SetsMatcherForParam;
105+
[Test]
106+
procedure WithQueryParam_GivenRegEx_ReturnsSelf;
107+
[Test]
108+
procedure WithQueryParam_GivenRegEx_SetsMatcherForParam;
101109
end;
102110

103111
implementation
@@ -408,6 +416,50 @@ procedure TWebMockStaticRequestStubTests.WithJSON_GivenPathAndString_SetsMatcher
408416
Assert.IsTrue(StubbedRequest.Matcher.Body is TWebMockJSONMatcher);
409417
end;
410418

419+
procedure TWebMockStaticRequestStubTests.WithQueryParam_GivenRegEx_ReturnsSelf;
420+
begin
421+
Assert.AreSame(
422+
StubbedRequest,
423+
StubbedRequest.WithQueryParam('name', TRegEx.Create('pattern'))
424+
);
425+
end;
426+
427+
procedure TWebMockStaticRequestStubTests.WithQueryParam_GivenRegEx_SetsMatcherForParam;
428+
var
429+
LParamName: string;
430+
LParamPattern: TRegEx;
431+
begin
432+
LParamName := 'Header2';
433+
LParamPattern := TRegEx.Create('');
434+
435+
StubbedRequest.WithQueryParam(LParamName, LParamPattern);
436+
437+
Assert.AreEqual(
438+
LParamPattern,
439+
(StubbedRequest.Matcher.QueryParams[LParamName] as TWebMockStringRegExMatcher).RegEx
440+
);
441+
end;
442+
443+
procedure TWebMockStaticRequestStubTests.WithQueryParam_GivenString_ReturnsSelf;
444+
begin
445+
Assert.AreSame(StubbedRequest, StubbedRequest.WithQueryParam('name', 'value'));
446+
end;
447+
448+
procedure TWebMockStaticRequestStubTests.WithQueryParam_GivenString_SetsMatcherForParam;
449+
var
450+
LParamName, LParamValue: string;
451+
begin
452+
LParamName := 'Name1';
453+
LParamValue := 'Value1';
454+
455+
StubbedRequest.WithQueryParam(LParamName, LParamValue);
456+
457+
Assert.AreEqual(
458+
LParamValue,
459+
(StubbedRequest.Matcher.QueryParams[LParamName] as TWebMockStringWildcardMatcher).Value
460+
);
461+
end;
462+
411463
initialization
412464
TDUnitX.RegisterTestFixture(TWebMockStaticRequestStubTests);
413465
end.

Tests/WebMocks.Tests.dproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<TargetedPlatforms>5251</TargetedPlatforms>
88
<AppType>Console</AppType>
99
<FrameworkType>None</FrameworkType>
10-
<ProjectVersion>19.1</ProjectVersion>
10+
<ProjectVersion>19.2</ProjectVersion>
1111
<Platform Condition="'$(Platform)'==''">Win32</Platform>
1212
</PropertyGroup>
1313
<PropertyGroup Condition="'$(Config)'=='Base' or '$(Base)'!=''">

0 commit comments

Comments
 (0)