Record all request even when identical + Expect request sequences when replaying #355

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

Originally created by @julien-azouz-beezup on GitHub (Sep 17, 2021).

I have a case where I want to test my actions on a Rest API I do not have control on.

This API is stateful so for example this sequence is possible :

  • Request A (GET method) -> Responds X
  • Request B (POST method)
  • Request A (GET method) -> Responds Y

With the static mapping, Request A will always be followed by response X.

I would like to :

  1. Save ALL requests even identicals during a session and ensure the order in which they are called remains.
  2. Use the resulting mapping to test a system and then ensure the requests where called in the expected order (the same as during the recording).

For now the only way those were :

  1. I could restart the session to have another set of mappings. But then I will also have to play with the states manually.
  2. The following code :
        public void ReplayAndCheckSequence()
        {
            var settings = new WireMockServerSettings
            {
                Urls = new[] { "http://localhost:9095/" },
                AllowPartialMapping = false,
            };

            _server = WireMockServer.Start(settings);
            _server.ReadStaticMappings("mappings");
            
            var serverMappings = _server.Mappings.ToList();

            // Let the System Under Test run
            Thread.Sleep(10000);
            
            var serverLogEntries = _server.LogEntries.ToList();
            Check.That(serverMappings.Count).Equals(serverLogEntries.Count);
            for (int i = 0; i < serverLogEntries.Count; i++)
            {
                var entry = serverLogEntries[i];
                var requestMatchResult = serverMappings[i].GetRequestMatchResult((RequestMessage) entry.RequestMessage, "");
                Check.That(requestMatchResult.IsPerfectMatch).IsTrue();
            }
        }

Last note : It seems that MockServer would be able to do all of this.

Unfortunately is it decicated to Java and while it exposes a RestAPI, the most up to date .NET client seem to be abandonned.
I also prefer running the server in the same process as the tests like WireMock.Net allows.

Originally created by @julien-azouz-beezup on GitHub (Sep 17, 2021). I have a case where I want to test my actions on a Rest API I do not have control on. This API is stateful so for example this sequence is possible : - Request A (GET method) -> Responds X - Request B (POST method) - Request A (GET method) -> Responds Y With the static mapping, Request A will always be followed by response X. I would like to : 1. Save ALL requests even identicals during a session and ensure the order in which they are called remains. 2. Use the resulting mapping to test a system and then ensure the requests where called in the expected order (the same as during the recording). For now the only way those were : 1. I could restart the session to have another set of mappings. But then I will also have to play with the states manually. 2. The following code : ``` c# public void ReplayAndCheckSequence() { var settings = new WireMockServerSettings { Urls = new[] { "http://localhost:9095/" }, AllowPartialMapping = false, }; _server = WireMockServer.Start(settings); _server.ReadStaticMappings("mappings"); var serverMappings = _server.Mappings.ToList(); // Let the System Under Test run Thread.Sleep(10000); var serverLogEntries = _server.LogEntries.ToList(); Check.That(serverMappings.Count).Equals(serverLogEntries.Count); for (int i = 0; i < serverLogEntries.Count; i++) { var entry = serverLogEntries[i]; var requestMatchResult = serverMappings[i].GetRequestMatchResult((RequestMessage) entry.RequestMessage, ""); Check.That(requestMatchResult.IsPerfectMatch).IsTrue(); } } ``` Last note : It seems that MockServer would be able to do all of this. Unfortunately is it decicated to Java and while it exposes a RestAPI, the most up to date .NET client seem to be abandonned. I also prefer running the server in the same process as the tests like WireMock.Net allows.
adam added the question label 2025-12-29 08:26:49 +01:00
Author
Owner

@StefH commented on GitHub (Sep 19, 2021):

@julien-azouz-beezup

Would Scenarios-and-States be a solution for your issue?

