Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change nuget to package reference for WireMock.Net.Console.Net472.Cla… #804

Merged
merged 1 commit into from
Sep 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions examples/WireMock.Net.Console.Net452.Classic/MainApp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Newtonsoft.Json;
using WireMock.Logging;
using WireMock.Matchers;
using WireMock.Models;
using WireMock.RequestBuilders;
using WireMock.ResponseBuilders;
using WireMock.Server;
Expand Down Expand Up @@ -576,6 +577,38 @@ public static void Run()
};
}));

server.Given(Request.Create().WithPath(new WildcardMatcher("/multi-webhook", true)).UsingPost())
.WithWebhook(new Webhook
{
Request = new WebhookRequest
{
Url = "https://any:12345/foo1",
Method = "post",
BodyData = new BodyData
{
BodyAsString = "OK 1!", DetectedBodyType = BodyType.String
},
Delay = 1000
}
})
.WithWebhook(new Webhook
{
Request = new WebhookRequest
{
Url = "https://localhost:12345/foo2",
Method = "post",
BodyData = new BodyData
{
BodyAsString = "OK 2!",
DetectedBodyType = BodyType.String
},
MinimumRandomDelay = 3000,
MaximumRandomDelay = 7000
}
})
.WithWebhookFireAndForget(true)
.RespondWith(Response.Create().WithBody("a-response"));

System.Console.WriteLine(JsonConvert.SerializeObject(server.MappingModels, Formatting.Indented));

System.Console.WriteLine("Press any key to stop the server");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -342,15 +342,6 @@
<Reference Include="TinyMapper, Version=3.0.1.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\..\packages\TinyMapper.3.0.3\lib\net40\TinyMapper.dll</HintPath>
</Reference>
<Reference Include="WireMock.Net, Version=1.5.3.0, Culture=neutral, PublicKeyToken=c8d65537854e1f03, processorArchitecture=MSIL">
<HintPath>..\..\packages\WireMock.Net.1.5.3\lib\net461\WireMock.Net.dll</HintPath>
</Reference>
<Reference Include="WireMock.Net.Abstractions, Version=1.5.3.0, Culture=neutral, PublicKeyToken=c8d65537854e1f03, processorArchitecture=MSIL">
<HintPath>..\..\packages\WireMock.Net.Abstractions.1.5.3\lib\net451\WireMock.Net.Abstractions.dll</HintPath>
</Reference>
<Reference Include="WireMock.Org.Abstractions, Version=1.5.3.0, Culture=neutral, PublicKeyToken=c8d65537854e1f03, processorArchitecture=MSIL">
<HintPath>..\..\packages\WireMock.Org.Abstractions.1.5.3\lib\net45\WireMock.Org.Abstractions.dll</HintPath>
</Reference>
<Reference Include="XPath2, Version=1.1.3.0, Culture=neutral, PublicKeyToken=463c6d7fb740c7e5, processorArchitecture=MSIL">
<HintPath>..\..\packages\XPath2.1.1.3\lib\net452\XPath2.dll</HintPath>
</Reference>
Expand Down Expand Up @@ -388,6 +379,16 @@
<None Include="App.config" />
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\WireMock.Net.Abstractions\WireMock.Net.Abstractions.csproj">
<Project>{b6269aac-170a-4346-8b9a-579ded3d9a94}</Project>
<Name>WireMock.Net.Abstractions</Name>
</ProjectReference>
<ProjectReference Include="..\..\src\WireMock.Net\WireMock.Net.csproj">
<Project>{d3804228-91f4-4502-9595-39584e5a01ad}</Project>
<Name>WireMock.Net</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
Expand Down
3 changes: 0 additions & 3 deletions examples/WireMock.Net.Console.Net472.Classic/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,6 @@
<package id="System.Threading.Tasks.Extensions" version="4.5.1" targetFramework="net472" />
<package id="System.ValueTuple" version="4.5.0" targetFramework="net472" />
<package id="TinyMapper" version="3.0.3" targetFramework="net472" />
<package id="WireMock.Net" version="1.5.3" targetFramework="net472" />
<package id="WireMock.Net.Abstractions" version="1.5.3" targetFramework="net472" />
<package id="WireMock.Org.Abstractions" version="1.5.3" targetFramework="net472" />
<package id="XPath2" version="1.1.3" targetFramework="net472" />
<package id="XPath2.Extensions" version="1.1.3" targetFramework="net472" />
</packages>
5 changes: 5 additions & 0 deletions src/WireMock.Net.Abstractions/Admin/Mappings/MappingModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,9 @@ public class MappingModel
/// The Webhooks.
/// </summary>
public WebhookModel[]? Webhooks { get; set; }

