HttpHeadersTest.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. try {
  54. headers.Add ("Expires", (string)null);
  55. if (HttpClientTestHelpers.UsingSocketsHandler)
  56. Assert.Fail ("#1");
  57. } catch (FormatException) {
  58. if (!HttpClientTestHelpers.UsingSocketsHandler)
  59. throw;
  60. }
  61. }
  62. [Test]
  63. public void Add_InvalidArguments ()
  64. {
  65. try {
  66. headers.Add (null, "value");
  67. Assert.Fail ("#1");
  68. } catch (ArgumentException) {
  69. }
  70. try {
  71. headers.Add ("", "value");
  72. Assert.Fail ("#2");
  73. } catch (ArgumentException) {
  74. }
  75. }
  76. [Test]
  77. public void Clear ()
  78. {
  79. headers.Add ("aa", "value");
  80. headers.Clear ();
  81. }
  82. [Test]
  83. public void GetEnumerator ()
  84. {
  85. headers.Add ("aa", "value");
  86. int i = 0;
  87. foreach (var entry in headers) {
  88. ++i;
  89. Assert.AreEqual ("aa", entry.Key);
  90. var values = entry.Value.ToList ();
  91. Assert.AreEqual (1, values.Count);
  92. Assert.AreEqual ("value", values[0]);
  93. }
  94. Assert.AreEqual (1, i, "#10");
  95. }
  96. [Test]
  97. public void GetValues ()
  98. {
  99. headers.Add ("aa", "v");
  100. headers.Add ("aa", "v");
  101. var r = headers.GetValues ("aa").ToList ();
  102. Assert.AreEqual ("v", r[0], "#1");
  103. Assert.AreEqual ("v", r[1], "#2");
  104. }
  105. [Test]
  106. public void GetValues_Invalid ()
  107. {
  108. try {
  109. headers.GetValues (null);
  110. Assert.Fail ("#1");
  111. } catch (ArgumentException) {
  112. }
  113. try {
  114. headers.GetValues (" ");
  115. Assert.Fail ("#2");
  116. } catch (FormatException) {
  117. }
  118. try {
  119. headers.GetValues ("x");
  120. Assert.Fail ("#3");
  121. } catch (InvalidOperationException) {
  122. }
  123. }
  124. [Test]
  125. public void TryGetValuesTest ()
  126. {
  127. IEnumerable<string> headerValues;
  128. Assert.IsFalse (headers.TryGetValues (null, out headerValues), "#1");
  129. Assert.IsFalse (headers.TryGetValues ("some-name", out headerValues), "#2");
  130. }
  131. [Test]
  132. public void ToStringTest ()
  133. {
  134. headers.Add ("aa", "v");
  135. headers.Add ("aa", "v");
  136. headers.Add ("x", "v");
  137. Assert.AreEqual ("aa: v, v\r\nx: v\r\n", headers.ToString ());
  138. }
  139. [Test]
  140. public void ToString_DifferentSeparator ()
  141. {
  142. headers.Add ("User-Agent", "MyApp/1.0.0.0 (iOS; 7.1.2; fr_FR) (Apple; iPhone3,1)");
  143. Assert.AreEqual ("User-Agent: MyApp/1.0.0.0 (iOS; 7.1.2; fr_FR) (Apple; iPhone3,1)\r\n", headers.ToString ());
  144. }
  145. }
  146. }