Click here to Skip to main content
15,897,371 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 62.1K   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.Text;
using Rilling.Web.Mhtml.Mime.Artifacts;
using System.IO;
using NUnit.Framework;

namespace Rilling.Web.Mhtml.Mime.Artifacts
{
	[TestFixture()]
	public class TextArtifactTest
	{
		private const string sampleArtifactPath =  "SampleData/TextSample.txt";

		private TextArtifact CreateTestArtifact()
		{
			FileStream fs = new FileStream(sampleArtifactPath, FileMode.Open, FileAccess.Read, FileShare.Read);
			TextArtifact artifact = new TextArtifact(fs);
			fs.Close();

			return (artifact);
		}

		[Test()]
		public void CreateInstance()
		{
			TextArtifact artifact = CreateTestArtifact();

			//
			// Verify that the stream contents are correct.
			//
			FileStream tmp = new FileStream(sampleArtifactPath, FileMode.Open);
			StreamReader sr1 = new StreamReader(tmp);
			StreamReader sr2 = new StreamReader(artifact.BaseStream);

			string origContent = sr1.ReadToEnd();
			string newContent = sr2.ReadToEnd();

			Assert.AreEqual(origContent.Length, 
				newContent.Length);
			Assert.AreEqual(origContent, newContent);

			tmp.Close();
		}

		[Test()]
		[ExpectedException(typeof(ArgumentNullException))]
		public void CreateInstance_NullReference()
		{
			TextArtifact artifact = new TextArtifact(null);
		}

		[Test()]
		public void EncodingProperty()
		{
			TextArtifact artifact = CreateTestArtifact();

			Encoding testValue = Encoding.UTF8;

			//
			// Verify the before and after state of the property.
			//
			Assert.IsNull(artifact.Encoding);

			artifact.Encoding = Encoding.UTF8;
			Assert.IsNotNull(artifact.Encoding);
			Assert.AreSame(testValue, artifact.Encoding);
			Assert.AreEqual("utf-8", artifact.CharacterSet);
		}

		[Test()]
		public void CharacterSetProperty()
		{
			TextArtifact artifact = CreateTestArtifact();

			string testValue = "utf-8";

			artifact.CharacterSet = testValue;
			Assert.AreEqual(testValue, artifact.CharacterSet);
			Assert.AreSame(Encoding.UTF8, artifact.Encoding);
		}

		[Test()]
		public void ToStringMethod()
		{
			TextArtifact artifact = CreateTestArtifact();

			//
			// Verify that the stream contents are correct.
			//
			FileStream tmp = new FileStream(sampleArtifactPath, FileMode.Open);
			StreamReader sr1 = new StreamReader(tmp);

			string origContent = sr1.ReadToEnd();
			string newContent = artifact.ToString();

			Assert.AreEqual(origContent, newContent);
		}
	}
}

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