CookieParserTest.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. using System;
  2. using System.IO;
  3. using System.Threading;
  4. using System.Globalization;
  5. using System.Net;
  6. using System.Net.Sockets;
  7. using NUnit.Framework;
  8. namespace MonoTests.System.Net
  9. {
  10. [TestFixture]
  11. public class CookieParserTest
  12. {
  13. public const string A = "Foo=Bar, expires=World; expires=Sat, 11-Oct-14 22:45:19 GMT, A=B";
  14. public const string B = "A=B=C, expires=Sat, 99-Dec-01 01:00:00 XDT; Hello=World, Foo=Bar";
  15. // Only the invariant culture is allowed.
  16. public const string C = "Foo=Bar, expires=Montag, 21. Juni 2012 23:11:45";
  17. // Only comma serves as separation character.
  18. public const string D = "A=B, C=D; E=F, G=H";
  19. public const string E = "A; C; expires=Tue, 25-Jun-19 00:51:34 GMT, E=F";
  20. public const string F = "Foo = \" A, B\"C D, E=F";
  21. CookieCollection DoRequest (string header)
  22. {
  23. HttpWebResponse res;
  24. using (var listener = new Listener ("Set-Cookie: " + header)) {
  25. var req = (HttpWebRequest)HttpWebRequest.Create (listener.URI);
  26. req.CookieContainer = new CookieContainer ();
  27. req.Method = "POST";
  28. res = (HttpWebResponse)req.GetResponse ();
  29. }
  30. Assert.AreEqual (header, res.Headers.Get ("Set-Cookie"));
  31. return res.Cookies;
  32. }
  33. void AssertCookie (Cookie cookie, string name, string value, long ticks)
  34. {
  35. AssertCookie (cookie, name, value);
  36. if (ticks == 0)
  37. Assert.AreEqual (0, cookie.Expires.Ticks);
  38. else
  39. Assert.AreEqual (ticks, cookie.Expires.ToUniversalTime ().Ticks);
  40. }
  41. void AssertCookie (Cookie cookie, string name, string value)
  42. {
  43. Assert.AreEqual (name, cookie.Name);
  44. Assert.AreEqual (value, cookie.Value);
  45. }
  46. [Test]
  47. public void TestDate ()
  48. {
  49. var d1 = "Sat, 11-Oct-14 22:45:19 GMT";
  50. var d2 = "Tue, 25-Jun-19 00:51:34 GMT";
  51. var format = "ddd, dd'-'MMM'-'yy HH':'mm':'ss 'GMT'";
  52. var p1 = DateTime.ParseExact (d1, format, CultureInfo.InvariantCulture);
  53. p1 = DateTime.SpecifyKind (p1, DateTimeKind.Utc);
  54. Assert.AreEqual ("10/11/2014 22:45:19", p1.ToString ("G", CultureInfo.InvariantCulture));
  55. Assert.AreEqual (p1.Ticks, p1.ToUniversalTime ().Ticks);
  56. Assert.AreEqual (635486643190000000, p1.Ticks);
  57. var p2 = DateTime.ParseExact (d2, format, CultureInfo.InvariantCulture);
  58. p2 = DateTime.SpecifyKind (p2, DateTimeKind.Utc);
  59. Assert.AreEqual ("06/25/2019 00:51:34", p2.ToString ("G", CultureInfo.InvariantCulture));
  60. Assert.AreEqual (p2.Ticks, p2.ToUniversalTime ().Ticks);
  61. Assert.AreEqual (636970206940000000, p2.Ticks);
  62. }
  63. [Test]
  64. #if FEATURE_NO_BSD_SOCKETS
  65. [ExpectedException (typeof (PlatformNotSupportedException))]
  66. #endif
  67. public void TestExpires ()
  68. {
  69. var cookies = DoRequest (A);
  70. Assert.AreEqual (3, cookies.Count);
  71. AssertCookie (cookies [0], "Foo", "Bar", 0);
  72. AssertCookie (cookies [1], "expires", "World", 635486643190000000);
  73. AssertCookie (cookies [2], "A", "B", 0);
  74. }
  75. [Test]
  76. #if FEATURE_NO_BSD_SOCKETS
  77. [ExpectedException (typeof (PlatformNotSupportedException))]
  78. #endif
  79. public void TestInvalidCookie ()
  80. {
  81. var cookies = DoRequest (B);
  82. Assert.AreEqual (3, cookies.Count);
  83. AssertCookie (cookies [0], "A", "B=C");
  84. AssertCookie (cookies [1], "expires", "Sat");
  85. AssertCookie (cookies [2], "Foo", "Bar");
  86. }
  87. [Test]
  88. #if FEATURE_NO_BSD_SOCKETS
  89. [ExpectedException (typeof (PlatformNotSupportedException))]
  90. #endif
  91. public void TestLocalCulture ()
  92. {
  93. var old = Thread.CurrentThread.CurrentCulture;
  94. try {
  95. var culture = new CultureInfo ("de-DE");
  96. Thread.CurrentThread.CurrentCulture = culture;
  97. var cookies = DoRequest (C);
  98. Assert.AreEqual (2, cookies.Count);
  99. AssertCookie (cookies [0], "Foo", "Bar");
  100. AssertCookie (cookies [1], "expires", "Montag");
  101. } finally {
  102. Thread.CurrentThread.CurrentCulture = old;
  103. }
  104. }
  105. [Test]
  106. #if FEATURE_NO_BSD_SOCKETS
  107. [ExpectedException (typeof (PlatformNotSupportedException))]
  108. #endif
  109. public void TestMultiple ()
  110. {
  111. var cookies = DoRequest (D);
  112. Assert.AreEqual (3, cookies.Count);
  113. AssertCookie (cookies [0], "A", "B");
  114. AssertCookie (cookies [1], "C", "D");
  115. AssertCookie (cookies [2], "G", "H");
  116. }
  117. [Test]
  118. #if FEATURE_NO_BSD_SOCKETS
  119. [ExpectedException (typeof (PlatformNotSupportedException))]
  120. #endif
  121. public void TestMultiple2 ()
  122. {
  123. var cookies = DoRequest (E);
  124. Assert.AreEqual (2, cookies.Count);
  125. AssertCookie (cookies [0], "A", string.Empty, 636970206940000000);
  126. AssertCookie (cookies [1], "E", "F");
  127. }
  128. [Test]
  129. #if FEATURE_NO_BSD_SOCKETS
  130. [ExpectedException (typeof (PlatformNotSupportedException))]
  131. #endif
  132. public void TestQuotation ()
  133. {
  134. var cookies = DoRequest (F);
  135. Assert.AreEqual (2, cookies.Count);
  136. AssertCookie (cookies [0], "Foo", "\" A, B\"");
  137. AssertCookie (cookies [1], "E", "F");
  138. }
  139. public class Listener : IDisposable
  140. {
  141. Socket socket;
  142. string[] headers;
  143. Exception ex;
  144. public Listener (params string[] headers)
  145. {
  146. this.headers = headers;
  147. Start ();
  148. }
  149. void Start ()
  150. {
  151. socket = new Socket (
  152. AddressFamily.InterNetwork, SocketType.Stream,
  153. ProtocolType.Tcp);
  154. socket.Bind (new IPEndPoint (IPAddress.Loopback, 0));
  155. socket.Listen (1);
  156. socket.BeginAccept ((result) => {
  157. try {
  158. var accepted = socket.EndAccept (result);
  159. HandleRequest (accepted);
  160. } catch (Exception e) {
  161. ex = e;
  162. }
  163. }, null);
  164. }
  165. void ThrowIfException ()
  166. {
  167. if (ex != null)
  168. throw ex;
  169. }
  170. public void Dispose ()
  171. {
  172. if (socket != null) {
  173. socket.Close ();
  174. socket = null;
  175. }
  176. ThrowIfException ();
  177. }
  178. void HandleRequest (Socket accepted)
  179. {
  180. using (var stream = new NetworkStream (accepted)) {
  181. using (var writer = new StreamWriter (stream)) {
  182. writer.WriteLine ("HTTP/1.1 200 OK");
  183. writer.WriteLine ("Content-Type: text/plain");
  184. foreach (var header in headers)
  185. writer.WriteLine (header);
  186. writer.WriteLine ();
  187. writer.WriteLine ("HELLO");
  188. }
  189. }
  190. }
  191. public EndPoint EndPoint {
  192. get {
  193. ThrowIfException ();
  194. return socket.LocalEndPoint;
  195. }
  196. }
  197. public string URI {
  198. get {
  199. ThrowIfException ();
  200. return string.Format ("http://{0}/", EndPoint);
  201. }
  202. }
  203. }
  204. }
  205. }