Skip to content

Commit

Permalink
start of outbound rule match on regular expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
iamandycohen committed Dec 30, 2014
1 parent ac2c8ba commit 47fe4fd
Show file tree
Hide file tree
Showing 7 changed files with 326 additions and 4 deletions.
27 changes: 27 additions & 0 deletions Hi.UrlRewrite.Tests/Hi.UrlRewrite.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
<ReferencePath>$(ProgramFiles)\Common Files\microsoft shared\VSTT\$(VisualStudioVersion)\UITestExtensionPackages</ReferencePath>
<IsCodedUITest>False</IsCodedUITest>
<TestProjectType>UnitTest</TestProjectType>
<SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">..\</SolutionDir>
<RestorePackages>true</RestorePackages>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
Expand All @@ -35,6 +37,9 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="Moq">
<HintPath>..\packages\Moq.4.2.1409.1722\lib\net40\Moq.dll</HintPath>
</Reference>
<Reference Include="Sitecore.Kernel, Version=6.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\Assemblies\Sitecore.Kernel.dll</HintPath>
Expand All @@ -59,6 +64,12 @@
</Choose>
<ItemGroup>
<Compile Include="InboundRulesData.cs" />
<Compile Include="OutboundRewriterTestData.Designer.cs">
<AutoGen>True</AutoGen>
<DesignTime>True</DesignTime>
<DependentUpon>OutboundRewriterTestData.resx</DependentUpon>
</Compile>
<Compile Include="OutboundRewriterTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="RewriteHelperTests.cs" />
<Compile Include="UrlRewriterTests.cs" />
Expand All @@ -69,6 +80,15 @@
<Name>Hi.UrlRewrite</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="OutboundRewriterTestData.resx">
<Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>OutboundRewriterTestData.Designer.cs</LastGenOutput>
</EmbeddedResource>
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<Choose>
<When Condition="'$(VisualStudioVersion)' == '10.0' And '$(IsCodedUITest)' == 'True'">
<ItemGroup>
Expand All @@ -89,6 +109,13 @@
</Choose>
<Import Project="$(VSToolsPath)\TeamTest\Microsoft.TestTools.targets" Condition="Exists('$(VSToolsPath)\TeamTest\Microsoft.TestTools.targets')" />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="$(SolutionDir)\.nuget\NuGet.targets" Condition="Exists('$(SolutionDir)\.nuget\NuGet.targets')" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
<ErrorText>This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('$(SolutionDir)\.nuget\NuGet.targets')" Text="$([System.String]::Format('$(ErrorText)', '$(SolutionDir)\.nuget\NuGet.targets'))" />
</Target>
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
Expand Down
77 changes: 77 additions & 0 deletions Hi.UrlRewrite.Tests/OutboundRewriterTestData.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

159 changes: 159 additions & 0 deletions Hi.UrlRewrite.Tests/OutboundRewriterTestData.resx
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema
Version 2.0
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.
Example:
... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
<value>[base64 mime encoded serialized .NET Framework object]</value>
</data>
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>
There are any number of "resheader" rows that contain simple
name/value pairs.
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.
mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="metadata">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="name" use="required" type="xsd:string" />
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="mimetype" type="xsd:string" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="assembly">
<xsd:complexType>
<xsd:attribute name="alias" type="xsd:string" />
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<data name="ResponseString" xml:space="preserve">
<value>&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
&lt;html lang="en" xml:lang="en" xmlns="http://www.w3.org/1999/xhtml"&gt;

&lt;head&gt;&lt;title&gt;
Welcome to Sitecore
&lt;/title&gt;&lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /&gt;&lt;meta name="CODE_LANGUAGE" content="C#" /&gt;&lt;meta name="vs_defaultClientScript" content="JavaScript" /&gt;&lt;meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5" /&gt;&lt;link href="/default.css" rel="stylesheet" /&gt;&lt;/head&gt;
&lt;body&gt;
&lt;form method="post" action="/" id="mainform"&gt;
&lt;div class="aspNetHidden"&gt;
&lt;input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwULLTE2OTQ2MzEwODVkZDRnhQT/fyeOmbSjEUzemGjInk97u+hf+6XGKTsH8GL0" /&gt;
&lt;/div&gt;

&lt;div class="aspNetHidden"&gt;

