FindRequestByMappingGuidAsync (#1043)

* FindRequestByMappingGuidAsync

* fix

* sc
This commit is contained in:
Stef Heyenrath
2023-12-21 20:03:22 +01:00
committed by GitHub
parent 4721b73a16
commit a8339a0867
8 changed files with 125 additions and 23 deletions

View File

@@ -15,12 +15,12 @@ public class LogEntryModel
/// <summary>
/// The request.
/// </summary>
public LogRequestModel Request { get; set; }
public required LogRequestModel Request { get; init; }
/// <summary>
/// The response.
/// </summary>
public LogResponseModel Response { get; set; }
public required LogResponseModel Response { get; init; }
/// <summary>
/// The mapping unique identifier.

View File

@@ -43,6 +43,11 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="PolySharp" Version="1.14.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
<ItemGroup Condition="$(TargetFramework.StartsWith('netstandard')) and '$(TargetFramework)' != 'netstandard1.0'">

View File

@@ -178,7 +178,7 @@ public interface IWireMockAdminApi
/// Get a request based on the guid
/// </summary>
/// <param name="guid">The Guid</param>
/// <returns>MappingModel</returns>
/// <returns>LogEntryModel</returns>
/// <param name="cancellationToken">The optional cancellationToken.</param>
[Get("requests/{guid}")]
Task<LogEntryModel> GetRequestAsync([Path] Guid guid, CancellationToken cancellationToken = default);
@@ -192,7 +192,7 @@ public interface IWireMockAdminApi
Task<StatusModel> DeleteRequestAsync([Path] Guid guid, CancellationToken cancellationToken = default);
/// <summary>
/// Find a request based on the criteria
/// Find requests based on the criteria (<see cref="RequestModel"/>)
/// </summary>
/// <param name="model">The RequestModel</param>
/// <param name="cancellationToken">The optional cancellationToken.</param>
@@ -200,6 +200,14 @@ public interface IWireMockAdminApi
[Header("Content-Type", "application/json")]
Task<IList<LogEntryModel>> FindRequestsAsync([Body] RequestModel model, CancellationToken cancellationToken = default);
/// <summary>
/// Find a request based on the Mapping Guid.
/// </summary>
/// <param name="mappingGuid">The Mapping Guid</param>
/// <param name="cancellationToken">The optional cancellationToken.</param>
[Get("requests/find")]
Task<LogEntryModel?> FindRequestByMappingGuidAsync([Query] Guid mappingGuid, CancellationToken cancellationToken = default);
/// <summary>
/// Get all scenarios
/// </summary>

View File

@@ -99,6 +99,7 @@ public partial class WireMockServer
// __admin/requests/find
Given(Request.Create().WithPath(AdminRequests + "/find").UsingPost()).AtPriority(WireMockConstants.AdminPriority).RespondWith(new DynamicResponseProvider(RequestsFind));
Given(Request.Create().WithPath(AdminRequests + "/find").UsingGet().WithParam("mappingGuid", new NotNullOrEmptyMatcher())).AtPriority(WireMockConstants.AdminPriority).RespondWith(new DynamicResponseProvider(RequestFindByMappingGuid));
// __admin/scenarios
Given(Request.Create().WithPath(AdminScenarios).UsingGet()).AtPriority(WireMockConstants.AdminPriority).RespondWith(new DynamicResponseProvider(ScenariosGet));
@@ -436,7 +437,7 @@ public partial class WireMockServer
var mappingModels = DeserializeRequestMessageToArray<MappingModel>(requestMessage);
if (mappingModels.Length == 1)
{
Guid? guid = ConvertMappingAndRegisterAsRespondProvider(mappingModels[0]);
var guid = ConvertMappingAndRegisterAsRespondProvider(mappingModels[0]);
return ResponseMessageBuilder.Create(201, "Mapping added", guid);
}
@@ -535,7 +536,7 @@ public partial class WireMockServer
{
if (TryParseGuidFromRequestMessage(requestMessage, out var guid))
{
var entry = LogEntries.FirstOrDefault(r => !r.RequestMessage.Path.StartsWith("/__admin/") && r.Guid == guid);
var entry = LogEntries.SingleOrDefault(r => !r.RequestMessage.Path.StartsWith("/__admin/") && r.Guid == guid);
if (entry is { })
{
var model = new LogEntryMapper(_options).Map(entry);
@@ -600,6 +601,26 @@ public partial class WireMockServer
return ToJson(result);
}
private IResponseMessage RequestFindByMappingGuid(IRequestMessage requestMessage)
{
if (requestMessage.Query != null &&
requestMessage.Query.TryGetValue("mappingGuid", out var value) &&
Guid.TryParse(value.ToString(), out var mappingGuid)
)
{
var logEntry = LogEntries.SingleOrDefault(le => !le.RequestMessage.Path.StartsWith("/__admin/") && le.MappingGuid == mappingGuid);
if (logEntry != null)
{
var logEntryMapper = new LogEntryMapper(_options);
return ToJson(logEntryMapper.Map(logEntry));
}
return ResponseMessageBuilder.Create(HttpStatusCode.OK);
}
return ResponseMessageBuilder.Create(HttpStatusCode.BadRequest);
}
#endregion Requests/find
#region Scenarios

View File

@@ -72,13 +72,6 @@
<PackageReference Include="TinyMapper" Version="3.0.3" />
</ItemGroup>
<ItemGroup Condition="'$(Configuration)' == 'Debug - Sonar'">
<PackageReference Include="SonarAnalyzer.CSharp" Version="7.8.0.7320">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
</ItemGroup>
<ItemGroup Condition=" '$(TargetFramework)' != 'netstandard1.3' ">
<PackageReference Include="XPath2.Extensions" Version="1.1.4" />
<PackageReference Include="Microsoft.IdentityModel.Protocols.OpenIdConnect" Version="6.12.2" />

View File

@@ -234,6 +234,10 @@ public class WireMockAdminApiTests
StartAdminInterface = true,
Logger = new WireMockNullLogger()
});
server
.Given(Request.Create().WithPath("/foo").UsingGet())
.RespondWith(Response.Create());
var serverUrl = "http://localhost:" + server.Ports[0];
await new HttpClient().GetAsync(serverUrl + "/foo").ConfigureAwait(false);
var api = RestClient.For<IWireMockAdminApi>(serverUrl);
@@ -242,11 +246,82 @@ public class WireMockAdminApiTests
var requests = await api.FindRequestsAsync(new RequestModel { Methods = new[] { "GET" } }).ConfigureAwait(false);
// Assert
Check.That(requests).HasSize(1);
requests.Should().HaveCount(1);
var requestLogged = requests.First();
Check.That(requestLogged.Request.Method).IsEqualTo("GET");
Check.That(requestLogged.Request.Body).IsNull();
Check.That(requestLogged.Request.Path).IsEqualTo("/foo");
requestLogged.Request.Method.Should().Be("GET");
requestLogged.Request.Body.Should().BeNull();
requestLogged.Request.Path.Should().Be("/foo");
}
[Fact]
public async Task IWireMockAdminApi_FindRequestByMappingGuidAsync_Found()
{
// Arrange
var mappingGuid = Guid.NewGuid();
var server = WireMockServer.Start(new WireMockServerSettings
{
StartAdminInterface = true,
Logger = new WireMockNullLogger()
});
server
.Given(Request.Create().WithPath("/foo").UsingGet())
.WithGuid(mappingGuid)
.RespondWith(Response.Create());
var serverUrl = "http://localhost:" + server.Ports[0];
await new HttpClient().GetAsync(serverUrl + "/foo").ConfigureAwait(false);
var api = RestClient.For<IWireMockAdminApi>(serverUrl);
// Act
var logEntryModel = await api.FindRequestByMappingGuidAsync(mappingGuid).ConfigureAwait(false);
// Assert
logEntryModel.Should().NotBeNull();
logEntryModel!.Request.Method.Should().Be("GET");
logEntryModel!.Request.Body.Should().BeNull();
logEntryModel!.Request.Path.Should().Be("/foo");
}
[Fact]
public async Task IWireMockAdminApi_FindRequestByMappingGuidAsync_NotFound()
{
// Arrange
var server = WireMockServer.Start(new WireMockServerSettings
{
StartAdminInterface = true,
Logger = new WireMockNullLogger()
});
server
.Given(Request.Create().WithPath("/foo").UsingGet())
.WithGuid(Guid.NewGuid())
.RespondWith(Response.Create());
var serverUrl = "http://localhost:" + server.Ports[0];
await new HttpClient().GetAsync(serverUrl + "/foo").ConfigureAwait(false);
var api = RestClient.For<IWireMockAdminApi>(serverUrl);
// Act
var logEntryModel = await api.FindRequestByMappingGuidAsync(Guid.NewGuid()).ConfigureAwait(false);
// Assert
logEntryModel.Should().BeNull();
}
[Fact]
public async Task IWireMockAdminApi_FindRequestByMappingGuidAsync_Invalid_ShouldReturnBadRequest()
{
// Arrange
var server = WireMockServer.Start(new WireMockServerSettings
{
StartAdminInterface = true,
Logger = new WireMockNullLogger()
});
// Act
var result = await server.CreateClient().GetAsync("/__admin/requests/find?mappingGuid=x");
// Assert
result.StatusCode.Should().Be(HttpStatusCode.BadRequest);
}
[Fact]
@@ -815,7 +890,7 @@ public class WireMockAdminApiTests
.RespondWith(
Response.Create()
.WithStatusCode(HttpStatusCode.AlreadyReported)
.WithBodyAsJson(new { @as = 1, b=1.2, d=true, e=false, f=new[]{1,2,3,4}, g= new{z1=1, z2=2, z3=new []{"a","b","c"}, z4=new[]{new {a=1, b=2},new {a=2, b=3}}}, date_field = new DateTime(2023,05,08,11,20,19), string_field_with_date="2021-03-13T21:04:00Z", multiline_text= @"This
.WithBodyAsJson(new { @as = 1, b = 1.2, d = true, e = false, f = new[] { 1, 2, 3, 4 }, g = new { z1 = 1, z2 = 2, z3 = new[] { "a", "b", "c" }, z4 = new[] { new { a = 1, b = 2 }, new { a = 2, b = 3 } } }, date_field = new DateTime(2023, 05, 08, 11, 20, 19), string_field_with_date = "2021-03-13T21:04:00Z", multiline_text = @"This
is
multiline
text
@@ -826,7 +901,7 @@ text
.Given(
Request.Create()
.WithPath("/foo3")
.WithBody(new JsonPartialMatcher(new { a=1, b=2}))
.WithBody(new JsonPartialMatcher(new { a = 1, b = 2 }))
.UsingPost()
)
.WithGuid(guid4)

View File

@@ -117,7 +117,7 @@ public class WireMockServerProxyTests
}
// Assert
server.Mappings.Should().HaveCount(34);
server.Mappings.Should().HaveCount(35);
}
[Fact]

View File

@@ -81,7 +81,7 @@ public class WireMockServerSettingsTests
// Assert
server.Mappings.Should().NotBeNull();
server.Mappings.Should().HaveCount(32);
server.Mappings.Should().HaveCount(33);
server.Mappings.All(m => m.Priority == WireMockConstants.AdminPriority).Should().BeTrue();
}
@@ -100,9 +100,9 @@ public class WireMockServerSettingsTests
// Assert
server.Mappings.Should().NotBeNull();
server.Mappings.Should().HaveCount(33);
server.Mappings.Should().HaveCount(34);
server.Mappings.Count(m => m.Priority == WireMockConstants.AdminPriority).Should().Be(32);
server.Mappings.Count(m => m.Priority == WireMockConstants.AdminPriority).Should().Be(33);
server.Mappings.Count(m => m.Priority == WireMockConstants.ProxyPriority).Should().Be(1);
}