Skip to content

Commit

Permalink
Merge branch 'release/1.1.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
rhatherall committed Feb 16, 2020
2 parents 3c66a7d + 94cbe3c commit 15c6d3f
Show file tree
Hide file tree
Showing 13 changed files with 880 additions and 185 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,5 @@ __recovery/

# Ignore Build outputs
Build
Tests/Win32
Tests/Win64
14 changes: 12 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
# Change Log
# Changelog

## [1.1.0](https://github.com/appercept/Delphi-WebMocks/tree/1.1.0) (2020-02-16)

[Full Changelog](https://github.com/appercept/Delphi-WebMocks/compare/1.0.1...1.1.0)

**Implemented enhancements:**

- Dynamic request matchers [\#22](https://github.com/appercept/Delphi-WebMocks/issues/22)
- Add dynamic request matching [\#23](https://github.com/appercept/Delphi-WebMocks/pull/23) ([rhatherall](https://github.com/rhatherall))
- Enable TestInsight [\#21](https://github.com/appercept/Delphi-WebMocks/pull/21) ([rhatherall](https://github.com/rhatherall))

## [1.0.1](https://github.com/appercept/Delphi-WebMocks/tree/1.0.1) (2019-11-12)

Expand Down Expand Up @@ -53,4 +63,4 @@



\* *This Change Log was automatically generated by [github_changelog_generator](https://github.com/skywinder/Github-Changelog-Generator)*
\* *This Changelog was automatically generated by [github_changelog_generator](https://github.com/github-changelog-generator/github-changelog-generator)*
104 changes: 104 additions & 0 deletions Delphi.WebMock.Dynamic.RequestStub.pas
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.
115 changes: 10 additions & 105 deletions Delphi.WebMock.RequestStub.pas
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
{ }
{ Delphi-WebMocks }
{ }
{ Copyright (c) 2019 Richard Hatherall }
{ Copyright (c) 2020 Richard Hatherall }
{ }
{ [email protected] }
{ https://appercept.com }
Expand All @@ -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.
Loading

0 comments on commit 15c6d3f

Please sign in to comment.