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

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

Open
julien-azouz-beezup opened this issue Sep 17, 2021 · 7 comments
Labels

Comments

@julien-azouz-beezup
Copy link

julien-azouz-beezup commented 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.

@StefH
Copy link
Collaborator

StefH commented Sep 19, 2021

@julien-azouz-beezup

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

@julien-azouz-beezup
Copy link
Author

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

@StefH
Copy link
Collaborator

StefH commented 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.

@julien-azouz-beezup
Copy link
Author

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 ?

@StefH
Copy link
Collaborator

StefH commented 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.

@julien-azouz-beezup
Copy link
Author

julien-azouz-beezup commented 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
Copy link
Author

julien-azouz-beezup commented 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 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