mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-04-20 15:31:20 +02:00
Added GET /__admin/mappings/{guid}
This commit is contained in:
@@ -13,9 +13,12 @@ The WireMock admin API provides functionality to define the mappings via a http
|
|||||||
### /__admin/mappings
|
### /__admin/mappings
|
||||||
The mappings defined in the mock service.
|
The mappings defined in the mock service.
|
||||||
* `GET /__admin/mappings` --> Gets all defined mappings
|
* `GET /__admin/mappings` --> Gets all defined mappings
|
||||||
* `DELETE /__admin/mappings` --> Create a new stub mapping
|
* `POST /__admin/mappings` --> Create a new stub mapping
|
||||||
* `POST /__admin/mappings` --> TODO
|
|
||||||
* `DELETE /__admin/mappings` --> TODO
|
* `DELETE /__admin/mappings` --> TODO
|
||||||
|
* `GET /__admin/mappings/{guid}` --> Get a single stub mapping
|
||||||
|
* `PUT /__admin/mappings/{guid}` --> TODO
|
||||||
|
* `POST /__admin/mappings/{guid}` --> TODO
|
||||||
|
* `DELETE /__admin/mappings/{guid}` --> TODO
|
||||||
|
|
||||||
### /__admin/requests
|
### /__admin/requests
|
||||||
Logged requests and responses received by the mock service.
|
Logged requests and responses received by the mock service.
|
||||||
|
|||||||
@@ -17,6 +17,9 @@ namespace WireMock.Server
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public partial class FluentMockServer
|
public partial class FluentMockServer
|
||||||
{
|
{
|
||||||
|
private const string AdminMappings = "/__admin/mappings";
|
||||||
|
private const string AdminRequests = "/__admin/requests";
|
||||||
|
|
||||||
private readonly JsonSerializerSettings _settings = new JsonSerializerSettings
|
private readonly JsonSerializerSettings _settings = new JsonSerializerSettings
|
||||||
{
|
{
|
||||||
Formatting = Formatting.None,
|
Formatting = Formatting.None,
|
||||||
@@ -25,10 +28,27 @@ namespace WireMock.Server
|
|||||||
|
|
||||||
private void InitAdmin()
|
private void InitAdmin()
|
||||||
{
|
{
|
||||||
Given(Request.Create().WithUrl("/__admin/mappings").UsingGet()).RespondWith(new DynamicResponseProvider(MappingsGet));
|
// __admin/mappings
|
||||||
Given(Request.Create().WithUrl("/__admin/mappings").UsingPost()).RespondWith(new DynamicResponseProvider(MappingsPost));
|
Given(Request.Create().WithUrl(AdminMappings).UsingGet()).RespondWith(new DynamicResponseProvider(MappingsGet));
|
||||||
|
Given(Request.Create().WithUrl(AdminMappings).UsingPost()).RespondWith(new DynamicResponseProvider(MappingsPost));
|
||||||
|
|
||||||
Given(Request.Create().WithUrl("/__admin/requests").UsingGet()).RespondWith(new DynamicResponseProvider(RequestsGet));
|
// __admin/mappings/{guid}
|
||||||
|
var guidPathMatcher = new RegexMatcher(@"^\/__admin\/mappings\/(\{{0,1}([0-9a-fA-F]){8}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){12}\}{0,1})$");
|
||||||
|
Given(Request.Create().WithPath(guidPathMatcher).UsingGet()).RespondWith(new DynamicResponseProvider(MappingGet));
|
||||||
|
|
||||||
|
|
||||||
|
// __admin/requests
|
||||||
|
Given(Request.Create().WithUrl(AdminRequests).UsingGet()).RespondWith(new DynamicResponseProvider(RequestsGet));
|
||||||
|
}
|
||||||
|
|
||||||
|
private ResponseMessage MappingGet(RequestMessage requestMessage)
|
||||||
|
{
|
||||||
|
Guid guid = Guid.Parse(requestMessage.Path.TrimStart(AdminMappings.ToCharArray()));
|
||||||
|
var mapping = Mappings.FirstOrDefault(m => !(m.Provider is DynamicResponseProvider) && m.Guid == guid);
|
||||||
|
|
||||||
|
var model = ToMappingModel(mapping);
|
||||||
|
|
||||||
|
return ToJson(model);
|
||||||
}
|
}
|
||||||
|
|
||||||
private ResponseMessage MappingsGet(RequestMessage requestMessage)
|
private ResponseMessage MappingsGet(RequestMessage requestMessage)
|
||||||
@@ -248,7 +268,7 @@ namespace WireMock.Server
|
|||||||
|
|
||||||
switch (matcher.Name)
|
switch (matcher.Name)
|
||||||
{
|
{
|
||||||
case "RegExMatcher":
|
case "RegexMatcher":
|
||||||
return new RegexMatcher(matcher.Pattern);
|
return new RegexMatcher(matcher.Pattern);
|
||||||
|
|
||||||
case "JsonPathMatcher":
|
case "JsonPathMatcher":
|
||||||
|
|||||||
@@ -183,6 +183,13 @@ namespace WireMock.Server
|
|||||||
{
|
{
|
||||||
lock (((ICollection)_mappings).SyncRoot)
|
lock (((ICollection)_mappings).SyncRoot)
|
||||||
{
|
{
|
||||||
|
// Check a mapping exists with the same GUI, if so, remove it first.
|
||||||
|
var existingMapping = _mappings.FirstOrDefault(m => m.Guid == mapping.Guid);
|
||||||
|
if (existingMapping != null)
|
||||||
|
{
|
||||||
|
_mappings.Remove(existingMapping);
|
||||||
|
}
|
||||||
|
|
||||||
_mappings.Add(mapping);
|
_mappings.Add(mapping);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -216,8 +223,8 @@ namespace WireMock.Server
|
|||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var targetRoute = _mappings.FirstOrDefault(route => route.IsRequestHandled(request));
|
var targetMapping = _mappings.FirstOrDefault(route => route.IsRequestHandled(request));
|
||||||
if (targetRoute == null)
|
if (targetMapping == null)
|
||||||
{
|
{
|
||||||
response = new ResponseMessage
|
response = new ResponseMessage
|
||||||
{
|
{
|
||||||
@@ -227,7 +234,7 @@ namespace WireMock.Server
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
response = await targetRoute.ResponseTo(request);
|
response = await targetMapping.ResponseTo(request);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ namespace WireMock.Net.Tests
|
|||||||
private FluentMockServer _server;
|
private FluentMockServer _server;
|
||||||
|
|
||||||
[Test]
|
[Test]
|
||||||
public void FluentMockServer_get_routes()
|
public void FluentMockServer_Admin_Mappings_Get()
|
||||||
{
|
{
|
||||||
var guid = Guid.Parse("90356dba-b36c-469a-a17e-669cd84f1f05");
|
var guid = Guid.Parse("90356dba-b36c-469a-a17e-669cd84f1f05");
|
||||||
_server = FluentMockServer.Start();
|
_server = FluentMockServer.Start();
|
||||||
@@ -32,16 +32,52 @@ namespace WireMock.Net.Tests
|
|||||||
_server.Given(Request.Create().WithUrl("/foo2").UsingGet())
|
_server.Given(Request.Create().WithUrl("/foo2").UsingGet())
|
||||||
.RespondWith(Response.Create().WithStatusCode(202).WithBody("2"));
|
.RespondWith(Response.Create().WithStatusCode(202).WithBody("2"));
|
||||||
|
|
||||||
var routes = _server.Mappings;
|
var mappings = _server.Mappings.ToArray();
|
||||||
|
Check.That(mappings).HasSize(2);
|
||||||
|
|
||||||
var enumerable = routes.ToArray();
|
Check.That(mappings.First().RequestMatcher).IsNotNull();
|
||||||
Check.That(enumerable).HasSize(2);
|
Check.That(mappings.First().Provider).IsNotNull();
|
||||||
|
Check.That(mappings.First().Guid).Equals(guid);
|
||||||
|
|
||||||
Check.That(enumerable.First().RequestMatcher).IsNotNull();
|
Check.That(mappings[1].Guid).Not.Equals(guid);
|
||||||
Check.That(enumerable.First().Provider).IsNotNull();
|
}
|
||||||
Check.That(enumerable.First().Guid).Equals(guid);
|
|
||||||
|
|
||||||
Check.That(enumerable[1].Guid).Not.Equals(guid);
|
[Test]
|
||||||
|
public void FluentMockServer_Admin_Mappings_Add_SameGuid()
|
||||||
|
{
|
||||||
|
var guid = Guid.Parse("90356dba-b36c-469a-a17e-669cd84f1f05");
|
||||||
|
_server = FluentMockServer.Start();
|
||||||
|
|
||||||
|
_server.Given(Request.Create().WithUrl("/1").UsingGet())
|
||||||
|
.WithGuid(guid)
|
||||||
|
.RespondWith(Response.Create().WithStatusCode(500));
|
||||||
|
|
||||||
|
var mappings = _server.Mappings.ToArray();
|
||||||
|
Check.That(mappings).HasSize(1);
|
||||||
|
Check.That(mappings.First().Guid).Equals(guid);
|
||||||
|
|
||||||
|
_server.Given(Request.Create().WithUrl("/2").UsingGet())
|
||||||
|
.WithGuid(guid)
|
||||||
|
.RespondWith(Response.Create().WithStatusCode(500));
|
||||||
|
|
||||||
|
Check.That(mappings).HasSize(1);
|
||||||
|
Check.That(mappings.First().Guid).Equals(guid);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task FluentMockServer_Admin_Requests_Get()
|
||||||
|
{
|
||||||
|
// given
|
||||||
|
_server = FluentMockServer.Start();
|
||||||
|
|
||||||
|
// when
|
||||||
|
await new HttpClient().GetAsync("http://localhost:" + _server.Port + "/foo");
|
||||||
|
|
||||||
|
// then
|
||||||
|
Check.That(_server.LogEntries).HasSize(1);
|
||||||
|
var requestLogged = _server.LogEntries.First();
|
||||||
|
Check.That(requestLogged.RequestMessage.Method).IsEqualTo("get");
|
||||||
|
Check.That(requestLogged.RequestMessage.BodyAsBytes).IsNull();
|
||||||
}
|
}
|
||||||
|
|
||||||
[Test]
|
[Test]
|
||||||
@@ -94,22 +130,6 @@ namespace WireMock.Net.Tests
|
|||||||
Check.That((int)response.StatusCode).IsEqualTo(404);
|
Check.That((int)response.StatusCode).IsEqualTo(404);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Test]
|
|
||||||
public async Task Should_record_requests_in_the_requestlogs()
|
|
||||||
{
|
|
||||||
// given
|
|
||||||
_server = FluentMockServer.Start();
|
|
||||||
|
|
||||||
// when
|
|
||||||
await new HttpClient().GetAsync("http://localhost:" + _server.Port + "/foo");
|
|
||||||
|
|
||||||
// then
|
|
||||||
Check.That(_server.LogEntries).HasSize(1);
|
|
||||||
var requestLogged = _server.LogEntries.First();
|
|
||||||
Check.That(requestLogged.RequestMessage.Method).IsEqualTo("get");
|
|
||||||
Check.That(requestLogged.RequestMessage.BodyAsBytes).IsNull();
|
|
||||||
}
|
|
||||||
|
|
||||||
[Test]
|
[Test]
|
||||||
public async Task Should_find_a_request_satisfying_a_request_spec()
|
public async Task Should_find_a_request_satisfying_a_request_spec()
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user