@StefH commented on GitHub (Sep 19, 2021): @julien-azouz-beezup Would [Scenarios-and-States](https://github.com/WireMock-Net/WireMock.Net/wiki/Scenarios-and-States) be a solution for your issue?
Author
Owner

@julien-azouz-beezup commented on GitHub (Sep 25, 2021):

Hello.
Sorry for the delay.

I can achieve part of what I want with scenarios and states as I've mentioned but there are not very convenient.
The main issue remains with the static mapping where one request cannot be mapped to multiple responses depending on a state.
Or I have to stop a restart separate records for every possible states... and then I have to sort and import them in a single mapping and adjust the states.

  1. I think it's possible to achieve but not very convenient then.
  2. Same for the checkings the calls
@julien-azouz-beezup commented on GitHub (Sep 25, 2021): Hello. Sorry for the delay. I can achieve part of what I want with scenarios and states as I've mentioned but there are not very convenient. The main issue remains with the static mapping where one request cannot be mapped to multiple responses depending on a state. Or I have to stop a restart separate records for every possible states... and then I have to sort and import them in a single mapping and adjust the states. 1. I think it's possible to achieve but not very convenient then. 2. Same for the checkings the calls
Author
Owner

@StefH commented on GitHub (Sep 28, 2021):

What you seek is a playback functionality.

So maybe adding a new property like PlaybackOrder to the mapping would solve your question. This PlaybackOrder will be used to determine which response will be sent back, and all matching functionality would then be disabled.

However I cannot see yet how to mix this functionality with the current static mapping json file support.

@StefH commented on GitHub (Sep 28, 2021): What you seek is a playback functionality. So maybe adding a new property like `PlaybackOrder` to the mapping would solve your question. This PlaybackOrder will be used to determine which response will be sent back, and all matching functionality would then be disabled. However I cannot see yet how to mix this functionality with the current static mapping json file support.
Author
Owner

@julien-azouz-beezup commented on GitHub (Sep 30, 2021):

Yes, this PlaybackOrder property will useful for 1. Save ALL requests even identicals during a session and ensure the order in which they are called remains.

I don't understand the issue with the json file support. I am not familiar with the code.
I naively think that we could rely on the index of the element in the file ?

@julien-azouz-beezup commented on GitHub (Sep 30, 2021): Yes, this PlaybackOrder property will useful for `1. Save ALL requests even identicals during a session and ensure the order in which they are called remains. ` I don't understand the issue with the json file support. I am not familiar with the code. I naively think that we could rely on the index of the element in the file ?
Author
Owner

@StefH commented on GitHub (Sep 30, 2021):

For me it's easier to check the PlaybackOrder (nullable int) property in case multiple matches are found.

One thing remains is that I need to keep track of which match was returned last time. And I guess there should also be a way to reset this back to the first option.

@StefH commented on GitHub (Sep 30, 2021): For me it's easier to check the PlaybackOrder (nullable int) property in case multiple matches are found. One thing remains is that I need to keep track of which match was returned last time. And I guess there should also be a way to reset this back to the first option.
Author
Owner

@julien-azouz-beezup commented on GitHub (Sep 30, 2021):

Can you leverage the existing scenarios and state feature for that ?

You could create a sort of scenario generator that will :

  1. Automatically append the PlaybackOrder property when recording
  2. Update the state as it receives requests

But maybe that appending that "managed" scenario will be confusing when existing along regular "unmanaged" ones.
Or maybe this should belong in a specific package ?

@julien-azouz-beezup commented on GitHub (Sep 30, 2021): Can you leverage the existing scenarios and state feature for that ? You could create a sort of scenario generator that will : 1. Automatically append the PlaybackOrder property when recording 2. Update the state as it receives requests But maybe that appending that "managed" scenario will be confusing when existing along regular "unmanaged" ones. Or maybe this should belong in a specific package ?
Author
Owner

@julien-azouz-beezup commented on GitHub (Sep 30, 2021):

Another suggested change, more trivial I think :

An option to record all requests even the identical ones in a "static" mapping.
The default behavior of request matching for this will simply fetch the first matching request.

Then I could leverage the scenarios and states feature to achieve this 1. Save ALL requests even identicals during a session and ensure the order in which they are called remains.

@julien-azouz-beezup commented on GitHub (Sep 30, 2021): Another suggested change, more trivial I think : An **option** to record all requests even the identical ones in a "static" mapping. The default behavior of request matching for this will simply fetch the first matching request. Then I could leverage the scenarios and states feature to achieve this `1. Save ALL requests even identicals during a session and ensure the order in which they are called remains.`
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/WireMock.Net#355