mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-04-24 17:28:27 +02:00
@@ -1,79 +1,109 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using log4net;
|
using log4net;
|
||||||
using log4net.Config;
|
using log4net.Config;
|
||||||
using log4net.Repository;
|
using log4net.Repository;
|
||||||
using WireMock.RequestBuilders;
|
using Microsoft.AspNetCore.Builder;
|
||||||
using WireMock.ResponseBuilders;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using WireMock.Server;
|
using WireMock.RequestBuilders;
|
||||||
using WireMock.Settings;
|
using WireMock.ResponseBuilders;
|
||||||
|
using WireMock.Server;
|
||||||
|
using WireMock.Settings;
|
||||||
using WireMock.Util;
|
using WireMock.Util;
|
||||||
|
|
||||||
namespace WireMock.Net.StandAlone.NETCoreApp
|
namespace WireMock.Net.StandAlone.NETCoreApp
|
||||||
{
|
{
|
||||||
static class Program
|
static class Program
|
||||||
{
|
{
|
||||||
private static readonly ILoggerRepository LogRepository = LogManager.GetRepository(Assembly.GetEntryAssembly());
|
private static readonly ILoggerRepository LogRepository = LogManager.GetRepository(Assembly.GetEntryAssembly());
|
||||||
// private static readonly ILog Log = LogManager.GetLogger(typeof(Program));
|
// private static readonly ILog Log = LogManager.GetLogger(typeof(Program));
|
||||||
|
|
||||||
private static int sleepTime = 30000;
|
private static int sleepTime = 30000;
|
||||||
private static WireMockServer _server;
|
private static WireMockServer _server;
|
||||||
|
|
||||||
static void Main(string[] args)
|
static void Main(string[] args)
|
||||||
{
|
{
|
||||||
XmlConfigurator.Configure(LogRepository, new FileInfo("log4net.config"));
|
XmlConfigurator.Configure(LogRepository, new FileInfo("log4net.config"));
|
||||||
|
|
||||||
if (!WireMockServerSettingsParser.TryParseArguments(args, out var settings, new WireMockLog4NetLogger()))
|
if (!WireMockServerSettingsParser.TryParseArguments(args, out var settings, new WireMockLog4NetLogger()))
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
settings.Logger.Debug("WireMock.Net server arguments [{0}]", string.Join(", ", args.Select(a => $"'{a}'")));
|
settings.Logger.Debug("WireMock.Net server arguments [{0}]", string.Join(", ", args.Select(a => $"'{a}'")));
|
||||||
|
|
||||||
_server = WireMockServer.Start(settings);
|
/* https://stackoverflow.com/questions/31942037/how-to-enable-cors-in-asp-net-core */
|
||||||
|
/* Enable Cors */
|
||||||
|
var policyName = "MyPolicy";
|
||||||
|
settings.AdditionalServiceRegistration = services =>
|
||||||
|
{
|
||||||
|
services.AddCors(corsOptions =>
|
||||||
|
corsOptions.AddPolicy(policyName,
|
||||||
|
corsPolicyBuilder =>
|
||||||
|
{
|
||||||
|
corsPolicyBuilder
|
||||||
|
.AllowAnyHeader()
|
||||||
|
.AllowAnyMethod()
|
||||||
|
.AllowAnyOrigin();
|
||||||
|
}));
|
||||||
|
|
||||||
|
settings.Logger.Debug("Enable Cors");
|
||||||
|
};
|
||||||
|
|
||||||
|
/* Use Cors */
|
||||||
|
settings.PreWireMockMiddlewareInit = app =>
|
||||||
|
{
|
||||||
|
var appBuilder = (IApplicationBuilder)app;
|
||||||
|
appBuilder.UseCors(policyName);
|
||||||
|
|
||||||
|
settings.Logger.Debug("Use Cors");
|
||||||
|
};
|
||||||
|
|
||||||
|
_server = WireMockServer.Start(settings);
|
||||||
|
|
||||||
_server.Given(Request.Create().WithPath("/api/sap")
|
_server.Given(Request.Create().WithPath("/api/sap")
|
||||||
.UsingPost()
|
.UsingPost()
|
||||||
.WithBody((IBodyData xmlData) => {
|
.WithBody((IBodyData xmlData) =>
|
||||||
|
{
|
||||||
//xmlData is always null
|
//xmlData is always null
|
||||||
return true;
|
return true;
|
||||||
}))
|
}))
|
||||||
.RespondWith(Response.Create().WithStatusCode(System.Net.HttpStatusCode.OK));
|
.RespondWith(Response.Create().WithStatusCode(System.Net.HttpStatusCode.OK));
|
||||||
|
|
||||||
_server
|
_server
|
||||||
.Given(Request.Create()
|
.Given(Request.Create()
|
||||||
.UsingAnyMethod())
|
.UsingAnyMethod())
|
||||||
.RespondWith(Response.Create()
|
.RespondWith(Response.Create()
|
||||||
.WithTransformer()
|
.WithTransformer()
|
||||||
.WithBody("{{Random Type=\"Integer\" Min=100 Max=999999}} {{DateTime.Now}} {{DateTime.Now \"yyyy-MMM\"}} {{String.Format (DateTime.Now) \"MMM-dd\"}}"));
|
.WithBody("{{Random Type=\"Integer\" Min=100 Max=999999}} {{DateTime.Now}} {{DateTime.Now \"yyyy-MMM\"}} {{String.Format (DateTime.Now) \"MMM-dd\"}}"));
|
||||||
|
|
||||||
Console.WriteLine($"{DateTime.UtcNow} Press Ctrl+C to shut down");
|
Console.WriteLine($"{DateTime.UtcNow} Press Ctrl+C to shut down");
|
||||||
|
|
||||||
Console.CancelKeyPress += (s, e) =>
|
Console.CancelKeyPress += (s, e) =>
|
||||||
{
|
{
|
||||||
Stop("CancelKeyPress");
|
Stop("CancelKeyPress");
|
||||||
};
|
};
|
||||||
|
|
||||||
System.Runtime.Loader.AssemblyLoadContext.Default.Unloading += ctx =>
|
System.Runtime.Loader.AssemblyLoadContext.Default.Unloading += ctx =>
|
||||||
{
|
{
|
||||||
Stop("AssemblyLoadContext.Default.Unloading");
|
Stop("AssemblyLoadContext.Default.Unloading");
|
||||||
};
|
};
|
||||||
|
|
||||||
while (true)
|
while (true)
|
||||||
{
|
{
|
||||||
Console.WriteLine($"{DateTime.UtcNow} WireMock.Net server running : {_server.IsStarted}");
|
Console.WriteLine($"{DateTime.UtcNow} WireMock.Net server running : {_server.IsStarted}");
|
||||||
Thread.Sleep(sleepTime);
|
Thread.Sleep(sleepTime);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void Stop(string why)
|
private static void Stop(string why)
|
||||||
{
|
{
|
||||||
Console.WriteLine($"{DateTime.UtcNow} WireMock.Net server stopping because '{why}'");
|
Console.WriteLine($"{DateTime.UtcNow} WireMock.Net server stopping because '{why}'");
|
||||||
_server.Stop();
|
_server.Stop();
|
||||||
Console.WriteLine($"{DateTime.UtcNow} WireMock.Net server stopped");
|
Console.WriteLine($"{DateTime.UtcNow} WireMock.Net server stopped");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
#if USE_ASPNETCORE
|
#if USE_ASPNETCORE
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
@@ -8,10 +9,10 @@ using JetBrains.Annotations;
|
|||||||
using Microsoft.AspNetCore.Builder;
|
using Microsoft.AspNetCore.Builder;
|
||||||
using Microsoft.AspNetCore.Hosting;
|
using Microsoft.AspNetCore.Hosting;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
using Stef.Validation;
|
||||||
using WireMock.Logging;
|
using WireMock.Logging;
|
||||||
using WireMock.Owin.Mappers;
|
using WireMock.Owin.Mappers;
|
||||||
using WireMock.Util;
|
using WireMock.Util;
|
||||||
using Stef.Validation;
|
|
||||||
|
|
||||||
namespace WireMock.Owin
|
namespace WireMock.Owin
|
||||||
{
|
{
|
||||||
@@ -54,7 +55,7 @@ namespace WireMock.Owin
|
|||||||
// a filesystem handler).
|
// a filesystem handler).
|
||||||
if (string.IsNullOrEmpty(AppContext.BaseDirectory))
|
if (string.IsNullOrEmpty(AppContext.BaseDirectory))
|
||||||
{
|
{
|
||||||
builder.UseContentRoot(System.IO.Directory.GetCurrentDirectory());
|
builder.UseContentRoot(Directory.GetCurrentDirectory());
|
||||||
}
|
}
|
||||||
|
|
||||||
_host = builder
|
_host = builder
|
||||||
|
|||||||
@@ -46,10 +46,7 @@ namespace WireMock.Owin
|
|||||||
[PublicAPI]
|
[PublicAPI]
|
||||||
public Task StartAsync()
|
public Task StartAsync()
|
||||||
{
|
{
|
||||||
return Task.Run(() =>
|
return Task.Run(StartServers, _cts.Token);
|
||||||
{
|
|
||||||
StartServers();
|
|
||||||
}, _cts.Token);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[PublicAPI]
|
[PublicAPI]
|
||||||
|
|||||||
Reference in New Issue
Block a user