Compare commits

...

8 Commits

Author SHA1 Message Date
Stef Heyenrath
9e44f7eb0e 1.0.4.15 2018-09-04 22:53:23 +02:00
Stef Heyenrath
41fd1ef99d #198 (#199) 2018-09-04 22:50:17 +02:00
Stef Heyenrath
666e1ab1b8 1.0.4.14 2018-09-02 21:48:31 +02:00
davide-romanini
5ccb992201 196 fix: set IsStarted = true in a IApplicationLifetime.ApplicationStarted listener (#197) 2018-09-02 21:42:57 +02:00
Stef Heyenrath
2075589e98 Update README.md 2018-09-01 10:39:27 +02:00
Stef Heyenrath
077b3c0891 Update README.md 2018-09-01 10:38:48 +02:00
Stef Heyenrath
37a42dc6aa 1.0.4.13 2018-08-31 20:46:08 +02:00
Stef Heyenrath
9f17948e9f Add LinqMatcher (#195)
* LinqMatcher

* LinqMatcher : revert

* LinqMatcher
2018-08-31 20:41:42 +02:00
15 changed files with 440 additions and 43 deletions

View File

@@ -1,3 +1,26 @@
# 1.0.4.15 (04 September 2018)
- [#199](https://github.com/WireMock-Net/WireMock.Net/pull/199) - Fix for .WithBody(Func<RequestMessage, string>...) contributed by Stef Heyenrath ([StefH](https://github.com/StefH))
- [#198](https://github.com/WireMock-Net/WireMock.Net/issues/198) - Issue : creating response using .WithBody(Func<RequestMessage, string>...) and .WithStatusCode +fix
Commits: 41fd1ef99d...41fd1ef99d
# 1.0.4.14 (02 September 2018)
- [#197](https://github.com/WireMock-Net/WireMock.Net/pull/197) - Set IsStarted = true in a IApplicationLifetime.ApplicationStarted listener contributed by ([davide-romanini](https://github.com/davide-romanini)) +fix
- [#196](https://github.com/WireMock-Net/WireMock.Net/issues/196) - Issue: AspNetCoreSelfHost.IsStarted set before the server actually started for real +fix
Commits: 077b3c0891...5ccb992201
# 1.0.4.13 (31 August 2018)
- [#195](https://github.com/WireMock-Net/WireMock.Net/pull/195) - Add LinqMatcher contributed by Stef Heyenrath ([StefH](https://github.com/StefH))
Commits: 9f17948e9f...9f17948e9f
# 1.0.4.12 (23 August 2018)
- [#191](https://github.com/WireMock-Net/WireMock.Net/pull/191) - Fix ignore case logic for header-name and cookie-name contributed by Stef Heyenrath ([StefH](https://github.com/StefH))

View File

@@ -1,5 +1,5 @@
https://github.com/GitTools/GitReleaseNotes
GitReleaseNotes.exe . /OutputFile CHANGELOG.md /Version 1.0.4.12
GitReleaseNotes.exe . /OutputFile CHANGELOG.md /Version 1.0.4.15
GitReleaseNotes.exe . /OutputFile CHANGELOG.md /allTags

View File

@@ -30,26 +30,21 @@ A C# .NET version based on [mock4net](https://github.com/alexvictoor/mock4net) w
| &nbsp;&nbsp;**WireMock.Net** | [![NuGet Badge WireMock.Net](https://buildstats.info/nuget/WireMock.Net)](https://www.nuget.org/packages/WireMock.Net) |
| &nbsp;&nbsp;**WireMock.Net.StandAlone** | [![NuGet Badge WireMock.Net.StandAlone](https://buildstats.info/nuget/WireMock.Net.StandAlone)](https://www.nuget.org/packages/WireMock.Net.StandAlone) |
### Frameworks
The following frameworks are supported:
- net 4.5.1 and up (Microsoft.AspNet.WebApi.OwinSelfHost version 5.2.6)
- net 4.6.1 and up (Microsoft.AspNetCore version 2.1.2)
- netstandard 1.3 (Microsoft.AspNetCore version 1.1.7)
- netstandard 2.0 (Microsoft.AspNetCore version 2.1.2)
### Build info
To build you need:
- Microsoft .NET Framework [4.5.1 Developer Pack](https://www.microsoft.com/en-us/download/details.aspx?id=40772)
- Microsoft .NET Framework [4.5.2 Developer Pack](https://www.microsoft.com/en-us/download/details.aspx?id=42637)
- Microsoft .NET Framework [4.6 Targeting Pack](https://www.microsoft.com/en-us/download/confirmation.aspx?id=48136)
- Microsoft .NET Framework [4.6.2 Developer Pack](https://www.microsoft.com/en-us/download/confirmation.aspx?id=53321)
- .NET Core 2.0 (https://www.microsoft.com/net/download)
## Development
For the supported frameworks and build information, see [this](https://github.com/WireMock-Net/WireMock.Net/wiki/Development-Information) page.
## Stubbing
A core feature of WireMock.Net is the ability to return canned/predefined HTTP responses for requests matching criteria, see [Wiki : Stubbing & Request Matching](https://github.com/WireMock-Net/WireMock.Net/wiki/Stubbing-and-Request-Matching).
A core feature of WireMock.Net is the ability to return predefined HTTP responses for requests matching criteria.
See [Wiki : Stubbing](https://github.com/WireMock-Net/WireMock.Net/wiki/Stubbing).
## Request Matching
WireMock.Net support advanced request-matching logic, see [Wiki : Request Matching](https://github.com/WireMock-Net/WireMock.Net/wiki/Request-Matching).
## Response Templating
The response which is returned WireMock.Net can be changed using templating. This is described here [Wiki : Response Templating](https://github.com/WireMock-Net/WireMock.Net/wiki/Response-Templating).
## Admin API Reference
The WireMock admin API provides functionality to define the mappings via a http interface, see [Wiki : Admin API Reference](https://github.com/StefH/WireMock.Net/wiki/Admin-API-Reference).
The WireMock admin API provides functionality to define the mappings via a http interface see [Wiki : Admin API Reference](https://github.com/StefH/WireMock.Net/wiki/Admin-API-Reference).
## Using
WireMock.Net can be used in several ways:

View File

@@ -350,6 +350,28 @@ namespace WireMock.Net.ConsoleApplication
.WithTransformer()
);
// https://stackoverflow.com/questions/51985089/wiremock-request-matching-with-comparison-between-two-query-parameters
server
.Given(Request.Create().WithPath("/linq")
.WithParam("from", new LinqMatcher("DateTime.Parse(it) > \"2018-03-01 00:00:00\"")))
.RespondWith(Response.Create()
.WithBody("linq match !!!")
);
server
.Given(Request.Create().WithPath("/myendpoint").UsingAnyMethod())
.RespondWith(Response.Create()
.WithStatusCode(500)
.WithBody(requestMessage =>
{
string returnStr = JsonConvert.SerializeObject(new
{
Message = "Test error"
});
return returnStr;
})
);
System.Console.WriteLine("Press any key to stop the server");
System.Console.ReadKey();
server.Stop();

View File

@@ -1,9 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<Description>Lightweight StandAlone Http Mocking Server for .Net.</Description>
<AssemblyTitle>WireMock.Net.StandAlone</AssemblyTitle>
<Version>1.0.4.12</Version>
<Version>1.0.4.15</Version>
<Authors>Stef Heyenrath</Authors>
<TargetFrameworks>net451;net452;net46;netstandard1.3;netstandard2.0</TargetFrameworks>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
@@ -46,5 +45,4 @@
<ItemGroup>
<ProjectReference Include="..\WireMock.Net\WireMock.Net.csproj" />
</ItemGroup>
</Project>

View File

@@ -0,0 +1,99 @@
using System.Linq;
using System.Linq.Dynamic.Core;
using JetBrains.Annotations;
namespace WireMock.Matchers
{
/// <summary>
/// System.Linq.Dynamic.Core Expression Matcher
/// </summary>
/// <inheritdoc cref="IStringMatcher"/>
public class LinqMatcher : IStringMatcher
{
private readonly string[] _patterns;
/// <inheritdoc cref="IMatcher.MatchBehaviour"/>
public MatchBehaviour MatchBehaviour { get; }
/// <summary>
/// Initializes a new instance of the <see cref="LinqMatcher"/> class.
/// </summary>
/// <param name="pattern">The pattern.</param>
public LinqMatcher([NotNull] string pattern) : this(new[] { pattern })
{
}
/// <summary>
/// Initializes a new instance of the <see cref="LinqMatcher"/> class.
/// </summary>
/// <param name="patterns">The patterns.</param>
public LinqMatcher([NotNull] string[] patterns) : this(MatchBehaviour.AcceptOnMatch, patterns)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="LinqMatcher"/> class.
/// </summary>
/// <param name="matchBehaviour">The match behaviour.</param>
/// <param name="pattern">The pattern.</param>
public LinqMatcher(MatchBehaviour matchBehaviour, [NotNull] string pattern) : this(matchBehaviour, new[] { pattern })
{
}
/// <summary>
/// Initializes a new instance of the <see cref="LinqMatcher"/> class.
/// </summary>
/// <param name="matchBehaviour">The match behaviour.</param>
/// <param name="patterns">The patterns.</param>
public LinqMatcher(MatchBehaviour matchBehaviour, [NotNull] string[] patterns)
{
MatchBehaviour = matchBehaviour;
_patterns = patterns;
}
/// <inheritdoc cref="IStringMatcher.IsMatch"/>
public double IsMatch(string input)
{
// Convert a single input string to a Queryable string-list with 1 entry.
IQueryable queryable = new[] { input }.AsQueryable();
// Use the Any(...) method to check if the result matches
double match = MatchScores.ToScore(_patterns.Select(pattern => queryable.Any(pattern)));
return MatchBehaviourHelper.Convert(MatchBehaviour, match);
}
///// <inheritdoc cref="IObjectMatcher.IsMatch"/>
//public double IsMatch(object input)
//{
// object value;
// switch (input)
// {
// case JObject valueAsJObject:
// value = valueAsJObject.ToObject<object>();
// break;
// default:
// value = input;
// break;
// }
// // Convert a single object to a Queryable object-list with 1 entry.
// IQueryable queryable = new[] { value }.AsQueryable().Select("new (it as x)");
// // Use the Any(...) method to check if the result matches
// double match = MatchScores.ToScore(_patterns.Select(pattern => queryable.Any(pattern)));
// return MatchBehaviourHelper.Convert(MatchBehaviour, match);
//}
/// <inheritdoc cref="IStringMatcher.GetPatterns"/>
public string[] GetPatterns()
{
return _patterns;
}
/// <inheritdoc cref="IMatcher.Name"/>
public string Name => "LinqMatcher";
}
}

View File

@@ -104,7 +104,9 @@ namespace WireMock.Owin
{
try
{
IsStarted = true;
var appLifetime = (IApplicationLifetime) _host.Services.GetService(typeof(IApplicationLifetime));
appLifetime.ApplicationStarted.Register(() => IsStarted = true);
#if NETSTANDARD1_3
_logger.Info("WireMock.Net server using netstandard1.3");
#elif NETSTANDARD2_0

View File

@@ -169,7 +169,14 @@ namespace WireMock.ResponseBuilders
/// <inheritdoc cref="IBodyResponseBuilder.WithBody(Func{RequestMessage, string}, string, Encoding)"/>
public IResponseBuilder WithBody(Func<RequestMessage, string> bodyFactory, string destination = BodyDestinationFormat.SameAsSource, Encoding encoding = null)
{
return WithCallback(req => new ResponseMessage { Body = bodyFactory(req) });
Check.NotNull(bodyFactory, nameof(bodyFactory));
return WithCallback(req => new ResponseMessage
{
Body = bodyFactory(req),
BodyDestination = destination,
BodyEncoding = encoding ?? Encoding.UTF8
});
}
/// <inheritdoc cref="IBodyResponseBuilder.WithBody(byte[], string, Encoding)"/>
@@ -353,6 +360,22 @@ namespace WireMock.ResponseBuilders
await Task.Delay(Delay.Value);
}
if (Callback != null)
{
var callbackResponseMessage = Callback(requestMessage);
// Copy StatusCode from ResponseMessage
callbackResponseMessage.StatusCode = ResponseMessage.StatusCode;
// Copy Headers from ResponseMessage (if defined)
if (ResponseMessage.Headers != null)
{
callbackResponseMessage.Headers = ResponseMessage.Headers;
}
return callbackResponseMessage;
}
if (ProxyUrl != null && _httpClientForProxy != null)
{
var requestUri = new Uri(requestMessage.Url);
@@ -367,11 +390,7 @@ namespace WireMock.ResponseBuilders
return ResponseMessageTransformer.Transform(requestMessage, ResponseMessage);
}
if (Callback != null)
{
return Callback(requestMessage);
}
// Just return normal defined ResponseMessage
return ResponseMessage;
}
}

View File

@@ -26,6 +26,9 @@ namespace WireMock.Serialization
switch (matcherName)
{
case "LinqMatcher":
return new LinqMatcher(matchBehaviour, stringPatterns);
case "ExactMatcher":
return new ExactMatcher(matchBehaviour, stringPatterns);

View File

@@ -1,9 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<Description>Lightweight Http Mocking Server for .Net, inspired by WireMock from the Java landscape.</Description>
<AssemblyTitle>WireMock.Net</AssemblyTitle>
<Version>1.0.4.12</Version>
<Version>1.0.4.15</Version>
<Authors>Stef Heyenrath</Authors>
<TargetFrameworks>net451;net452;net46;net461;netstandard1.3;netstandard2.0</TargetFrameworks>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
@@ -50,6 +49,7 @@
<PackageReference Include="System.Net.Http" Version="4.3.3" />
<PackageReference Include="RestEase" Version="1.4.4" />
<PackageReference Include="MimeKitLite" Version="2.0.1" />
<PackageReference Include="System.Linq.Dynamic.Core" Version="1.0.8.17" />
</ItemGroup>
<ItemGroup Condition=" '$(TargetFramework)' == 'net451' ">
@@ -99,5 +99,4 @@
<PackageReference Include="Microsoft.AspNetCore" Version="2.1.2" />
<PackageReference Include="XPath2" Version="1.0.5.1" />
</ItemGroup>
</Project>

View File

@@ -48,15 +48,15 @@ namespace WireMock.Net.Tests
[Fact]
public void FluentMockServer_Admin_StartStop()
{
var server1 = FluentMockServer.Start("http://localhost:9091");
var server1 = FluentMockServer.Start("http://localhost:19091");
Check.That(server1.Urls[0]).Equals("http://localhost:9091");
Check.That(server1.Urls[0]).Equals("http://localhost:19091");
server1.Stop();
var server2 = FluentMockServer.Start("http://localhost:9091/");
var server2 = FluentMockServer.Start("http://localhost:19091/");
Check.That(server2.Urls[0]).Equals("http://localhost:9091/");
Check.That(server2.Urls[0]).Equals("http://localhost:19091/");
server2.Stop();
}

View File

@@ -106,7 +106,7 @@ namespace WireMock.Net.Tests
[Fact]
public async Task FluentMockServer_Should_respond_to_request_bodyAsCallback()
{
// given
// Assign
var _server = FluentMockServer.Start();
_server
@@ -114,14 +114,18 @@ namespace WireMock.Net.Tests
.WithPath("/foo")
.UsingGet())
.RespondWith(Response.Create()
.WithStatusCode(200)
.WithBody(req => $"{{ path: '{req.Path}' }}"));
.WithStatusCode(500)
.WithHeader("H1", "X1")
.WithBody(req => $"path: {req.Path}"));
// when
var response = await new HttpClient().GetStringAsync("http://localhost:" + _server.Ports[0] + "/foo");
// Act
var response = await new HttpClient().GetAsync("http://localhost:" + _server.Ports[0] + "/foo");
// then
Check.That(response).IsEqualTo("{ path: '/foo' }");
// Assert
string content = await response.Content.ReadAsStringAsync();
Check.That(content).IsEqualTo("path: /foo");
Check.That((int) response.StatusCode).IsEqualTo(500);
Check.That(response.Headers.GetValues("H1")).ContainsExactly("X1");
}
[Fact]

View File

@@ -0,0 +1,97 @@
using System;
using NFluent;
using WireMock.Admin.Mappings;
using WireMock.Matchers;
using WireMock.Serialization;
using Xunit;
namespace WireMock.Net.Tests
{
public class MatcherMapperTests
{
[Fact]
public void MatcherMapper_Map_MatcherModel_Null()
{
// Act
var result = MatcherMapper.Map((MatcherModel)null);
// Assert
Check.That(result).IsNull();
}
[Fact]
public void MatcherMapper_Map_MatcherModel_Exception()
{
// Assign
var model = new MatcherModel { Name = "test" };
// Act and Assert
Check.ThatCode(() => MatcherMapper.Map(model)).Throws<NotSupportedException>();
}
[Fact]
public void MatcherMapper_Map_MatcherModel_LinqMatcher_Pattern()
{
// Assign
var model = new MatcherModel
{
Name = "LinqMatcher",
Pattern = "p"
};
// Act
var matcher = MatcherMapper.Map(model) as LinqMatcher;
// Assert
Check.That(matcher).IsNotNull();
Check.That(matcher.MatchBehaviour).IsEqualTo(MatchBehaviour.AcceptOnMatch);
Check.That(matcher.GetPatterns()).ContainsExactly("p");
}
[Fact]
public void MatcherMapper_Map_MatcherModel_LinqMatcher_Patterns()
{
// Assign
var model = new MatcherModel
{
Name = "LinqMatcher",
Patterns = new[] { "p1", "p2" }
};
// Act
var matcher = MatcherMapper.Map(model) as LinqMatcher;
// Assert
Check.That(matcher).IsNotNull();
Check.That(matcher.MatchBehaviour).IsEqualTo(MatchBehaviour.AcceptOnMatch);
Check.That(matcher.GetPatterns()).Contains(new[] { "p1", "p2" });
}
[Fact]
public void MatcherMapper_Map_IMatcher_Null()
{
// Act
var result = MatcherMapper.Map((IMatcher)null);
// Assert
Check.That(result).IsNull();
}
[Fact]
public void MatcherMapper_Map_IMatcher_LinqMatcher_Pattern()
{
// Assign
var matcher = new LinqMatcher(MatchBehaviour.AcceptOnMatch, "p");
// Act
var result = MatcherMapper.Map(matcher);
// Assert
Check.That(result).IsNotNull();
Check.That(result.Name).IsEqualTo("LinqMatcher");
Check.That(result.IgnoreCase).IsNull();
Check.That(result.Pattern).IsEqualTo("p");
Check.That(result.Patterns).IsNull();
}
}
}

View File

@@ -0,0 +1,112 @@
using NFluent;
using WireMock.Matchers;
using Xunit;
namespace WireMock.Net.Tests.Matchers
{
public class LinqMatcherTests
{
[Fact]
public void LinqMatcher_For_String_SinglePattern_IsMatch_Positive()
{
// Assign
string input = "2018-08-31 13:59:59";
// Act
var matcher = new LinqMatcher("DateTime.Parse(it) > \"2018-08-01 13:50:00\"");
// Assert
Check.That(matcher.IsMatch(input)).IsEqualTo(1.0d);
}
[Fact]
public void LinqMatcher_For_String_IsMatch_Negative()
{
// Assign
string input = "2018-08-31 13:59:59";
// Act
var matcher = new LinqMatcher("DateTime.Parse(it) > \"2019-01-01 00:00:00\"");
// Assert
Check.That(matcher.IsMatch(input)).IsEqualTo(0.0d);
}
[Fact]
public void LinqMatcher_For_String_IsMatch_RejectOnMatch()
{
// Assign
string input = "2018-08-31 13:59:59";
// Act
var matcher = new LinqMatcher(MatchBehaviour.RejectOnMatch, "DateTime.Parse(it) > \"2018-08-01 13:50:00\"");
// Assert
Check.That(matcher.IsMatch(input)).IsEqualTo(0.0d);
}
//[Fact]
//public void LinqMatcher_For_Object_IsMatch()
//{
// // Assign
// var input = new
// {
// Id = 9,
// Name = "Test"
// };
// // Act
// var matcher = new LinqMatcher("Id > 1 AND Name == \"Test\"");
// double match = matcher.IsMatch(input);
// // Assert
// Assert.Equal(1.0, match);
//}
//[Fact]
//public void LinqMatcher_For_JObject_IsMatch()
//{
// // Assign
// var input = new JObject
// {
// { "Id", new JValue(9) },
// { "Name", new JValue("Test") }
// };
// // Act
// var matcher = new LinqMatcher("it.Id > 1 AND it.Name == \"Test\"");
// double match = matcher.IsMatch(input);
// // Assert
// Assert.Equal(1.0, match);
//}
[Fact]
public void LinqMatcher_GetName()
{
// Assign
var matcher = new LinqMatcher("x");
// Act
string name = matcher.Name;
// Assert
Check.That(name).Equals("LinqMatcher");
}
[Fact]
public void LinqMatcher_GetPatterns()
{
// Assign
var matcher = new LinqMatcher("x");
// Act
string[] patterns = matcher.GetPatterns();
// Assert
Check.That(patterns).ContainsExactly("x");
}
}
}

View File

@@ -1,5 +1,4 @@
using System;
using System.Text;
using System.Text;
using System.Threading.Tasks;
using NFluent;
using WireMock.Models;
@@ -150,5 +149,30 @@ namespace WireMock.Net.Tests.ResponseBuilderTests
Check.That(((dynamic)responseMessage.BodyAsJson).value).Equals(42);
Check.That(responseMessage.BodyEncoding).Equals(Encoding.ASCII);
}
[Fact]
public async Task Response_ProvideResponse_WithBody_Func()
{
// Assign
var request = new RequestMessage(new UrlDetails("http://localhost/test"), "GET", ClientIp);
var response = Response.Create()
.WithStatusCode(500)
.WithHeader("H1", "X1")
.WithHeader("H2", "X2")
.WithBody(req => $"path: {req.Path}");
// Act
var responseMessage = await response.ProvideResponseAsync(request);
// Assert
Check.That(responseMessage.Body).IsEqualTo("path: /test");
Check.That(responseMessage.BodyAsBytes).IsNull();
Check.That(responseMessage.BodyAsJson).IsNull();
Check.That(responseMessage.BodyEncoding.CodePage).Equals(Encoding.UTF8.CodePage);
Check.That(responseMessage.StatusCode).IsEqualTo(500);
Check.That(responseMessage.Headers["H1"].ToString()).IsEqualTo("X1");
Check.That(responseMessage.Headers["H2"].ToString()).IsEqualTo("X2");
}
}
}