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");
36 Assert.IsFalse (c.HttpOnly, "#A10");
38 Assert.AreEqual (string.Empty, c.Path, "#A11");
39 Assert.IsFalse (c.Secure, "#A12");
40 Assert.AreEqual (0, c.Version, "#A13");
41 Assert.AreEqual (string.Empty, c.ToString (), "#A14");
43 c.Expires = DateTime.Now;
44 Assert.IsTrue (c.Expired, "#A15");
47 Assert.AreEqual (string.Empty, c.Port, "#A16");
50 Assert.AreEqual (string.Empty, c.Value, "#A17");
54 public void Constructors ()
56 Cookie c = new Cookie ("somename", null, null, null);
59 c = new Cookie (null, null, null, null);
60 Assert.Fail ("#1: Name cannot be null");
62 catch (CookieException)
70 Cookie c = new Cookie ("SomeName", "SomeValue");
71 Assert.AreEqual (c.Name, "SomeName", "#1");
77 catch (CookieException)
79 Assert.AreEqual ("SomeName", c.Name, "#2b");
86 catch (CookieException)
88 Assert.AreEqual ("SomeName", c.Name, "#2d");
95 catch (CookieException)
97 // bah! this fails, yet the name is changed..
98 // inconsistent with previous test
99 Assert.AreEqual (String.Empty, c.Name, "#2f");
106 catch (CookieException)
108 Assert.AreEqual (String.Empty, c.Name, "#2h");
112 c.Name = "xxx" + (char)0x80;
114 catch (CookieException)
121 Assert.Fail ("#3a: Name cannot start with '$' character");
123 catch (CookieException)
125 Assert.AreEqual (String.Empty, c.Name, "#3b");
127 c.Name = "SomeName$";
128 Assert.AreEqual (c.Name, "SomeName$", "#4");
131 c.Name = "Some=Name";
132 Assert.Fail ("#5a: Name cannot contain '=' character");
134 catch (CookieException)
136 Assert.AreEqual (String.Empty, c.Name, "#5b");
139 Assert.AreEqual (c.Name, "domain", "#6");
145 Cookie c = new Cookie ();
146 c.Path = "/Whatever";
147 Assert.AreEqual ("/Whatever", c.Path, "#1");
149 Assert.AreEqual (string.Empty, c.Path, "#2");
151 Assert.AreEqual ("ok", c.Path, "#3");
152 c.Path = string.Empty;
153 Assert.AreEqual (string.Empty, c.Path, "#4");
159 // LAMESPEC: According to .Net specs the Value property should not accept
160 // the semicolon and comma characters, yet it does
162 Cookie c = new Cookie("SomeName", "SomeValue");
164 c.Value = "Some;Value";
165 Assert.Fail ("#1: semicolon should not be accepted");
166 } catch (CookieException) {
169 c.Value = "Some,Value";
170 Assert.Fail ("#2: comma should not be accepted");
171 } catch (CookieException) {
173 c.Value = "Some\tValue";
174 Assert.AreEqual (c.Value, "Some\tValue", "#3");
181 Cookie c = new Cookie ("SomeName", "SomeValue");
185 Assert.Fail ("#1: port must start and end with double quotes");
187 catch (CookieException)
192 Assert.AreEqual (0, c.Version, "#6.1");
194 Assert.AreEqual (1, c.Version, "#6.2");
196 catch (CookieException)
202 c.Port = "\"123;124\"";
205 catch (CookieException)
210 c.Port = "\"123,123,124\"";
212 catch (CookieException)
218 c.Port = "\"123,124\"";
220 catch (CookieException)
227 public void Equals ()
229 Cookie c1 = new Cookie ("NAME", "VALUE", "PATH", "DOMAIN");
230 Cookie c2 = new Cookie ("name", "value", "path", "domain");
231 Assert.IsTrue (!c1.Equals (c2), "#1");
234 Assert.IsTrue (c1.Equals (c2), "#2");
236 Assert.IsTrue (!c1.Equals (c2), "#3");
240 public void ToStringTest ()
242 Cookie c1 = new Cookie ("NAME", "VALUE", "/", "example.com");
243 Assert.AreEqual ("NAME=VALUE", c1.ToString (), "#A1");
245 Cookie c2 = new Cookie ();
246 Assert.AreEqual (string.Empty, c2.ToString (), "#A2");
248 Cookie c3 = new Cookie("NAME", "VALUE");
249 Assert.AreEqual ("NAME=VALUE", c3.ToString (), "#A3");
251 Cookie c4 = new Cookie ("NAME", "VALUE", "/", "example.com");
253 Assert.AreEqual ("$Version=1; NAME=VALUE; $Path=/; $Domain=example.com", c4.ToString (), "#A4");
255 Cookie c5 = new Cookie ("NAME", "VALUE", "/", "example.com");
256 c5.Port = "\"8080\"";
257 Assert.AreEqual ("$Version=1; NAME=VALUE; $Path=/; $Domain=example.com; $Port=\"8080\"", c5.ToString (), "#A5");
259 Cookie c6 = new Cookie ("NAME", "VALUE");
261 Assert.AreEqual ("$Version=1; NAME=VALUE", c6.ToString (), "#A6");