Skip to content

Commit

Permalink
Add support for asserting HEAD requests
Browse files Browse the repository at this point in the history
Resolves #47.

It is now possible to assert HEAD requests. For example:
```Delphi
WebMock.Assert.Head('/').WasRequested;
```
  • Loading branch information
rhatherall committed Apr 5, 2021
1 parent 5e10129 commit edc7c01
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 0 deletions.
7 changes: 7 additions & 0 deletions Source/WebMock.Assertion.pas
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ interface
WebMock.HTTP.RequestMatcher;

type

TWebMockAssertion = class(TObject)
private
FMatcher: IWebMockHTTPRequestMatcher;
Expand All @@ -45,6 +46,7 @@ TWebMockAssertion = class(TObject)
constructor Create(const AHistory: IInterfaceList);
function Delete(const AURI: string): TWebMockAssertion;
function Get(const AURI: string): TWebMockAssertion;
function Head(const AURI: string): TWebMockAssertion;
function Patch(const AURI: string): TWebMockAssertion;
function Post(const AURI: string): TWebMockAssertion;
function Put(const AURI: string): TWebMockAssertion;
Expand Down Expand Up @@ -95,6 +97,11 @@ function TWebMockAssertion.Get(const AURI: string): TWebMockAssertion;
Result := Request('GET', AURI);
end;

function TWebMockAssertion.Head(const AURI: string): TWebMockAssertion;
begin
Result := Request('HEAD', AURI);
end;

function TWebMockAssertion.MatchesHistory: Boolean;
var
I: Integer;
Expand Down
30 changes: 30 additions & 0 deletions Tests/Features/WebMock.Assertions.Tests.pas
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ TWebMockAssertionsTests = class(TObject)
[Test]
procedure GetWasRequested_NotMatchingRequest_Fails;
[Test]
procedure HeadWasRequested_MatchingRequest_Passes;
[Test]
procedure HeadWasRequested_NotMatchingRequest_Fails;
[Test]
procedure PatchWasRequested_MatchingRequest_Passes;
[Test]
procedure PatchWasRequested_NotMatchingRequest_Fails;
Expand Down Expand Up @@ -171,6 +175,32 @@ procedure TWebMockAssertionsTests.GetWasRequested_NotMatchingRequest_Fails;
);
end;

procedure TWebMockAssertionsTests.HeadWasRequested_MatchingRequest_Passes;
begin
WebClient.Head(WebMock.URLFor('/'));

Assert.WillRaise(
procedure
begin
WebMock.Assert.Head('/').WasRequested;
end,
ETestPass
);
end;

procedure TWebMockAssertionsTests.HeadWasRequested_NotMatchingRequest_Fails;
begin
WebClient.Head(WebMock.URLFor('/'));

Assert.WillRaise(
procedure
begin
WebMock.Assert.Head('/resource').WasRequested;
end,
ETestFailure
);
end;

procedure TWebMockAssertionsTests.WasNotRequested_MatchingRequest_Fails;
begin
WebClient.Get(WebMock.URLFor('/'));
Expand Down
24 changes: 24 additions & 0 deletions Tests/WebMock.Assertion.Tests.pas
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ TWebMockAssertionTests = class(TObject)
[Test]
procedure Get_Always_ReturnsSelf;
[Test]
procedure Head_GivenMethodAndURI_SetsMatcherValues;
[Test]
procedure Head_Always_ReturnsSelf;
[Test]
procedure Patch_GivenMethodAndURI_SetsMatcherValues;
[Test]
procedure Patch_Always_ReturnsSelf;
Expand Down Expand Up @@ -199,6 +203,26 @@ procedure TWebMockAssertionTests.Get_GivenMethodAndURI_SetsMatcherValues;
Assertion.Free;
end;

procedure TWebMockAssertionTests.Head_Always_ReturnsSelf;
begin
Assert.AreSame(Assertion, Assertion.Head('/'));

Assertion.Free;
end;

procedure TWebMockAssertionTests.Head_GivenMethodAndURI_SetsMatcherValues;
var
LURI: string;
begin
LURI := '/resource';

Assertion.Head(LURI);

Assert.IsMatch('HEAD\s/resource', Assertion.Matcher.ToString);

Assertion.Free;
end;

procedure TWebMockAssertionTests.Patch_Always_ReturnsSelf;
begin
Assert.AreSame(Assertion, Assertion.Patch('/'));
Expand Down

0 comments on commit edc7c01

Please sign in to comment.