mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-04-24 09:48:51 +02:00
FindRequestByMappingGuidAsync (#1043)
* FindRequestByMappingGuidAsync * fix * sc
This commit is contained in:
@@ -15,12 +15,12 @@ public class LogEntryModel
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// The request.
|
/// The request.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public LogRequestModel Request { get; set; }
|
public required LogRequestModel Request { get; init; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The response.
|
/// The response.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public LogResponseModel Response { get; set; }
|
public required LogResponseModel Response { get; init; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The mapping unique identifier.
|
/// The mapping unique identifier.
|
||||||
|
|||||||
@@ -43,6 +43,11 @@
|
|||||||
<PrivateAssets>all</PrivateAssets>
|
<PrivateAssets>all</PrivateAssets>
|
||||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||||
</PackageReference>
|
</PackageReference>
|
||||||
|
|
||||||
|
<PackageReference Include="PolySharp" Version="1.14.0">
|
||||||
|
<PrivateAssets>all</PrivateAssets>
|
||||||
|
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||||
|
</PackageReference>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup Condition="$(TargetFramework.StartsWith('netstandard')) and '$(TargetFramework)' != 'netstandard1.0'">
|
<ItemGroup Condition="$(TargetFramework.StartsWith('netstandard')) and '$(TargetFramework)' != 'netstandard1.0'">
|
||||||
|
|||||||
@@ -178,7 +178,7 @@ public interface IWireMockAdminApi
|
|||||||
/// Get a request based on the guid
|
/// Get a request based on the guid
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="guid">The Guid</param>
|
/// <param name="guid">The Guid</param>
|
||||||
/// <returns>MappingModel</returns>
|
/// <returns>LogEntryModel</returns>
|
||||||
/// <param name="cancellationToken">The optional cancellationToken.</param>
|
/// <param name="cancellationToken">The optional cancellationToken.</param>
|
||||||
[Get("requests/{guid}")]
|
[Get("requests/{guid}")]
|
||||||
Task<LogEntryModel> GetRequestAsync([Path] Guid guid, CancellationToken cancellationToken = default);
|
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);
|
Task<StatusModel> DeleteRequestAsync([Path] Guid guid, CancellationToken cancellationToken = default);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Find a request based on the criteria
|
/// Find requests based on the criteria (<see cref="RequestModel"/>)
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="model">The RequestModel</param>
|
/// <param name="model">The RequestModel</param>
|
||||||
/// <param name="cancellationToken">The optional cancellationToken.</param>
|
/// <param name="cancellationToken">The optional cancellationToken.</param>
|
||||||
@@ -200,6 +200,14 @@ public interface IWireMockAdminApi
|
|||||||
[Header("Content-Type", "application/json")]
|
[Header("Content-Type", "application/json")]
|
||||||
Task<IList<LogEntryModel>> FindRequestsAsync([Body] RequestModel model, CancellationToken cancellationToken = default);
|
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>
|
/// <summary>
|
||||||
/// Get all scenarios
|
/// Get all scenarios
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@@ -99,6 +99,7 @@ public partial class WireMockServer
|
|||||||
|
|
||||||
// __admin/requests/find
|
// __admin/requests/find
|
||||||
Given(Request.Create().WithPath(AdminRequests + "/find").UsingPost()).AtPriority(WireMockConstants.AdminPriority).RespondWith(new DynamicResponseProvider(RequestsFind));
|
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
|
// __admin/scenarios
|
||||||
Given(Request.Create().WithPath(AdminScenarios).UsingGet()).AtPriority(WireMockConstants.AdminPriority).RespondWith(new DynamicResponseProvider(ScenariosGet));
|
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);
|
var mappingModels = DeserializeRequestMessageToArray<MappingModel>(requestMessage);
|
||||||
if (mappingModels.Length == 1)
|
if (mappingModels.Length == 1)
|
||||||
{
|
{
|
||||||
Guid? guid = ConvertMappingAndRegisterAsRespondProvider(mappingModels[0]);
|
var guid = ConvertMappingAndRegisterAsRespondProvider(mappingModels[0]);
|
||||||
return ResponseMessageBuilder.Create(201, "Mapping added", guid);
|
return ResponseMessageBuilder.Create(201, "Mapping added", guid);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -535,7 +536,7 @@ public partial class WireMockServer
|
|||||||
{
|
{
|
||||||
if (TryParseGuidFromRequestMessage(requestMessage, out var guid))
|
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 { })
|
if (entry is { })
|
||||||
{
|
{
|
||||||
var model = new LogEntryMapper(_options).Map(entry);
|
var model = new LogEntryMapper(_options).Map(entry);
|
||||||
@@ -600,6 +601,26 @@ public partial class WireMockServer
|
|||||||
|
|
||||||
return ToJson(result);
|
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
|
#endregion Requests/find
|
||||||
|
|
||||||
#region Scenarios
|
#region Scenarios
|
||||||
|
|||||||
@@ -72,13 +72,6 @@
|
|||||||
<PackageReference Include="TinyMapper" Version="3.0.3" />
|
<PackageReference Include="TinyMapper" Version="3.0.3" />
|
||||||
</ItemGroup>
|
</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' ">
|
<ItemGroup Condition=" '$(TargetFramework)' != 'netstandard1.3' ">
|
||||||
<PackageReference Include="XPath2.Extensions" Version="1.1.4" />
|
<PackageReference Include="XPath2.Extensions" Version="1.1.4" />
|
||||||
<PackageReference Include="Microsoft.IdentityModel.Protocols.OpenIdConnect" Version="6.12.2" />
|
<PackageReference Include="Microsoft.IdentityModel.Protocols.OpenIdConnect" Version="6.12.2" />
|
||||||
|
|||||||
@@ -234,6 +234,10 @@ public class WireMockAdminApiTests
|
|||||||
StartAdminInterface = true,
|
StartAdminInterface = true,
|
||||||
Logger = new WireMockNullLogger()
|
Logger = new WireMockNullLogger()
|
||||||
});
|
});
|
||||||
|
server
|
||||||
|
.Given(Request.Create().WithPath("/foo").UsingGet())
|
||||||
|
.RespondWith(Response.Create());
|
||||||
|
|
||||||
var serverUrl = "http://localhost:" + server.Ports[0];
|
var serverUrl = "http://localhost:" + server.Ports[0];
|
||||||
await new HttpClient().GetAsync(serverUrl + "/foo").ConfigureAwait(false);
|
await new HttpClient().GetAsync(serverUrl + "/foo").ConfigureAwait(false);
|
||||||
var api = RestClient.For<IWireMockAdminApi>(serverUrl);
|
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);
|
var requests = await api.FindRequestsAsync(new RequestModel { Methods = new[] { "GET" } }).ConfigureAwait(false);
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
Check.That(requests).HasSize(1);
|
requests.Should().HaveCount(1);
|
||||||
var requestLogged = requests.First();
|
var requestLogged = requests.First();
|
||||||
Check.That(requestLogged.Request.Method).IsEqualTo("GET");
|
requestLogged.Request.Method.Should().Be("GET");
|
||||||
Check.That(requestLogged.Request.Body).IsNull();
|
requestLogged.Request.Body.Should().BeNull();
|
||||||
Check.That(requestLogged.Request.Path).IsEqualTo("/foo");
|
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]
|
[Fact]
|
||||||
@@ -815,7 +890,7 @@ public class WireMockAdminApiTests
|
|||||||
.RespondWith(
|
.RespondWith(
|
||||||
Response.Create()
|
Response.Create()
|
||||||
.WithStatusCode(HttpStatusCode.AlreadyReported)
|
.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
|
is
|
||||||
multiline
|
multiline
|
||||||
text
|
text
|
||||||
@@ -826,7 +901,7 @@ text
|
|||||||
.Given(
|
.Given(
|
||||||
Request.Create()
|
Request.Create()
|
||||||
.WithPath("/foo3")
|
.WithPath("/foo3")
|
||||||
.WithBody(new JsonPartialMatcher(new { a=1, b=2}))
|
.WithBody(new JsonPartialMatcher(new { a = 1, b = 2 }))
|
||||||
.UsingPost()
|
.UsingPost()
|
||||||
)
|
)
|
||||||
.WithGuid(guid4)
|
.WithGuid(guid4)
|
||||||
|
|||||||
@@ -117,7 +117,7 @@ public class WireMockServerProxyTests
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
server.Mappings.Should().HaveCount(34);
|
server.Mappings.Should().HaveCount(35);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
|
|||||||
@@ -81,7 +81,7 @@ public class WireMockServerSettingsTests
|
|||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
server.Mappings.Should().NotBeNull();
|
server.Mappings.Should().NotBeNull();
|
||||||
server.Mappings.Should().HaveCount(32);
|
server.Mappings.Should().HaveCount(33);
|
||||||
server.Mappings.All(m => m.Priority == WireMockConstants.AdminPriority).Should().BeTrue();
|
server.Mappings.All(m => m.Priority == WireMockConstants.AdminPriority).Should().BeTrue();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -100,9 +100,9 @@ public class WireMockServerSettingsTests
|
|||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
server.Mappings.Should().NotBeNull();
|
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);
|
server.Mappings.Count(m => m.Priority == WireMockConstants.ProxyPriority).Should().Be(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user