Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 48 additions & 16 deletions examples/WireMock.Net.Console.NET8/MainApp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ private static void RunOnLocal()
}
}

public static void Run()
public static async Task RunAsync()
{
//RunSse();
//RunOnLocal();
Expand All @@ -290,24 +290,56 @@ public static void Run()

var server = WireMockServer.Start();

//server
// .Given(Request.Create()
// .WithPath("todos")
// .UsingGet()
// )
// .RespondWith(Response.Create()
// .WithBodyAsJson(todos.Values)
// );

//server
// .Given(Request.Create()
// .UsingGet()
// .WithPath("todos")
// .WithParam("id")
// )
// .RespondWith(Response.Create()
// .WithBodyAsJson(rm => todos[int.Parse(rm.Query!["id"].ToString())])
// );

var pX = 0.40;
server
.Given(Request.Create()
.WithPath("todos")
.UsingGet()
)
.RespondWith(Response.Create()
.WithBodyAsJson(todos.Values)
);
.Given(Request.Create().UsingGet().WithPath("/p"))
.WithProbability(pX)
.RespondWith(Response.Create().WithStatusCode(200).WithBody("X"));

server
.Given(Request.Create()
.UsingGet()
.WithPath("todos")
.WithParam("id")
)
.RespondWith(Response.Create()
.WithBodyAsJson(rm => todos[int.Parse(rm.Query!["id"].ToString())])
);
.Given(Request.Create().UsingGet().WithPath("/p"))
.RespondWith(Response.Create().WithStatusCode(200).WithBody("default"));

// Act
var requestUri = new Uri($"http://localhost:{server.Port}/p");
var c = server.CreateClient();
var xCount = 0;
var defaultCount = 0;
var tot = 1000;
for (var i = 0; i < tot; i++)
{
var response = await c.GetAsync(requestUri);
var value = await response.Content.ReadAsStringAsync();
if (value == "X")
{
xCount++;
}
else if (value == "default")
{
defaultCount++;
}
}
System.Console.WriteLine("X = {0} ; default = {1} ; pX = {2} ; valueX = {3:0.00}", xCount, defaultCount, pX, 1.0 * xCount / tot);
return;

using var httpAndHttpsWithPort = WireMockServer.Start(new WireMockServerSettings
{
Expand Down
5 changes: 3 additions & 2 deletions examples/WireMock.Net.Console.NET8/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

using System.IO;
using System.Reflection;
using System.Threading.Tasks;
using log4net;
using log4net.Config;
using log4net.Repository;
Expand All @@ -14,10 +15,10 @@ static class Program
private static readonly ILoggerRepository LogRepository = LogManager.GetRepository(Assembly.GetEntryAssembly());
private static readonly ILog Log = LogManager.GetLogger(typeof(Program));

static void Main(params string[] args)
static async Task Main(params string[] args)
{
XmlConfigurator.Configure(LogRepository, new FileInfo("log4net.config"));

MainApp.Run();
await MainApp.RunAsync();
}
}
15 changes: 10 additions & 5 deletions src/WireMock.Net.Minimal/Owin/MappingMatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public MappingMatcher(IWireMockMiddlewareOptions options, IRandomizerDoubleBetwe

var mappings = _options.Mappings.Values
.Where(m => m.TimeSettings.IsValid())
.Where(m => m.Probability is null || m.Probability <= _randomizerDoubleBetween0And1.Generate())
.Where(m => m.Probability is null || m.Probability > _randomizerDoubleBetween0And1.Generate())
.ToArray();

foreach (var mapping in mappings)
Expand Down Expand Up @@ -62,14 +62,16 @@ public MappingMatcher(IWireMockMiddlewareOptions options, IRandomizerDoubleBetwe
}
}

var partialMappings = possibleMappings
var partialMatches = possibleMappings
.Where(pm => (pm.Mapping.IsAdminInterface && pm.RequestMatchResult.IsPerfectMatch) || !pm.Mapping.IsAdminInterface)
.OrderBy(m => m.RequestMatchResult)
.ThenBy(m => m.RequestMatchResult.TotalNumber)
.ThenBy(m => m.Mapping.Priority)
.ThenByDescending(m => m.Mapping.Probability ?? 0)
.ThenByDescending(m => m.Mapping.UpdatedAt)
.ToList();
var partialMatch = partialMappings.FirstOrDefault(pm => pm.RequestMatchResult.AverageTotalScore > 0.0);
.Where(pm => pm.RequestMatchResult.AverageTotalScore > 0.0)
.ToArray();
var partialMatch = partialMatches.FirstOrDefault();

if (_options.AllowPartialMapping == true)
{
Expand All @@ -78,7 +80,10 @@ public MappingMatcher(IWireMockMiddlewareOptions options, IRandomizerDoubleBetwe

var match = possibleMappings
.Where(m => m.RequestMatchResult.IsPerfectMatch)
.OrderBy(m => m.Mapping.Priority).ThenBy(m => m.RequestMatchResult).ThenByDescending(m => m.Mapping.UpdatedAt)
.OrderBy(m => m.Mapping.Priority)
.ThenBy(m => m.RequestMatchResult)
.ThenByDescending(m => m.Mapping.Probability ?? 0)
.ThenByDescending(m => m.Mapping.UpdatedAt)
.FirstOrDefault();

return (match, partialMatch);
Expand Down
Loading