#20100: Fixing multi-lines parsing in WebActivityTask

Work Item: 20100
This commit is contained in:
Sebastien Ros
2013-09-11 11:29:44 -07:00
parent a228fae249
commit 13a7b70531
3 changed files with 68 additions and 3 deletions

View File

@@ -181,6 +181,7 @@
<Compile Include="Widgets\RuleEngine\UrlRuleProviderTest.cs" />
<Compile Include="Widgets\Services\WidgetsServiceTest.cs" />
<Compile Include="Widgets\WidgetsTests.cs" />
<Compile Include="Workflows\Activities\WebRequestActivityTests.cs" />
<Compile Include="XmlRpc\Controllers\LiveWriterControllerTests.cs" />
<Compile Include="XmlRpc\Controllers\HomeControllerTests.cs" />
<Compile Include="XmlRpc\Services\XmlRpcReaderTests.cs" />
@@ -279,6 +280,10 @@
<Project>{194D3CCC-1153-474D-8176-FDE8D7D0D0BD}</Project>
<Name>Orchard.Widgets</Name>
</ProjectReference>
<ProjectReference Include="..\Orchard.Web\Modules\Orchard.Workflows\Orchard.Workflows.csproj">
<Project>{7059493C-8251-4764-9C1E-2368B8B485BC}</Project>
<Name>Orchard.Workflows</Name>
</ProjectReference>
<ProjectReference Include="..\Orchard\Orchard.Framework.csproj">
<Project>{2D1D92BB-4555-4CBE-8D0E-63563D6CE4C6}</Project>
<Name>Orchard.Framework</Name>

View File

@@ -0,0 +1,37 @@
using System.Collections.Generic;
using System.Reflection;
using NUnit.Framework;
using Orchard.Workflows.Activities;
namespace Orchard.Tests.Modules.Workflows.Activities {
[TestFixture]
public class WebRequestActivityTests {
private readonly MethodInfo _parseKeyValueString = typeof(WebRequestActivity).GetMethod("ParseKeyValueString", BindingFlags.NonPublic | BindingFlags.Static);
private IEnumerable<KeyValuePair<string, string>> ParseKeyValueString(string text) {
return (IEnumerable<KeyValuePair<string, string>>)_parseKeyValueString.Invoke(null, new object[] { text });
}
[Test]
public void ParseKeyValueStringShouldRecognizeNewLinePatterns() {
Assert.That(ParseKeyValueString("a=b\nc=d\r\ne=f"), Is.EquivalentTo( new Dictionary<string, string> { {"a", "b"}, {"c", "d"}, {"e","f"}}));
}
[Test]
public void ParseKeyValueStringShouldIgnoreComments() {
Assert.That(ParseKeyValueString("a=b\n#c=d\ne=f"), Is.EquivalentTo(new Dictionary<string, string> { { "a", "b" }, { "e", "f" } }));
}
[Test]
public void ParseKeyValueStringShouldIgnoreEmptyLines() {
Assert.That(ParseKeyValueString("a=b\n\n\n\n\ne=f"), Is.EquivalentTo(new Dictionary<string, string> { { "a", "b" }, { "e", "f" } }));
}
[Test]
public void ParseKeyValueStringShouldIgnoreMalformedLines() {
Assert.That(ParseKeyValueString("a=b\nc:d\ne=f"), Is.EquivalentTo(new Dictionary<string, string> { { "a", "b" }, { "e", "f" } }));
}
}
}

View File

@@ -1,10 +1,9 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Text.RegularExpressions;
using Orchard.Localization;
using Orchard.Workflows.Models;
using Orchard.Workflows.Services;
@@ -93,7 +92,31 @@ namespace Orchard.Workflows.Activities {
}
private static IEnumerable<KeyValuePair<string, string>> ParseKeyValueString(string text) {
return Regex.Split(text, "\n\r").Select(x => x.Split(new[] { '=' })).ToDictionary(x => x[0].Trim(), x => x[1].Trim());
using (var reader = new StringReader(text)) {
string line;
while (null != (line = reader.ReadLine())) {
line = line.Trim();
// ignore empty lines
if (String.IsNullOrWhiteSpace(line)) {
continue;
}
// step comments
if (line.StartsWith("#")) {
continue;
}
var index = line.IndexOf("=", StringComparison.InvariantCulture);
if (index != -1 && index != line.Length - 1) {
var name = line.Substring(0, index);
var value = line.Substring(index + 1);
yield return new KeyValuePair<string, string>(name, value);
}
}
}
}
}
}