-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Resolves #16. It is now possible to create responses dynamically. This enables greater customisation on a per-request basis. For example: ```Delphi WebMock.StubRequest('*', '*') .ToRespondWith( procedure (const ARequest: IWebMockHTTPRequest; const AResponse: IWebMockResponseBuilder) begin AReponse .WithStatus(202) .WithHeader('header-1', 'a-value') .WithBody('Some content...'); end ); ``` The `ARequest: IWebMockHTTPRequest` parameter is available for inspection. The internals have been updated to introduce a "responder" layer allowing different handlers for responses which should be useful beyond this change.
- Loading branch information
1 parent
0c1358f
commit 1f63ed6
Showing
21 changed files
with
1,097 additions
and
234 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
{******************************************************************************} | ||
{ } | ||
{ Delphi-WebMocks } | ||
{ } | ||
{ Copyright (c) 2020 Richard Hatherall } | ||
{ } | ||
{ [email protected] } | ||
{ https://appercept.com } | ||
{ } | ||
{******************************************************************************} | ||
{ } | ||
{ Licensed under the Apache License, Version 2.0 (the "License"); } | ||
{ you may not use this file except in compliance with the License. } | ||
{ You may obtain a copy of the License at } | ||
{ } | ||
{ http://www.apache.org/licenses/LICENSE-2.0 } | ||
{ } | ||
{ Unless required by applicable law or agreed to in writing, software } | ||
{ distributed under the License is distributed on an "AS IS" BASIS, } | ||
{ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. } | ||
{ See the License for the specific language governing permissions and } | ||
{ limitations under the License. } | ||
{ } | ||
{******************************************************************************} | ||
|
||
unit WebMock.Dynamic.Responder; | ||
|
||
interface | ||
|
||
uses | ||
WebMock.HTTP.Messages, | ||
WebMock.Responder, | ||
WebMock.Response; | ||
|
||
type | ||
TWebMockDynamicResponse = reference to procedure ( | ||
const ARequest: IWebMockHTTPRequest; | ||
const AResponse: IWebMockResponseBuilder | ||
); | ||
|
||
TWebMockDynamicResponder = class(TInterfacedObject, IWebMockResponder) | ||
private | ||
FProc: TWebMockDynamicResponse; | ||
public | ||
constructor Create(const AProc: TWebMockDynamicResponse); | ||
function GetResponseTo(const ARequest: IWebMockHTTPRequest): TWebMockResponse; | ||
end; | ||
|
||
implementation | ||
|
||
{ TWebMockDynamicResponder } | ||
|
||
constructor TWebMockDynamicResponder.Create( | ||
const AProc: TWebMockDynamicResponse); | ||
begin | ||
inherited Create; | ||
FProc := AProc; | ||
end; | ||
|
||
function TWebMockDynamicResponder.GetResponseTo( | ||
const ARequest: IWebMockHTTPRequest): TWebMockResponse; | ||
var | ||
LResponse: TWebMockResponse; | ||
begin | ||
LResponse := TWebMockResponse.Create; | ||
FProc(ARequest, LResponse); | ||
Result := LResponse; | ||
end; | ||
|
||
end. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,7 @@ | |
{ } | ||
{ Delphi-WebMocks } | ||
{ } | ||
{ Copyright (c) 2019 Richard Hatherall } | ||
{ Copyright (c) 2019-2020 Richard Hatherall } | ||
{ } | ||
{ [email protected] } | ||
{ https://appercept.com } | ||
|
@@ -28,7 +28,8 @@ | |
interface | ||
|
||
uses | ||
IdCustomHTTPServer, IdHeaderList, | ||
IdCustomHTTPServer, | ||
IdHeaderList, | ||
System.Classes, | ||
WebMock.HTTP.Messages; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,7 @@ | |
{ } | ||
{ Delphi-WebMocks } | ||
{ } | ||
{ Copyright (c) 2019 Richard Hatherall } | ||
{ Copyright (c) 2019-2020 Richard Hatherall } | ||
{ } | ||
{ [email protected] } | ||
{ https://appercept.com } | ||
|
@@ -28,17 +28,48 @@ | |
interface | ||
|
||
uses | ||
IdCustomHTTPServer, IdHeaderList, | ||
System.Classes, System.Generics.Collections, System.RegularExpressions, | ||
WebMock.HTTP.Messages, WebMock.StringMatcher; | ||
IdCustomHTTPServer, | ||
IdHeaderList, | ||
System.Classes, | ||
System.Generics.Collections, | ||
System.RegularExpressions, | ||
WebMock.HTTP.Messages, | ||
WebMock.StringMatcher; | ||
|
||
type | ||
TWebMockHTTPRequestMatcher = class(TObject) | ||
IWebMockHTTPRequestMatcherBuilder = interface(IInterface) | ||
['{47AE1CA8-9395-4B80-AD6F-5F0A7017ED7D}'] | ||
function WithBody(const AContent: string): IWebMockHTTPRequestMatcherBuilder; overload; | ||
function WithBody(const APattern: TRegEx): IWebMockHTTPRequestMatcherBuilder; overload; | ||
function WithHeader(const AName, AValue: string): IWebMockHTTPRequestMatcherBuilder; overload; | ||
function WithHeader(const AName: string; APattern: TRegEx): IWebMockHTTPRequestMatcherBuilder; overload; | ||
function WithHeaders(const AHeaders: TStrings): IWebMockHTTPRequestMatcherBuilder; | ||
end; | ||
|
||
TWebMockHTTPRequestMatcher = class(TInterfacedObject, IWebMockHTTPRequestMatcherBuilder) | ||
public type | ||
|
||
TBuilder = class(TInterfacedObject, IWebMockHTTPRequestMatcherBuilder) | ||
private | ||
FMatcher: TWebMockHTTPRequestMatcher; | ||
property Matcher: TWebMockHTTPRequestMatcher read FMatcher; | ||
public | ||
constructor Create(const AMatcher: TWebMockHTTPRequestMatcher); | ||
|
||
{ IWebMockHTTPRequestMatcherBuilder } | ||
function WithBody(const AContent: string): IWebMockHTTPRequestMatcherBuilder; overload; | ||
function WithBody(const APattern: TRegEx): IWebMockHTTPRequestMatcherBuilder; overload; | ||
function WithHeader(const AName, AValue: string): IWebMockHTTPRequestMatcherBuilder; overload; | ||
function WithHeader(const AName: string; APattern: TRegEx): IWebMockHTTPRequestMatcherBuilder; overload; | ||
function WithHeaders(const AHeaders: TStrings): IWebMockHTTPRequestMatcherBuilder; | ||
end; | ||
|
||
private | ||
FContent: IStringMatcher; | ||
FHeaders: TDictionary<string, IStringMatcher>; | ||
FHTTPMethod: string; | ||
FURIMatcher: IStringMatcher; | ||
FBuilder: TBuilder; | ||
function HeadersMatches( | ||
AHeaders: TStrings): Boolean; | ||
function HTTPMethodMatches(AHTTPMethod: string): Boolean; | ||
|
@@ -50,6 +81,7 @@ TWebMockHTTPRequestMatcher = class(TObject) | |
function IsMatch(ARequest: IWebMockHTTPRequest): Boolean; | ||
function ToString: string; override; | ||
property Body: IStringMatcher read FContent write FContent; | ||
property Builder: TBuilder read FBuilder implements IWebMockHTTPRequestMatcherBuilder; | ||
property Headers: TDictionary<string, IStringMatcher> read FHeaders; | ||
property HTTPMethod: string read FHTTPMethod write FHTTPMethod; | ||
property URIMatcher: IStringMatcher read FURIMatcher; | ||
|
@@ -61,13 +93,15 @@ implementation | |
|
||
uses | ||
System.SysUtils, | ||
WebMock.StringWildcardMatcher, WebMock.StringAnyMatcher, | ||
WebMock.StringWildcardMatcher, | ||
WebMock.StringAnyMatcher, | ||
WebMock.StringRegExMatcher; | ||
|
||
constructor TWebMockHTTPRequestMatcher.Create(AURI: string; | ||
AHTTPMethod: string = 'GET'); | ||
begin | ||
inherited Create; | ||
FBuilder := TBuilder.Create(Self); | ||
FContent := TWebMockStringAnyMatcher.Create; | ||
FHeaders := TDictionary<string, IStringMatcher>.Create; | ||
FURIMatcher := TWebMockStringWildcardMatcher.Create(AURI); | ||
|
@@ -78,6 +112,7 @@ constructor TWebMockHTTPRequestMatcher.Create(AURIPattern: TRegEx; | |
AHTTPMethod: string = 'GET'); | ||
begin | ||
inherited Create; | ||
FBuilder := TBuilder.Create(Self); | ||
FContent := TWebMockStringAnyMatcher.Create; | ||
FHeaders := TDictionary<string, IStringMatcher>.Create; | ||
FURIMatcher := TWebMockStringRegExMatcher.Create(AURIPattern); | ||
|
@@ -145,4 +180,63 @@ function TWebMockHTTPRequestMatcher.ToString: string; | |
Result := Format('%s' + ^I + '%s', [HTTPMethod, URIMatcher.ToString]); | ||
end; | ||
|
||
{ TWebMockHTTPRequestMatcher.TBuilder } | ||
|
||
function TWebMockHTTPRequestMatcher.TBuilder.WithBody( | ||
const AContent: string): IWebMockHTTPRequestMatcherBuilder; | ||
begin | ||
Matcher.Body := TWebMockStringWildcardMatcher.Create(AContent); | ||
|
||
Result := Self; | ||
end; | ||
|
||
constructor TWebMockHTTPRequestMatcher.TBuilder.Create( | ||
const AMatcher: TWebMockHTTPRequestMatcher); | ||
begin | ||
inherited Create; | ||
FMatcher := AMatcher; | ||
end; | ||
|
||
function TWebMockHTTPRequestMatcher.TBuilder.WithBody( | ||
const APattern: TRegEx): IWebMockHTTPRequestMatcherBuilder; | ||
begin | ||
Matcher.Body := TWebMockStringRegExMatcher.Create(APattern); | ||
|
||
Result := Self; | ||
end; | ||
|
||
function TWebMockHTTPRequestMatcher.TBuilder.WithHeader(const AName, | ||
AValue: string): IWebMockHTTPRequestMatcherBuilder; | ||
begin | ||
Matcher.Headers.AddOrSetValue( | ||
AName, | ||
TWebMockStringWildcardMatcher.Create(AValue) | ||
); | ||
|
||
Result := Self; | ||
end; | ||
|
||
function TWebMockHTTPRequestMatcher.TBuilder.WithHeader(const AName: string; | ||
APattern: TRegEx): IWebMockHTTPRequestMatcherBuilder; | ||
begin | ||
Matcher.Headers.AddOrSetValue( | ||
AName, | ||
TWebMockStringRegExMatcher.Create(APattern) | ||
); | ||
|
||
Result := Self; | ||
end; | ||
|
||
|
||
function TWebMockHTTPRequestMatcher.TBuilder.WithHeaders( | ||
const AHeaders: TStrings): IWebMockHTTPRequestMatcherBuilder; | ||
var | ||
I: Integer; | ||
begin | ||
for I := 0 to AHeaders.Count - 1 do | ||
WithHeader(AHeaders.Names[I], AHeaders.ValueFromIndex[I]); | ||
|
||
Result := Self; | ||
end; | ||
|
||
end. |
Oops, something went wrong.