&lt;input type="hidden" name="__VIEWSTATEGENERATOR" id="__VIEWSTATEGENERATOR" value="51A9F16C" /&gt;
&lt;/div&gt;
&lt;div id="MainPanel"&gt;

&lt;div id="CenterColumn"&gt;

&lt;div id="Banner"&gt;
&lt;img width="42" height="42" alt="Sitecore Logo" src="/sitecore/shell/themes/standard/Images/HomeButton2.png?w=42&amp;amp;h=42&amp;amp;as=1" /&gt;
&lt;/div&gt;
&lt;div id="Content"&gt;
&lt;div&gt;&lt;h1&gt;Andy&amp;#39;s Playground&lt;/h1&gt;&lt;div&gt;&lt;p&gt;Welcome to Andy's Playground&lt;/p&gt;
&lt;p&gt;Here is a &lt;a href="http://localhost/article/342/some-article-title" another="attribute"&gt;link&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Here is a &lt;a href='http://localhost/article/342/some-article-title'&gt;link&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Here is another&amp;nbsp;&lt;a href="http://localhost/article/346/another-article-title"&gt;link&lt;/a&gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;/p&gt;&lt;/div&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;

&lt;/div&gt;
&lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;</value>
</data>
</root>
39 changes: 39 additions & 0 deletions Hi.UrlRewrite.Tests/OutboundRewriterTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Hi.UrlRewrite.Processing;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using Hi.UrlRewrite.Entities.Match;

namespace Hi.UrlRewrite.Tests
{
[TestClass]
public class OutboundRewriterTests
{

[TestMethod]
public void TestRuleMatches()
{
// arrange
var responseString = OutboundRewriterTestData.ResponseString;
var matchScope = new Mock<IMatchScope>();
var matchTags = new List<MatchTag>
{
new MatchTag {Tag = "a", Attribute = "href"}
};
matchScope.Setup(e => e.MatchTheContentWithin).Returns(matchTags);

matchScope.Object.MatchTheContentWithin = matchTags;
System.Text.RegularExpressions.Match match;

// act
OutboundRewriter.TestRuleMatches(responseString, matchScope.Object, out match);


// assert
}
}
}
4 changes: 4 additions & 0 deletions Hi.UrlRewrite.Tests/packages.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Moq" version="4.2.1409.1722" targetFramework="net45" />
</packages>
23 changes: 19 additions & 4 deletions Hi.UrlRewrite/Processing/OutboundRewriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,19 +133,34 @@ private OutboundRuleResult ProcessRegularExpressionOutboundRule(OutboundRuleResu
return ruleResult;
}

private bool TestRuleMatches(string responseString, OutboundRule outboundRule, out Match outboundRuleMatch)
public static bool TestRuleMatches(string responseString, IMatchScope outboundRule, out Match outboundRuleMatch)
{
// TODO: test against all of the "match the content within"
string regexPattern = @"<{0}((\s+\w+(\s*=\s*(?:"".*?""|'.*?'|[^'"">\s]+))?)+\s*|\s*)\/?>";

outboundRuleMatch = new Regex(@"").Match(@"");
var matchTags = outboundRule.MatchTheContentWithin;
var regexPatternFormat = @"<{0}((\s+{1}(?:\s*=\s*(?:"".*?""|'.*?'|[^'"">\s]+))?)+\s*|\s*)\/?>";

foreach (var matchTag in matchTags)
{
var tag = matchTag.Tag;
var attribute = matchTag.Attribute;
var regexPattern = string.Format(regexPatternFormat, tag, attribute);
var regex = new Regex(regexPattern);
var matches = regex.Matches(responseString);

foreach (Match match in matches)
{
var attributes = match.Groups[1];
}
}

outboundRuleMatch = null;
return true;
}

internal PreconditionResult CheckPreconditions(HttpContextBase httpContext, List<OutboundRule> outboundRules)
{

var originalUri = httpContext.Request.Url;
Match lastConditionMatch = null;
bool isPreconditionMatch = false;

Expand Down
1 change: 1 addition & 0 deletions packages/repositories.config
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<repositories>
<repository path="..\Hi.UrlRewrite.Tests\packages.config" />
<repository path="..\Hi.UrlRewrite\packages.config" />
</repositories>

0 comments on commit 47fe4fd

Please sign in to comment.