public void TestRemovingHeaders() { var headers = new HeaderList(); headers.Add("From", "sender@localhost"); headers.Add("To", "first@localhost"); headers.Add("To", "second@localhost"); headers.Add("To", "third@localhost"); headers.Add("To", "fourth@localhost"); headers.Add("Cc", "carbon.copy@localhost"); Assert.IsFalse(headers.IsReadOnly); Assert.IsFalse(headers.Contains(new Header(HeaderId.Received, "value"))); Assert.AreEqual(-1, headers.IndexOf(new Header(HeaderId.Received, "value"))); Assert.AreEqual(-1, headers.IndexOf("Received")); Assert.AreEqual(-1, headers.LastIndexOf(HeaderId.Received)); Assert.AreEqual(null, headers[HeaderId.Received]); Assert.IsTrue(headers.Remove("Cc")); // try removing a header that no longer exists Assert.IsFalse(headers.Remove(new Header(HeaderId.Cc, "value"))); Assert.IsFalse(headers.Remove(HeaderId.Cc)); Assert.IsFalse(headers.Remove("Cc")); // removing this will change the result of headers[HeaderId.To] Assert.AreEqual("first@localhost", headers[HeaderId.To]); Assert.IsTrue(headers.Remove(HeaderId.To)); Assert.AreEqual("second@localhost", headers[HeaderId.To]); Assert.IsTrue(headers.Remove("To")); Assert.AreEqual("third@localhost", headers[HeaderId.To]); headers.RemoveAt(headers.IndexOf("To")); Assert.AreEqual("fourth@localhost", headers[HeaderId.To]); }
public void TestArgumentExceptions() { var list = new HeaderList(); Header header; string value; using (var stream = new MemoryStream()) { Assert.Throws <ArgumentNullException> (() => HeaderList.Load(null, "filename.txt")); Assert.Throws <ArgumentNullException> (() => HeaderList.Load(ParserOptions.Default, (string)null)); Assert.Throws <ArgumentNullException> (() => HeaderList.Load(null, stream)); Assert.Throws <ArgumentNullException> (() => HeaderList.Load(ParserOptions.Default, (Stream)null)); Assert.ThrowsAsync <ArgumentNullException> (async() => await HeaderList.LoadAsync(null, "filename.txt")); Assert.ThrowsAsync <ArgumentNullException> (async() => await HeaderList.LoadAsync(ParserOptions.Default, (string)null)); Assert.ThrowsAsync <ArgumentNullException> (async() => await HeaderList.LoadAsync(null, stream)); Assert.ThrowsAsync <ArgumentNullException> (async() => await HeaderList.LoadAsync(ParserOptions.Default, (Stream)null)); } // Add Assert.Throws <ArgumentNullException> (() => list.Add(null)); Assert.Throws <ArgumentOutOfRangeException> (() => list.Add(HeaderId.Unknown, "value")); Assert.Throws <ArgumentNullException> (() => list.Add(HeaderId.AdHoc, null)); Assert.Throws <ArgumentNullException> (() => list.Add(null, "value")); Assert.Throws <ArgumentNullException> (() => list.Add("field", null)); Assert.Throws <ArgumentOutOfRangeException> (() => list.Add(HeaderId.Unknown, Encoding.UTF8, "value")); Assert.Throws <ArgumentNullException> (() => list.Add(HeaderId.AdHoc, null, "value")); Assert.Throws <ArgumentNullException> (() => list.Add(HeaderId.AdHoc, Encoding.UTF8, null)); Assert.Throws <ArgumentNullException> (() => list.Add(null, Encoding.UTF8, "value")); Assert.Throws <ArgumentNullException> (() => list.Add("field", null, "value")); Assert.Throws <ArgumentNullException> (() => list.Add("field", Encoding.UTF8, null)); // Contains Assert.Throws <ArgumentOutOfRangeException> (() => list.Contains(HeaderId.Unknown)); Assert.Throws <ArgumentNullException> (() => list.Contains((Header)null)); Assert.Throws <ArgumentNullException> (() => list.Contains((string)null)); // CopyTo Assert.Throws <ArgumentOutOfRangeException> (() => list.CopyTo(new Header[0], -1)); Assert.Throws <ArgumentNullException> (() => list.CopyTo(null, 0)); // IndexOf Assert.Throws <ArgumentOutOfRangeException> (() => list.IndexOf(HeaderId.Unknown)); Assert.Throws <ArgumentNullException> (() => list.IndexOf((Header)null)); Assert.Throws <ArgumentNullException> (() => list.IndexOf((string)null)); // Insert list.Add("field", "value"); Assert.Throws <ArgumentOutOfRangeException> (() => list.Insert(-1, new Header(HeaderId.AdHoc, "value"))); Assert.Throws <ArgumentOutOfRangeException> (() => list.Insert(-1, HeaderId.AdHoc, Encoding.UTF8, "value")); Assert.Throws <ArgumentOutOfRangeException> (() => list.Insert(-1, "field", Encoding.UTF8, "value")); Assert.Throws <ArgumentOutOfRangeException> (() => list.Insert(-1, HeaderId.AdHoc, "value")); Assert.Throws <ArgumentOutOfRangeException> (() => list.Insert(-1, "field", "value")); Assert.Throws <ArgumentOutOfRangeException> (() => list.Insert(0, HeaderId.Unknown, Encoding.UTF8, "value")); Assert.Throws <ArgumentOutOfRangeException> (() => list.Insert(0, HeaderId.Unknown, "value")); Assert.Throws <ArgumentNullException> (() => list.Insert(0, HeaderId.AdHoc, Encoding.UTF8, null)); Assert.Throws <ArgumentNullException> (() => list.Insert(0, HeaderId.AdHoc, null, "value")); Assert.Throws <ArgumentNullException> (() => list.Insert(0, HeaderId.AdHoc, null)); Assert.Throws <ArgumentNullException> (() => list.Insert(0, null, "value")); Assert.Throws <ArgumentNullException> (() => list.Insert(0, "field", null)); Assert.Throws <ArgumentNullException> (() => list.Insert(0, null)); // LastIndexOf Assert.Throws <ArgumentOutOfRangeException> (() => list.LastIndexOf(HeaderId.Unknown)); Assert.Throws <ArgumentNullException> (() => list.LastIndexOf((string)null)); // Remove Assert.Throws <ArgumentOutOfRangeException> (() => list.Remove(HeaderId.Unknown)); Assert.Throws <ArgumentNullException> (() => list.Remove((Header)null)); Assert.Throws <ArgumentNullException> (() => list.Remove((string)null)); // RemoveAll Assert.Throws <ArgumentOutOfRangeException> (() => list.RemoveAll(HeaderId.Unknown)); Assert.Throws <ArgumentNullException> (() => list.RemoveAll((string)null)); // RemoveAt Assert.Throws <ArgumentOutOfRangeException> (() => list.RemoveAt(-1)); // Replace Assert.Throws <ArgumentNullException> (() => list.Replace(null)); Assert.Throws <ArgumentOutOfRangeException> (() => list.Replace(HeaderId.Unknown, "value")); Assert.Throws <ArgumentNullException> (() => list.Replace(HeaderId.AdHoc, null)); Assert.Throws <ArgumentNullException> (() => list.Replace(null, "value")); Assert.Throws <ArgumentNullException> (() => list.Replace("field", null)); Assert.Throws <ArgumentOutOfRangeException> (() => list.Replace(HeaderId.Unknown, Encoding.UTF8, "value")); Assert.Throws <ArgumentNullException> (() => list.Replace(HeaderId.AdHoc, null, "value")); Assert.Throws <ArgumentNullException> (() => list.Replace(HeaderId.AdHoc, Encoding.UTF8, null)); Assert.Throws <ArgumentNullException> (() => list.Replace(null, Encoding.UTF8, "value")); Assert.Throws <ArgumentNullException> (() => list.Replace("field", null, "value")); Assert.Throws <ArgumentNullException> (() => list.Replace("field", Encoding.UTF8, null)); using (var stream = new MemoryStream()) { // Load Assert.Throws <ArgumentNullException> (() => HeaderList.Load(ParserOptions.Default, (Stream)null)); Assert.Throws <ArgumentNullException> (() => HeaderList.Load(ParserOptions.Default, (string)null)); Assert.Throws <ArgumentNullException> (() => HeaderList.Load(null, stream)); Assert.Throws <ArgumentNullException> (() => HeaderList.Load((Stream)null)); Assert.Throws <ArgumentNullException> (() => HeaderList.Load((string)null)); // LoadAsync Assert.ThrowsAsync <ArgumentNullException> (async() => await HeaderList.LoadAsync(ParserOptions.Default, (Stream)null)); Assert.ThrowsAsync <ArgumentNullException> (async() => await HeaderList.LoadAsync(ParserOptions.Default, (string)null)); Assert.ThrowsAsync <ArgumentNullException> (async() => await HeaderList.LoadAsync(null, stream)); Assert.ThrowsAsync <ArgumentNullException> (async() => await HeaderList.LoadAsync((Stream)null)); Assert.ThrowsAsync <ArgumentNullException> (async() => await HeaderList.LoadAsync((string)null)); // WriteTo Assert.Throws <ArgumentNullException> (() => list.WriteTo(FormatOptions.Default, null)); Assert.Throws <ArgumentNullException> (() => list.WriteTo(null, stream)); Assert.Throws <ArgumentNullException> (() => list.WriteTo(null)); // WriteToAsync Assert.ThrowsAsync <ArgumentNullException> (async() => await list.WriteToAsync(FormatOptions.Default, null)); Assert.ThrowsAsync <ArgumentNullException> (async() => await list.WriteToAsync(null, stream)); Assert.ThrowsAsync <ArgumentNullException> (async() => await list.WriteToAsync(null)); } // Indexers Assert.Throws <ArgumentOutOfRangeException> (() => list[-1] = new Header(HeaderId.AdHoc, "value")); Assert.Throws <ArgumentOutOfRangeException> (() => list[HeaderId.Unknown] = "value"); Assert.Throws <ArgumentOutOfRangeException> (() => value = list[HeaderId.Unknown]); Assert.Throws <ArgumentOutOfRangeException> (() => header = list[-1]); Assert.Throws <ArgumentNullException> (() => list[HeaderId.AdHoc] = null); Assert.Throws <ArgumentNullException> (() => value = list[null]); Assert.Throws <ArgumentNullException> (() => list[null] = "value"); Assert.Throws <ArgumentNullException> (() => list["field"] = null); Assert.Throws <ArgumentNullException> (() => list[0] = null); }
public void TestArgumentExceptions () { var list = new HeaderList (); Header header; string value; // Add Assert.Throws<ArgumentNullException> (() => list.Add (null)); Assert.Throws<ArgumentOutOfRangeException> (() => list.Add (HeaderId.Unknown, "value")); Assert.Throws<ArgumentNullException> (() => list.Add (HeaderId.AdHoc, null)); Assert.Throws<ArgumentNullException> (() => list.Add (null, "value")); Assert.Throws<ArgumentNullException> (() => list.Add ("field", null)); Assert.Throws<ArgumentOutOfRangeException> (() => list.Add (HeaderId.Unknown, Encoding.UTF8, "value")); Assert.Throws<ArgumentNullException> (() => list.Add (HeaderId.AdHoc, null, "value")); Assert.Throws<ArgumentNullException> (() => list.Add (HeaderId.AdHoc, Encoding.UTF8, null)); Assert.Throws<ArgumentNullException> (() => list.Add (null, Encoding.UTF8, "value")); Assert.Throws<ArgumentNullException> (() => list.Add ("field", null, "value")); Assert.Throws<ArgumentNullException> (() => list.Add ("field", Encoding.UTF8, null)); // Contains Assert.Throws<ArgumentOutOfRangeException> (() => list.Contains (HeaderId.Unknown)); Assert.Throws<ArgumentNullException> (() => list.Contains ((Header) null)); Assert.Throws<ArgumentNullException> (() => list.Contains ((string) null)); // CopyTo Assert.Throws<ArgumentOutOfRangeException> (() => list.CopyTo (new Header[0], -1)); Assert.Throws<ArgumentNullException> (() => list.CopyTo (null, 0)); // IndexOf Assert.Throws<ArgumentOutOfRangeException> (() => list.IndexOf (HeaderId.Unknown)); Assert.Throws<ArgumentNullException> (() => list.IndexOf ((Header) null)); Assert.Throws<ArgumentNullException> (() => list.IndexOf ((string) null)); // Insert list.Add ("field", "value"); Assert.Throws<ArgumentOutOfRangeException> (() => list.Insert (-1, new Header (HeaderId.AdHoc, "value"))); Assert.Throws<ArgumentOutOfRangeException> (() => list.Insert (-1, HeaderId.AdHoc, Encoding.UTF8, "value")); Assert.Throws<ArgumentOutOfRangeException> (() => list.Insert (-1, "field", Encoding.UTF8, "value")); Assert.Throws<ArgumentOutOfRangeException> (() => list.Insert (-1, HeaderId.AdHoc, "value")); Assert.Throws<ArgumentOutOfRangeException> (() => list.Insert (-1, "field", "value")); Assert.Throws<ArgumentOutOfRangeException> (() => list.Insert (0, HeaderId.Unknown, Encoding.UTF8, "value")); Assert.Throws<ArgumentOutOfRangeException> (() => list.Insert (0, HeaderId.Unknown, "value")); Assert.Throws<ArgumentNullException> (() => list.Insert (0, HeaderId.AdHoc, Encoding.UTF8, null)); Assert.Throws<ArgumentNullException> (() => list.Insert (0, HeaderId.AdHoc, null, "value")); Assert.Throws<ArgumentNullException> (() => list.Insert (0, HeaderId.AdHoc, null)); Assert.Throws<ArgumentNullException> (() => list.Insert (0, null, "value")); Assert.Throws<ArgumentNullException> (() => list.Insert (0, "field", null)); Assert.Throws<ArgumentNullException> (() => list.Insert (0, null)); // LastIndexOf Assert.Throws<ArgumentOutOfRangeException> (() => list.LastIndexOf (HeaderId.Unknown)); Assert.Throws<ArgumentNullException> (() => list.LastIndexOf ((string) null)); // Remove Assert.Throws<ArgumentOutOfRangeException> (() => list.Remove (HeaderId.Unknown)); Assert.Throws<ArgumentNullException> (() => list.Remove ((Header) null)); Assert.Throws<ArgumentNullException> (() => list.Remove ((string) null)); // RemoveAll Assert.Throws<ArgumentOutOfRangeException> (() => list.RemoveAll (HeaderId.Unknown)); Assert.Throws<ArgumentNullException> (() => list.RemoveAll ((string) null)); // RemoveAt Assert.Throws<ArgumentOutOfRangeException> (() => list.RemoveAt (-1)); // Replace Assert.Throws<ArgumentNullException> (() => list.Replace (null)); Assert.Throws<ArgumentOutOfRangeException> (() => list.Replace (HeaderId.Unknown, "value")); Assert.Throws<ArgumentNullException> (() => list.Replace (HeaderId.AdHoc, null)); Assert.Throws<ArgumentNullException> (() => list.Replace (null, "value")); Assert.Throws<ArgumentNullException> (() => list.Replace ("field", null)); Assert.Throws<ArgumentOutOfRangeException> (() => list.Replace (HeaderId.Unknown, Encoding.UTF8, "value")); Assert.Throws<ArgumentNullException> (() => list.Replace (HeaderId.AdHoc, null, "value")); Assert.Throws<ArgumentNullException> (() => list.Replace (HeaderId.AdHoc, Encoding.UTF8, null)); Assert.Throws<ArgumentNullException> (() => list.Replace (null, Encoding.UTF8, "value")); Assert.Throws<ArgumentNullException> (() => list.Replace ("field", null, "value")); Assert.Throws<ArgumentNullException> (() => list.Replace ("field", Encoding.UTF8, null)); using (var stream = new MemoryStream ()) { // Load Assert.Throws<ArgumentNullException> (() => HeaderList.Load (ParserOptions.Default, (Stream) null)); Assert.Throws<ArgumentNullException> (() => HeaderList.Load (ParserOptions.Default, (string) null)); Assert.Throws<ArgumentNullException> (() => HeaderList.Load (null, stream)); Assert.Throws<ArgumentNullException> (() => HeaderList.Load ((Stream) null)); Assert.Throws<ArgumentNullException> (() => HeaderList.Load ((string) null)); // WriteTo Assert.Throws<ArgumentNullException> (() => list.WriteTo (FormatOptions.Default, null)); Assert.Throws<ArgumentNullException> (() => list.WriteTo (null, stream)); Assert.Throws<ArgumentNullException> (() => list.WriteTo (null)); } // Indexers Assert.Throws<ArgumentOutOfRangeException> (() => list[-1] = new Header (HeaderId.AdHoc, "value")); Assert.Throws<ArgumentOutOfRangeException> (() => list[HeaderId.Unknown] = "value"); Assert.Throws<ArgumentOutOfRangeException> (() => value = list[HeaderId.Unknown]); Assert.Throws<ArgumentOutOfRangeException> (() => header = list[-1]); Assert.Throws<ArgumentNullException> (() => list[HeaderId.AdHoc] = null); Assert.Throws<ArgumentNullException> (() => value = list[null]); Assert.Throws<ArgumentNullException> (() => list[null] = "value"); Assert.Throws<ArgumentNullException> (() => list["field"] = null); Assert.Throws<ArgumentNullException> (() => list[0] = null); }
/// <summary> /// 删除最后一个Header /// </summary> public void Delete() { HeaderList.RemoveAt(HeaderList.Count - 1); }