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

ICallbackResponseBuilder + added more unit-tests #101

Merged
merged 2 commits into from
Mar 2, 2018
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
62 changes: 0 additions & 62 deletions src/WireMock.Net/DynamicResponseProvider.cs

This file was deleted.

1 change: 1 addition & 0 deletions src/WireMock.Net/Mapping.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Threading.Tasks;
using JetBrains.Annotations;
using WireMock.Matchers.Request;
using WireMock.ResponseProviders;

namespace WireMock
{
Expand Down
19 changes: 19 additions & 0 deletions src/WireMock.Net/ResponseBuilders/ICallbackResponseBuilder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System;
using JetBrains.Annotations;
using WireMock.ResponseProviders;

namespace WireMock.ResponseBuilders
{
/// <summary>
/// The CallbackResponseBuilder interface.
/// </summary>
public interface ICallbackResponseBuilder : IResponseProvider
{
/// <summary>
/// The callback builder
/// </summary>
/// <returns>The <see cref="IResponseBuilder"/>.</returns>
[PublicAPI]
IResponseBuilder WithCallback([NotNull] Func<RequestMessage, ResponseMessage> callbackHandler);
}
}
2 changes: 1 addition & 1 deletion src/WireMock.Net/ResponseBuilders/IDelayResponseBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace WireMock.ResponseBuilders
/// <summary>
/// The DelayResponseBuilder interface.
/// </summary>
public interface IDelayResponseBuilder : IResponseProvider
public interface IDelayResponseBuilder : ICallbackResponseBuilder
{
/// <summary>
/// The with delay.
Expand Down
23 changes: 20 additions & 3 deletions src/WireMock.Net/ResponseBuilders/Response.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,13 @@ public class Response : IResponseBuilder
/// <summary>
/// Gets the response message.
/// </summary>
/// <value>
/// The response message.
/// </value>
public ResponseMessage ResponseMessage { get; }

/// <summary>
/// A delegate to execute to generate the response
/// </summary>
public Func<RequestMessage, ResponseMessage> Callback { get; private set; }

/// <summary>
/// Creates this instance.
/// </summary>
Expand Down Expand Up @@ -308,6 +310,16 @@ public IResponseBuilder WithProxy(IProxyAndRecordSettings settings)
return WithProxy(settings.Url, settings.ClientX509Certificate2ThumbprintOrSubjectName);
}

/// <inheritdoc cref="ICallbackResponseBuilder.WithCallback"/>
public IResponseBuilder WithCallback(Func<RequestMessage, ResponseMessage> callbackHandler)
{
Check.NotNull(callbackHandler, nameof(callbackHandler));

Callback = callbackHandler;

return this;
}

/// <summary>
/// The provide response.
/// </summary>
Expand Down Expand Up @@ -336,6 +348,11 @@ public async Task<ResponseMessage> ProvideResponseAsync(RequestMessage requestMe
return ResponseMessageTransformer.Transform(requestMessage, ResponseMessage);
}

if (Callback != null)
{
return Callback(requestMessage);
}

return ResponseMessage;
}
}
Expand Down
24 changes: 24 additions & 0 deletions src/WireMock.Net/ResponseProviders/DynamicAsyncResponseProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System;
using System.Threading.Tasks;
using JetBrains.Annotations;
using WireMock.Validation;

namespace WireMock.ResponseProviders
{
internal class DynamicAsyncResponseProvider : IResponseProvider
{
private readonly Func<RequestMessage, Task<ResponseMessage>> _responseMessageFunc;

public DynamicAsyncResponseProvider([NotNull] Func<RequestMessage, Task<ResponseMessage>> responseMessageFunc)
{
Check.NotNull(responseMessageFunc, nameof(responseMessageFunc));

_responseMessageFunc = responseMessageFunc;
}

public Task<ResponseMessage> ProvideResponseAsync(RequestMessage requestMessage)
{
return _responseMessageFunc(requestMessage);
}
}
}
24 changes: 24 additions & 0 deletions src/WireMock.Net/ResponseProviders/DynamicResponseProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System;
using System.Threading.Tasks;
using JetBrains.Annotations;
using WireMock.Validation;

