2
0

WarningHeaderValueTest.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. //
  2. // WarningHeaderValueTest.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. namespace MonoTests.System.Net.Http.Headers
  34. {
  35. [TestFixture]
  36. public class WarningHeaderValueTest
  37. {
  38. [Test]
  39. public void Ctor_InvalidArguments ()
  40. {
  41. try {
  42. new WarningHeaderValue (1000, "rb", "\"\"");
  43. Assert.Fail ("#1");
  44. } catch (ArgumentOutOfRangeException) {
  45. }
  46. try {
  47. new WarningHeaderValue (1, null, null);
  48. Assert.Fail ("#2");
  49. } catch (ArgumentException) {
  50. }
  51. try {
  52. new WarningHeaderValue (1, "a", null);
  53. Assert.Fail ("#3");
  54. } catch (ArgumentException) {
  55. }
  56. }
  57. [Test]
  58. public void Equals ()
  59. {
  60. var value = new WarningHeaderValue (13, "x", "\"v\"");
  61. Assert.AreEqual (value, new WarningHeaderValue (13, "X", "\"v\""), "#1");
  62. Assert.AreNotEqual (value, new WarningHeaderValue (13, "x", "\"V\""), "#2");
  63. Assert.AreNotEqual (value, new WarningHeaderValue (13, "y", "\"V\""), "#3");
  64. Assert.AreNotEqual (value, new WarningHeaderValue (1, "x", "\"V\""), "#4");
  65. value = new WarningHeaderValue (6, "DD", "\"c\"", DateTimeOffset.MaxValue);
  66. Assert.AreEqual (value, new WarningHeaderValue (6, "DD", "\"c\"", DateTimeOffset.MaxValue), "#5");
  67. Assert.AreNotEqual (value, new WarningHeaderValue (6, "y", "\"V\""), "#6");
  68. }
  69. [Test]
  70. public void Parse ()
  71. {
  72. var res = WarningHeaderValue.Parse ("1 n \"\"");
  73. Assert.IsNull (res.Date, "#1");
  74. Assert.AreEqual (1, res.Code, "#2");
  75. Assert.AreEqual ("n", res.Agent, "#3");
  76. Assert.AreEqual ("\"\"", res.Text, "#4");
  77. Assert.AreEqual ("001 n \"\"", res.ToString (), "#5");
  78. res = WarningHeaderValue.Parse ("155 foo:8080 \"tttext \" \"Sunday, 06-Nov-94 08:49:37 GMT\" ");
  79. Assert.AreEqual (new DateTimeOffset (1994, 11, 6, 8, 49, 37, TimeSpan.Zero), res.Date, "#11");
  80. Assert.AreEqual (155, res.Code, "#12");
  81. Assert.AreEqual ("foo:8080", res.Agent, "#13");
  82. Assert.AreEqual ("\"tttext \"", res.Text, "#14");
  83. Assert.AreEqual ("155 foo:8080 \"tttext \" \"Sun, 06 Nov 1994 08:49:37 GMT\"", res.ToString (), "#5");
  84. }
  85. [Test]
  86. public void Parse_Invalid ()
  87. {
  88. try {
  89. WarningHeaderValue.Parse (null);
  90. Assert.Fail ("#1");
  91. } catch (FormatException) {
  92. }
  93. try {
  94. WarningHeaderValue.Parse (" ");
  95. Assert.Fail ("#2");
  96. } catch (FormatException) {
  97. }
  98. try {
  99. WarningHeaderValue.Parse ("a");
  100. Assert.Fail ("#3");
  101. } catch (FormatException) {
  102. }
  103. try {
  104. WarningHeaderValue.Parse ("5555 foo:8080 \"\"");
  105. Assert.Fail ("#4");
  106. } catch (FormatException) {
  107. }
  108. }
  109. [Test]
  110. public void Properties ()
  111. {
  112. var value = new WarningHeaderValue (5, "ag", "\"tt\"");
  113. Assert.IsNull (value.Date, "#1");
  114. Assert.AreEqual (5, value.Code, "#2");
  115. Assert.AreEqual ("ag", value.Agent, "#3");
  116. Assert.AreEqual ("\"tt\"", value.Text, "#4");
  117. value = new WarningHeaderValue (5, "ag", "\"tt\"", DateTimeOffset.MinValue);
  118. Assert.AreEqual (DateTimeOffset.MinValue, value.Date, "#5");
  119. Assert.AreEqual (5, value.Code, "#6");
  120. Assert.AreEqual ("ag", value.Agent, "#7");
  121. Assert.AreEqual ("\"tt\"", value.Text, "#8");
  122. }
  123. [Test]
  124. public void TryParse ()
  125. {
  126. WarningHeaderValue res;
  127. Assert.IsTrue (WarningHeaderValue.TryParse ("22 a \"b\"", out res), "#1");
  128. Assert.IsNull (res.Date, "#2");
  129. Assert.AreEqual (22, res.Code, "#3");
  130. Assert.AreEqual ("a", res.Agent, "#4");
  131. Assert.AreEqual ("\"b\"", res.Text, "#5");
  132. }
  133. [Test]
  134. public void TryParse_Invalid ()
  135. {
  136. WarningHeaderValue res;
  137. Assert.IsFalse (WarningHeaderValue.TryParse ("", out res), "#1");
  138. Assert.IsNull (res, "#2");
  139. }
  140. }
  141. }