Handling wrong date format from XmlRpc clients

http://orchard.codeplex.com/workitem/16623

--HG--
branch : dev
This commit is contained in:
Sebastien Ros
2010-11-16 15:14:03 -08:00
parent 8150b36dde
commit 50da9410f5
2 changed files with 21 additions and 2 deletions

View File

@@ -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(@"
<struct>
<member><name>seven</name><value><dateTime.iso8601>FOO</dateTime.iso8601></value></member>
</struct>");
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(@"

View File

@@ -22,7 +22,13 @@ namespace Orchard.Core.XmlRpc.Services {
{"boolean", x=>new XRpcData<bool> { Value = ((string)x=="1") }},
{"string", x=>new XRpcData<string> { Value = (string)x }},
{"double", x=>new XRpcData<double> { Value = (double)x }},
{"dateTime.iso8601", x=>new XRpcData<DateTime> { 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<DateTime> {Value = parsedDateTime};
}},
{"base64", x=>new XRpcData<byte[]> { Value = Convert.FromBase64String((string)x) }},
{"struct", x=>XRpcData.For(Map<XRpcStruct>(x))} ,
{"array", x=>XRpcData.For(Map<XRpcArray>(x))} ,
@@ -46,7 +52,7 @@ namespace Orchard.Core.XmlRpc.Services {
XRpcMethodCall IMapper<XElement, XRpcMethodCall>.Map(XElement source) {
return new XRpcMethodCall {
MethodName = (string)source.Element("methodName"),
Params = source.Elements("params").Elements("param").Select(x => Map<XRpcData>(x)).ToList()
Params = source.Elements("params").Elements("param").Select(Map<XRpcData>).ToList()
};
}