File: CookieParserTest.cs

package info (click to toggle)
mono 4.6.2.7+dfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 778,148 kB
  • ctags: 914,052
  • sloc: cs: 5,779,509; xml: 2,773,713; ansic: 432,645; sh: 14,749; makefile: 12,361; perl: 2,488; python: 1,434; cpp: 849; asm: 531; sql: 95; sed: 16; php: 1
file content (193 lines) | stat: -rw-r--r-- 5,354 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
using System;
using System.IO;
using System.Threading;
using System.Globalization;
using System.Net;
using System.Net.Sockets;
using NUnit.Framework;

namespace MonoTests.System.Net
{
	[TestFixture]
	public class CookieParserTest
	{
		public const string A = "Foo=Bar, expires=World; expires=Sat, 11-Oct-14 22:45:19 GMT, A=B";
		public const string B = "A=B=C, expires=Sat, 99-Dec-01 01:00:00 XDT; Hello=World, Foo=Bar";
		// Only the invariant culture is allowed.
		public const string C = "Foo=Bar, expires=Montag, 21. Juni 2012 23:11:45";
		// Only comma serves as separation character.
		public const string D = "A=B, C=D; E=F, G=H";
		public const string E = "A; C; expires=Tue, 25-Jun-19 00:51:34 GMT, E=F";
		public const string F = "Foo = \" A, B\"C D, E=F";

		CookieCollection DoRequest (string header)
		{
			HttpWebResponse res;
			using (var listener = new Listener ("Set-Cookie: " + header)) {
				var req = (HttpWebRequest)HttpWebRequest.Create (listener.URI);
				req.CookieContainer = new CookieContainer ();
				req.Method = "POST";
				res = (HttpWebResponse)req.GetResponse ();
			}

			Assert.AreEqual (header, res.Headers.Get ("Set-Cookie"));
			return res.Cookies;
		}

		void AssertCookie (Cookie cookie, string name, string value, long ticks)
		{
			AssertCookie (cookie, name, value);
			if (ticks == 0)
				Assert.AreEqual (0, cookie.Expires.Ticks);
			else
				Assert.AreEqual (ticks, cookie.Expires.ToUniversalTime ().Ticks);
		}

		void AssertCookie (Cookie cookie, string name, string value)
		{
			Assert.AreEqual (name, cookie.Name);
			Assert.AreEqual (value, cookie.Value);
		}

		[Test]
		public void TestDate ()
		{
			var d1 = "Sat, 11-Oct-14 22:45:19 GMT";
			var d2 = "Tue, 25-Jun-19 00:51:34 GMT";

			var format = "ddd, dd'-'MMM'-'yy HH':'mm':'ss 'GMT'";
			var p1 = DateTime.ParseExact (d1, format, CultureInfo.InvariantCulture);
			p1 = DateTime.SpecifyKind (p1, DateTimeKind.Utc);

			Assert.AreEqual ("10/11/2014 22:45:19", p1.ToString ("G", CultureInfo.InvariantCulture));
			Assert.AreEqual (p1.Ticks, p1.ToUniversalTime ().Ticks);
			Assert.AreEqual (635486643190000000, p1.Ticks);

			var p2 = DateTime.ParseExact (d2, format, CultureInfo.InvariantCulture);
			p2 = DateTime.SpecifyKind (p2, DateTimeKind.Utc);
			Assert.AreEqual ("06/25/2019 00:51:34", p2.ToString ("G", CultureInfo.InvariantCulture));
			Assert.AreEqual (p2.Ticks, p2.ToUniversalTime ().Ticks);
			Assert.AreEqual (636970206940000000, p2.Ticks);
		}

		[Test]
		public void TestExpires ()
		{
			var cookies = DoRequest (A);
			Assert.AreEqual (3, cookies.Count);
			AssertCookie (cookies [0], "Foo", "Bar", 0);
			AssertCookie (cookies [1], "expires", "World", 635486643190000000);
			AssertCookie (cookies [2], "A", "B", 0);
		}

		[Test]
		public void TestInvalidCookie ()
		{
			var cookies = DoRequest (B);
			Assert.AreEqual (3, cookies.Count);
			AssertCookie (cookies [0], "A", "B=C");
			AssertCookie (cookies [1], "expires", "Sat");
			AssertCookie (cookies [2], "Foo", "Bar");
		}

		[Test]
		public void TestLocalCulture ()
		{
			var old = Thread.CurrentThread.CurrentCulture;
			try {
				var culture = new CultureInfo ("de-DE");
				Thread.CurrentThread.CurrentCulture = culture;

				var cookies = DoRequest (C);
				Assert.AreEqual (2, cookies.Count);
				AssertCookie (cookies [0], "Foo", "Bar");
				AssertCookie (cookies [1], "expires", "Montag");
			} finally {
				Thread.CurrentThread.CurrentCulture = old;
			}
		}

		[Test]
		public void TestMultiple ()
		{
			var cookies = DoRequest (D);
			Assert.AreEqual (3, cookies.Count);
			AssertCookie (cookies [0], "A", "B");
			AssertCookie (cookies [1], "C", "D");
			AssertCookie (cookies [2], "G", "H");
		}

		[Test]
		public void TestMultiple2 ()
		{
			var cookies = DoRequest (E);
			Assert.AreEqual (2, cookies.Count);
			AssertCookie (cookies [0], "A", string.Empty, 636970206940000000);
			AssertCookie (cookies [1], "E", "F");
		}

		[Test]
		public void TestQuotation ()
		{
			var cookies = DoRequest (F);
			Assert.AreEqual (2, cookies.Count);
			AssertCookie (cookies [0], "Foo", "\" A, B\"");
			AssertCookie (cookies [1], "E", "F");
		}

		public class Listener : IDisposable
		{
			Socket socket;
			string[] headers;

			public Listener (params string[] headers)
			{
				this.headers = headers;
				Start ();
			}

			void Start ()
			{
				socket = new Socket (
					AddressFamily.InterNetwork, SocketType.Stream,
					ProtocolType.Tcp);
				socket.Bind (new IPEndPoint (IPAddress.Loopback, 0));
				socket.Listen (1);
				socket.BeginAccept ((result) => {
					var accepted = socket.EndAccept (result);
					HandleRequest (accepted);
				}, null);
			}

			public void Dispose ()
			{
				if (socket != null) {
					socket.Close ();
					socket = null;
				}
			}

			void HandleRequest (Socket accepted)
			{
				using (var stream = new NetworkStream (accepted)) {
					using (var writer = new StreamWriter (stream)) {
						writer.WriteLine ("HTTP/1.1 200 OK");
						writer.WriteLine ("Content-Type: text/plain");
						foreach (var header in headers)
							writer.WriteLine (header);
						writer.WriteLine ();
						writer.WriteLine ("HELLO");
					}
				}
			}

			public EndPoint EndPoint {
				get { return socket.LocalEndPoint; }
			}

			public string URI {
				get { return string.Format ("http://{0}/", EndPoint); }
			}
		}
	}
}