Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change FindRequestByMappingGuidAsync to return a collection of entries #1046

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 3 additions & 3 deletions src/WireMock.Net.RestClient/IWireMockAdminApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -201,12 +201,12 @@ public interface IWireMockAdminApi
Task<IList<LogEntryModel>> FindRequestsAsync([Body] RequestModel model, CancellationToken cancellationToken = default);

/// <summary>
/// Find a request based on the Mapping Guid.
/// Find requests 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);
Task<IList<LogEntryModel>> FindRequestsByMappingGuidAsync([Query] Guid mappingGuid, CancellationToken cancellationToken = default);

/// <summary>
/// Get all scenarios
Expand Down Expand Up @@ -304,4 +304,4 @@ public interface IWireMockAdminApi
/// <param name="cancellationToken">The optional cancellationToken.</param>
[Post("openapi/save")]
Task<StatusModel> OpenApiSaveAsync([Body] string text, CancellationToken cancellationToken = default);
}
}
18 changes: 7 additions & 11 deletions src/WireMock.Net/Server/WireMockServer.Admin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ private void InitAdmin()

// __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));
Given(Request.Create().WithPath(AdminRequests + "/find").UsingGet().WithParam("mappingGuid", new NotNullOrEmptyMatcher())).AtPriority(WireMockConstants.AdminPriority).RespondWith(new DynamicResponseProvider(RequestsFindByMappingGuid));

// __admin/scenarios
Given(Request.Create().WithPath(AdminScenarios).UsingGet()).AtPriority(WireMockConstants.AdminPriority).RespondWith(new DynamicResponseProvider(ScenariosGet));
Expand Down Expand Up @@ -602,21 +602,17 @@ private IResponseMessage RequestsFind(IRequestMessage requestMessage)
return ToJson(result);
}

private IResponseMessage RequestFindByMappingGuid(IRequestMessage requestMessage)
private IResponseMessage RequestsFindByMappingGuid(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);
var logEntries = LogEntries.Where(le => !le.RequestMessage.Path.StartsWith("/__admin/") && le.MappingGuid == mappingGuid);
var logEntryMapper = new LogEntryMapper(_options);
var result = logEntries.Select(logEntryMapper.Map);
return ToJson(result);
}

return ResponseMessageBuilder.Create(HttpStatusCode.BadRequest);
Expand Down Expand Up @@ -833,4 +829,4 @@ private static T[] DeserializeObjectToArray<T>(object value)
var singleResult = ((JObject)value).ToObject<T>();
return new[] { singleResult! };
}
}
}
38 changes: 26 additions & 12 deletions test/WireMock.Net.Tests/WireMockAdminApiTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#if !(NET452 || NET461 || NETCOREAPP3_1)
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
Expand All @@ -25,6 +26,7 @@
using WireMock.ResponseBuilders;
using WireMock.Server;
using WireMock.Settings;
using WireMock.Types;
using Xunit;

namespace WireMock.Net.Tests;
Expand Down Expand Up @@ -254,7 +256,7 @@ public async Task IWireMockAdminApi_FindRequestsAsync()
}

[Fact]
public async Task IWireMockAdminApi_FindRequestByMappingGuidAsync_Found()
public async Task IWireMockAdminApi_FindRequestsByMappingGuidAsync_Found()
{
// Arrange
var mappingGuid = Guid.NewGuid();
Expand All @@ -269,21 +271,33 @@ public async Task IWireMockAdminApi_FindRequestByMappingGuidAsync_Found()
.RespondWith(Response.Create());

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);

// Act
var logEntryModel = await api.FindRequestByMappingGuidAsync(mappingGuid).ConfigureAwait(false);
var logEntryModels = await api.FindRequestsByMappingGuidAsync(mappingGuid).ConfigureAwait(false);

// Assert
logEntryModel.Should().NotBeNull();
logEntryModel!.Request.Method.Should().Be("GET");
logEntryModel!.Request.Body.Should().BeNull();
logEntryModel!.Request.Path.Should().Be("/foo");
logEntryModels.Should().HaveCount(2);
logEntryModels[0].Should().NotBeNull();
logEntryModels[0]!.Request.Method.Should().Be("GET");
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]
public async Task IWireMockAdminApi_FindRequestByMappingGuidAsync_NotFound()
public async Task IWireMockAdminApi_FindRequestsByMappingGuidAsync_NotFound()
{
// Arrange
var server = WireMockServer.Start(new WireMockServerSettings
Expand All @@ -301,14 +315,14 @@ public async Task IWireMockAdminApi_FindRequestByMappingGuidAsync_NotFound()
var api = RestClient.For<IWireMockAdminApi>(serverUrl);

// Act
var logEntryModel = await api.FindRequestByMappingGuidAsync(Guid.NewGuid()).ConfigureAwait(false);
var logEntryModels = await api.FindRequestsByMappingGuidAsync(Guid.NewGuid()).ConfigureAwait(false);

// Assert
logEntryModel.Should().BeNull();
logEntryModels.Should().BeEmpty();
}

[Fact]
public async Task IWireMockAdminApi_FindRequestByMappingGuidAsync_Invalid_ShouldReturnBadRequest()
public async Task IWireMockAdminApi_FindRequestsByMappingGuidAsync_Invalid_ShouldReturnBadRequest()
{
// Arrange
var server = WireMockServer.Start(new WireMockServerSettings
Expand Down Expand Up @@ -1001,4 +1015,4 @@ public async Task IWireMockAdminApi_OpenApiSave_Yml()
server.Stop();
}
}
#endif
#endif
Loading