Click here to Skip to main content
15,885,244 members
Articles / Programming Languages / C#

Harvesting Web Content into MHTML Archive

Rate me:
Please Sign up or sign in to vote.
4.69/5 (14 votes)
18 Feb 20068 min read 61.7K   729   49  
Capture and archive web resources by saving them in RFC-2557 (MHTML) compliant format. This library includes a framework for augmenting the capture process and allowing programmatic control, from downloading through saving. A replacement for CDOSYS/CDONTS.
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");
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Software Developer (Senior)
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions