Library for stubbing and setting expectations on HTTP requests in Delphi with DUnitX.
WebMocks is built by Appercept. We wrote it to test-drive the Appercept AWS SDK for Delphi and still use it every day.
* WebMocks was developed in Delphi 10.3 (Rio) and 10.4 (Sydney) and until
version 3.0 was compatible back to XE8. As WebMocks makes use of the
System.Net library introduced with XE8 it will not be compatible with earlier
versions. Should you require installing on Delphi versions prior to 10.3 you
should install version
2.0.0.
WebMocks is available through Embarcadero's package manager for Delphi GetIt. If you have a recent version of Delphi including GetIt then this should be the preferred installation method.
WebMocks should now be listed in Delphinus package manager.
Be sure to restart Delphi after installing via Delphinus otherwise the units may not be found in your test projects.
- Download and extract the latest release.
- In "Tools > Options" under the "Language / Delphi / Library" add the
extracted
Sourcedirectory to the "Library path" and "Browsing path".
// Stub a request
WebMock.StubRequest('GET', '/endpoint');
// Point your code at the mock server
Subject.EndpointURL := WebMock.URLFor('endpoint');
// Assert the request was made
WebMock.Assert.Get('/endpoint').WasRequested;WebMocks spins up a local HTTP server that your code talks to like any real service. Stub responses, then assert that the requests you expected actually happened.
For a gentle introduction, see the article series Testing HTTP clients in Delphi with DUnitX and WebMocks and the accompanying Delphi-WebMocks-Demos.
Version 2 has dropped the Delphi. namespace from all units. Any projects
upgrade to version 2 or later will need to drop the Delphi. prefix from any
included WebMocks units.
In your test unit file a couple of simple steps are required.
- Add
WebMockto your interfaceuses. - In your
TestFixtureclass useSetupandTearDownto create/destroy an instance ofTWebMock.
unit MyTestObjectTests;
interface
uses
DUnitX.TestFramework,
MyObjectUnit,
WebMock;
type
TMyObjectTests = class(TObject)
private
WebMock: TWebMock;
Subject: TMyObject;
public
[Setup]
procedure Setup;
[TearDown]
procedure TearDown;
[Test]
procedure TestGet;
end;
implementation
procedure TMyObjectTests.Setup;
begin
WebMock := TWebMock.Create;
end;
procedure TMyObjectTests.TearDown;
begin
WebMock.Free;
end;
procedure TMyObjectTests.TestGet;
begin
// Arrange
// Stub the request
WebMock.StubRequest('GET', '/endpoint');
// Create your subject and point it at the endpoint
Subject := TMyObject.Create;
Subject.EndpointURL := WebMock.URLFor('endpoint');
// Act
Subject.Get;
// Assert: check your subject behaved correctly
Assert.IsTrue(Subject.ReceivedResponse);
end;
initialization
TDUnitX.RegisterTestFixture(TMyObjectTests);
end.By default TWebMock will bind to a port dynamically assigned starting at 8080.
This behaviour can be overridden by specifying a port at creation.
WebMock := TWebMock.Create(8088);The use of WebMock.URLFor function within your tests is to simplify
constructing a valid URL. The Port property contains the current bound port
and BaseURL property contains a valid URL for the server root.
Detailed API documentation with examples is available in the docs/
folder:
- Stubbing Requests — matching by method, URI, headers, body, form-data, JSON, XML, regular expressions, and predicate functions
- Configuring Responses — status codes, headers, string and file body content, and dynamic responses
- Request Assertions — request history, the fluent assertion API, and negative assertions
- TestInsight is required to run the Delphi-WebMocks test suite, so, if you're considering contributing and need to run the test suite, install it. If you do TDD in Delphi I would recommend installing and using it in your own projects.
This project follows Semantic Versioning.
Copyright ©2019-2024 Richard Hatherall richard@appercept.com
WebMocks is distributed under the terms of the Apache License (Version 2.0).
See LICENSE for details.