namespace WireMock.ResponseProviders
{
internal class DynamicResponseProvider : IResponseProvider
{
private readonly Func<RequestMessage, ResponseMessage> _responseMessageFunc;

public DynamicResponseProvider([NotNull] Func<RequestMessage, ResponseMessage> responseMessageFunc)
{
Check.NotNull(responseMessageFunc, nameof(responseMessageFunc));

_responseMessageFunc = responseMessageFunc;
}

public Task<ResponseMessage> ProvideResponseAsync(RequestMessage requestMessage)
{
return Task.FromResult(_responseMessageFunc(requestMessage));
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System.Threading.Tasks;
using JetBrains.Annotations;

namespace WireMock
namespace WireMock.ResponseProviders
{
/// <summary>
/// The Response Provider interface.
Expand Down
28 changes: 28 additions & 0 deletions src/WireMock.Net/ResponseProviders/ProxyAsyncResponseProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System;
using System.Threading.Tasks;
using JetBrains.Annotations;
using WireMock.Settings;
using WireMock.Validation;

namespace WireMock.ResponseProviders
{
internal class ProxyAsyncResponseProvider : IResponseProvider
{
private readonly Func<RequestMessage, IProxyAndRecordSettings, Task<ResponseMessage>> _responseMessageFunc;
private readonly IProxyAndRecordSettings _settings;

public ProxyAsyncResponseProvider([NotNull] Func<RequestMessage, IProxyAndRecordSettings, Task<ResponseMessage>> responseMessageFunc, [NotNull] IProxyAndRecordSettings settings)
{
Check.NotNull(responseMessageFunc, nameof(responseMessageFunc));
Check.NotNull(settings, nameof(settings));

_responseMessageFunc = responseMessageFunc;
_settings = settings;
}

public Task<ResponseMessage> ProvideResponseAsync(RequestMessage requestMessage)
{
return _responseMessageFunc(requestMessage, _settings);
}
}
}
1 change: 1 addition & 0 deletions src/WireMock.Net/Server/FluentMockServer.Admin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
using WireMock.Matchers.Request;
using WireMock.RequestBuilders;
using WireMock.ResponseBuilders;
using WireMock.ResponseProviders;
using WireMock.Serialization;
using WireMock.Settings;
using WireMock.Util;
Expand Down
1 change: 1 addition & 0 deletions src/WireMock.Net/Server/FluentMockServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using WireMock.Settings;
using WireMock.Validation;
using WireMock.Owin;
using WireMock.ResponseProviders;

namespace WireMock.Server
{
Expand Down
1 change: 1 addition & 0 deletions src/WireMock.Net/Server/IRespondWithAProvider.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using WireMock.ResponseProviders;

namespace WireMock.Server
{
Expand Down
1 change: 1 addition & 0 deletions src/WireMock.Net/Server/RespondWithAProvider.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using WireMock.Matchers.Request;
using WireMock.ResponseProviders;

namespace WireMock.Server
{
Expand Down
35 changes: 32 additions & 3 deletions test/WireMock.Net.Tests/FluentMockServerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,12 @@ public void FluentMockServer_ReadStaticMappings()
}

[Fact]
public void FluentMockServer_Admin_Mappings_Get()
public void FluentMockServer_Admin_Mappings_WithGuid_Get()
{
Guid guid = Guid.Parse("90356dba-b36c-469a-a17e-669cd84f1f05");
_server = FluentMockServer.Start();

_server.Given(Request.Create().WithPath("/foo1").UsingGet())
.WithGuid(guid)
_server.Given(Request.Create().WithPath("/foo1").UsingGet()).WithGuid(guid)
.RespondWith(Response.Create().WithStatusCode(201).WithBody("1"));

_server.Given(Request.Create().WithPath("/foo2").UsingGet())
Expand All @@ -105,6 +104,19 @@ public void FluentMockServer_Admin_Mappings_Get()
Check.That(mappings).HasSize(2);
}

[Fact]
public void FluentMockServer_Admin_Mappings_WithGuidAsString_Get()
{
string guid = "90356dba-b36c-469a-a17e-669cd84f1f05";
_server = FluentMockServer.Start();

_server.Given(Request.Create().WithPath("/foo1").UsingGet()).WithGuid(guid)
.RespondWith(Response.Create().WithStatusCode(201).WithBody("1"));

var mappings = _server.Mappings.ToArray();
Check.That(mappings).HasSize(1);
}

[Fact]
public void FluentMockServer_Admin_Mappings_Add_SameGuid()
{
Expand Down Expand Up @@ -432,6 +444,23 @@ public async Task FluentMockServer_Logging_SetMaxRequestLogCount()
Check.That(requestLoggedB.RequestMessage.Path).EndsWith("/foo3");
}

[Fact]
public async Task FluentMockServer_Should_respond_to_request_callback()
{
// Assign
_server = FluentMockServer.Start();

_server
.Given(Request.Create().WithPath("/foo").UsingGet())
.RespondWith(Response.Create().WithCallback(req => new ResponseMessage { Body = req.Path + "Bar" }));

// Act
string response = await new HttpClient().GetStringAsync("http://localhost:" + _server.Ports[0] + "/foo");

// Assert
Check.That(response).IsEqualTo("/fooBar");
}

public void Dispose()
{
_server?.Stop();
Expand Down
23 changes: 23 additions & 0 deletions test/WireMock.Net.Tests/Matchers/ExactObjectMatcherTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using NFluent;
using WireMock.Matchers;
using Xunit;

namespace WireMock.Net.Tests.Matchers
{
public class ExactObjectMatcherTests
{
[Fact]
public void ExactObjectMatcher_GetName()
{
// Assign
object obj = 1;

// Act
var matcher = new ExactObjectMatcher(obj);
string name = matcher.GetName();

// Assert
Check.That(name).Equals("ExactObjectMatcher");
}
}
}
Loading