-
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.
- Loading branch information
Showing
13 changed files
with
880 additions
and
185 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -67,3 +67,5 @@ __recovery/ | |
|
||
# Ignore Build outputs | ||
Build | ||
Tests/Win32 | ||
Tests/Win64 |
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,104 @@ | ||
{******************************************************************************} | ||
{ } | ||
{ 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 Delphi.WebMock.Dynamic.RequestStub; | ||
|
||
interface | ||
|
||
uses | ||
Delphi.WebMock.HTTP.Messages, Delphi.WebMock.RequestStub, | ||
Delphi.WebMock.Response, Delphi.WebMock.ResponseStatus; | ||
|
||
type | ||
TWebMockDynamicRequestMatcher = reference to function( | ||
ARequest: IWebMockHTTPRequest | ||
): Boolean; | ||
|
||
TWebMockDynamicRequestStub = class(TInterfacedObject, IWebMockRequestStub) | ||
private | ||
FMatcher: TWebMockDynamicRequestMatcher; | ||
FResponse: TWebMockResponse; | ||
public | ||
constructor Create(const AMatcher: TWebMockDynamicRequestMatcher); | ||
function ToRespond(AResponseStatus: TWebMockResponseStatus = nil) | ||
: TWebMockResponse; | ||
|
||
// IWebMockRequestStub | ||
function IsMatch(ARequest: IWebMockHTTPRequest): Boolean; | ||
function GetResponse: TWebMockResponse; | ||
procedure SetResponse(const AResponse: TWebMockResponse); | ||
function ToString: string; override; | ||
property Response: TWebMockResponse read GetResponse write SetResponse; | ||
|
||
property Matcher: TWebMockDynamicRequestMatcher read FMatcher; | ||
end; | ||
|
||
implementation | ||
|
||
uses | ||
System.SysUtils; | ||
|
||
{ TWebMockDynamicRequestStub } | ||
|
||
constructor TWebMockDynamicRequestStub.Create( | ||
const AMatcher: TWebMockDynamicRequestMatcher); | ||
begin | ||
inherited Create; | ||
FMatcher := AMatcher; | ||
FResponse := TWebMockResponse.Create; | ||
end; | ||
|
||
function TWebMockDynamicRequestStub.GetResponse: TWebMockResponse; | ||
begin | ||
Result := FResponse; | ||
end; | ||
|
||
function TWebMockDynamicRequestStub.IsMatch( | ||
ARequest: IWebMockHTTPRequest): Boolean; | ||
begin | ||
Result := Matcher(ARequest); | ||
end; | ||
|
||
procedure TWebMockDynamicRequestStub.SetResponse( | ||
const AResponse: TWebMockResponse); | ||
begin | ||
FResponse := AResponse; | ||
end; | ||
|
||
function TWebMockDynamicRequestStub.ToRespond( | ||
AResponseStatus: TWebMockResponseStatus): TWebMockResponse; | ||
begin | ||
if Assigned(AResponseStatus) then | ||
Response.Status := AResponseStatus; | ||
|
||
Result := Response; | ||
end; | ||
|
||
function TWebMockDynamicRequestStub.ToString: string; | ||
begin | ||
Result := Format('(Dynamic Matcher)' + ^I + '%s', [Response.ToString]); | ||
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) 2020 Richard Hatherall } | ||
{ } | ||
{ [email protected] } | ||
{ https://appercept.com } | ||
|
@@ -28,114 +28,19 @@ | |
interface | ||
|
||
uses | ||
Delphi.WebMock.HTTP.RequestMatcher, Delphi.WebMock.Response, | ||
Delphi.WebMock.ResponseStatus, | ||
IdCustomHTTPServer, | ||
System.Classes, System.Generics.Collections, System.RegularExpressions; | ||
Delphi.WebMock.HTTP.Messages, Delphi.WebMock.Response, | ||
System.Classes; | ||
|
||
type | ||
TWebMockRequestStub = class(TObject) | ||
private | ||
FMatcher: TWebMockHTTPRequestMatcher; | ||
FResponse: TWebMockResponse; | ||
public | ||
constructor Create(AMatcher: TWebMockHTTPRequestMatcher); | ||
destructor Destroy; override; | ||
function ToString: string; override; | ||
function ToRespond(AResponseStatus: TWebMockResponseStatus = nil) | ||
: TWebMockResponse; | ||
function WithBody(const AContent: string): TWebMockRequestStub; overload; | ||
function WithBody(const APattern: TRegEx): TWebMockRequestStub; overload; | ||
function WithHeader(AName, AValue: string): TWebMockRequestStub; overload; | ||
function WithHeader(AName: string; APattern: TRegEx) | ||
: TWebMockRequestStub; overload; | ||
function WithHeaders(AHeaders: TStringList): TWebMockRequestStub; | ||
property Matcher: TWebMockHTTPRequestMatcher read FMatcher; | ||
property Response: TWebMockResponse read FResponse write FResponse; | ||
IWebMockRequestStub = interface(IInterface) | ||
['{AA474C0C-CA37-44CF-A66A-3B024CC79BE6}'] | ||
function IsMatch(ARequest: IWebMockHTTPRequest): Boolean; | ||
function GetResponse: TWebMockResponse; | ||
procedure SetResponse(const AResponse: TWebMockResponse); | ||
function ToString: string; | ||
property Response: TWebMockResponse read GetResponse write SetResponse; | ||
end; | ||
|
||
implementation | ||
|
||
uses | ||
Delphi.WebMock.StringWildcardMatcher, Delphi.WebMock.StringRegExMatcher, | ||
System.SysUtils; | ||
|
||
{ TWebMockRequestStub } | ||
|
||
constructor TWebMockRequestStub.Create(AMatcher: TWebMockHTTPRequestMatcher); | ||
begin | ||
inherited Create; | ||
FMatcher := AMatcher; | ||
FResponse := TWebMockResponse.Create; | ||
end; | ||
|
||
destructor TWebMockRequestStub.Destroy; | ||
|
||
begin | ||
FResponse.Free; | ||
FMatcher.Free; | ||
inherited; | ||
end; | ||
|
||
function TWebMockRequestStub.ToRespond( | ||
AResponseStatus: TWebMockResponseStatus = nil): TWebMockResponse; | ||
begin | ||
if Assigned(AResponseStatus) then | ||
Response.Status := AResponseStatus; | ||
|
||
Result := Response; | ||
end; | ||
|
||
function TWebMockRequestStub.ToString: string; | ||
begin | ||
Result := Format('%s' + ^I + '%s', [Matcher.ToString, Response.ToString]); | ||
end; | ||
|
||
function TWebMockRequestStub.WithHeader(AName, AValue: string): TWebMockRequestStub; | ||
begin | ||
Matcher.Headers.AddOrSetValue( | ||
AName, | ||
TWebMockStringWildcardMatcher.Create(AValue) | ||
); | ||
|
||
Result := Self; | ||
end; | ||
|
||
function TWebMockRequestStub.WithBody( | ||
const AContent: string): TWebMockRequestStub; | ||
begin | ||
Matcher.Body := TWebMockStringWildcardMatcher.Create(AContent); | ||
|
||
Result := Self; | ||
end; | ||
|
||
function TWebMockRequestStub.WithBody( | ||
const APattern: TRegEx): TWebMockRequestStub; | ||
begin | ||
Matcher.Body := TWebMockStringRegExMatcher.Create(APattern); | ||
|
||
Result := Self; | ||
end; | ||
|
||
function TWebMockRequestStub.WithHeader(AName: string; | ||
APattern: TRegEx): TWebMockRequestStub; | ||
begin | ||
Matcher.Headers.AddOrSetValue( | ||
AName, | ||
TWebMockStringRegExMatcher.Create(APattern) | ||
); | ||
|
||
Result := Self; | ||
end; | ||
|
||
function TWebMockRequestStub.WithHeaders(AHeaders: TStringList): TWebMockRequestStub; | ||
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.