예제 #1
0
		public void TestArgumentExceptions ()
		{
			var disposition = new ContentDisposition ();

			Assert.Throws<ArgumentNullException> (() => disposition.Disposition = null, "Setting the disposition to null value should throw.");
			Assert.Throws<ArgumentException> (() => disposition.Disposition = string.Empty, "Setting the disposition to an empty value should throw.");
			Assert.Throws<ArgumentException> (() => disposition.Disposition = "žádost", "Setting the disposition to a non-ascii value should throw.");
			Assert.Throws<ArgumentException> (() => disposition.Disposition = "two atoms", "Setting the disposition to multiple atom tokens should throw.");

			Assert.Throws<ArgumentNullException> (() => disposition.ToString (null, Encoding.UTF8, true));
			Assert.Throws<ArgumentNullException> (() => disposition.ToString (FormatOptions.Default, null, true));
		}
예제 #2
0
		public void TestDispositionParameters ()
		{
			const string expected = "Content-Disposition: attachment; filename=document.doc;\n" +
				"\tcreation-date=\"Sat, 04 Jan 1997 15:22:17 -0400\";\n" +
				"\tmodification-date=\"Thu, 04 Jan 2007 15:22:17 -0400\";\n" +
				"\tread-date=\"Wed, 04 Jan 2012 15:22:17 -0400\"; size=37001";
			var ctime = new DateTimeOffset (1997, 1, 4, 15, 22, 17, new TimeSpan (-4, 0, 0));
			var mtime = new DateTimeOffset (2007, 1, 4, 15, 22, 17, new TimeSpan (-4, 0, 0));
			var atime = new DateTimeOffset (2012, 1, 4, 15, 22, 17, new TimeSpan (-4, 0, 0));
			var disposition = new ContentDisposition ();
			var format = FormatOptions.Default.Clone ();
			const long size = 37001;
			Parameter param;
			string encoded;

			format.NewLineFormat = NewLineFormat.Unix;

			Assert.AreEqual (ContentDisposition.Attachment, disposition.Disposition, "The disposition should be 'attachment'.");
			Assert.IsTrue (disposition.IsAttachment, "IsAttachment should be true by default.");

			Assert.IsNull (disposition.FileName, "The filename should default to null.");
			Assert.IsNull (disposition.CreationDate, "The creation-date should default to null.");
			Assert.IsNull (disposition.ModificationDate, "The modification-date should default to null.");
			Assert.IsNull (disposition.ReadDate, "The read-date should default to null.");
			Assert.IsNull (disposition.Size, "The size should default to null.");

			disposition.FileName = "document.doc";
			disposition.CreationDate = ctime;
			disposition.ModificationDate = mtime;
			disposition.ReadDate = atime;
			disposition.Size = size;

			encoded = disposition.ToString (format, Encoding.UTF8, true);

			Assert.AreEqual (expected, encoded, "The encoded Content-Disposition does not match.");

			disposition = ContentDisposition.Parse (encoded.Substring ("Content-Disposition:".Length));

			Assert.AreEqual ("document.doc", disposition.FileName, "The filename parameter does not match.");
			Assert.AreEqual (ctime, disposition.CreationDate, "The creation-date parameter does not match.");
			Assert.AreEqual (mtime, disposition.ModificationDate, "The modification-date parameter does not match.");
			Assert.AreEqual (atime, disposition.ReadDate, "The read-date parameter does not match.");
			Assert.AreEqual (size, disposition.Size, "The size parameter does not match.");

			disposition.CreationDate = null;
			Assert.IsFalse (disposition.Parameters.TryGetValue ("creation-date", out param), "The creation-date parameter should have been removed.");

			disposition.ModificationDate = null;
			Assert.IsFalse (disposition.Parameters.TryGetValue ("modification-date", out param), "The modification-date parameter should have been removed.");

			disposition.ReadDate = null;
			Assert.IsFalse (disposition.Parameters.TryGetValue ("read-date", out param), "The read-date parameter should have been removed.");

			disposition.FileName = null;
			Assert.IsFalse (disposition.Parameters.TryGetValue ("filename", out param), "The filename parameter should have been removed.");

			disposition.Size = null;
			Assert.IsFalse (disposition.Parameters.TryGetValue ("size", out param), "The size parameter should have been removed.");

			disposition.IsAttachment = false;
			Assert.AreEqual (ContentDisposition.Inline, disposition.Disposition, "The disposition should be 'inline'.");
			Assert.IsFalse (disposition.IsAttachment, "IsAttachment should be false.");
		}