AtPriority (#16)

This commit is contained in:
Stef Heyenrath
2017-01-31 09:28:04 +01:00
parent 8a4e5b5790
commit 84901ab1e4
9 changed files with 96 additions and 29 deletions

View File

@@ -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")

View File

@@ -15,6 +15,14 @@ namespace WireMock.Admin.Mappings
/// </value>
public Guid? Guid { get; set; }
/// <summary>
/// Gets or sets the priority.
/// </summary>
/// <value>
/// The priority.
/// </value>
public int? Priority { get; set; }
/// <summary>
/// Gets or sets the request.
/// </summary>

View File

@@ -9,6 +9,14 @@ namespace WireMock
/// </summary>
public class Mapping
{
/// <summary>
/// Gets the priority.
/// </summary>
/// <value>
/// The priority.
/// </value>
public int Priority { get; }
/// <summary>
/// Gets the unique identifier.
/// </summary>
@@ -33,8 +41,10 @@ namespace WireMock
/// <param name="guid">The the unique identifier.</param>
/// <param name="requestMatcher">The request matcher.</param>
/// <param name="provider">The provider.</param>
public Mapping(Guid guid, IRequestMatcher requestMatcher, IResponseProvider provider)
/// <param name="priority">The priority for this mapping.</param>
public Mapping(Guid guid, IRequestMatcher requestMatcher, IResponseProvider provider, int priority)
{
Priority = priority;
Guid = guid;
RequestMatcher = requestMatcher;
Provider = provider;

View File

@@ -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

View File

@@ -265,8 +265,8 @@ namespace WireMock.Server
/// The given.
/// </summary>
/// <param name="requestMatcher">The request matcher.</param>
/// <returns>The <see cref="IRespondWithAProviderGuid"/>.</returns>
public IRespondWithAProviderGuid Given(IRequestMatcher requestMatcher)
/// <returns>The <see cref="IRespondWithAProvider"/>.</returns>
public IRespondWithAProvider Given(IRequestMatcher requestMatcher)
{
return new RespondWithAProvider(RegisterMapping, requestMatcher);
}
@@ -306,9 +306,12 @@ namespace WireMock.Server
/// <param name="ctx">The HttpListenerContext.</param>
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

View File

@@ -1,10 +1,26 @@
namespace WireMock.Server
using System;
namespace WireMock.Server
{
/// <summary>
/// IRespondWithAProvider
/// </summary>
public interface IRespondWithAProvider
{
/// <summary>
/// Define a unique identifier for this mapping.
/// </summary>
/// <param name="guid">The unique identifier.</param>
/// <returns>The <see cref="IRespondWithAProvider"/>.</returns>
IRespondWithAProvider WithGuid(Guid guid);
/// <summary>
/// Define the priority for this mapping.
/// </summary>
/// <param name="priority">The priority.</param>
/// <returns>The <see cref="IRespondWithAProvider"/>.</returns>
IRespondWithAProvider AtPriority(int priority);
/// <summary>
/// The respond with.
/// </summary>

View File

@@ -1,17 +0,0 @@
using System;
namespace WireMock.Server
{
/// <summary>
/// IRespondWithAProviderGuid
/// </summary>
public interface IRespondWithAProviderGuid : IRespondWithAProvider
{
/// <summary>
/// Define a unique identifier for this mapping.
/// </summary>
/// <param name="guid">The unique identifier.</param>
/// <returns>The <see cref="IRespondWithAProviderGuid"/>.</returns>
IRespondWithAProviderGuid WithGuid(Guid guid);
}
}

View File

@@ -6,8 +6,9 @@ namespace WireMock.Server
/// <summary>
/// The respond with a provider.
/// </summary>
internal class RespondWithAProvider : IRespondWithAProviderGuid
internal class RespondWithAProvider : IRespondWithAProvider
{
private int _priority;
private Guid? _guid;
/// <summary>
@@ -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));
}
/// <summary>
/// Define a unique identifier for this mapping.
/// </summary>
/// <param name="guid">The unique identifier.</param>
/// <returns>The <see cref="IRespondWithAProviderGuid"/>.</returns>
public IRespondWithAProviderGuid WithGuid(Guid guid)
/// <returns>The <see cref="IRespondWithAProvider"/>.</returns>
public IRespondWithAProvider WithGuid(Guid guid)
{
_guid = guid;
return this;
}
/// <summary>
/// Define the priority for this mapping.
/// </summary>
/// <param name="priority">The priority.</param>
/// <returns>The <see cref="IRespondWithAProvider"/>.</returns>
public IRespondWithAProvider AtPriority(int priority)
{
_priority = priority;
return this;
}
}
}

View File

@@ -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()
{