Add log4net logging (#89)

* logging + fixed proxy isses

* Add more logging

* comments

* 1.0.3.1
This commit is contained in:
Stef Heyenrath
2018-02-14 16:13:05 +00:00
committed by GitHub
parent 9778c5adbe
commit e21582aacf
35 changed files with 466 additions and 154 deletions

View File

@@ -1,11 +1,21 @@
using WireMock.Net.ConsoleApplication; using System.IO;
using System.Reflection;
using log4net;
using log4net.Config;
using log4net.Repository;
using WireMock.Net.ConsoleApplication;
namespace WireMock.Net.Console.NETCoreApp namespace WireMock.Net.Console.NETCoreApp
{ {
static class Program static class Program
{ {
private static readonly ILoggerRepository LogRepository = LogManager.GetRepository(Assembly.GetEntryAssembly());
private static readonly ILog Log = LogManager.GetLogger(typeof(Program));
static void Main(params string[] args) static void Main(params string[] args)
{ {
XmlConfigurator.Configure(LogRepository, new FileInfo("log4net.config"));
MainApp.Run(); MainApp.Run();
} }
} }

View File

@@ -22,10 +22,17 @@
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\..\src\WireMock.Net\WireMock.Net.csproj" /> <ProjectReference Include="..\..\src\WireMock.Net\WireMock.Net.csproj" />
<PackageReference Include="log4net" Version="2.0.8" />
<PackageReference Include="Newtonsoft.Json" Version="10.0.3" /> <PackageReference Include="Newtonsoft.Json" Version="10.0.3" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<None Update="log4net.config">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="nlog.config">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="__admin\mappings\791a3f31-6946-4ce7-8e6f-0237c7443275.json"> <None Update="__admin\mappings\791a3f31-6946-4ce7-8e6f-0237c7443275.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None> </None>

View File

@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="log4net" type="System.Configuration.IgnoreSectionHandler" />
</configSections>
<appSettings>
<add key="log4net.Internal.Debug" value="true"/>
</appSettings>
<log4net>
<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger{1} - %message%newline" />
</layout>
</appender>
<root>
<level value="DEBUG" />
<appender-ref ref="ConsoleAppender" />
</root>
</log4net>
</configuration>

View File

@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
autoReload="true"
internalLogLevel="info"
internalLogFile="c:\temp\wiremock-internal-nlog.log">
<targets>
<!-- write logs to file -->
<target xsi:type="File" name="all" fileName="c:\temp\wiremock-${shortdate}.log"
layout="${longdate}|${event-properties:item=EventId.Id}|${uppercase:${level}}|${logger}|${message} ${exception}" />
</targets>
<!-- rules to map from logger name to target -->
<rules>
<logger name="*" minlevel="Debug" writeTo="all" />
</rules>
</nlog>

View File

@@ -54,8 +54,9 @@ namespace WireMock.Net.ConsoleApplication
.Given(Request.Create().WithPath("/headers", "/headers_test").UsingPost().WithHeader("Content-Type", "application/json*")) .Given(Request.Create().WithPath("/headers", "/headers_test").UsingPost().WithHeader("Content-Type", "application/json*"))
.RespondWith(Response.Create() .RespondWith(Response.Create()
.WithStatusCode(201) .WithStatusCode(201)
.WithHeader("MyHeader", "application/json", "application/json2") //.WithHeader("MyHeader", "application/json", "application/json2")
.WithBody(@"{ ""result"": ""data posted with 201""}")); .WithHeader("Content-Type", "application/json")
.WithBodyAsJson(new { result = "data:headers posted with 201" }));
server server
.Given(Request.Create().WithPath("/file").UsingGet()) .Given(Request.Create().WithPath("/file").UsingGet())
@@ -117,7 +118,7 @@ namespace WireMock.Net.ConsoleApplication
.RespondWith(Response.Create() .RespondWith(Response.Create()
.WithStatusCode(201) .WithStatusCode(201)
.WithHeader("Content-Type", "application/json") .WithHeader("Content-Type", "application/json")
.WithBody(@"{ ""result"": ""data posted with FUNC 201""}")); .WithBodyAsJson(new { result = "data posted with FUNC 201" }));
server server
.Given(Request.Create().WithPath("/json").UsingPost().WithBody(new JsonPathMatcher("$.things[?(@.name == 'RequiredThing')]"))) .Given(Request.Create().WithPath("/json").UsingPost().WithBody(new JsonPathMatcher("$.things[?(@.name == 'RequiredThing')]")))

View File

@@ -1,9 +1,14 @@
namespace WireMock.Net.ConsoleApplication using System.IO;
using log4net.Config;
namespace WireMock.Net.ConsoleApplication
{ {
static class Program static class Program
{ {
static void Main(params string[] args) static void Main(params string[] args)
{ {
XmlConfigurator.Configure(new FileInfo("log4net.config"));
MainApp.Run(); MainApp.Run();
} }
} }

View File

@@ -36,17 +36,22 @@
<ApplicationIcon>..\..\WireMock.Net-Logo.ico</ApplicationIcon> <ApplicationIcon>..\..\WireMock.Net-Logo.ico</ApplicationIcon>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<Reference Include="log4net, Version=2.0.8.0, Culture=neutral, PublicKeyToken=669e0ddf0bb1aa2a, processorArchitecture=MSIL">
<HintPath>..\..\packages\log4net.2.0.8\lib\net45-full\log4net.dll</HintPath>
</Reference>
<Reference Include="Microsoft.CSharp" />
<Reference Include="Microsoft.Owin.Host.HttpListener, Version=3.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL"> <Reference Include="Microsoft.Owin.Host.HttpListener, Version=3.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\..\packages\Microsoft.Owin.Host.HttpListener.3.1.0\lib\net45\Microsoft.Owin.Host.HttpListener.dll</HintPath> <HintPath>..\..\packages\Microsoft.Owin.Host.HttpListener.3.1.0\lib\net45\Microsoft.Owin.Host.HttpListener.dll</HintPath>
</Reference> </Reference>
<Reference Include="Newtonsoft.Json, Version=10.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL"> <Reference Include="Newtonsoft.Json, Version=10.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\..\packages\Newtonsoft.Json.10.0.2\lib\net45\Newtonsoft.Json.dll</HintPath> <HintPath>..\..\packages\Newtonsoft.Json.10.0.2\lib\net45\Newtonsoft.Json.dll</HintPath>
</Reference> </Reference>
<Reference Include="SimMetrics.Net, Version=1.0.3.0, Culture=neutral, processorArchitecture=MSIL"> <Reference Include="SimMetrics.Net, Version=1.0.4.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\..\packages\SimMetrics.Net.1.0.3\lib\net45\SimMetrics.Net.dll</HintPath> <HintPath>..\..\packages\SimMetrics.Net.1.0.4\lib\net45\SimMetrics.Net.dll</HintPath>
</Reference> </Reference>
<Reference Include="System" /> <Reference Include="System" />
<Reference Include="System.Core" /> <Reference Include="System.Core" />
<Reference Include="System.XML" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="MainApp.cs" /> <Compile Include="MainApp.cs" />
@@ -57,6 +62,9 @@
<None Include="App.config"> <None Include="App.config">
<SubType>Designer</SubType> <SubType>Designer</SubType>
</None> </None>
<None Include="log4net.config">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Include="packages.config"> <None Include="packages.config">
<SubType>Designer</SubType> <SubType>Designer</SubType>
</None> </None>

View File

@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="log4net" type="System.Configuration.IgnoreSectionHandler" />
</configSections>
<appSettings>
<add key="log4net.Internal.Debug" value="true"/>
</appSettings>
<log4net>
<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger{1} - %message%newline" />
</layout>
</appender>
<root>
<level value="DEBUG" />
<appender-ref ref="ConsoleAppender" />
</root>
</log4net>
</configuration>

View File

@@ -1,6 +1,7 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<packages> <packages>
<package id="log4net" version="2.0.8" targetFramework="net452" />
<package id="Microsoft.Owin.Host.HttpListener" version="3.1.0" targetFramework="net452" /> <package id="Microsoft.Owin.Host.HttpListener" version="3.1.0" targetFramework="net452" />
<package id="Newtonsoft.Json" version="10.0.2" targetFramework="net452" /> <package id="Newtonsoft.Json" version="10.0.2" targetFramework="net452" />
<package id="SimMetrics.Net" version="1.0.3" targetFramework="net452" /> <package id="SimMetrics.Net" version="1.0.4" targetFramework="net452" />
</packages> </packages>

View File

@@ -1,16 +1,26 @@
using System; using System;
using System.IO;
using System.Reflection;
using System.Threading; using System.Threading;
using log4net;
using log4net.Config;
using log4net.Repository;
using WireMock.Server; using WireMock.Server;
namespace WireMock.Net.StandAlone.NETCoreApp namespace WireMock.Net.StandAlone.NETCoreApp
{ {
class Program static class Program
{ {
private static readonly ILoggerRepository LogRepository = LogManager.GetRepository(Assembly.GetEntryAssembly());
// private static readonly ILog Log = LogManager.GetLogger(typeof(Program));
private static int sleepTime = 30000; private static int sleepTime = 30000;
private static FluentMockServer _server; private static FluentMockServer _server;
static void Main(string[] args) static void Main(string[] args)
{ {
XmlConfigurator.Configure(LogRepository, new FileInfo("log4net.config"));
_server = StandAloneApp.Start(args); _server = StandAloneApp.Start(args);
Console.WriteLine($"{DateTime.UtcNow} Press Ctrl+C to shut down"); Console.WriteLine($"{DateTime.UtcNow} Press Ctrl+C to shut down");

View File

@@ -6,8 +6,18 @@
<ApplicationIcon>../../WireMock.Net-Logo.ico</ApplicationIcon> <ApplicationIcon>../../WireMock.Net-Logo.ico</ApplicationIcon>
</PropertyGroup> </PropertyGroup>
<ItemGroup>
<PackageReference Include="log4net" Version="2.0.8" />
</ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\..\src\WireMock.Net.StandAlone\WireMock.Net.StandAlone.csproj" /> <ProjectReference Include="..\..\src\WireMock.Net.StandAlone\WireMock.Net.StandAlone.csproj" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<None Update="log4net.config">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project> </Project>

View File

@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="log4net" type="System.Configuration.IgnoreSectionHandler" />
</configSections>
<appSettings>
<add key="log4net.Internal.Debug" value="true"/>
</appSettings>
<log4net>
<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger{1} - %message%newline" />
</layout>
</appender>
<root>
<level value="DEBUG" />
<appender-ref ref="ConsoleAppender" />
</root>
</log4net>
</configuration>

View File

@@ -1,4 +1,6 @@
using System; using System;
using System.IO;
using log4net.Config;
namespace WireMock.Net.StandAlone.Net452 namespace WireMock.Net.StandAlone.Net452
{ {
@@ -6,6 +8,8 @@ namespace WireMock.Net.StandAlone.Net452
{ {
static void Main(params string[] args) static void Main(params string[] args)
{ {
XmlConfigurator.Configure(new FileInfo("log4net.config"));
StandAloneApp.Start(args); StandAloneApp.Start(args);
Console.WriteLine("Press any key to stop the server"); Console.WriteLine("Press any key to stop the server");

View File

@@ -39,6 +39,9 @@
<StartupObject>WireMock.Net.StandAlone.Net452.Program</StartupObject> <StartupObject>WireMock.Net.StandAlone.Net452.Program</StartupObject>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<Reference Include="log4net, Version=2.0.8.0, Culture=neutral, PublicKeyToken=669e0ddf0bb1aa2a, processorArchitecture=MSIL">
<HintPath>..\..\packages\log4net.2.0.8\lib\net45-full\log4net.dll</HintPath>
</Reference>
<Reference Include="System" /> <Reference Include="System" />
<Reference Include="System.Core" /> <Reference Include="System.Core" />
<Reference Include="System.Numerics" /> <Reference Include="System.Numerics" />
@@ -50,6 +53,9 @@
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<None Include="log4net.config">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Include="packages.config" /> <None Include="packages.config" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

View File

@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="log4net" type="System.Configuration.IgnoreSectionHandler" />
</configSections>
<appSettings>
<add key="log4net.Internal.Debug" value="true"/>
</appSettings>
<log4net>
<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger{1} - %message%newline" />
</layout>
</appender>
<root>
<level value="DEBUG" />
<appender-ref ref="ConsoleAppender" />
</root>
</log4net>
</configuration>

View File

@@ -1,3 +1,4 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<packages> <packages>
<package id="log4net" version="2.0.8" targetFramework="net452" />
</packages> </packages>

View File

@@ -1,10 +1,9 @@
using System; using System.Linq;
using System.Linq;
using WireMock.Server; using WireMock.Server;
using WireMock.Settings; using WireMock.Settings;
using WireMock.Validation; using WireMock.Validation;
using JetBrains.Annotations; using JetBrains.Annotations;
using Newtonsoft.Json; using log4net;
namespace WireMock.Net.StandAlone namespace WireMock.Net.StandAlone
{ {
@@ -13,8 +12,10 @@ namespace WireMock.Net.StandAlone
/// </summary> /// </summary>
public static class StandAloneApp public static class StandAloneApp
{ {
private static readonly ILog Log = LogManager.GetLogger(typeof(StandAloneApp));
/// <summary> /// <summary>
/// Start WireMock.Net standalone based on the FluentMockServerSettings. /// Start WireMock.Net standalone Server based on the FluentMockServerSettings.
/// </summary> /// </summary>
/// <param name="settings">The FluentMockServerSettings</param> /// <param name="settings">The FluentMockServerSettings</param>
[PublicAPI] [PublicAPI]
@@ -26,7 +27,7 @@ namespace WireMock.Net.StandAlone
} }
/// <summary> /// <summary>
/// Start WireMock.Net standalone based on the commandline arguments. /// Start WireMock.Net standalone Server based on the commandline arguments.
/// </summary> /// </summary>
/// <param name="args">The commandline arguments</param> /// <param name="args">The commandline arguments</param>
[PublicAPI] [PublicAPI]
@@ -34,7 +35,7 @@ namespace WireMock.Net.StandAlone
{ {
Check.NotNull(args, nameof(args)); Check.NotNull(args, nameof(args));
Console.WriteLine("WireMock.Net server arguments [{0}]", string.Join(", ", args.Select(a => $"'{a}'"))); Log.DebugFormat("WireMock.Net server arguments [{0}]", string.Join(", ", args.Select(a => $"'{a}'")));
var parser = new SimpleCommandLineParser(); var parser = new SimpleCommandLineParser();
parser.Parse(args); parser.Parse(args);
@@ -73,11 +74,9 @@ namespace WireMock.Net.StandAlone
}; };
} }
Console.WriteLine("WireMock.Net server settings {0}", JsonConvert.SerializeObject(settings, Formatting.Indented));
FluentMockServer server = Start(settings); FluentMockServer server = Start(settings);
Console.WriteLine("WireMock.Net server listening at {0}", string.Join(",", server.Urls)); Log.InfoFormat("WireMock.Net server listening at {0}", string.Join(",", server.Urls));
return server; return server;
} }

View File

@@ -3,7 +3,7 @@
<PropertyGroup> <PropertyGroup>
<Description>Lightweight StandAlone Http Mocking Server for .Net.</Description> <Description>Lightweight StandAlone Http Mocking Server for .Net.</Description>
<AssemblyTitle>WireMock.Net.StandAlone</AssemblyTitle> <AssemblyTitle>WireMock.Net.StandAlone</AssemblyTitle>
<Version>1.0.3</Version> <Version>1.0.3.1</Version>
<Authors>Stef Heyenrath</Authors> <Authors>Stef Heyenrath</Authors>
<TargetFrameworks>net452;net46;netstandard1.3;netstandard2.0</TargetFrameworks> <TargetFrameworks>net452;net46;netstandard1.3;netstandard2.0</TargetFrameworks>
<GenerateDocumentationFile>true</GenerateDocumentationFile> <GenerateDocumentationFile>true</GenerateDocumentationFile>
@@ -36,10 +36,7 @@
<PackageReference Include="JetBrains.Annotations" Version="10.4.0"> <PackageReference Include="JetBrains.Annotations" Version="10.4.0">
<PrivateAssets>All</PrivateAssets> <PrivateAssets>All</PrivateAssets>
</PackageReference> </PackageReference>
<!-- <PackageReference Include="CommandLineArgumentsParser" Version="3.0.16" /> --> <PackageReference Include="log4net" Version="2.0.8" />
<!-- <PackageReference Include="Marsonsoft.CommandLineParser" Version="1.0.34" /> -->
<!-- <PackageReference Include="BurnSystems.CommandLine" Version="1.1.0" /> -->
<!-- <ProjectReference Include="..\..\..\CommandLineParser\src\CommandLineArgumentsParser\CommandLineArgumentsParser.csproj" /> -->
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

View File

@@ -8,51 +8,33 @@ namespace WireMock.Admin.Requests
public class LogEntryModel public class LogEntryModel
{ {
/// <summary> /// <summary>
/// Gets or sets the unique identifier.
/// </summary>
/// <value>
/// The unique identifier. /// The unique identifier.
/// </value> /// </summary>
public Guid Guid { get; set; } public Guid Guid { get; set; }
/// <summary> /// <summary>
/// Gets or sets the request.
/// </summary>
/// <value>
/// The request. /// The request.
/// </value> /// </summary>
public LogRequestModel Request { get; set; } public LogRequestModel Request { get; set; }
/// <summary> /// <summary>
/// Gets or sets the response.
/// </summary>
/// <value>
/// The response. /// The response.
/// </value> /// </summary>
public LogResponseModel Response { get; set; } public LogResponseModel Response { get; set; }
/// <summary> /// <summary>
/// Gets or sets the mapping unique identifier.
/// </summary>
/// <value>
/// The mapping unique identifier. /// The mapping unique identifier.
/// </value> /// </summary>
public Guid? MappingGuid { get; set; } public Guid? MappingGuid { get; set; }
/// <summary> /// <summary>
/// Gets or sets the mapping unique title.
/// </summary>
/// <value>
/// The mapping unique title. /// The mapping unique title.
/// </value> /// </summary>
public string MappingTitle { get; set; } public string MappingTitle { get; set; }
/// <summary> /// <summary>
/// Gets or sets the request match result.
/// </summary>
/// <value>
/// The request match result. /// The request match result.
/// </value> /// </summary>
public LogRequestMatchModel RequestMatchResult { get; set; } public LogRequestMatchModel RequestMatchResult { get; set; }
} }
} }

View File

@@ -11,52 +11,62 @@ namespace WireMock.Admin.Requests
public class LogRequestModel public class LogRequestModel
{ {
/// <summary> /// <summary>
/// Gets the Client IP Address. /// The Client IP Address.
/// </summary> /// </summary>
public string ClientIP { get; set; } public string ClientIP { get; set; }
/// <summary> /// <summary>
/// Gets the DateTime. /// The DateTime.
/// </summary> /// </summary>
public DateTime DateTime { get; set; } public DateTime DateTime { get; set; }
/// <summary> /// <summary>
/// Gets or sets the Path. /// The Path.
/// </summary> /// </summary>
public string Path { get; set; } public string Path { get; set; }
/// <summary> /// <summary>
/// Gets or sets the absolete URL. ///The absolete URL.
/// </summary> /// </summary>
public string AbsoluteUrl { get; set; } public string AbsoluteUrl { get; set; }
/// <summary> /// <summary>
/// Gets the query. /// The query.
/// </summary> /// </summary>
public IDictionary<string, WireMockList<string>> Query { get; set; } public IDictionary<string, WireMockList<string>> Query { get; set; }
/// <summary> /// <summary>
/// Gets or sets the method. /// The method.
/// </summary> /// </summary>
public string Method { get; set; } public string Method { get; set; }
/// <summary> /// <summary>
/// Gets or sets the Headers. /// The Headers.
/// </summary> /// </summary>
public IDictionary<string, WireMockList<string>> Headers { get; set; } public IDictionary<string, WireMockList<string>> Headers { get; set; }
/// <summary> /// <summary>
/// Gets or sets the Cookies. /// Tthe Cookies.
/// </summary> /// </summary>
public IDictionary<string, string> Cookies { get; set; } public IDictionary<string, string> Cookies { get; set; }
/// <summary> /// <summary>
/// Gets or sets the body. /// The body (as string).
/// </summary> /// </summary>
public string Body { get; set; } public string Body { get; set; }
/// <summary> /// <summary>
/// Gets or sets the body encoding. /// The body (as JSON object).
/// </summary>
public object BodyAsJson { get; set; }
/// <summary>
/// The body (as bytearray).
/// </summary>
public byte[] BodyAsBytes { get; set; }
/// <summary>
/// The body encoding.
/// </summary> /// </summary>
public EncodingModel BodyEncoding { get; set; } public EncodingModel BodyEncoding { get; set; }
} }

View File

@@ -25,12 +25,17 @@ namespace WireMock.Admin.Requests
public string BodyDestination { get; set; } public string BodyDestination { get; set; }
/// <summary> /// <summary>
/// Gets or sets the body. /// The body (as string).
/// </summary> /// </summary>
public string Body { get; set; } public string Body { get; set; }
/// <summary> /// <summary>
/// Gets or sets the body as bytes. /// The body (as JSON object).
/// </summary>
public object BodyAsJson { get; set; }
/// <summary>
/// The body (as bytearray).
/// </summary> /// </summary>
public byte[] BodyAsBytes { get; set; } public byte[] BodyAsBytes { get; set; }

View File

@@ -4,8 +4,10 @@ using System.Linq;
using System.Net; using System.Net;
using System.Net.Http; using System.Net.Http;
using System.Threading.Tasks; using System.Threading.Tasks;
using JetBrains.Annotations;
using Newtonsoft.Json; using Newtonsoft.Json;
using WireMock.HttpsCertificate; using WireMock.HttpsCertificate;
using WireMock.Util;
using WireMock.Validation; using WireMock.Validation;
namespace WireMock.Http namespace WireMock.Http
@@ -51,20 +53,36 @@ namespace WireMock.Http
return client; return client;
} }
public static async Task<ResponseMessage> SendAsync(HttpClient client, RequestMessage requestMessage, string url) public static async Task<ResponseMessage> SendAsync([NotNull] HttpClient client, [NotNull] RequestMessage requestMessage, string url)
{ {
Check.NotNull(client, nameof(client)); Check.NotNull(client, nameof(client));
Check.NotNull(requestMessage, nameof(requestMessage));
var originalUri = new Uri(requestMessage.Url); var originalUri = new Uri(requestMessage.Url);
var requiredUri = new Uri(url); var requiredUri = new Uri(url);
var httpRequestMessage = new HttpRequestMessage(new HttpMethod(requestMessage.Method), url); var httpRequestMessage = new HttpRequestMessage(new HttpMethod(requestMessage.Method), url);
WireMockList<string> contentTypeHeader = null;
bool contentTypeHeaderPresent = requestMessage.Headers.Any(header => string.Equals(header.Key, HttpKnownHeaderNames.ContentType, StringComparison.OrdinalIgnoreCase));
if (contentTypeHeaderPresent)
{
contentTypeHeader = requestMessage.Headers[HttpKnownHeaderNames.ContentType];
}
// Set Body if present // Set Body if present
if (requestMessage.BodyAsBytes != null) if (requestMessage.BodyAsBytes != null)
{ {
httpRequestMessage.Content = new ByteArrayContent(requestMessage.BodyAsBytes); httpRequestMessage.Content = new ByteArrayContent(requestMessage.BodyAsBytes);
} }
else if (requestMessage.BodyAsJson != null)
{
httpRequestMessage.Content = new StringContent(JsonConvert.SerializeObject(requestMessage.BodyAsJson), requestMessage.BodyEncoding);
}
else if (requestMessage.Body != null)
{
httpRequestMessage.Content = new StringContent(requestMessage.Body, requestMessage.BodyEncoding);
}
// Overwrite the host header // Overwrite the host header
httpRequestMessage.Headers.Host = requiredUri.Authority; httpRequestMessage.Headers.Host = requiredUri.Authority;
@@ -90,10 +108,13 @@ namespace WireMock.Http
// Set both content and response headers, replacing URLs in values // Set both content and response headers, replacing URLs in values
var headers = (httpResponseMessage.Content?.Headers.Union(httpResponseMessage.Headers) ?? Enumerable.Empty<KeyValuePair<string, IEnumerable<string>>>()).ToArray(); var headers = (httpResponseMessage.Content?.Headers.Union(httpResponseMessage.Headers) ?? Enumerable.Empty<KeyValuePair<string, IEnumerable<string>>>()).ToArray();
var contentTypeHeader = headers.FirstOrDefault(header => string.Equals(header.Key, HttpKnownHeaderNames.ContentType, StringComparison.OrdinalIgnoreCase));
if (httpResponseMessage.Content != null) if (httpResponseMessage.Content != null)
{ {
SetBody(httpResponseMessage.Content, contentTypeHeader, responseMessage); var stream = await httpResponseMessage.Content.ReadAsStreamAsync();
var body = await BodyParser.Parse(stream, contentTypeHeader?.FirstOrDefault());
responseMessage.Body = body.BodyAsString;
responseMessage.BodyAsJson = body.BodyAsJson;
responseMessage.BodyAsBytes = body.BodyAsBytes;
} }
foreach (var header in headers) foreach (var header in headers)
@@ -115,41 +136,5 @@ namespace WireMock.Http
return responseMessage; return responseMessage;
} }
private static async void SetBody(HttpContent content, KeyValuePair<string, IEnumerable<string>> contentTypeHeader, ResponseMessage responseMessage)
{
bool contentTypeIsDefault = contentTypeHeader.Equals(default(KeyValuePair<string, IEnumerable<string>>));
string[] textContentTypes = { "text/", "application/xml", "application/javascript", "application/typescript", "application/xhtml+xml" };
if (!contentTypeIsDefault && contentTypeHeader.Value.Any(value => textContentTypes.Any(t => value != null && value.StartsWith(t, StringComparison.OrdinalIgnoreCase))))
{
try
{
responseMessage.Body = await content.ReadAsStringAsync();
}
catch
{
// Reading as string failed, just get the ByteArray.
responseMessage.BodyAsBytes = await content.ReadAsByteArrayAsync();
}
}
else if (!contentTypeIsDefault && contentTypeHeader.Value.Any(value => value != null && value.StartsWith("application/json", StringComparison.OrdinalIgnoreCase)))
{
string stringContent = await content.ReadAsStringAsync();
try
{
responseMessage.BodyAsJson = JsonConvert.DeserializeObject(stringContent, new JsonSerializerSettings { Formatting = Formatting.Indented });
}
catch
{
// JsonConvert failed, just set the Body as string.
responseMessage.Body = stringContent;
}
}
else
{
responseMessage.BodyAsBytes = await content.ReadAsByteArrayAsync();
}
}
} }
} }

View File

@@ -1,5 +1,6 @@
using System; using System;
using System.Threading.Tasks; using System.Threading.Tasks;
using log4net;
using Newtonsoft.Json; using Newtonsoft.Json;
#if !NETSTANDARD #if !NETSTANDARD
using Microsoft.Owin; using Microsoft.Owin;
@@ -15,6 +16,7 @@ namespace WireMock.Owin
internal class GlobalExceptionMiddleware internal class GlobalExceptionMiddleware
#endif #endif
{ {
private static readonly ILog Log = LogManager.GetLogger(typeof(GlobalExceptionMiddleware));
#if !NETSTANDARD #if !NETSTANDARD
public GlobalExceptionMiddleware(OwinMiddleware next) : base(next) { } public GlobalExceptionMiddleware(OwinMiddleware next) : base(next) { }
#else #else
@@ -42,6 +44,7 @@ namespace WireMock.Owin
} }
catch (Exception ex) catch (Exception ex)
{ {
Log.Error("HttpStatusCode set to 500", ex);
await _responseMapper.MapAsync(new ResponseMessage { StatusCode = 500, Body = JsonConvert.SerializeObject(ex) }, ctx.Response); await _responseMapper.MapAsync(new ResponseMessage { StatusCode = 500, Body = JsonConvert.SerializeObject(ex) }, ctx.Response);
} }
} }

View File

@@ -4,6 +4,7 @@ using System.IO;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using WireMock.Util;
#if !NETSTANDARD #if !NETSTANDARD
using Microsoft.Owin; using Microsoft.Owin;
#else #else
@@ -16,7 +17,7 @@ namespace WireMock.Owin
/// <summary> /// <summary>
/// OwinRequestMapper /// OwinRequestMapper
/// </summary> /// </summary>
public class OwinRequestMapper internal class OwinRequestMapper
{ {
/// <summary> /// <summary>
/// MapAsync IOwinRequest to RequestMessage /// MapAsync IOwinRequest to RequestMessage
@@ -43,20 +44,6 @@ namespace WireMock.Owin
#endif #endif
string method = request.Method; string method = request.Method;
string bodyAsString = null;
byte[] body = null;
Encoding bodyEncoding = null;
if (ParseBody(method) && request.Body != null)
{
using (var streamReader = new StreamReader(request.Body))
{
bodyAsString = await streamReader.ReadToEndAsync();
bodyEncoding = streamReader.CurrentEncoding;
}
body = bodyEncoding.GetBytes(bodyAsString);
}
Dictionary<string, string[]> headers = null; Dictionary<string, string[]> headers = null;
if (request.Headers.Any()) if (request.Headers.Any())
{ {
@@ -77,10 +64,16 @@ namespace WireMock.Owin
} }
} }
return new RequestMessage(url, method, clientIP, body, bodyAsString, bodyEncoding, headers, cookies) { DateTime = DateTime.Now }; BodyData body = null;
if (request.Body != null && ShouldParseBody(method))
{
body = await BodyParser.Parse(request.Body, request.ContentType);
}
return new RequestMessage(url, method, clientIP, body, headers, cookies) { DateTime = DateTime.Now };
} }
private bool ParseBody(string method) private bool ShouldParseBody(string method)
{ {
/* /*
HEAD - No defined body semantics. HEAD - No defined body semantics.

View File

@@ -3,9 +3,11 @@ using System.Threading.Tasks;
using WireMock.Logging; using WireMock.Logging;
using WireMock.Matchers.Request; using WireMock.Matchers.Request;
using System.Linq; using System.Linq;
using log4net;
using WireMock.Matchers; using WireMock.Matchers;
using WireMock.Util; using WireMock.Util;
using Newtonsoft.Json; using Newtonsoft.Json;
using WireMock.Http;
#if !NETSTANDARD #if !NETSTANDARD
using Microsoft.Owin; using Microsoft.Owin;
#else #else
@@ -20,6 +22,7 @@ namespace WireMock.Owin
internal class WireMockMiddleware internal class WireMockMiddleware
#endif #endif
{ {
private static readonly ILog Log = LogManager.GetLogger(typeof(WireMockMiddleware));
private static readonly Task CompletedTask = Task.FromResult(false); private static readonly Task CompletedTask = Task.FromResult(false);
private readonly WireMockMiddlewareOptions _options; private readonly WireMockMiddlewareOptions _options;
@@ -95,6 +98,7 @@ namespace WireMock.Owin
if (targetMapping == null) if (targetMapping == null)
{ {
logRequest = true; logRequest = true;
Log.Warn("HttpStatusCode set to 404 : No matching mapping found");
response = new ResponseMessage { StatusCode = 404, Body = "No matching mapping found" }; response = new ResponseMessage { StatusCode = 404, Body = "No matching mapping found" };
return; return;
} }
@@ -103,9 +107,10 @@ namespace WireMock.Owin
if (targetMapping.IsAdminInterface && _options.AuthorizationMatcher != null) if (targetMapping.IsAdminInterface && _options.AuthorizationMatcher != null)
{ {
bool present = request.Headers.TryGetValue("Authorization", out WireMockList<string> authorization); bool present = request.Headers.TryGetValue(HttpKnownHeaderNames.Authorization, out WireMockList<string> authorization);
if (!present || _options.AuthorizationMatcher.IsMatch(authorization.ToString()) < MatchScores.Perfect) if (!present || _options.AuthorizationMatcher.IsMatch(authorization.ToString()) < MatchScores.Perfect)
{ {
Log.Error("HttpStatusCode set to 401");
response = new ResponseMessage { StatusCode = 401 }; response = new ResponseMessage { StatusCode = 401 };
return; return;
} }
@@ -125,6 +130,7 @@ namespace WireMock.Owin
} }
catch (Exception ex) catch (Exception ex)
{ {
Log.Error("HttpStatusCode set to 500", ex);
response = new ResponseMessage { StatusCode = 500, Body = JsonConvert.SerializeObject(ex) }; response = new ResponseMessage { StatusCode = 500, Body = JsonConvert.SerializeObject(ex) };
} }
finally finally

View File

@@ -60,15 +60,20 @@ namespace WireMock
public string RawQuery { get; } public string RawQuery { get; }
/// <summary> /// <summary>
/// Gets the bodyAsBytes. /// The body as string.
/// </summary>
public byte[] BodyAsBytes { get; }
/// <summary>
/// Gets the body.
/// </summary> /// </summary>
public string Body { get; } public string Body { get; }
/// <summary>
/// The body (as JSON object).
/// </summary>
public object BodyAsJson { get; set; }
/// <summary>
/// The body (as bytearray).
/// </summary>
public byte[] BodyAsBytes { get; set; }
/// <summary> /// <summary>
/// Gets the Host /// Gets the Host
/// </summary> /// </summary>
@@ -90,10 +95,45 @@ namespace WireMock
public string Origin { get; } public string Origin { get; }
/// <summary> /// <summary>
/// Gets the body encoding. /// The body encoding.
/// </summary> /// </summary>
public Encoding BodyEncoding { get; } public Encoding BodyEncoding { get; }
/// <summary>
/// Initializes a new instance of the <see cref="RequestMessage"/> class.
/// </summary>
/// <param name="url">The original url.</param>
/// <param name="method">The HTTP method.</param>
/// <param name="clientIP">The client IP Address.</param>
/// <param name="body">The body.</param>
/// <param name="headers">The headers.</param>
/// <param name="cookies">The cookies.</param>
public RequestMessage([NotNull] Uri url, [NotNull] string method, [NotNull] string clientIP, [CanBeNull] BodyData body, [CanBeNull] IDictionary<string, string[]> headers = null, [CanBeNull] IDictionary<string, string> cookies = null)
{
Check.NotNull(url, nameof(url));
Check.NotNull(method, nameof(method));
Check.NotNull(clientIP, nameof(clientIP));
Url = url.ToString();
Protocol = url.Scheme;
Host = url.Host;
Port = url.Port;
Origin = $"{url.Scheme}://{url.Host}:{url.Port}";
Path = WebUtility.UrlDecode(url.AbsolutePath);
Method = method.ToLower();
ClientIP = clientIP;
Body = body?.BodyAsString;
BodyEncoding = body?.Encoding;
BodyAsJson = body?.BodyAsJson;
BodyAsBytes = body?.BodyAsBytes;
Headers = headers?.ToDictionary(header => header.Key, header => new WireMockList<string>(header.Value));
Cookies = cookies;
RawQuery = WebUtility.UrlDecode(url.Query);
Query = ParseQuery(RawQuery);
}
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="RequestMessage"/> class. /// Initializes a new instance of the <see cref="RequestMessage"/> class.
/// </summary> /// </summary>

View File

@@ -247,16 +247,9 @@ namespace WireMock.ResponseBuilders
{ {
Check.NotNull(body, nameof(body)); Check.NotNull(body, nameof(body));
string jsonBody = JsonConvert.SerializeObject(body, new JsonSerializerSettings { Formatting = Formatting.None, NullValueHandling = NullValueHandling.Ignore });
if (encoding != null && !encoding.Equals(Encoding.UTF8))
{
jsonBody = encoding.GetString(Encoding.UTF8.GetBytes(jsonBody));
ResponseMessage.BodyEncoding = encoding;
}
ResponseMessage.BodyDestination = null; ResponseMessage.BodyDestination = null;
ResponseMessage.Body = jsonBody; ResponseMessage.BodyAsJson = body;
ResponseMessage.BodyEncoding = encoding;
return this; return this;
} }

View File

@@ -7,6 +7,7 @@ using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using JetBrains.Annotations; using JetBrains.Annotations;
using Newtonsoft.Json; using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using WireMock.Admin.Mappings; using WireMock.Admin.Mappings;
using WireMock.Admin.Requests; using WireMock.Admin.Requests;
using WireMock.Admin.Settings; using WireMock.Admin.Settings;
@@ -112,6 +113,7 @@ namespace WireMock.Server
foreach (string filename in Directory.EnumerateFiles(folder).OrderBy(f => f)) foreach (string filename in Directory.EnumerateFiles(folder).OrderBy(f => f))
{ {
Log.InfoFormat("Reading Static MappingFile : '{0}'", filename);
ReadStaticMappingAndAddOrUpdate(filename); ReadStaticMappingAndAddOrUpdate(filename);
} }
} }
@@ -133,17 +135,22 @@ namespace WireMock.Server
return; return;
} }
var watcher = new EnhancedFileSystemWatcher(folder, "*.json", 500); Log.InfoFormat("Watching folder '{0}' for new, updated and deleted MappingFiles.", folder);
var watcher = new EnhancedFileSystemWatcher(folder, "*.json", 1000);
watcher.Created += (sender, args) => watcher.Created += (sender, args) =>
{ {
Log.InfoFormat("New MappingFile created : '{0}'", args.FullPath);
ReadStaticMappingAndAddOrUpdate(args.FullPath); ReadStaticMappingAndAddOrUpdate(args.FullPath);
}; };
watcher.Changed += (sender, args) => watcher.Changed += (sender, args) =>
{ {
Log.InfoFormat("New MappingFile updated : '{0}'", args.FullPath);
ReadStaticMappingAndAddOrUpdate(args.FullPath); ReadStaticMappingAndAddOrUpdate(args.FullPath);
}; };
watcher.Deleted += (sender, args) => watcher.Deleted += (sender, args) =>
{ {
Log.InfoFormat("New MappingFile deleted : '{0}'", args.FullPath);
string filenameWithoutExtension = Path.GetFileNameWithoutExtension(args.FullPath); string filenameWithoutExtension = Path.GetFileNameWithoutExtension(args.FullPath);
if (Guid.TryParse(filenameWithoutExtension, out Guid guidFromFilename)) if (Guid.TryParse(filenameWithoutExtension, out Guid guidFromFilename))
@@ -170,13 +177,14 @@ namespace WireMock.Server
string filenameWithoutExtension = Path.GetFileNameWithoutExtension(path); string filenameWithoutExtension = Path.GetFileNameWithoutExtension(path);
MappingModel mappingModel = JsonConvert.DeserializeObject<MappingModel>(FileHelper.ReadAllText(path));
if (Guid.TryParse(filenameWithoutExtension, out Guid guidFromFilename)) if (Guid.TryParse(filenameWithoutExtension, out Guid guidFromFilename))
{ {
DeserializeAndAddOrUpdateMapping(FileHelper.ReadAllText(path), guidFromFilename, path); DeserializeAndAddOrUpdateMapping(mappingModel, guidFromFilename, path);
} }
else else
{ {
DeserializeAndAddOrUpdateMapping(FileHelper.ReadAllText(path), null, path); DeserializeAndAddOrUpdateMapping(mappingModel, null, path);
} }
} }
#endregion #endregion
@@ -256,7 +264,7 @@ namespace WireMock.Server
private ResponseMessage SettingsUpdate(RequestMessage requestMessage) private ResponseMessage SettingsUpdate(RequestMessage requestMessage)
{ {
var settings = JsonConvert.DeserializeObject<SettingsModel>(requestMessage.Body); var settings = requestMessage.Body != null ? JsonConvert.DeserializeObject<SettingsModel>(requestMessage.Body) : ((JObject)requestMessage.BodyAsJson).ToObject<SettingsModel>();
if (settings.AllowPartialMapping != null) if (settings.AllowPartialMapping != null)
_options.AllowPartialMapping = settings.AllowPartialMapping.Value; _options.AllowPartialMapping = settings.AllowPartialMapping.Value;
@@ -280,6 +288,7 @@ namespace WireMock.Server
if (mapping == null) if (mapping == null)
{ {
Log.Warn("HttpStatusCode set to 404 : Mapping not found");
return new ResponseMessage { StatusCode = 404, Body = "Mapping not found" }; return new ResponseMessage { StatusCode = 404, Body = "Mapping not found" };
} }
@@ -292,7 +301,8 @@ namespace WireMock.Server
{ {
Guid guid = Guid.Parse(requestMessage.Path.TrimStart(AdminMappings.ToCharArray())); Guid guid = Guid.Parse(requestMessage.Path.TrimStart(AdminMappings.ToCharArray()));
DeserializeAndAddOrUpdateMapping(requestMessage.Body, guid); MappingModel mappingModel = requestMessage.Body != null ? JsonConvert.DeserializeObject<MappingModel>(requestMessage.Body) : ((JObject)requestMessage.BodyAsJson).ToObject<MappingModel>();
DeserializeAndAddOrUpdateMapping(mappingModel, guid);
return new ResponseMessage { Body = "Mapping added or updated" }; return new ResponseMessage { Body = "Mapping added or updated" };
} }
@@ -330,10 +340,12 @@ namespace WireMock.Server
} }
var model = MappingConverter.ToMappingModel(mapping); var model = MappingConverter.ToMappingModel(mapping);
string json = JsonConvert.SerializeObject(model, _settings);
string filename = !string.IsNullOrEmpty(mapping.Title) ? SanitizeFileName(mapping.Title) : mapping.Guid.ToString(); string filename = !string.IsNullOrEmpty(mapping.Title) ? SanitizeFileName(mapping.Title) : mapping.Guid.ToString();
File.WriteAllText(Path.Combine(folder, filename + ".json"), json); string filePath = Path.Combine(folder, filename + ".json");
Log.InfoFormat("Saving Mapping to file {0}", filePath);
File.WriteAllText(filePath, JsonConvert.SerializeObject(model, _settings));
} }
private static string SanitizeFileName(string name, char replaceChar = '_') private static string SanitizeFileName(string name, char replaceChar = '_')
@@ -357,24 +369,25 @@ namespace WireMock.Server
{ {
try try
{ {
DeserializeAndAddOrUpdateMapping(requestMessage.Body); MappingModel mappingModel = requestMessage.Body != null ? JsonConvert.DeserializeObject<MappingModel>(requestMessage.Body) : ((JObject)requestMessage.BodyAsJson).ToObject<MappingModel>();
DeserializeAndAddOrUpdateMapping(mappingModel);
} }
catch (ArgumentException a) catch (ArgumentException a)
{ {
Log.Error("HttpStatusCode set to 400", a);
return new ResponseMessage { StatusCode = 400, Body = a.Message }; return new ResponseMessage { StatusCode = 400, Body = a.Message };
} }
catch (Exception e) catch (Exception e)
{ {
Log.Error("HttpStatusCode set to 500", e);
return new ResponseMessage { StatusCode = 500, Body = e.ToString() }; return new ResponseMessage { StatusCode = 500, Body = e.ToString() };
} }
return new ResponseMessage { StatusCode = 201, Body = "Mapping added" }; return new ResponseMessage { StatusCode = 201, Body = "Mapping added" };
} }
private void DeserializeAndAddOrUpdateMapping(string json, Guid? guid = null, string path = null) private void DeserializeAndAddOrUpdateMapping(MappingModel mappingModel, Guid? guid = null, string path = null)
{ {
var mappingModel = JsonConvert.DeserializeObject<MappingModel>(json);
Check.NotNull(mappingModel, nameof(mappingModel)); Check.NotNull(mappingModel, nameof(mappingModel));
Check.NotNull(mappingModel.Request, nameof(mappingModel.Request)); Check.NotNull(mappingModel.Request, nameof(mappingModel.Request));
Check.NotNull(mappingModel.Response, nameof(mappingModel.Response)); Check.NotNull(mappingModel.Response, nameof(mappingModel.Response));
@@ -435,7 +448,10 @@ namespace WireMock.Server
var entry = LogEntries.FirstOrDefault(r => !r.RequestMessage.Path.StartsWith("/__admin/") && r.Guid == guid); var entry = LogEntries.FirstOrDefault(r => !r.RequestMessage.Path.StartsWith("/__admin/") && r.Guid == guid);
if (entry == null) if (entry == null)
return new ResponseMessage { StatusCode = 404, Body = "Request not found" }; {
Log.Warn("HttpStatusCode set to 404 : Request not found");
return new ResponseMessage {StatusCode = 404, Body = "Request not found"};
}
var model = ToLogEntryModel(entry); var model = ToLogEntryModel(entry);
@@ -477,6 +493,8 @@ namespace WireMock.Server
Query = logEntry.RequestMessage.Query, Query = logEntry.RequestMessage.Query,
Method = logEntry.RequestMessage.Method, Method = logEntry.RequestMessage.Method,
Body = logEntry.RequestMessage.Body, Body = logEntry.RequestMessage.Body,
BodyAsJson = logEntry.RequestMessage.BodyAsJson,
BodyAsBytes = logEntry.RequestMessage.BodyAsBytes,
Headers = logEntry.RequestMessage.Headers, Headers = logEntry.RequestMessage.Headers,
Cookies = logEntry.RequestMessage.Cookies, Cookies = logEntry.RequestMessage.Cookies,
BodyEncoding = logEntry.RequestMessage.BodyEncoding != null ? new EncodingModel BodyEncoding = logEntry.RequestMessage.BodyEncoding != null ? new EncodingModel
@@ -491,6 +509,7 @@ namespace WireMock.Server
StatusCode = logEntry.ResponseMessage.StatusCode, StatusCode = logEntry.ResponseMessage.StatusCode,
BodyDestination = logEntry.ResponseMessage.BodyDestination, BodyDestination = logEntry.ResponseMessage.BodyDestination,
Body = logEntry.ResponseMessage.Body, Body = logEntry.ResponseMessage.Body,
BodyAsJson = logEntry.ResponseMessage.BodyAsJson,
BodyAsBytes = logEntry.ResponseMessage.BodyAsBytes, BodyAsBytes = logEntry.ResponseMessage.BodyAsBytes,
BodyOriginal = logEntry.ResponseMessage.BodyOriginal, BodyOriginal = logEntry.ResponseMessage.BodyOriginal,
BodyAsFile = logEntry.ResponseMessage.BodyAsFile, BodyAsFile = logEntry.ResponseMessage.BodyAsFile,
@@ -531,7 +550,7 @@ namespace WireMock.Server
#region Requests/find #region Requests/find
private ResponseMessage RequestsFind(RequestMessage requestMessage) private ResponseMessage RequestsFind(RequestMessage requestMessage)
{ {
var requestModel = JsonConvert.DeserializeObject<RequestModel>(requestMessage.Body); var requestModel = requestMessage.Body != null ? JsonConvert.DeserializeObject<RequestModel>(requestMessage.Body) : ((JObject)requestMessage.BodyAsJson).ToObject<RequestModel>();
var request = (Request)InitRequestBuilder(requestModel); var request = (Request)InitRequestBuilder(requestModel);

View File

@@ -6,6 +6,8 @@ using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using JetBrains.Annotations; using JetBrains.Annotations;
using log4net;
using Newtonsoft.Json;
using WireMock.Http; using WireMock.Http;
using WireMock.Matchers; using WireMock.Matchers;
using WireMock.Matchers.Request; using WireMock.Matchers.Request;
@@ -13,7 +15,6 @@ using WireMock.RequestBuilders;
using WireMock.Settings; using WireMock.Settings;
using WireMock.Validation; using WireMock.Validation;
using WireMock.Owin; using WireMock.Owin;
using WireMock.Serialization;
namespace WireMock.Server namespace WireMock.Server
{ {
@@ -22,6 +23,7 @@ namespace WireMock.Server
/// </summary> /// </summary>
public partial class FluentMockServer : IDisposable public partial class FluentMockServer : IDisposable
{ {
private static readonly ILog Log = LogManager.GetLogger(typeof(FluentMockServer));
private const int ServerStartDelay = 100; private const int ServerStartDelay = 100;
private readonly IOwinSelfHost _httpServer; private readonly IOwinSelfHost _httpServer;
private readonly WireMockMiddlewareOptions _options = new WireMockMiddlewareOptions(); private readonly WireMockMiddlewareOptions _options = new WireMockMiddlewareOptions();
@@ -156,6 +158,8 @@ namespace WireMock.Server
private FluentMockServer(IFluentMockServerSettings settings) private FluentMockServer(IFluentMockServerSettings settings)
{ {
Log.DebugFormat("WireMock.Net server settings {0}", JsonConvert.SerializeObject(settings, Formatting.Indented));
if (settings.Urls != null) if (settings.Urls != null)
{ {
Urls = settings.Urls.Select(u => u.EndsWith("/") ? u : $"{u}/").ToArray(); Urls = settings.Urls.Select(u => u.EndsWith("/") ? u : $"{u}/").ToArray();
@@ -315,9 +319,10 @@ namespace WireMock.Server
/// Allows the partial mapping. /// Allows the partial mapping.
/// </summary> /// </summary>
[PublicAPI] [PublicAPI]
public void AllowPartialMapping() public void AllowPartialMapping(bool allow = true)
{ {
_options.AllowPartialMapping = true; Log.InfoFormat("AllowPartialMapping is set to {0}", allow);
_options.AllowPartialMapping = allow;
} }
/// <summary> /// <summary>

View File

@@ -1,5 +1,6 @@
using System; using System;
using JetBrains.Annotations; using JetBrains.Annotations;
using Newtonsoft.Json;
namespace WireMock.Settings namespace WireMock.Settings
{ {
@@ -63,10 +64,12 @@ namespace WireMock.Settings
/// <inheritdoc cref="IFluentMockServerSettings.PreWireMockMiddlewareInit"/> /// <inheritdoc cref="IFluentMockServerSettings.PreWireMockMiddlewareInit"/>
[PublicAPI] [PublicAPI]
[JsonIgnore]
public Action<object> PreWireMockMiddlewareInit { get; set; } public Action<object> PreWireMockMiddlewareInit { get; set; }
/// <inheritdoc cref="IFluentMockServerSettings.PostWireMockMiddlewareInit"/> /// <inheritdoc cref="IFluentMockServerSettings.PostWireMockMiddlewareInit"/>
[PublicAPI] [PublicAPI]
[JsonIgnore]
public Action<object> PostWireMockMiddlewareInit { get; set; } public Action<object> PostWireMockMiddlewareInit { get; set; }
} }
} }

View File

@@ -0,0 +1,30 @@
using System.Text;
namespace WireMock.Util
{
/// <summary>
/// BodyData
/// </summary>
public class BodyData
{
/// <summary>
/// The body encoding.
/// </summary>
public Encoding Encoding { get; set; }
/// <summary>
/// The body as string.
/// </summary>
public string BodyAsString { get; set; }
/// <summary>
/// The body (as JSON object).
/// </summary>
public object BodyAsJson { get; set; }
/// <summary>
/// The body (as bytearray).
/// </summary>
public byte[] BodyAsBytes { get; set; }
}
}

View File

@@ -0,0 +1,75 @@
using System;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using JetBrains.Annotations;
using Newtonsoft.Json;
namespace WireMock.Util
{
internal static class BodyParser
{
private static readonly string[] TextContentTypes = { "text/", "application/xml", "application/javascript", "application/typescript", "application/xhtml+xml" };
private static async Task<Tuple<string, Encoding>> ReadStringAsync(Stream stream)
{
using (var streamReader = new StreamReader(stream))
{
string content = await streamReader.ReadToEndAsync();
return new Tuple<string, Encoding>(content, streamReader.CurrentEncoding);
}
}
private static async Task<byte[]> ReadBytesAsync(Stream stream)
{
using (var memoryStream = new MemoryStream())
{
await stream.CopyToAsync(memoryStream);
return memoryStream.ToArray();
}
}
public static async Task<BodyData> Parse([NotNull] Stream stream, [CanBeNull] string contentTypeHeaderValue)
{
var data = new BodyData();
if (contentTypeHeaderValue != null && TextContentTypes.Any(t => contentTypeHeaderValue.StartsWith(t, StringComparison.OrdinalIgnoreCase)))
{
try
{
var stringData = await ReadStringAsync(stream);
data.BodyAsString = stringData.Item1;
data.Encoding = stringData.Item2;
}
catch
{
// Reading as string failed, just get the ByteArray.
data.BodyAsBytes = await ReadBytesAsync(stream);
}
}
else if (contentTypeHeaderValue != null && contentTypeHeaderValue.StartsWith("application/json", StringComparison.OrdinalIgnoreCase))
{
var stringData = await ReadStringAsync(stream);
data.Encoding = stringData.Item2;
try
{
data.BodyAsJson = JsonConvert.DeserializeObject(stringData.Item1, new JsonSerializerSettings { Formatting = Formatting.Indented });
}
catch
{
// JsonConvert failed, just set the Body as string.
data.BodyAsString = stringData.Item1;
}
}
else
{
data.BodyAsBytes = await ReadBytesAsync(stream);
}
return data;
}
}
}

View File

@@ -3,7 +3,7 @@
<PropertyGroup> <PropertyGroup>
<Description>Lightweight Http Mocking Server for .Net, inspired by WireMock from the Java landscape.</Description> <Description>Lightweight Http Mocking Server for .Net, inspired by WireMock from the Java landscape.</Description>
<AssemblyTitle>WireMock.Net</AssemblyTitle> <AssemblyTitle>WireMock.Net</AssemblyTitle>
<Version>1.0.3</Version> <Version>1.0.3.1</Version>
<Authors>Alexandre Victoor;Stef Heyenrath</Authors> <Authors>Alexandre Victoor;Stef Heyenrath</Authors>
<TargetFrameworks>net452;net46;netstandard1.3;netstandard2.0</TargetFrameworks> <TargetFrameworks>net452;net46;netstandard1.3;netstandard2.0</TargetFrameworks>
<GenerateDocumentationFile>true</GenerateDocumentationFile> <GenerateDocumentationFile>true</GenerateDocumentationFile>
@@ -32,6 +32,10 @@
<DefineConstants>NETSTANDARD</DefineConstants> <DefineConstants>NETSTANDARD</DefineConstants>
</PropertyGroup> </PropertyGroup>
<ItemGroup>
<None Remove="Server\FluentMockServer.cs~RF44936b9f.TMP" />
</ItemGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="JetBrains.Annotations" Version="11.1.0"> <PackageReference Include="JetBrains.Annotations" Version="11.1.0">
<PrivateAssets>All</PrivateAssets> <PrivateAssets>All</PrivateAssets>
@@ -41,7 +45,7 @@
<PackageReference Include="SimMetrics.Net" Version="1.0.4" /> <PackageReference Include="SimMetrics.Net" Version="1.0.4" />
<PackageReference Include="System.Net.Http" Version="4.3.3" /> <PackageReference Include="System.Net.Http" Version="4.3.3" />
<PackageReference Include="RestEase" Version="1.4.4" /> <PackageReference Include="RestEase" Version="1.4.4" />
<!--<PackageReference Include="OpenSSL.X509Certificate2.Provider" Version="1.0.2" />--> <PackageReference Include="log4net" Version="2.0.8" />
</ItemGroup> </ItemGroup>
<ItemGroup Condition=" '$(TargetFramework)' == 'net452' "> <ItemGroup Condition=" '$(TargetFramework)' == 'net452' ">

View File

@@ -72,7 +72,7 @@ namespace WireMock.Net.Tests
var receivedRequest = _serverForProxyForwarding.LogEntries.First().RequestMessage; var receivedRequest = _serverForProxyForwarding.LogEntries.First().RequestMessage;
Check.That(receivedRequest.Body).IsEqualTo("stringContent"); Check.That(receivedRequest.Body).IsEqualTo("stringContent");
Check.That(receivedRequest.Headers).ContainsKey("Content-Type"); Check.That(receivedRequest.Headers).ContainsKey("Content-Type");
Check.That(receivedRequest.Headers["Content-Type"]).ContainsExactly("text/plain"); Check.That(receivedRequest.Headers["Content-Type"].First()).Contains("text/plain");
Check.That(receivedRequest.Headers).ContainsKey("bbb"); Check.That(receivedRequest.Headers).ContainsKey("bbb");
var mapping = _server.Mappings.Last(); var mapping = _server.Mappings.Last();
@@ -151,7 +151,7 @@ namespace WireMock.Net.Tests
var receivedRequest = _serverForProxyForwarding.LogEntries.First().RequestMessage; var receivedRequest = _serverForProxyForwarding.LogEntries.First().RequestMessage;
Check.That(receivedRequest.Body).IsEqualTo(""); Check.That(receivedRequest.Body).IsEqualTo("");
Check.That(receivedRequest.Headers).ContainsKey("Content-Type"); Check.That(receivedRequest.Headers).ContainsKey("Content-Type");
Check.That(receivedRequest.Headers["Content-Type"]).ContainsExactly("text/plain"); Check.That(receivedRequest.Headers["Content-Type"].First()).Contains("text/plain");
} }
[Fact] [Fact]

View File

@@ -75,13 +75,15 @@ namespace WireMock.Net.Tests
byte[] body = Encoding.UTF8.GetBytes(bodyAsString); byte[] body = Encoding.UTF8.GetBytes(bodyAsString);
var request = new RequestMessage(new Uri("http://localhost/foo"), "POST", ClientIp, body, bodyAsString, Encoding.UTF8); var request = new RequestMessage(new Uri("http://localhost/foo"), "POST", ClientIp, body, bodyAsString, Encoding.UTF8);
var response = Response.Create().WithBodyAsJson(new { value = "test" }, Encoding.ASCII); object x = new { value = "test" };
var response = Response.Create().WithBodyAsJson(x, Encoding.ASCII);
// act // act
var responseMessage = await response.ProvideResponseAsync(request); var responseMessage = await response.ProvideResponseAsync(request);
// then // then
Check.That(responseMessage.Body).Equals("{\"value\":\"test\"}"); Check.That(responseMessage.BodyAsJson).IsNotNull();
Check.That(responseMessage.BodyAsJson).Equals(x);
Check.That(responseMessage.BodyEncoding).Equals(Encoding.ASCII); Check.That(responseMessage.BodyEncoding).Equals(Encoding.ASCII);
} }
} }