Compare commits

...

6 Commits

Author SHA1 Message Date
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
11 changed files with 352 additions and 24 deletions

View File

@@ -1,3 +1,18 @@
# 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.14
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
|   **WireMock.Net** | [![NuGet Badge WireMock.Net](https://buildstats.info/nuget/WireMock.Net)](https://www.nuget.org/packages/WireMock.Net) |
|   **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,14 @@ 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 !!!")
);
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.14</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

@@ -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.14</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

@@ -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");
}
}
}