/// <summary>
/// Fire and forget for webhooks.
/// </summary>
public bool? UseWebhooksFireAndForget { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,6 @@ public class WebhookRequestModel
/// </summary>
public string? TransformerReplaceNodeOptions { get; set; }

/// <summary>
/// Use Fire and Forget (do not wait on result). Default value is false.
/// </summary>
public bool? UseFireAndForget { get; set; }

/// <summary>
/// Gets or sets the delay in milliseconds.
/// </summary>
Expand Down
5 changes: 0 additions & 5 deletions src/WireMock.Net.Abstractions/Models/IWebhookRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,6 @@ public interface IWebhookRequest
/// </summary>
ReplaceNodeOptions TransformerReplaceNodeOptions { get; set; }

/// <summary>
/// Use Fire and Forget (do not wait on result). Default value is false.
/// </summary>
bool? UseFireAndForget { get; set; }

/// <summary>
/// Gets or sets the delay in milliseconds.
/// </summary>
Expand Down
25 changes: 0 additions & 25 deletions src/WireMock.Net/Http/WebhookSender.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,34 +102,9 @@ IResponseMessage originalResponseMessage
// Call the URL
var sendTask = client.SendAsync(httpRequestMessage);

if (webhookRequest.UseFireAndForget == true)
{
try
{
FireAndForget(sendTask);
}
catch
{
// Ignore
}
return HttpResponseMessageOk;
}

return await SendAsync(sendTask);
}

private static async void FireAndForget(Task sendTask)
{
try
{
await sendTask;
}
catch
{
// Ignore
}
}

private static async Task<HttpResponseMessage> SendAsync(Task<HttpResponseMessage> sendTask)
{
return await sendTask;
Expand Down
5 changes: 5 additions & 0 deletions src/WireMock.Net/IMapping.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,11 @@ public interface IMapping
/// </summary>
IWebhook[]? Webhooks { get; }

/// <summary>
///
/// </summary>
public bool? UseWebhooksFireAndForget { get; set; }

/// <summary>
/// ProvideResponseAsync
/// </summary>
Expand Down
6 changes: 6 additions & 0 deletions src/WireMock.Net/Mapping.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ public class Mapping : IMapping
/// <inheritdoc />
public IWebhook[]? Webhooks { get; }

/// <inheritdoc />
public bool? UseWebhooksFireAndForget { get; set; }

/// <inheritdoc />
public ITimeSettings? TimeSettings { get; }

Expand All @@ -82,6 +85,7 @@ public class Mapping : IMapping
/// <param name="nextState">The next state which will occur after the current mapping execution. [Optional]</param>
/// <param name="stateTimes">Only when the current state is executed this number, the next state which will occur. [Optional]</param>
/// <param name="webhooks">The Webhooks. [Optional]</param>
/// <param name="useWebhooksFireAndForget">Use Fire and Forget for any defined webhooks</param>
/// <param name="timeSettings">The TimeSettings. [Optional]</param>
public Mapping(
Guid guid,
Expand All @@ -97,6 +101,7 @@ public Mapping(
string? nextState,
int? stateTimes,
IWebhook[]? webhooks,
bool? useWebhooksFireAndForget,
ITimeSettings? timeSettings)
{
Guid = guid;
Expand All @@ -112,6 +117,7 @@ public Mapping(
NextState = nextState;
StateTimes = stateTimes;
Webhooks = webhooks;
UseWebhooksFireAndForget = useWebhooksFireAndForget;
TimeSettings = timeSettings;
}

Expand Down
3 changes: 0 additions & 3 deletions src/WireMock.Net/Models/WebhookRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,6 @@ public class WebhookRequest : IWebhookRequest
/// <inheritdoc />
public ReplaceNodeOptions TransformerReplaceNodeOptions { get; set; }

/// <inheritdoc />
public bool? UseFireAndForget { get; set; }

/// <inheritdoc />
public int? Delay { get; set; }

Expand Down
25 changes: 24 additions & 1 deletion src/WireMock.Net/Owin/WireMockMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
using WireMock.ResponseBuilders;
using WireMock.Settings;
using System.Collections.Generic;
using System.Net.Http;
using WireMock.Models;
using WireMock.Org.Abstractions;
#if !USE_ASPNETCORE
using IContext = Microsoft.Owin.IOwinContext;
using OwinMiddleware = Microsoft.Owin.OwinMiddleware;
Expand Down Expand Up @@ -223,7 +226,27 @@ private async Task SendToWebhooksAsync(IMapping mapping, IRequestMessage request
});
}

await Task.WhenAll(tasks.Select(async task => await task.Invoke()));
await FireWebhooks(tasks, mapping.UseWebhooksFireAndForget ?? false);
}

