mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-04-18 23:20:11 +02:00
Change FindRequestByMappingGuidAsync to return a collection of entries (#1046)
This commit is contained in:
@@ -201,12 +201,12 @@ public interface IWireMockAdminApi
|
|||||||
Task<IList<LogEntryModel>> FindRequestsAsync([Body] RequestModel model, CancellationToken cancellationToken = default);
|
Task<IList<LogEntryModel>> FindRequestsAsync([Body] RequestModel model, CancellationToken cancellationToken = default);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Find a request based on the Mapping Guid.
|
/// Find requests based on the Mapping Guid.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="mappingGuid">The Mapping Guid</param>
|
/// <param name="mappingGuid">The Mapping Guid</param>
|
||||||
/// <param name="cancellationToken">The optional cancellationToken.</param>
|
/// <param name="cancellationToken">The optional cancellationToken.</param>
|
||||||
[Get("requests/find")]
|
[Get("requests/find")]
|
||||||
Task<LogEntryModel?> FindRequestByMappingGuidAsync([Query] Guid mappingGuid, CancellationToken cancellationToken = default);
|
Task<IList<LogEntryModel>> FindRequestsByMappingGuidAsync([Query] Guid mappingGuid, CancellationToken cancellationToken = default);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Get all scenarios
|
/// Get all scenarios
|
||||||
@@ -304,4 +304,4 @@ public interface IWireMockAdminApi
|
|||||||
/// <param name="cancellationToken">The optional cancellationToken.</param>
|
/// <param name="cancellationToken">The optional cancellationToken.</param>
|
||||||
[Post("openapi/save")]
|
[Post("openapi/save")]
|
||||||
Task<StatusModel> OpenApiSaveAsync([Body] string text, CancellationToken cancellationToken = default);
|
Task<StatusModel> OpenApiSaveAsync([Body] string text, CancellationToken cancellationToken = default);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -99,7 +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));
|
Given(Request.Create().WithPath(AdminRequests + "/find").UsingGet().WithParam("mappingGuid", new NotNullOrEmptyMatcher())).AtPriority(WireMockConstants.AdminPriority).RespondWith(new DynamicResponseProvider(RequestsFindByMappingGuid));
|
||||||
|
|
||||||
// __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));
|
||||||
@@ -602,21 +602,17 @@ public partial class WireMockServer
|
|||||||
return ToJson(result);
|
return ToJson(result);
|
||||||
}
|
}
|
||||||
|
|
||||||
private IResponseMessage RequestFindByMappingGuid(IRequestMessage requestMessage)
|
private IResponseMessage RequestsFindByMappingGuid(IRequestMessage requestMessage)
|
||||||
{
|
{
|
||||||
if (requestMessage.Query != null &&
|
if (requestMessage.Query != null &&
|
||||||
requestMessage.Query.TryGetValue("mappingGuid", out var value) &&
|
requestMessage.Query.TryGetValue("mappingGuid", out var value) &&
|
||||||
Guid.TryParse(value.ToString(), out var mappingGuid)
|
Guid.TryParse(value.ToString(), out var mappingGuid)
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
var logEntry = LogEntries.SingleOrDefault(le => !le.RequestMessage.Path.StartsWith("/__admin/") && le.MappingGuid == mappingGuid);
|
var logEntries = LogEntries.Where(le => !le.RequestMessage.Path.StartsWith("/__admin/") && le.MappingGuid == mappingGuid);
|
||||||
if (logEntry != null)
|
var logEntryMapper = new LogEntryMapper(_options);
|
||||||
{
|
var result = logEntries.Select(logEntryMapper.Map);
|
||||||
var logEntryMapper = new LogEntryMapper(_options);
|
return ToJson(result);
|
||||||
return ToJson(logEntryMapper.Map(logEntry));
|
|
||||||
}
|
|
||||||
|
|
||||||
return ResponseMessageBuilder.Create(HttpStatusCode.OK);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return ResponseMessageBuilder.Create(HttpStatusCode.BadRequest);
|
return ResponseMessageBuilder.Create(HttpStatusCode.BadRequest);
|
||||||
@@ -833,4 +829,4 @@ public partial class WireMockServer
|
|||||||
var singleResult = ((JObject)value).ToObject<T>();
|
var singleResult = ((JObject)value).ToObject<T>();
|
||||||
return new[] { singleResult! };
|
return new[] { singleResult! };
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
#if !(NET452 || NET461 || NETCOREAPP3_1)
|
#if !(NET452 || NET461 || NETCOREAPP3_1)
|
||||||
using System;
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Net;
|
using System.Net;
|
||||||
@@ -25,6 +26,7 @@ using WireMock.RequestBuilders;
|
|||||||
using WireMock.ResponseBuilders;
|
using WireMock.ResponseBuilders;
|
||||||
using WireMock.Server;
|
using WireMock.Server;
|
||||||
using WireMock.Settings;
|
using WireMock.Settings;
|
||||||
|
using WireMock.Types;
|
||||||
using Xunit;
|
using Xunit;
|
||||||
|
|
||||||
namespace WireMock.Net.Tests;
|
namespace WireMock.Net.Tests;
|
||||||
@@ -254,7 +256,7 @@ public class WireMockAdminApiTests
|
|||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public async Task IWireMockAdminApi_FindRequestByMappingGuidAsync_Found()
|
public async Task IWireMockAdminApi_FindRequestsByMappingGuidAsync_Found()
|
||||||
{
|
{
|
||||||
// Arrange
|
// Arrange
|
||||||
var mappingGuid = Guid.NewGuid();
|
var mappingGuid = Guid.NewGuid();
|
||||||
@@ -269,21 +271,33 @@ public class WireMockAdminApiTests
|
|||||||
.RespondWith(Response.Create());
|
.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);
|
using var client = new HttpClient();
|
||||||
|
await client.GetAsync(serverUrl + "/foo").ConfigureAwait(false);
|
||||||
|
await client.GetAsync(serverUrl + "/foo?bar=baz").ConfigureAwait(false);
|
||||||
var api = RestClient.For<IWireMockAdminApi>(serverUrl);
|
var api = RestClient.For<IWireMockAdminApi>(serverUrl);
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
var logEntryModel = await api.FindRequestByMappingGuidAsync(mappingGuid).ConfigureAwait(false);
|
var logEntryModels = await api.FindRequestsByMappingGuidAsync(mappingGuid).ConfigureAwait(false);
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
logEntryModel.Should().NotBeNull();
|
logEntryModels.Should().HaveCount(2);
|
||||||
logEntryModel!.Request.Method.Should().Be("GET");
|
logEntryModels[0].Should().NotBeNull();
|
||||||
logEntryModel!.Request.Body.Should().BeNull();
|
logEntryModels[0]!.Request.Method.Should().Be("GET");
|
||||||
logEntryModel!.Request.Path.Should().Be("/foo");
|
logEntryModels[0]!.Request.Body.Should().BeNull();
|
||||||
|
logEntryModels[0]!.Request.Path.Should().Be("/foo");
|
||||||
|
logEntryModels[0]!.Request.Query.Should().BeNullOrEmpty();
|
||||||
|
logEntryModels[1].Should().NotBeNull();
|
||||||
|
logEntryModels[1]!.Request.Method.Should().Be("GET");
|
||||||
|
logEntryModels[1]!.Request.Body.Should().BeNull();
|
||||||
|
logEntryModels[1]!.Request.Path.Should().Be("/foo");
|
||||||
|
logEntryModels[1]!.Request.Query.Should().BeEquivalentTo(new Dictionary<string, WireMockList<string>>
|
||||||
|
{
|
||||||
|
{"bar", new WireMockList<string>("baz")}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public async Task IWireMockAdminApi_FindRequestByMappingGuidAsync_NotFound()
|
public async Task IWireMockAdminApi_FindRequestsByMappingGuidAsync_NotFound()
|
||||||
{
|
{
|
||||||
// Arrange
|
// Arrange
|
||||||
var server = WireMockServer.Start(new WireMockServerSettings
|
var server = WireMockServer.Start(new WireMockServerSettings
|
||||||
@@ -301,14 +315,14 @@ public class WireMockAdminApiTests
|
|||||||
var api = RestClient.For<IWireMockAdminApi>(serverUrl);
|
var api = RestClient.For<IWireMockAdminApi>(serverUrl);
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
var logEntryModel = await api.FindRequestByMappingGuidAsync(Guid.NewGuid()).ConfigureAwait(false);
|
var logEntryModels = await api.FindRequestsByMappingGuidAsync(Guid.NewGuid()).ConfigureAwait(false);
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
logEntryModel.Should().BeNull();
|
logEntryModels.Should().BeEmpty();
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public async Task IWireMockAdminApi_FindRequestByMappingGuidAsync_Invalid_ShouldReturnBadRequest()
|
public async Task IWireMockAdminApi_FindRequestsByMappingGuidAsync_Invalid_ShouldReturnBadRequest()
|
||||||
{
|
{
|
||||||
// Arrange
|
// Arrange
|
||||||
var server = WireMockServer.Start(new WireMockServerSettings
|
var server = WireMockServer.Start(new WireMockServerSettings
|
||||||
@@ -1001,4 +1015,4 @@ text
|
|||||||
server.Stop();
|
server.Stop();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
Reference in New Issue
Block a user