mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-03-17 23:03:46 +01:00
Initial support for static mapping files.
This commit is contained in:
@@ -59,6 +59,9 @@
|
||||
<None Include="packages.config">
|
||||
<SubType>Designer</SubType>
|
||||
</None>
|
||||
<Content Include="__admin\mappings\11111110-a633-40e8-a244-5cb80bc0ab66.json">
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</Content>
|
||||
</ItemGroup>
|
||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||
<!-- 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.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using JetBrains.Annotations;
|
||||
using Newtonsoft.Json;
|
||||
@@ -12,6 +13,7 @@ using WireMock.Matchers.Request;
|
||||
using WireMock.RequestBuilders;
|
||||
using WireMock.ResponseBuilders;
|
||||
using WireMock.Util;
|
||||
using WireMock.Validation;
|
||||
|
||||
namespace WireMock.Server
|
||||
{
|
||||
@@ -20,6 +22,7 @@ namespace WireMock.Server
|
||||
/// </summary>
|
||||
public partial class FluentMockServer
|
||||
{
|
||||
private const string AdminMappingsFolder = @"\__admin\mappings";
|
||||
private const string AdminMappings = "/__admin/mappings";
|
||||
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})$");
|
||||
@@ -31,6 +34,18 @@ namespace WireMock.Server
|
||||
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()
|
||||
{
|
||||
// __admin/mappings
|
||||
@@ -114,28 +129,48 @@ namespace WireMock.Server
|
||||
|
||||
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 { StatusCode = 400, Body = "Request missing" };
|
||||
return new ResponseMessage { Body = "Mapping added" };
|
||||
}
|
||||
|
||||
if (mappingModel.Response == null)
|
||||
return new ResponseMessage { StatusCode = 400, Body = "Response missing" };
|
||||
private void DeserializeAndAddMapping(string json, Guid? guid = null)
|
||||
{
|
||||
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 responseBuilder = InitResponseBuilder(mappingModel);
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
if (mappingModel.Priority != null)
|
||||
respondProvider = respondProvider.AtPriority(mappingModel.Priority.Value);
|
||||
|
||||
respondProvider.RespondWith(responseBuilder);
|
||||
|
||||
return new ResponseMessage { Body = "Mapping added" };
|
||||
}
|
||||
|
||||
private ResponseMessage MappingsDelete(RequestMessage requestMessage)
|
||||
|
||||
@@ -152,6 +152,8 @@ namespace WireMock.Server
|
||||
{
|
||||
InitAdmin();
|
||||
}
|
||||
|
||||
ReadStaticMappings();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
{
|
||||
"version": "1.0.1.0",
|
||||
"version": "1.0.1.1",
|
||||
"title": "WireMock.Net",
|
||||
"description": "Lightweight Http Mocking Server for .Net, inspired by WireMock from the Java landscape.",
|
||||
"authors": [ "Alexandre Victoor", "Stef Heyenrath" ],
|
||||
@@ -15,7 +15,7 @@
|
||||
"projectUrl": "https://github.com/StefH/WireMock.Net",
|
||||
"iconUrl": "https://raw.githubusercontent.com/StefH/WireMock.Net/master/WireMock.Net-Logo.png",
|
||||
"licenseUrl": "https://raw.githubusercontent.com/StefH/WireMock.Net/master/LICENSE",
|
||||
"releaseNotes": "Added Admin-Interface"
|
||||
"releaseNotes": "Added static mapping.json file support"
|
||||
},
|
||||
|
||||
"buildOptions": {
|
||||
|
||||
Reference in New Issue
Block a user