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

Fix JsonMatcher (parsing DateTimeOffset) #358

Merged
merged 4 commits into from
Oct 9, 2019
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
7 changes: 4 additions & 3 deletions src/WireMock.Net/Matchers/JsonMatcher.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
using System.Linq;
using JetBrains.Annotations;
using JetBrains.Annotations;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Linq;
using WireMock.Util;
using WireMock.Validation;

namespace WireMock.Matchers
Expand Down Expand Up @@ -93,7 +94,7 @@ public double IsMatch(object input)
break;

case string stringValue:
jtokenValue = JToken.Parse(stringValue);
jtokenValue = JsonUtils.Parse(stringValue);
break;

default:
Expand Down
11 changes: 6 additions & 5 deletions src/WireMock.Net/Transformers/HandleBarsJsonPath.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
using System;
using System.Linq;
using HandlebarsDotNet;
using HandlebarsDotNet;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Linq;
using WireMock.Util;
using WireMock.Validation;

namespace WireMock.Transformers
Expand Down Expand Up @@ -56,7 +57,7 @@ private static (JObject valueToProcess, string jsonpath) ParseArguments(object[]
switch (arguments[0])
{
case string jsonAsString:
valueToProcess = JObject.Parse(jsonAsString);
valueToProcess = JsonUtils.Parse(jsonAsString);
break;

case JObject jsonAsJObject:
Expand All @@ -67,7 +68,7 @@ private static (JObject valueToProcess, string jsonpath) ParseArguments(object[]
throw new NotSupportedException($"The value '{arguments[0]}' with type '{arguments[0]?.GetType()}' cannot be used in Handlebars JsonPath.");
}

return (valueToProcess, (string) arguments[1]);
return (valueToProcess, (string)arguments[1]);
}
}
}
19 changes: 18 additions & 1 deletion src/WireMock.Net/Util/JsonUtils.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
Expand All @@ -8,6 +9,22 @@ namespace WireMock.Util
{
internal static class JsonUtils
{
private static readonly JsonSerializerSettings JsonSerializerSettings = new JsonSerializerSettings
{
DateParseHandling = DateParseHandling.None
};

/// <summary>
/// Load a Newtonsoft.Json.Linq.JObject from a string that contains JSON.
/// Using : DateParseHandling = DateParseHandling.None
/// </summary>
/// <param name="json">A System.String that contains JSON.</param>
/// <returns>A Newtonsoft.Json.Linq.JObject populated from the string that contains JSON.</returns>
public static JObject Parse(string json)
{
return JsonConvert.DeserializeObject<JObject>(json, JsonSerializerSettings);
}

public static T ParseJTokenToObject<T>(object value)
{
switch (value)
Expand Down
17 changes: 17 additions & 0 deletions test/WireMock.Net.Tests/Matchers/JsonMatcherTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -192,5 +192,22 @@ public void JsonMatcher_IsMatch_JObjectAsString_RejectOnMatch()
// Assert
Assert.Equal(0.0, match);
}

[Fact]
public void JsonMatcher_IsMatch_JObjectWithDateTimeOffsetAsString()
{
// Assign
var matcher = new JsonMatcher("{ \"preferredAt\" : \"2019-11-21T10:32:53.2210009+00:00\" }");

// Act
var jobject = new JObject
{
{ "preferredAt", new JValue("2019-11-21T10:32:53.2210009+00:00") }
};
double match = matcher.IsMatch(jobject);

// Assert
Assert.Equal(1.0, match);
}
}
}