Save proxied requests during an XUnit integration test? #572

Open
opened 2025-12-29 08:30:23 +01:00 by adam · 7 comments
Owner

Originally created by @dahlsailrunner on GitHub (Feb 13, 2024).

Originally assigned to: @StefH on GitHub.

I have an API that I have written integration tests for using XUnit and a custom implementation of WebApplicationFactory<TEntryPoint>. If I make calls to that through a wiremock proxy with "save mappings" turned on the proxying does indeed work but I don't see any mappings getting saved anywhere.

If I run the same code and settings with a console app against the actual running API the mappings get saved to files just fine.

The test project would look something like this: https://github.com/dahlsailrunner/testing-examples/tree/main/01-simple-api/SimpleApi.Tests

But I would simply run the Client http requests through the proxy. Should this be possible? I just don't get any saved mappings.

I can provide an actual project / tests if it seems like this should be possible and such an example would be helpful.

Originally created by @dahlsailrunner on GitHub (Feb 13, 2024). Originally assigned to: @StefH on GitHub. I have an API that I have written integration tests for using XUnit and a custom implementation of `WebApplicationFactory<TEntryPoint>`. If I make calls to that through a wiremock proxy with "save mappings" turned on the proxying does indeed work but I don't see any mappings getting saved anywhere. If I run the same code and settings with a console app against the actual running API the mappings get saved to files just fine. The test project would look something like this: https://github.com/dahlsailrunner/testing-examples/tree/main/01-simple-api/SimpleApi.Tests But I would simply run the `Client` http requests through the proxy. Should this be possible? I just don't get any saved mappings. I can provide an actual project / tests if it seems like this *should* be possible and such an example would be helpful.
adam added the question label 2025-12-29 08:30:23 +01:00
Author
Owner

@StefH commented on GitHub (Feb 23, 2024):

@dahlsailrunner
This xunit test does what you need?
ce833c1f29/test/WireMock.Net.Tests/AdminApi/WireMockAdminApiTests.GetMappingsAsync.cs

@StefH commented on GitHub (Feb 23, 2024): @dahlsailrunner This xunit test does what you need? https://github.com/WireMock-Net/WireMock.Net/blob/ce833c1f29579f2480e9b7235d2f11a96e659afd/test/WireMock.Net.Tests/AdminApi/WireMockAdminApiTests.GetMappingsAsync.cs
Author
Owner

@dahlsailrunner commented on GitHub (Feb 25, 2024):

I had a quick look and I don't think that's what I'm looking for. Will review more closely soon and post a sample with more precise details if the the above is indeed not what I'm after. Thanks for the response and will keep you posted.

@dahlsailrunner commented on GitHub (Feb 25, 2024): I had a quick look and I don't **think** that's what I'm looking for. Will review more closely soon and post a sample with more precise details if the the above is indeed not what I'm after. Thanks for the response and will keep you posted.
Author
Owner

@StefH commented on GitHub (Mar 11, 2024):

@dahlsailrunner : did you time to verify?

@StefH commented on GitHub (Mar 11, 2024): @dahlsailrunner : did you time to verify?
Author
Owner

@dahlsailrunner commented on GitHub (Mar 11, 2024):

I have not but should have time this week. Thanks for the reminder here - I will let you know by Sunday for sure.

@dahlsailrunner commented on GitHub (Mar 11, 2024): I have not but should have time this week. Thanks for the reminder here - I will let you know by Sunday for sure.
Author
Owner

@dahlsailrunner commented on GitHub (Mar 12, 2024):

I checked - I don't think it does what I'm looking for but I've got a repo that lays things out (and tried the admin api rest client too).

I'm fairly certain you should be able to clone this repo: https://github.com/dahlsailrunner/automation-testing-strategies-aspnet/tree/main

and then run/debug the tests.

The tests/CarvedRock.InnerLoop.Tests/ProductControllerTests.cs file contains some simple tests against an API that should pass. As the calls to the API endpoints happen during the test run, I was hoping to be able to record those results so that they could be used as the wiremock setup for a UI that calls that API.

The code that invokes the WireMock call starts in the Utilities/BaseTest.cs file, but all of the interesting stuff is in the Utilities/SharedFixture.cs file (see ProxyAndRecordApiCalls method and DisposeAsync).

