Request matching WithProbability strange behaviour #611

Closed
opened 2025-12-29 08:30:55 +01:00 by adam · 5 comments
Owner

Originally created by @mihailtrifonovio on GitHub (Jun 20, 2024).

Originally assigned to: @StefH on GitHub.

Based on article for "Chaos Engineering with Fault Injections" I've added following code:

var server = StandAloneApp.Start(new WireMockServerSettings {  Port = 8888 } );

        server
          .Given(Request.Create().WithPath("/test").UsingGet())
          .WithProbability(0.5)
          .RespondWith(Response.Create()
              .WithStatusCode(200)
              .WithBody(@"Success")
            );

        server
          .Given(Request.Create().WithPath("/test").UsingGet())
          .RespondWith(Response.Create()
              .WithStatusCode(500)
              .WithBody(@"Internal Server error")
          );

My expectation is that around 50% of the time there will be faults. However it is 100% of the time. If second matching is removed then there is some distribution between 200 and 404(no matching found), but is not 1:1 and more like 5:1 in favor of 404.

Is there something I'm missing?

Originally created by @mihailtrifonovio on GitHub (Jun 20, 2024). Originally assigned to: @StefH on GitHub. Based on article for "Chaos Engineering with Fault Injections" I've added following code: ``` c# var server = StandAloneApp.Start(new WireMockServerSettings { Port = 8888 } ); server .Given(Request.Create().WithPath("/test").UsingGet()) .WithProbability(0.5) .RespondWith(Response.Create() .WithStatusCode(200) .WithBody(@"Success") ); server .Given(Request.Create().WithPath("/test").UsingGet()) .RespondWith(Response.Create() .WithStatusCode(500) .WithBody(@"Internal Server error") ); ``` My expectation is that around 50% of the time there will be faults. However it is 100% of the time. If second matching is removed then there is some distribution between 200 and 404(no matching found), but is not 1:1 and more like 5:1 in favor of 404. Is there something I'm missing?
adam added the bug label 2025-12-29 08:30:55 +01:00
adam closed this issue 2025-12-29 08:30:55 +01:00
Author
Owner

@StefH commented on GitHub (Oct 16, 2024):

@mihailtrifonovio
Can you provide a example project?

@StefH commented on GitHub (Oct 16, 2024): @mihailtrifonovio Can you provide a example project?
Author
Owner

@StefH commented on GitHub (Dec 7, 2024):

@mihailtrifonovio
Can you provide a example project?

@StefH commented on GitHub (Dec 7, 2024): @mihailtrifonovio Can you provide a example project?
Author
Owner

@StefH commented on GitHub (Jan 11, 2025):

@mihailtrifonovio
Can you provide a example project?

@StefH commented on GitHub (Jan 11, 2025): @mihailtrifonovio Can you provide a example project?
Author
Owner

@TheRubble commented on GitHub (Apr 16, 2025):

Did this ever get sorted? I'm using the aspire integration (1.7.4 & 1.8.0-prview-01).

var apiServiceUsedForDocs = builder
    .AddWireMock("apiservice1", WireMockServerArguments.DefaultPort)
    .WithApiMappingBuilder(adminApiBuilder =>
    {
        adminApiBuilder.Given(b => b
            .WithRequest(request => request
                .UsingGet()
                .WithPath("/test")
            )
            .WithProbability(0.7)
            .WithResponse(response => response
                .WithStatusCode(200)
                .WithBody("{ \"message\": \"Success\" }")
                .WithHeaders(new Dictionary<string, object>()
                {
                    {"Content-Type", "application/json"}
                })
            )
        );

        adminApiBuilder.Given(b => b
            .WithRequest(request => request
                .UsingGet()
                .WithPath("/test")
            )
            .WithResponse(response => response

                .WithStatusCode(500)
                .WithBody("{ \"message\": \"500\" }")
                .WithHeaders(new Dictionary<string, object>()
                {
                    {"Content-Type", "application/json"}
                }
                )
            )
        );

        return adminApiBuilder.BuildAndPostAsync();
    });

It's just returning the failure (500) response.

@TheRubble commented on GitHub (Apr 16, 2025): Did this ever get sorted? I'm using the aspire integration (1.7.4 & 1.8.0-prview-01). ``` c# var apiServiceUsedForDocs = builder .AddWireMock("apiservice1", WireMockServerArguments.DefaultPort) .WithApiMappingBuilder(adminApiBuilder => { adminApiBuilder.Given(b => b .WithRequest(request => request .UsingGet() .WithPath("/test") ) .WithProbability(0.7) .WithResponse(response => response .WithStatusCode(200) .WithBody("{ \"message\": \"Success\" }") .WithHeaders(new Dictionary<string, object>() { {"Content-Type", "application/json"} }) ) ); adminApiBuilder.Given(b => b .WithRequest(request => request .UsingGet() .WithPath("/test") ) .WithResponse(response => response .WithStatusCode(500) .WithBody("{ \"message\": \"500\" }") .WithHeaders(new Dictionary<string, object>() { {"Content-Type", "application/json"} } ) ) ); return adminApiBuilder.BuildAndPostAsync(); }); ``` It's just returning the failure (500) response.
Author
Owner

@StefH commented on GitHub (Oct 13, 2025):

@TheRubble

Thanks for reporting, this is indeed a bug. The Probability logic was wrong.

This PR should fix it https://github.com/wiremock/WireMock.Net/pull/1367


However when using more WithProbability for the same mapping, the logic is still not 100% ok.
So if you have

  1. WithProbability(0.5)
  2. WithProbability(0.1)
  3. no Probability

The second one will only be chosen about 5% of the time, not 10%...
I'll fix this maybe later.

@StefH commented on GitHub (Oct 13, 2025): @TheRubble Thanks for reporting, this is indeed a bug. The Probability logic was wrong. This PR should fix it https://github.com/wiremock/WireMock.Net/pull/1367 --- However when using more WithProbability for the same mapping, the logic is still not 100% ok. So if you have 1. WithProbability(0.5) 2. WithProbability(0.1) 3. no Probability The second one will only be chosen about 5% of the time, not 10%... I'll fix this maybe later.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/WireMock.Net#611