TransferCodingHeaderValueTest.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. //
  2. // TransferCodingHeaderValueTest.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 TransferCodingHeaderValueTest
  38. {
  39. [Test]
  40. public void Ctor_Invalid ()
  41. {
  42. try {
  43. var tfhv = new TransferCodingHeaderValue (null);
  44. Assert.Fail ("#1");
  45. } catch (ArgumentException) {
  46. }
  47. try {
  48. var tfhv = new TransferCodingHeaderValue ("my value");
  49. Assert.Fail ("#2");
  50. } catch (FormatException) {
  51. }
  52. }
  53. [Test]
  54. public void Equals ()
  55. {
  56. var tfhv = new TransferCodingHeaderValue ("abc");
  57. Assert.AreEqual (tfhv, new TransferCodingHeaderValue ("abc"), "#1");
  58. Assert.AreEqual (tfhv, new TransferCodingHeaderValue ("AbC"), "#2");
  59. tfhv.Parameters.Add (new NameValueHeaderValue ("p", "v"));
  60. Assert.AreNotEqual (tfhv, new TransferCodingHeaderValue ("abc"), "#3");
  61. var tfhv2 = new TransferCodingHeaderValue ("abc");
  62. Assert.AreNotEqual (tfhv, tfhv2, "#4");
  63. tfhv2.Parameters.Add (new NameValueHeaderValue ("p", "v"));
  64. Assert.AreEqual (tfhv, tfhv2, "#5");
  65. }
  66. [Test]
  67. public void Parse ()
  68. {
  69. var res = TransferCodingHeaderValue.Parse ("content");
  70. Assert.AreEqual ("content", res.Value, "#1");
  71. Assert.AreEqual ("content", res.ToString (), "#1a");
  72. res = TransferCodingHeaderValue.Parse (" a ;b;c ");
  73. Assert.AreEqual ("a", res.Value, "#2");
  74. Assert.AreEqual ("a; b; c", res.ToString (), "#2a");
  75. Assert.AreEqual ("b", res.Parameters.First ().ToString (), "#2b");
  76. res = TransferCodingHeaderValue.Parse (" a ; v = m ");
  77. Assert.AreEqual ("a", res.Value, "#3");
  78. Assert.AreEqual ("a; v=m", res.ToString (), "#3a");
  79. Assert.AreEqual ("m", res.Parameters.First ().Value, "#3b");
  80. res = TransferCodingHeaderValue.Parse ("\ta; v = \"mmm\" ");
  81. Assert.AreEqual ("a", res.Value, "#4");
  82. Assert.AreEqual ("a; v=\"mmm\"", res.ToString (), "#4a");
  83. Assert.AreEqual ("\"mmm\"", res.Parameters.First ().Value, "#4b");
  84. }
  85. [Test]
  86. public void Parse_Invalid ()
  87. {
  88. try {
  89. TransferCodingHeaderValue.Parse (null);
  90. Assert.Fail ("#1");
  91. } catch (FormatException) {
  92. }
  93. try {
  94. TransferCodingHeaderValue.Parse (" ");
  95. Assert.Fail ("#2");
  96. } catch (FormatException) {
  97. }
  98. try {
  99. TransferCodingHeaderValue.Parse ("a b");
  100. Assert.Fail ("#3");
  101. } catch (FormatException) {
  102. }
  103. try {
  104. TransferCodingHeaderValue.Parse ("a;");
  105. Assert.Fail ("#4");
  106. } catch (FormatException) {
  107. }
  108. try {
  109. TransferCodingHeaderValue.Parse ("u;v=\"\"\"\"");
  110. Assert.Fail ("#5");
  111. } catch (FormatException) {
  112. }
  113. }
  114. [Test]
  115. public void TryParse ()
  116. {
  117. TransferCodingHeaderValue res;
  118. Assert.IsTrue (TransferCodingHeaderValue.TryParse ("content", out res), "#1");
  119. Assert.AreEqual ("content", res.Value, "#2");
  120. }
  121. [Test]
  122. public void TryParse_Invalid ()
  123. {
  124. TransferCodingHeaderValue res;
  125. Assert.IsFalse (TransferCodingHeaderValue.TryParse ("a b", out res), "#1");
  126. Assert.IsNull (res, "#2");
  127. }
  128. [Test]
  129. public void Value ()
  130. {
  131. var tfhv = new TransferCodingHeaderValue ("value");
  132. Assert.AreEqual ("value", tfhv.Value, "#1");
  133. Assert.IsNotNull (tfhv.Parameters, "#2");
  134. }
  135. }
  136. }