mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-04-19 15:31:39 +02:00
AtPriority (#16)
This commit is contained in:
@@ -20,6 +20,7 @@ namespace WireMock.Net.ConsoleApplication
|
|||||||
|
|
||||||
server
|
server
|
||||||
.Given(Request.Create().WithPath(u => u.Contains("x")).UsingGet())
|
.Given(Request.Create().WithPath(u => u.Contains("x")).UsingGet())
|
||||||
|
.AtPriority(4)
|
||||||
.RespondWith(Response.Create()
|
.RespondWith(Response.Create()
|
||||||
.WithStatusCode(200)
|
.WithStatusCode(200)
|
||||||
.WithHeader("Content-Type", "application/json")
|
.WithHeader("Content-Type", "application/json")
|
||||||
|
|||||||
@@ -15,6 +15,14 @@ namespace WireMock.Admin.Mappings
|
|||||||
/// </value>
|
/// </value>
|
||||||
public Guid? Guid { get; set; }
|
public Guid? Guid { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets or sets the priority.
|
||||||
|
/// </summary>
|
||||||
|
/// <value>
|
||||||
|
/// The priority.
|
||||||
|
/// </value>
|
||||||
|
public int? Priority { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets or sets the request.
|
/// Gets or sets the request.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@@ -9,6 +9,14 @@ namespace WireMock
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public class Mapping
|
public class Mapping
|
||||||
{
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the priority.
|
||||||
|
/// </summary>
|
||||||
|
/// <value>
|
||||||
|
/// The priority.
|
||||||
|
/// </value>
|
||||||
|
public int Priority { get; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets the unique identifier.
|
/// Gets the unique identifier.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -33,8 +41,10 @@ namespace WireMock
|
|||||||
/// <param name="guid">The the unique identifier.</param>
|
/// <param name="guid">The the unique identifier.</param>
|
||||||
/// <param name="requestMatcher">The request matcher.</param>
|
/// <param name="requestMatcher">The request matcher.</param>
|
||||||
/// <param name="provider">The provider.</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;
|
Guid = guid;
|
||||||
RequestMatcher = requestMatcher;
|
RequestMatcher = requestMatcher;
|
||||||
Provider = provider;
|
Provider = provider;
|
||||||
|
|||||||
@@ -124,11 +124,14 @@ namespace WireMock.Server
|
|||||||
var requestBuilder = InitRequestBuilder(mappingModel);
|
var requestBuilder = InitRequestBuilder(mappingModel);
|
||||||
var responseBuilder = InitResponseBuilder(mappingModel);
|
var responseBuilder = InitResponseBuilder(mappingModel);
|
||||||
|
|
||||||
IRespondWithAProviderGuid respondProvider = Given(requestBuilder);
|
IRespondWithAProvider respondProvider = Given(requestBuilder);
|
||||||
|
|
||||||
if (mappingModel.Guid != null && mappingModel.Guid != Guid.Empty)
|
if (mappingModel.Guid != null && mappingModel.Guid != Guid.Empty)
|
||||||
respondProvider = respondProvider.WithGuid(mappingModel.Guid.Value);
|
respondProvider = respondProvider.WithGuid(mappingModel.Guid.Value);
|
||||||
|
|
||||||
|
if (mappingModel.Priority != null)
|
||||||
|
respondProvider = respondProvider.AtPriority(mappingModel.Priority.Value);
|
||||||
|
|
||||||
respondProvider.RespondWith(responseBuilder);
|
respondProvider.RespondWith(responseBuilder);
|
||||||
|
|
||||||
return new ResponseMessage { Body = "Mapping added" };
|
return new ResponseMessage { Body = "Mapping added" };
|
||||||
@@ -304,6 +307,7 @@ namespace WireMock.Server
|
|||||||
return new MappingModel
|
return new MappingModel
|
||||||
{
|
{
|
||||||
Guid = mapping.Guid,
|
Guid = mapping.Guid,
|
||||||
|
Priority = mapping.Priority,
|
||||||
Request = new RequestModel
|
Request = new RequestModel
|
||||||
{
|
{
|
||||||
Path = new PathModel
|
Path = new PathModel
|
||||||
|
|||||||
@@ -265,8 +265,8 @@ namespace WireMock.Server
|
|||||||
/// The given.
|
/// The given.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="requestMatcher">The request matcher.</param>
|
/// <param name="requestMatcher">The request matcher.</param>
|
||||||
/// <returns>The <see cref="IRespondWithAProviderGuid"/>.</returns>
|
/// <returns>The <see cref="IRespondWithAProvider"/>.</returns>
|
||||||
public IRespondWithAProviderGuid Given(IRequestMatcher requestMatcher)
|
public IRespondWithAProvider Given(IRequestMatcher requestMatcher)
|
||||||
{
|
{
|
||||||
return new RespondWithAProvider(RegisterMapping, requestMatcher);
|
return new RespondWithAProvider(RegisterMapping, requestMatcher);
|
||||||
}
|
}
|
||||||
@@ -306,9 +306,12 @@ namespace WireMock.Server
|
|||||||
/// <param name="ctx">The HttpListenerContext.</param>
|
/// <param name="ctx">The HttpListenerContext.</param>
|
||||||
private async void HandleRequestAsync(HttpListenerContext ctx)
|
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);
|
var request = _requestMapper.Map(ctx.Request);
|
||||||
@@ -317,7 +320,10 @@ namespace WireMock.Server
|
|||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var targetMapping = _mappings.FirstOrDefault(route => route.IsRequestHandled(request));
|
var targetMapping = _mappings
|
||||||
|
.OrderBy(m => m.Priority)
|
||||||
|
.FirstOrDefault(m => m.IsRequestHandled(request));
|
||||||
|
|
||||||
if (targetMapping == null)
|
if (targetMapping == null)
|
||||||
{
|
{
|
||||||
response = new ResponseMessage
|
response = new ResponseMessage
|
||||||
|
|||||||
@@ -1,10 +1,26 @@
|
|||||||
namespace WireMock.Server
|
using System;
|
||||||
|
|
||||||
|
namespace WireMock.Server
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// IRespondWithAProvider
|
/// IRespondWithAProvider
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public interface IRespondWithAProvider
|
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>
|
/// <summary>
|
||||||
/// The respond with.
|
/// The respond with.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@@ -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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -6,8 +6,9 @@ namespace WireMock.Server
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// The respond with a provider.
|
/// The respond with a provider.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
internal class RespondWithAProvider : IRespondWithAProviderGuid
|
internal class RespondWithAProvider : IRespondWithAProvider
|
||||||
{
|
{
|
||||||
|
private int _priority;
|
||||||
private Guid? _guid;
|
private Guid? _guid;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -40,19 +41,31 @@ namespace WireMock.Server
|
|||||||
public void RespondWith(IResponseProvider provider)
|
public void RespondWith(IResponseProvider provider)
|
||||||
{
|
{
|
||||||
var mappingGuid = _guid ?? Guid.NewGuid();
|
var mappingGuid = _guid ?? Guid.NewGuid();
|
||||||
_registrationCallback(new Mapping(mappingGuid, _requestMatcher, provider));
|
_registrationCallback(new Mapping(mappingGuid, _requestMatcher, provider, _priority));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Define a unique identifier for this mapping.
|
/// Define a unique identifier for this mapping.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="guid">The unique identifier.</param>
|
/// <param name="guid">The unique identifier.</param>
|
||||||
/// <returns>The <see cref="IRespondWithAProviderGuid"/>.</returns>
|
/// <returns>The <see cref="IRespondWithAProvider"/>.</returns>
|
||||||
public IRespondWithAProviderGuid WithGuid(Guid guid)
|
public IRespondWithAProvider WithGuid(Guid guid)
|
||||||
{
|
{
|
||||||
_guid = guid;
|
_guid = guid;
|
||||||
|
|
||||||
return this;
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -64,6 +64,32 @@ namespace WireMock.Net.Tests
|
|||||||
Check.That(mappings.First().Guid).Equals(guid);
|
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]
|
[Test]
|
||||||
public async Task FluentMockServer_Admin_Requests_Get()
|
public async Task FluentMockServer_Admin_Requests_Get()
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user