If you put a breakpoint in the DisposeAsync method I think you'll see that the only mapping I get back is the "map everything" one and nothing about the proxied api calls made in the tests.

I was actually trying to get the proxied calls saved to a file but couldn't find the file(s) anywhere after the test run.

If there's a way to do this, it would be great. Then we could "run a test against the API", then use the recorded API test calls as the wiremock back end for at least some of the tests from a UI that calls the API that was just tested.

Thanks in advance for any help / guidance here.

@dahlsailrunner commented on GitHub (Mar 12, 2024): I checked - I don't think it does what I'm looking for but I've got a repo that lays things out (and tried the admin api rest client too). I'm fairly certain you should be able to clone this repo: https://github.com/dahlsailrunner/automation-testing-strategies-aspnet/tree/main and then run/debug the tests. The `tests/CarvedRock.InnerLoop.Tests/ProductControllerTests.cs` file contains some simple tests against an API that should pass. As the calls to the API endpoints happen _during the test run_, I was hoping to be able to record those results so that they could be used as the wiremock setup for a UI that calls that API. The code that invokes the WireMock call starts in the `Utilities/BaseTest.cs` file, but all of the interesting stuff is in the `Utilities/SharedFixture.cs` file (see `ProxyAndRecordApiCalls` method and `DisposeAsync`). If you put a breakpoint in the `DisposeAsync` method I think you'll see that the only mapping I get back is the "map everything" one and nothing about the proxied api calls made in the tests. I was actually trying to get the proxied calls saved to a file but couldn't find the file(s) anywhere after the test run. If there's a way to do this, it would be great. Then we could "run a test against the API", then use the recorded API test calls as the wiremock back end for at least some of the tests from a UI that calls the API that was just tested. Thanks in advance for any help / guidance here.
Author
Owner

@StefH commented on GitHub (Mar 12, 2024):

I did have a quick look, however I don't understand yet what you are trying to test, because when I comment out WireMock, all tests still work fine?

Like:

public string ProxyAndRecordApiCalls(Uri apiBaseUrl)
    {
        var settings = new WireMockServerSettings
        {
            StartAdminInterface = true,
//             FileSystemHandler = new TestLocalFileSystemHandler()
        };

        var server = WireMockServer.Start(settings);
        _mockServerUrl = server.Url!;

        //server.Given(Request.Create().WithPath("*"))
        //    .RespondWith(Response.Create().WithProxy(
        //        new ProxyAndRecordSettings
        //        {
        //            Url = apiBaseUrl.ToString(),
        //            //FileSystemHandler = new TestLocalFileSystemHandler()
        //            SaveMapping = true,
        //            SaveMappingToFile = true
        //        }));

        return _mockServerUrl;
    }
@StefH commented on GitHub (Mar 12, 2024): I did have a quick look, however I don't understand yet what you are trying to test, because when I comment out WireMock, all tests still work fine? Like: ``` c# public string ProxyAndRecordApiCalls(Uri apiBaseUrl) { var settings = new WireMockServerSettings { StartAdminInterface = true, // FileSystemHandler = new TestLocalFileSystemHandler() }; var server = WireMockServer.Start(settings); _mockServerUrl = server.Url!; //server.Given(Request.Create().WithPath("*")) // .RespondWith(Response.Create().WithProxy( // new ProxyAndRecordSettings // { // Url = apiBaseUrl.ToString(), // //FileSystemHandler = new TestLocalFileSystemHandler() // SaveMapping = true, // SaveMappingToFile = true // })); return _mockServerUrl; } ```
Author
Owner

@dahlsailrunner commented on GitHub (Mar 12, 2024):

I'm trying to record the calls in the test. Then use those calls later. There is no need for wire mock in this set of tests other than to record the calls for a test that will use these calls (with WireMock) -- recording this test would simply let me avoid hand-coding the mapping. Does that make sense?

@dahlsailrunner commented on GitHub (Mar 12, 2024): I'm trying to record the calls in the test. Then use those calls later. There is no need for wire mock in this set of tests other than to record the calls for a test that will use these calls (with WireMock) -- recording this test would simply let me avoid hand-coding the mapping. Does that make sense?
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/WireMock.Net#572