> Request validation using HttPlaceholder + .NET

Posted on: 2022-10-02 16:48

The code for this post can be found in https://github.com/dukeofharen/httplaceholder/blob/master/code-samples/dotnet.

In this small tutorial, we are going to take a look on how to verify a request made to a specific stub using the HttPlaceholder .NET client. In general, the following steps are executed:

  • Add a stub to HttPlaceholder.
  • Make a request to HttPlaceholder so the stub matches.
  • Verify if the stub you just made was hit.

The code can be found here and uses Testcontainers.NET to run a HttPlaceholder Docker container to run the tests against.

Setting up the container

The following code makes sure to start a Docker container with HttPlaceholder and maps the application to port 1337.

var container = new TestcontainersBuilder<TestcontainersContainer>()
    .WithPortBinding(1337, 5000)
await container.StartAsync();

Creating the client

After the container has started, we can create an HttPlaceholder client.

var client = HttPlaceholderClientFactory.CreateHttPlaceholderClient(
    new HttPlaceholderClientConfiguration
        RootUrl = "http://localhost:1337"

Creating a stub

var stub = StubBuilder.Begin()
        .WithTextResponseBody("This is the response!"))
await client.CreateStubAsync(stub);

The code above creates a simple stub which returns a plain text response.

Performing the request

Now that the stub has been added, we can actually make a request to it and verify the correct response was returned.

var httpClient = new HttpClient();
using var response = await httpClient.GetAsync("http://localhost:1337/someUrl?queryParam=someValue");
var contents = await response.Content.ReadAsStringAsync();
Assert.AreEqual("This is the response!", contents);

Verify the stub has been called in HttPlaceholder

By using the verification method in the HttPlaceholder client, you can verify whether a specific stub was called and with what parameters.

await client.VerifyStubCalledAsync("some-stub-id", TimesModel.ExactlyOnce(), DateTime.UtcNow.AddSeconds(-10));

The code above checks whether the stub with stub ID some-stub-id (the stub we have defined above) has been called "exactly once". Furthermore, the request should not have been done more than 10 seconds ago. If this is all correct, the method will continue without throwing an exception. If the verification fails however, an exception will be thrown containing the verification details.

There are more overloads for the verification method, which you can read more about here.