From 84901ab1e49e46dcd3d9b0f6b1e2719f80e25ab8 Mon Sep 17 00:00:00 2001 From: Stef Heyenrath Date: Tue, 31 Jan 2017 09:28:04 +0100 Subject: [PATCH] AtPriority (#16) --- .../Program.cs | 1 + .../Admin/Mappings/MappingModel.cs | 8 ++++++ src/WireMock.Net/Mapping.cs | 12 ++++++++- .../Server/FluentMockServer.Admin.cs | 6 ++++- src/WireMock.Net/Server/FluentMockServer.cs | 16 ++++++++---- .../Server/IRespondWithAProvider.cs | 18 ++++++++++++- .../Server/IRespondWithAProviderGuid.cs | 17 ------------ .../Server/RespondWithAProvider.cs | 21 ++++++++++++--- .../FluentMockServerTests.cs | 26 +++++++++++++++++++ 9 files changed, 96 insertions(+), 29 deletions(-) delete mode 100644 src/WireMock.Net/Server/IRespondWithAProviderGuid.cs diff --git a/examples/WireMock.Net.ConsoleApplication/Program.cs b/examples/WireMock.Net.ConsoleApplication/Program.cs index 3541cb74..c588efdd 100644 --- a/examples/WireMock.Net.ConsoleApplication/Program.cs +++ b/examples/WireMock.Net.ConsoleApplication/Program.cs @@ -20,6 +20,7 @@ namespace WireMock.Net.ConsoleApplication server .Given(Request.Create().WithPath(u => u.Contains("x")).UsingGet()) + .AtPriority(4) .RespondWith(Response.Create() .WithStatusCode(200) .WithHeader("Content-Type", "application/json") diff --git a/src/WireMock.Net/Admin/Mappings/MappingModel.cs b/src/WireMock.Net/Admin/Mappings/MappingModel.cs index 84f100a9..797fa4fd 100644 --- a/src/WireMock.Net/Admin/Mappings/MappingModel.cs +++ b/src/WireMock.Net/Admin/Mappings/MappingModel.cs @@ -15,6 +15,14 @@ namespace WireMock.Admin.Mappings /// public Guid? Guid { get; set; } + /// + /// Gets or sets the priority. + /// + /// + /// The priority. + /// + public int? Priority { get; set; } + /// /// Gets or sets the request. /// diff --git a/src/WireMock.Net/Mapping.cs b/src/WireMock.Net/Mapping.cs index b3dfdb64..319f3c0b 100644 --- a/src/WireMock.Net/Mapping.cs +++ b/src/WireMock.Net/Mapping.cs @@ -9,6 +9,14 @@ namespace WireMock /// public class Mapping { + /// + /// Gets the priority. + /// + /// + /// The priority. + /// + public int Priority { get; } + /// /// Gets the unique identifier. /// @@ -33,8 +41,10 @@ namespace WireMock /// The the unique identifier. /// The request matcher. /// The provider. - public Mapping(Guid guid, IRequestMatcher requestMatcher, IResponseProvider provider) + /// The priority for this mapping. + public Mapping(Guid guid, IRequestMatcher requestMatcher, IResponseProvider provider, int priority) { + Priority = priority; Guid = guid; RequestMatcher = requestMatcher; Provider = provider; diff --git a/src/WireMock.Net/Server/FluentMockServer.Admin.cs b/src/WireMock.Net/Server/FluentMockServer.Admin.cs index dcd03164..91a05bea 100644 --- a/src/WireMock.Net/Server/FluentMockServer.Admin.cs +++ b/src/WireMock.Net/Server/FluentMockServer.Admin.cs @@ -124,11 +124,14 @@ namespace WireMock.Server var requestBuilder = InitRequestBuilder(mappingModel); var responseBuilder = InitResponseBuilder(mappingModel); - IRespondWithAProviderGuid respondProvider = Given(requestBuilder); + IRespondWithAProvider respondProvider = Given(requestBuilder); if (mappingModel.Guid != null && mappingModel.Guid != Guid.Empty) respondProvider = respondProvider.WithGuid(mappingModel.Guid.Value); + if (mappingModel.Priority != null) + respondProvider = respondProvider.AtPriority(mappingModel.Priority.Value); + respondProvider.RespondWith(responseBuilder); return new ResponseMessage { Body = "Mapping added" }; @@ -304,6 +307,7 @@ namespace WireMock.Server return new MappingModel { Guid = mapping.Guid, + Priority = mapping.Priority, Request = new RequestModel { Path = new PathModel diff --git a/src/WireMock.Net/Server/FluentMockServer.cs b/src/WireMock.Net/Server/FluentMockServer.cs index 2fe4b0db..9b6a2743 100644 --- a/src/WireMock.Net/Server/FluentMockServer.cs +++ b/src/WireMock.Net/Server/FluentMockServer.cs @@ -265,8 +265,8 @@ namespace WireMock.Server /// The given. /// /// The request matcher. - /// The . - public IRespondWithAProviderGuid Given(IRequestMatcher requestMatcher) + /// The . + public IRespondWithAProvider Given(IRequestMatcher requestMatcher) { return new RespondWithAProvider(RegisterMapping, requestMatcher); } @@ -306,9 +306,12 @@ namespace WireMock.Server /// The HttpListenerContext. private async void HandleRequestAsync(HttpListenerContext ctx) { - lock (_syncRoot) + if (_requestProcessingDelay > TimeSpan.Zero) { - Task.Delay(_requestProcessingDelay).Wait(); + lock (_syncRoot) + { + Task.Delay(_requestProcessingDelay).Wait(); + } } var request = _requestMapper.Map(ctx.Request); @@ -317,7 +320,10 @@ namespace WireMock.Server try { - var targetMapping = _mappings.FirstOrDefault(route => route.IsRequestHandled(request)); + var targetMapping = _mappings + .OrderBy(m => m.Priority) + .FirstOrDefault(m => m.IsRequestHandled(request)); + if (targetMapping == null) { response = new ResponseMessage diff --git a/src/WireMock.Net/Server/IRespondWithAProvider.cs b/src/WireMock.Net/Server/IRespondWithAProvider.cs index 51439006..b590ae31 100644 --- a/src/WireMock.Net/Server/IRespondWithAProvider.cs +++ b/src/WireMock.Net/Server/IRespondWithAProvider.cs @@ -1,10 +1,26 @@ -namespace WireMock.Server +using System; + +namespace WireMock.Server { /// /// IRespondWithAProvider /// public interface IRespondWithAProvider { + /// + /// Define a unique identifier for this mapping. + /// + /// The unique identifier. + /// The . + IRespondWithAProvider WithGuid(Guid guid); + + /// + /// Define the priority for this mapping. + /// + /// The priority. + /// The . + IRespondWithAProvider AtPriority(int priority); + /// /// The respond with. /// diff --git a/src/WireMock.Net/Server/IRespondWithAProviderGuid.cs b/src/WireMock.Net/Server/IRespondWithAProviderGuid.cs deleted file mode 100644 index b3ba38e0..00000000 --- a/src/WireMock.Net/Server/IRespondWithAProviderGuid.cs +++ /dev/null @@ -1,17 +0,0 @@ -using System; - -namespace WireMock.Server -{ - /// - /// IRespondWithAProviderGuid - /// - public interface IRespondWithAProviderGuid : IRespondWithAProvider - { - /// - /// Define a unique identifier for this mapping. - /// - /// The unique identifier. - /// The . - IRespondWithAProviderGuid WithGuid(Guid guid); - } -} \ No newline at end of file diff --git a/src/WireMock.Net/Server/RespondWithAProvider.cs b/src/WireMock.Net/Server/RespondWithAProvider.cs index d99b4657..9cd7481c 100644 --- a/src/WireMock.Net/Server/RespondWithAProvider.cs +++ b/src/WireMock.Net/Server/RespondWithAProvider.cs @@ -6,8 +6,9 @@ namespace WireMock.Server /// /// The respond with a provider. /// - internal class RespondWithAProvider : IRespondWithAProviderGuid + internal class RespondWithAProvider : IRespondWithAProvider { + private int _priority; private Guid? _guid; /// @@ -40,19 +41,31 @@ namespace WireMock.Server public void RespondWith(IResponseProvider provider) { var mappingGuid = _guid ?? Guid.NewGuid(); - _registrationCallback(new Mapping(mappingGuid, _requestMatcher, provider)); + _registrationCallback(new Mapping(mappingGuid, _requestMatcher, provider, _priority)); } /// /// Define a unique identifier for this mapping. /// /// The unique identifier. - /// The . - public IRespondWithAProviderGuid WithGuid(Guid guid) + /// The . + public IRespondWithAProvider WithGuid(Guid guid) { _guid = guid; return this; } + + /// + /// Define the priority for this mapping. + /// + /// The priority. + /// The . + public IRespondWithAProvider AtPriority(int priority) + { + _priority = priority; + + return this; + } } } \ No newline at end of file diff --git a/test/WireMock.Net.Tests/FluentMockServerTests.cs b/test/WireMock.Net.Tests/FluentMockServerTests.cs index f6b2e0b6..16f1363a 100644 --- a/test/WireMock.Net.Tests/FluentMockServerTests.cs +++ b/test/WireMock.Net.Tests/FluentMockServerTests.cs @@ -64,6 +64,32 @@ namespace WireMock.Net.Tests Check.That(mappings.First().Guid).Equals(guid); } + [Test] + public async Task FluentMockServer_Admin_Mappings_AtPriority() + { + _server = FluentMockServer.Start(); + + // given + _server.Given(Request.Create().WithPath("/1").UsingGet()) + .AtPriority(2) + .RespondWith(Response.Create().WithStatusCode(200)); + + _server.Given(Request.Create().WithPath("/1").UsingGet()) + .AtPriority(1) + .RespondWith(Response.Create().WithStatusCode(400)); + + var mappings = _server.Mappings.ToArray(); + Check.That(mappings).HasSize(2); + Check.That(mappings[0].Priority).Equals(2); + Check.That(mappings[1].Priority).Equals(1); + + // when + var response = await new HttpClient().GetAsync("http://localhost:" + _server.Ports[0] + "/1"); + + // then + Check.That((int)response.StatusCode).IsEqualTo(400); + } + [Test] public async Task FluentMockServer_Admin_Requests_Get() {