HttpHeadersTest.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. //
  2. // HttpHeadersTest.cs
  3. //
  4. // Authors:
  5. // Marek Safar <[email protected]>
  6. //
  7. // Copyright (C) 2011 Xamarin Inc (http://www.xamarin.com)
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using System;
  29. using System.Collections;
  30. using System.Collections.Generic;
  31. using NUnit.Framework;
  32. using System.Net.Http.Headers;
  33. using System.Linq;
  34. namespace MonoTests.System.Net.Http.Headers
  35. {
  36. [TestFixture]
  37. public class HttpHeadersTest
  38. {
  39. HttpHeaders headers;
  40. class HttpHeadersMock : HttpHeaders
  41. {
  42. }
  43. [SetUp]
  44. public void Setup ()
  45. {
  46. headers = new HttpHeadersMock ();
  47. }
  48. [Test]
  49. public void Add ()
  50. {
  51. headers.Add ("aa", "value");
  52. headers.Add ("aa", "value");
  53. headers.Add ("Expires", (string) null);
  54. }
  55. [Test]
  56. public void Add_InvalidArguments ()
  57. {
  58. try {
  59. headers.Add (null, "value");
  60. Assert.Fail ("#1");
  61. } catch (ArgumentException) {
  62. }
  63. try {
  64. headers.Add ("", "value");
  65. Assert.Fail ("#2");
  66. } catch (ArgumentException) {
  67. }
  68. }
  69. [Test]
  70. public void Clear ()
  71. {
  72. headers.Add ("aa", "value");
  73. headers.Clear ();
  74. }
  75. [Test]
  76. public void GetEnumerator ()
  77. {
  78. headers.Add ("aa", "value");
  79. int i = 0;
  80. foreach (var entry in headers) {
  81. ++i;
  82. Assert.AreEqual ("aa", entry.Key);
  83. var values = entry.Value.ToList ();
  84. Assert.AreEqual (1, values.Count);
  85. Assert.AreEqual ("value", values[0]);
  86. }
  87. Assert.AreEqual (1, i, "#10");
  88. }
  89. [Test]
  90. public void GetValues ()
  91. {
  92. headers.Add ("aa", "v");
  93. headers.Add ("aa", "v");
  94. var r = headers.GetValues ("aa").ToList ();
  95. Assert.AreEqual ("v", r[0], "#1");
  96. Assert.AreEqual ("v", r[1], "#2");
  97. }
  98. [Test]
  99. public void GetValues_Invalid ()
  100. {
  101. try {
  102. headers.GetValues (null);
  103. Assert.Fail ("#1");
  104. } catch (ArgumentException) {
  105. }
  106. try {
  107. headers.GetValues (" ");
  108. Assert.Fail ("#2");
  109. } catch (FormatException) {
  110. }
  111. try {
  112. headers.GetValues ("x");
  113. Assert.Fail ("#3");
  114. } catch (InvalidOperationException) {
  115. }
  116. }
  117. [Test]
  118. public void ToStringTest ()
  119. {
  120. headers.Add ("aa", "v");
  121. headers.Add ("aa", "v");
  122. headers.Add ("x", "v");
  123. Assert.AreEqual ("aa: v, v\r\nx: v\r\n", headers.ToString ());
  124. }
  125. }
  126. }