mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-04-21 00:11:44 +02:00
Initial support for static mapping files.
This commit is contained in:
@@ -59,6 +59,9 @@
|
|||||||
<None Include="packages.config">
|
<None Include="packages.config">
|
||||||
<SubType>Designer</SubType>
|
<SubType>Designer</SubType>
|
||||||
</None>
|
</None>
|
||||||
|
<Content Include="__admin\mappings\11111110-a633-40e8-a244-5cb80bc0ab66.json">
|
||||||
|
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||||
|
</Content>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||||
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
|
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
|
||||||
|
|||||||
@@ -0,0 +1,21 @@
|
|||||||
|
{
|
||||||
|
"Request": {
|
||||||
|
"Path": {
|
||||||
|
"Matchers": [
|
||||||
|
{
|
||||||
|
"Name": "WildcardMatcher",
|
||||||
|
"Pattern": "/static/mapping"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"Methods": [
|
||||||
|
"get"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"Response": {
|
||||||
|
"BodyAsJson": { "body": "static mapping" },
|
||||||
|
"Headers": {
|
||||||
|
"Content-Type": "application/json"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using JetBrains.Annotations;
|
using JetBrains.Annotations;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
@@ -12,6 +13,7 @@ using WireMock.Matchers.Request;
|
|||||||
using WireMock.RequestBuilders;
|
using WireMock.RequestBuilders;
|
||||||
using WireMock.ResponseBuilders;
|
using WireMock.ResponseBuilders;
|
||||||
using WireMock.Util;
|
using WireMock.Util;
|
||||||
|
using WireMock.Validation;
|
||||||
|
|
||||||
namespace WireMock.Server
|
namespace WireMock.Server
|
||||||
{
|
{
|
||||||
@@ -20,6 +22,7 @@ namespace WireMock.Server
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public partial class FluentMockServer
|
public partial class FluentMockServer
|
||||||
{
|
{
|
||||||
|
private const string AdminMappingsFolder = @"\__admin\mappings";
|
||||||
private const string AdminMappings = "/__admin/mappings";
|
private const string AdminMappings = "/__admin/mappings";
|
||||||
private const string AdminRequests = "/__admin/requests";
|
private const string AdminRequests = "/__admin/requests";
|
||||||
private readonly RegexMatcher _adminMappingsGuidPathMatcher = 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})$");
|
private readonly RegexMatcher _adminMappingsGuidPathMatcher = 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})$");
|
||||||
@@ -31,6 +34,18 @@ namespace WireMock.Server
|
|||||||
NullValueHandling = NullValueHandling.Ignore
|
NullValueHandling = NullValueHandling.Ignore
|
||||||
};
|
};
|
||||||
|
|
||||||
|
private void ReadStaticMappings()
|
||||||
|
{
|
||||||
|
if (!Directory.Exists(Directory.GetCurrentDirectory() + AdminMappingsFolder))
|
||||||
|
return;
|
||||||
|
|
||||||
|
foreach (string filename in Directory.EnumerateFiles(Directory.GetCurrentDirectory() + AdminMappingsFolder))
|
||||||
|
{
|
||||||
|
var json = File.OpenText(filename).ReadToEnd();
|
||||||
|
DeserializeAndAddMapping(json, Guid.Parse(Path.GetFileNameWithoutExtension(filename)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private void InitAdmin()
|
private void InitAdmin()
|
||||||
{
|
{
|
||||||
// __admin/mappings
|
// __admin/mappings
|
||||||
@@ -114,28 +129,48 @@ namespace WireMock.Server
|
|||||||
|
|
||||||
private ResponseMessage MappingsPost(RequestMessage requestMessage)
|
private ResponseMessage MappingsPost(RequestMessage requestMessage)
|
||||||
{
|
{
|
||||||
var mappingModel = JsonConvert.DeserializeObject<MappingModel>(requestMessage.Body);
|
try
|
||||||
|
{
|
||||||
|
DeserializeAndAddMapping(requestMessage.Body);
|
||||||
|
}
|
||||||
|
catch (ArgumentException a)
|
||||||
|
{
|
||||||
|
return new ResponseMessage { StatusCode = 400, Body = a.Message };
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
return new ResponseMessage { StatusCode = 500, Body = e.ToString() };
|
||||||
|
}
|
||||||
|
|
||||||
if (mappingModel.Request == null)
|
return new ResponseMessage { Body = "Mapping added" };
|
||||||
return new ResponseMessage { StatusCode = 400, Body = "Request missing" };
|
}
|
||||||
|
|
||||||
if (mappingModel.Response == null)
|
private void DeserializeAndAddMapping(string json, Guid? guid = null)
|
||||||
return new ResponseMessage { StatusCode = 400, Body = "Response missing" };
|
{
|
||||||
|
var mappingModel = JsonConvert.DeserializeObject<MappingModel>(json);
|
||||||
|
|
||||||
|
Check.NotNull(mappingModel, nameof(mappingModel));
|
||||||
|
Check.NotNull(mappingModel.Request, nameof(mappingModel.Request));
|
||||||
|
Check.NotNull(mappingModel.Response, nameof(mappingModel.Response));
|
||||||
|
|
||||||
var requestBuilder = InitRequestBuilder(mappingModel);
|
var requestBuilder = InitRequestBuilder(mappingModel);
|
||||||
var responseBuilder = InitResponseBuilder(mappingModel);
|
var responseBuilder = InitResponseBuilder(mappingModel);
|
||||||
|
|
||||||
IRespondWithAProvider respondProvider = Given(requestBuilder);
|
IRespondWithAProvider respondProvider = Given(requestBuilder);
|
||||||
|
|
||||||
if (mappingModel.Guid != null && mappingModel.Guid != Guid.Empty)
|
if (guid != null)
|
||||||
|
{
|
||||||
|
respondProvider = respondProvider.WithGuid(guid.Value);
|
||||||
|
}
|
||||||
|
else if (mappingModel.Guid != null && mappingModel.Guid != Guid.Empty)
|
||||||
|
{
|
||||||
respondProvider = respondProvider.WithGuid(mappingModel.Guid.Value);
|
respondProvider = respondProvider.WithGuid(mappingModel.Guid.Value);
|
||||||
|
}
|
||||||
|
|
||||||
if (mappingModel.Priority != null)
|
if (mappingModel.Priority != null)
|
||||||
respondProvider = respondProvider.AtPriority(mappingModel.Priority.Value);
|
respondProvider = respondProvider.AtPriority(mappingModel.Priority.Value);
|
||||||
|
|
||||||
respondProvider.RespondWith(responseBuilder);
|
respondProvider.RespondWith(responseBuilder);
|
||||||
|
|
||||||
return new ResponseMessage { Body = "Mapping added" };
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private ResponseMessage MappingsDelete(RequestMessage requestMessage)
|
private ResponseMessage MappingsDelete(RequestMessage requestMessage)
|
||||||
|
|||||||
@@ -152,6 +152,8 @@ namespace WireMock.Server
|
|||||||
{
|
{
|
||||||
InitAdmin();
|
InitAdmin();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ReadStaticMappings();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
{
|
{
|
||||||
"version": "1.0.1.0",
|
"version": "1.0.1.1",
|
||||||
"title": "WireMock.Net",
|
"title": "WireMock.Net",
|
||||||
"description": "Lightweight Http Mocking Server for .Net, inspired by WireMock from the Java landscape.",
|
"description": "Lightweight Http Mocking Server for .Net, inspired by WireMock from the Java landscape.",
|
||||||
"authors": [ "Alexandre Victoor", "Stef Heyenrath" ],
|
"authors": [ "Alexandre Victoor", "Stef Heyenrath" ],
|
||||||
@@ -15,7 +15,7 @@
|
|||||||
"projectUrl": "https://github.com/StefH/WireMock.Net",
|
"projectUrl": "https://github.com/StefH/WireMock.Net",
|
||||||
"iconUrl": "https://raw.githubusercontent.com/StefH/WireMock.Net/master/WireMock.Net-Logo.png",
|
"iconUrl": "https://raw.githubusercontent.com/StefH/WireMock.Net/master/WireMock.Net-Logo.png",
|
||||||
"licenseUrl": "https://raw.githubusercontent.com/StefH/WireMock.Net/master/LICENSE",
|
"licenseUrl": "https://raw.githubusercontent.com/StefH/WireMock.Net/master/LICENSE",
|
||||||
"releaseNotes": "Added Admin-Interface"
|
"releaseNotes": "Added static mapping.json file support"
|
||||||
},
|
},
|
||||||
|
|
||||||
"buildOptions": {
|
"buildOptions": {
|
||||||
|
|||||||
Reference in New Issue
Block a user