private async Task FireWebhooks(List<Func<Task>> sendTasks, bool fireAndForget = false)
{
if (fireAndForget == true)
{
try
{
// Do not await
await Task.WhenAll(sendTasks.Select(async task => task.Invoke()));
}
catch
{
// Ignore
}
}
else
{
await Task.WhenAll(sendTasks.Select(async task => await task.Invoke()));
}
}

private void UpdateScenarioState(IMapping mapping)
Expand Down
1 change: 1 addition & 0 deletions src/WireMock.Net/Proxy/ProxyHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ private IMapping ToMapping(ProxyAndRecordSettings proxyAndRecordSettings, IReque
nextState: null,
stateTimes: null,
webhooks: null,
useWebhooksFireAndForget: null,
timeSettings: null
);
}
Expand Down
1 change: 1 addition & 0 deletions src/WireMock.Net/Serialization/MappingConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public MappingModel ToMappingModel(IMapping mapping)
TimeSettings = TimeSettingsMapper.Map(mapping.TimeSettings),
Title = mapping.Title,
Description = mapping.Description,
UseWebhooksFireAndForget = mapping.UseWebhooksFireAndForget,
Priority = mapping.Priority != 0 ? mapping.Priority : null,
Scenario = mapping.Scenario,
WhenStateIs = mapping.ExecutionConditionState,
Expand Down
2 changes: 0 additions & 2 deletions src/WireMock.Net/Serialization/WebhookMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ public static IWebhook Map(WebhookModel model)
{
Url = model.Request.Url,
Method = model.Request.Method,
UseFireAndForget = model.Request.UseFireAndForget,
Delay = model.Request.Delay,
MinimumRandomDelay = model.Request.MinimumRandomDelay,
MaximumRandomDelay = model.Request.MaximumRandomDelay,
Expand Down Expand Up @@ -87,7 +86,6 @@ public static WebhookModel Map(IWebhook webhook)
UseTransformer = webhook.Request.UseTransformer,
TransformerType = webhook.Request.UseTransformer == true ? webhook.Request.TransformerType.ToString() : null,
TransformerReplaceNodeOptions = webhook.Request.TransformerReplaceNodeOptions.ToString(),
UseFireAndForget = webhook.Request.UseFireAndForget,
Delay = webhook.Request.Delay,
MinimumRandomDelay = webhook.Request.MinimumRandomDelay,
MaximumRandomDelay = webhook.Request.MaximumRandomDelay,
Expand Down
7 changes: 7 additions & 0 deletions src/WireMock.Net/Server/IRespondWithAProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,13 @@ public interface IRespondWithAProvider
/// <returns>The <see cref="IRespondWithAProvider"/>.</returns>
IRespondWithAProvider WithWebhook(params IWebhook[] webhooks);

/// <summary>
/// Support FireAndForget for any configured Webhooks
/// </summary>
/// <param name="UseWebhooksFireAndForget"></param>
/// <returns></returns>
IRespondWithAProvider WithWebhookFireAndForget(bool UseWebhooksFireAndForget);

/// <summary>
/// Add a Webhook to call after the response has been generated.
/// </summary>
Expand Down
11 changes: 10 additions & 1 deletion src/WireMock.Net/Server/RespondWithAProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ internal class RespondWithAProvider : IRespondWithAProvider
private readonly WireMockServerSettings _settings;
private readonly bool _saveToFile;

private bool _useWebhookFireAndForget = false;

public Guid Guid { get; private set; } = Guid.NewGuid();

public IWebhook[]? Webhooks { get; private set; }
Expand Down Expand Up @@ -57,7 +59,7 @@ public RespondWithAProvider(RegistrationCallback registrationCallback, IRequestM
/// <param name="provider">The provider.</param>
public void RespondWith(IResponseProvider provider)
{
_registrationCallback(new Mapping(Guid, _title, _description, _path, _settings, _requestMatcher, provider, _priority, _scenario, _executionConditionState, _nextState, _timesInSameState, Webhooks, TimeSettings), _saveToFile);
_registrationCallback(new Mapping(Guid, _title, _description, _path, _settings, _requestMatcher, provider, _priority, _scenario, _executionConditionState, _nextState, _timesInSameState, Webhooks, _useWebhookFireAndForget, TimeSettings), _saveToFile);
}

/// <inheritdoc />
Expand Down Expand Up @@ -233,6 +235,13 @@ public IRespondWithAProvider WithWebhook(
return this;
}

public IRespondWithAProvider WithWebhookFireAndForget(bool useWebhooksFireAndForget)
{
_useWebhookFireAndForget = useWebhooksFireAndForget;

return this;
}

private static IWebhook InitWebhook(
string url,
string method,
Expand Down
4 changes: 2 additions & 2 deletions test/WireMock.Net.Tests/Owin/WireMockMiddlewareTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ public async Task WireMockMiddleware_Invoke_Mapping_Has_ProxyAndRecordSettings_A
_mappingMock.SetupGet(m => m.Provider).Returns(responseBuilder);
_mappingMock.SetupGet(m => m.Settings).Returns(settings);

var newMappingFromProxy = new Mapping(Guid.NewGuid(), string.Empty, string.Empty, null, settings, Request.Create(), Response.Create(), 0, null, null, null, null, null, null);
var newMappingFromProxy = new Mapping(Guid.NewGuid(), string.Empty, string.Empty, null, settings, Request.Create(), Response.Create(), 0, null, null, null, null, null, false, null);
_mappingMock.Setup(m => m.ProvideResponseAsync(It.IsAny<RequestMessage>())).ReturnsAsync((new ResponseMessage(), newMappingFromProxy));

var requestBuilder = Request.Create().UsingAnyMethod();
Expand Down Expand Up @@ -230,7 +230,7 @@ public async Task WireMockMiddleware_Invoke_Mapping_Has_ProxyAndRecordSettings_A
_mappingMock.SetupGet(m => m.Provider).Returns(responseBuilder);
_mappingMock.SetupGet(m => m.Settings).Returns(settings);

var newMappingFromProxy = new Mapping(Guid.NewGuid(), "my-title", "my-description", null, settings, Request.Create(), Response.Create(), 0, null, null, null, null, null, null);
var newMappingFromProxy = new Mapping(Guid.NewGuid(), "my-title", "my-description", null, settings, Request.Create(), Response.Create(), 0, null, null, null, null, null, false, null);
_mappingMock.Setup(m => m.ProvideResponseAsync(It.IsAny<RequestMessage>())).ReturnsAsync((new ResponseMessage(), newMappingFromProxy));

var requestBuilder = Request.Create().UsingAnyMethod();
Expand Down
Loading