using System;
using System.Xml;
using System.Collections.Generic;
using System.Text;
using NUnit.Framework;
using System.IO;
using Rilling.MhtmlLib.Media;
using Rilling.MhtmlLib.Media.Artifacts;
using Rilling.MhtmlLib.Media.Resolvers;
using Rilling.MhtmlLib.Media;
namespace Rilling.Web.Mhtml.TestHarness
{
[TestFixture()]
public class ScriptLinkResolverTest
{
string[] testVals1 = new string[]
{
"<script src=\"myscript.js\"/>",
"<script src=\"myscript.js\" xxx=\"nothing\"/>",
"<script xxx=\"nothing\" src=\"myscript.js\"/>",
"<script src='myscript.js'/>",
"<script src=myscript.js/>"
};
string[] testVals2 = new string[]
{
"<script src=\"myscript.vbs\"/>",
"<script src=\"myscript.vbs\" xxx=\"nothing\"/>",
"<script xxx=\"nothing\" src=\"myscript.vbs\"/>",
"<script src='myscript.vbs'/>",
"<script src=myscript.vbs/>"
};
ScriptLinkResolver testObj = null;
[SetUp]
public void InitTest()
{
testObj = new ScriptLinkResolver();
}
[Test]
[ExpectedException(typeof(ArgumentNullException))]
public void Call_LocateReferences_NullParam_ContentData()
{
testObj.LocateReferences(null, new Uri("http://www.rilling.net/"));
}
[Test]
[ExpectedException(typeof(ArgumentNullException))]
public void Call_LocateReferences_NullParam_BaseUrl()
{
testObj.LocateReferences(new TextArtifact(new MemoryStream()), null);
}
private void CallLocateReferencesHelper(string baseUri,
string[] testVals,
string expected)
{
foreach (string testVal in testVals)
{
//
// Prepare the artifact.
//
MemoryStream stream = new MemoryStream();
StreamWriter writer = new StreamWriter(stream);
writer.Write(testVal);
writer.Flush();
TextArtifact artifact = new TextArtifact(stream);
artifact.ContentType = ContentTypes.Html;
//
// Try to locate and validate the found link.
//
LinkIdentification[] results =
testObj.LocateReferences(artifact, new Uri(baseUri));
//
// Make sure it is as expected.
//
Assert.AreEqual(1, results.Length, "Unable to locate any links (" + testVal + ")");
Assert.AreEqual(expected, results[0].Url.ToString(), "Unable to locate any links (" + testVal + ")");
}
}
[Test]
[ExpectedException(typeof(ArgumentNullException))]
public void Call_InitializeContext_NullParam()
{
testObj.InitializeContext(null);
}
[Test]
public void Call_InitializeContext()
{
XmlDocument testVal = new XmlDocument();
testVal.LoadXml("<context includeJs=\"False\" includeVbs=\"False\"/>");
testObj.InitializeContext(testVal.FirstChild);
Assert.AreEqual(false, testObj.IncludeJSScripts);
Assert.AreEqual(false, testObj.IncludeVBScripts);
}
[Test()]
public void Call_LocateReferences_FolderBaseUri_ForJs()
{
CallLocateReferencesHelper("http://www.rilling.net/nowhere/",
testVals1,
"http://www.rilling.net/nowhere/myscript.js");
}
[Test()]
public void Call_LocateReferences_FileBaseUri_ForJs()
{
CallLocateReferencesHelper("http://www.rilling.net/nowhere",
testVals1,
"http://www.rilling.net/myscript.js");
}
[Test()]
public void Call_LocateReferences_FolderBaseUri_ForVbs()
{
CallLocateReferencesHelper("http://www.rilling.net/nowhere/",
testVals2,
"http://www.rilling.net/nowhere/myscript.vbs");
}
[Test()]
public void Call_LocateReferences_FileBaseUri_ForVbs()
{
CallLocateReferencesHelper("http://www.rilling.net/nowhere",
testVals2,
"http://www.rilling.net/myscript.vbs");
}
}
}