2 // CookieTest.cs - NUnit Test Cases for System.Net.Cookie
5 // Lawrence Pit (loz@cable.a2000.nl)
6 // Martin Willemoes Hansen (mwh@sysrq.dk)
7 // Daniel Nauck (dna(at)mono-project(dot)de)
9 // (C) 2003 Martin Willemoes Hansen
12 using NUnit.Framework;
16 namespace MonoTests.System.Net
20 public class CookieTest
23 public void PublicFields ()
25 Cookie c = new Cookie ();
26 Assert.AreEqual (string.Empty, c.Name, "#A1");
27 Assert.AreEqual (string.Empty, c.Value, "#A2");
28 Assert.AreEqual (string.Empty, c.Domain, "#A3");
29 Assert.AreEqual (string.Empty, c.Port, "#A4");
30 Assert.AreEqual (string.Empty, c.Comment, "#A5");
31 Assert.AreEqual (null, c.CommentUri, "#A6");
32 Assert.IsFalse (c.Discard, "#A7");
33 Assert.IsFalse (c.Expired, "#A8");
34 Assert.AreEqual (DateTime.MinValue, c.Expires, "#A9");
35 Assert.IsFalse (c.HttpOnly, "#A10");
36 Assert.AreEqual (string.Empty, c.Path, "#A11");
37 Assert.IsFalse (c.Secure, "#A12");
38 Assert.AreEqual (0, c.Version, "#A13");
39 Assert.AreEqual (string.Empty, c.ToString (), "#A14");
41 c.Expires = DateTime.Now;
42 Assert.IsTrue (c.Expired, "#A15");
45 Assert.AreEqual (string.Empty, c.Port, "#A16");
48 Assert.AreEqual (string.Empty, c.Value, "#A17");
52 public void Constructors ()
54 Cookie c = new Cookie ("somename", null, null, null);
57 c = new Cookie (null, null, null, null);
58 Assert.Fail ("#1: Name cannot be null");
60 catch (CookieException)
68 Cookie c = new Cookie ("SomeName", "SomeValue");
69 Assert.AreEqual (c.Name, "SomeName", "#1");
75 catch (CookieException)
77 Assert.AreEqual ("SomeName", c.Name, "#2b");
84 catch (CookieException)
86 Assert.AreEqual ("SomeName", c.Name, "#2d");
93 catch (CookieException)
95 // bah! this fails, yet the name is changed..
96 // inconsistent with previous test
97 Assert.AreEqual (String.Empty, c.Name, "#2f");
104 catch (CookieException)
106 Assert.AreEqual (String.Empty, c.Name, "#2h");
110 c.Name = "xxx" + (char)0x80;
112 catch (CookieException)
119 Assert.Fail ("#3a: Name cannot start with '$' character");
121 catch (CookieException)
123 Assert.AreEqual (String.Empty, c.Name, "#3b");
125 c.Name = "SomeName$";
126 Assert.AreEqual (c.Name, "SomeName$", "#4");
129 c.Name = "Some=Name";
130 Assert.Fail ("#5a: Name cannot contain '=' character");
132 catch (CookieException)
134 Assert.AreEqual (String.Empty, c.Name, "#5b");
137 Assert.AreEqual (c.Name, "domain", "#6");
143 Cookie c = new Cookie ();
144 c.Path = "/Whatever";
145 Assert.AreEqual ("/Whatever", c.Path, "#1");
147 Assert.AreEqual (string.Empty, c.Path, "#2");
149 Assert.AreEqual ("ok", c.Path, "#3");
150 c.Path = string.Empty;
151 Assert.AreEqual (string.Empty, c.Path, "#4");
157 // LAMESPEC: According to .Net specs the Value property should not accept
158 // the semicolon and comma characters, yet it does
160 Cookie c = new Cookie("SomeName", "SomeValue");
162 c.Value = "Some;Value";
163 Assert.Fail ("#1: semicolon should not be accepted");
164 } catch (CookieException) {
167 c.Value = "Some,Value";
168 Assert.Fail ("#2: comma should not be accepted");
169 } catch (CookieException) {
171 c.Value = "Some\tValue";
172 Assert.AreEqual (c.Value, "Some\tValue", "#3");
179 Cookie c = new Cookie ("SomeName", "SomeValue");
183 Assert.Fail ("#1: port must start and end with double quotes");
185 catch (CookieException)
190 Assert.AreEqual (0, c.Version, "#6.1");
192 Assert.AreEqual (1, c.Version, "#6.2");
194 catch (CookieException)
200 c.Port = "\"123;124\"";
203 catch (CookieException)
208 c.Port = "\"123,123,124\"";
210 catch (CookieException)
216 c.Port = "\"123,124\"";
218 catch (CookieException)
225 public void Equals ()
227 Cookie c1 = new Cookie ("NAME", "VALUE", "PATH", "DOMAIN");
228 Cookie c2 = new Cookie ("name", "value", "path", "domain");
229 Assert.IsTrue (!c1.Equals (c2), "#1");
232 Assert.IsTrue (c1.Equals (c2), "#2");
234 Assert.IsTrue (!c1.Equals (c2), "#3");
238 public void ToStringTest ()
240 Cookie c1 = new Cookie ("NAME", "VALUE", "/", "example.com");
241 Assert.AreEqual ("NAME=VALUE", c1.ToString (), "#A1");
243 Cookie c2 = new Cookie ();
244 Assert.AreEqual (string.Empty, c2.ToString (), "#A2");
246 Cookie c3 = new Cookie("NAME", "VALUE");
247 Assert.AreEqual ("NAME=VALUE", c3.ToString (), "#A3");
249 Cookie c4 = new Cookie ("NAME", "VALUE", "/", "example.com");
251 Assert.AreEqual ("$Version=1; NAME=VALUE; $Path=/; $Domain=example.com", c4.ToString (), "#A4");
253 Cookie c5 = new Cookie ("NAME", "VALUE", "/", "example.com");
254 c5.Port = "\"8080\"";
255 Assert.AreEqual ("$Version=1; NAME=VALUE; $Path=/; $Domain=example.com; $Port=\"8080\"", c5.ToString (), "#A5");
257 Cookie c6 = new Cookie ("NAME", "VALUE");
259 Assert.AreEqual ("$Version=1; NAME=VALUE", c6.ToString (), "#A6");