Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Save proxied requests during an XUnit integration test? #1066

Open
dahlsailrunner opened this issue Feb 13, 2024 · 7 comments
Open

Save proxied requests during an XUnit integration test? #1066

dahlsailrunner opened this issue Feb 13, 2024 · 7 comments
Assignees
Labels

Comments

@dahlsailrunner
Copy link

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.

@StefH StefH self-assigned this Feb 16, 2024
@dahlsailrunner
Copy link
Author

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.

@StefH
Copy link
Collaborator

StefH commented Mar 11, 2024

@dahlsailrunner : did you time to verify?

@dahlsailrunner
Copy link
Author

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

@dahlsailrunner
Copy link
Author

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.

@StefH
Copy link
Collaborator

StefH commented 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;
    }

@dahlsailrunner
Copy link
Author

dahlsailrunner commented 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 up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants