From 50da9410f5a27dba65286c6363329254be2f9010 Mon Sep 17 00:00:00 2001 From: Sebastien Ros Date: Tue, 16 Nov 2010 15:14:03 -0800 Subject: [PATCH] Handling wrong date format from XmlRpc clients http://orchard.codeplex.com/workitem/16623 --HG-- branch : dev --- .../XmlRpc/Services/XmlRpcReaderTests.cs | 13 +++++++++++++ .../Core/XmlRpc/Services/XmlRpcReader.cs | 10 ++++++++-- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/src/Orchard.Tests.Modules/XmlRpc/Services/XmlRpcReaderTests.cs b/src/Orchard.Tests.Modules/XmlRpc/Services/XmlRpcReaderTests.cs index 8122ba015..496bde2da 100644 --- a/src/Orchard.Tests.Modules/XmlRpc/Services/XmlRpcReaderTests.cs +++ b/src/Orchard.Tests.Modules/XmlRpc/Services/XmlRpcReaderTests.cs @@ -1,4 +1,5 @@ using System; +using System.Threading; using System.Xml.Linq; using NUnit.Framework; using Orchard.Core.XmlRpc.Models; @@ -87,6 +88,18 @@ namespace Orchard.Tests.Modules.XmlRpc.Services { } + [Test] + public void StructShouldMapDefaultDateTimeWithBadFormat() { + var source = XElement.Parse(@" + + sevenFOO +"); + + var xmlStruct = _structMapper.Map(source); + Assert.That(xmlStruct["seven"], Is.GreaterThan(DateTime.Now.AddSeconds(-1))); + Assert.That(xmlStruct["seven"], Is.LessThan(DateTime.Now.AddSeconds(1))); + } + [Test] public void ArrayShouldBringDataItemsWithCorrectType() { var source = XElement.Parse(@" diff --git a/src/Orchard.Web/Core/XmlRpc/Services/XmlRpcReader.cs b/src/Orchard.Web/Core/XmlRpc/Services/XmlRpcReader.cs index 0fed27019..0af6c37cc 100644 --- a/src/Orchard.Web/Core/XmlRpc/Services/XmlRpcReader.cs +++ b/src/Orchard.Web/Core/XmlRpc/Services/XmlRpcReader.cs @@ -22,7 +22,13 @@ namespace Orchard.Core.XmlRpc.Services { {"boolean", x=>new XRpcData { Value = ((string)x=="1") }}, {"string", x=>new XRpcData { Value = (string)x }}, {"double", x=>new XRpcData { Value = (double)x }}, - {"dateTime.iso8601", x=>new XRpcData { Value = DateTime.Parse((string)x, null, DateTimeStyles.RoundtripKind) }}, + {"dateTime.iso8601", x=> { + DateTime parsedDateTime; + if(!DateTime.TryParse(x.Value, out parsedDateTime)) { + parsedDateTime = DateTime.Now; + } + return new XRpcData {Value = parsedDateTime}; + }}, {"base64", x=>new XRpcData { Value = Convert.FromBase64String((string)x) }}, {"struct", x=>XRpcData.For(Map(x))} , {"array", x=>XRpcData.For(Map(x))} , @@ -46,7 +52,7 @@ namespace Orchard.Core.XmlRpc.Services { XRpcMethodCall IMapper.Map(XElement source) { return new XRpcMethodCall { MethodName = (string)source.Element("methodName"), - Params = source.Elements("params").Elements("param").Select(x => Map(x)).ToList() + Params = source.Elements("params").Elements("param").Select(Map).ToList() }; }