Merge pull request #201 from QuickJack/master
[mono.git] / mcs / class / System / Test / System.Net / CookieTest.cs
1 //
2 // CookieTest.cs - NUnit Test Cases for System.Net.Cookie
3 //
4 // Authors:
5 //      Lawrence Pit (loz@cable.a2000.nl)
6 //      Martin Willemoes Hansen (mwh@sysrq.dk)
7 //      Daniel Nauck    (dna(at)mono-project(dot)de)
8 //
9 // (C) 2003 Martin Willemoes Hansen
10 //
11
12 using NUnit.Framework;
13 using System;
14 using System.Net;
15
16 namespace MonoTests.System.Net
17 {
18
19         [TestFixture]
20         public class CookieTest
21         {
22                 [Test]
23                 public void PublicFields ()
24                 {
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 #if NET_2_0
36                         Assert.IsFalse (c.HttpOnly, "#A10");
37 #endif
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");
42
43                         c.Expires = DateTime.Now;
44                         Assert.IsTrue (c.Expired, "#A15");
45
46                         c.Port = null;
47                         Assert.AreEqual (string.Empty, c.Port, "#A16");
48
49                         c.Value = null;
50                         Assert.AreEqual (string.Empty, c.Value, "#A17");
51                 }
52
53                 [Test]
54                 public void Constructors ()
55                 {
56                         Cookie c = new Cookie ("somename", null, null, null);
57                         try
58                         {
59                                 c = new Cookie (null, null, null, null);
60                                 Assert.Fail ("#1: Name cannot be null");
61                         }
62                         catch (CookieException)
63                         {
64                         }
65                 }
66
67                 [Test]
68                 public void Name ()
69                 {
70                         Cookie c = new Cookie ("SomeName", "SomeValue");
71                         Assert.AreEqual (c.Name, "SomeName", "#1");
72                         try
73                         {
74                                 c.Name = null;
75                                 Assert.Fail ("#2a");
76                         }
77                         catch (CookieException)
78                         {
79                                 Assert.AreEqual ("SomeName", c.Name, "#2b");
80                         }
81                         try
82                         {
83                                 c.Name = "";
84                                 Assert.Fail ("#2c");
85                         }
86                         catch (CookieException)
87                         {
88                                 Assert.AreEqual ("SomeName", c.Name, "#2d");
89                         }
90                         try
91                         {
92                                 c.Name = " ";
93                                 Assert.Fail ("#2e");
94                         }
95                         catch (CookieException)
96                         {
97                                 // bah! this fails, yet the name is changed.. 
98                                 // inconsistent with previous test
99                                 Assert.AreEqual (String.Empty, c.Name, "#2f");
100                         }
101                         try
102                         {
103                                 c.Name = "xxx\r\n";
104                                 Assert.Fail ("#2g");
105                         }
106                         catch (CookieException)
107                         {
108                                 Assert.AreEqual (String.Empty, c.Name, "#2h");
109                         }
110                         try
111                         {
112                                 c.Name = "xxx" + (char)0x80;
113                         }
114                         catch (CookieException)
115                         {
116                                 Assert.Fail ("#2i");
117                         }
118                         try
119                         {
120                                 c.Name = "$omeName";
121                                 Assert.Fail ("#3a: Name cannot start with '$' character");
122                         }
123                         catch (CookieException)
124                         {
125                                 Assert.AreEqual (String.Empty, c.Name, "#3b");
126                         }
127                         c.Name = "SomeName$";
128                         Assert.AreEqual (c.Name, "SomeName$", "#4");
129                         try
130                         {
131                                 c.Name = "Some=Name";
132                                 Assert.Fail ("#5a: Name cannot contain '=' character");
133                         }
134                         catch (CookieException)
135                         {
136                                 Assert.AreEqual (String.Empty, c.Name, "#5b");
137                         }
138                         c.Name = "domain";
139                         Assert.AreEqual (c.Name, "domain", "#6");
140                 }
141
142                 [Test]
143                 public void Path ()
144                 {
145                         Cookie c = new Cookie ();
146                         c.Path = "/Whatever";
147                         Assert.AreEqual ("/Whatever", c.Path, "#1");
148                         c.Path = null;
149                         Assert.AreEqual (string.Empty, c.Path, "#2");
150                         c.Path = "ok";
151                         Assert.AreEqual ("ok", c.Path, "#3");
152                         c.Path = string.Empty;
153                         Assert.AreEqual (string.Empty, c.Path, "#4");
154                 }
155
156                 [Test]
157                 public void Value ()
158                 {
159                         // LAMESPEC: According to .Net specs the Value property should not accept 
160                         // the semicolon and comma characters, yet it does
161                         /*
162                         Cookie c = new Cookie("SomeName", "SomeValue");
163                         try {
164                                 c.Value = "Some;Value";
165                                 Assert.Fail ("#1: semicolon should not be accepted");
166                         } catch (CookieException) {
167                         }
168                         try {
169                                 c.Value = "Some,Value";
170                                 Assert.Fail ("#2: comma should not be accepted");
171                         } catch (CookieException) {
172                         }
173                         c.Value = "Some\tValue";
174                         Assert.AreEqual (c.Value, "Some\tValue", "#3");
175                         */
176                 }
177
178                 [Test]
179                 public void Port ()
180                 {
181                         Cookie c = new Cookie ("SomeName", "SomeValue");
182                         try
183                         {
184                                 c.Port = "123";
185                                 Assert.Fail ("#1: port must start and end with double quotes");
186                         }
187                         catch (CookieException)
188                         {
189                         }
190                         try
191                         {
192                                 Assert.AreEqual (0, c.Version, "#6.1");
193                                 c.Port = "\"123\"";
194                                 Assert.AreEqual (1, c.Version, "#6.2");
195                         }
196                         catch (CookieException)
197                         {
198                                 Assert.Fail ("#2");
199                         }
200                         try
201                         {
202                                 c.Port = "\"123;124\"";
203                                 Assert.Fail ("#3");
204                         }
205                         catch (CookieException)
206                         {
207                         }
208                         try
209                         {
210                                 c.Port = "\"123,123,124\"";
211                         }
212                         catch (CookieException)
213                         {
214                                 Assert.Fail ("#4");
215                         }
216                         try
217                         {
218                                 c.Port = "\"123,124\"";
219                         }
220                         catch (CookieException)
221                         {
222                                 Assert.Fail ("#5");
223                         }
224                 }
225
226                 [Test]
227                 public void Equals ()
228                 {
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");
232                         c2.Value = "VALUE";
233                         c2.Path = "PATH";
234                         Assert.IsTrue (c1.Equals (c2), "#2");
235                         c2.Version = 1;
236                         Assert.IsTrue (!c1.Equals (c2), "#3");
237                 }
238
239                 [Test]
240                 public void ToStringTest ()
241                 {
242                         Cookie c1 = new Cookie ("NAME", "VALUE", "/", "example.com");
243                         Assert.AreEqual ("NAME=VALUE", c1.ToString (), "#A1");
244
245                         Cookie c2 = new Cookie ();
246                         Assert.AreEqual (string.Empty, c2.ToString (), "#A2");
247
248                         Cookie c3 = new Cookie("NAME", "VALUE");
249                         Assert.AreEqual ("NAME=VALUE", c3.ToString (), "#A3");
250
251                         Cookie c4 = new Cookie ("NAME", "VALUE", "/", "example.com");
252                         c4.Version = 1;
253                         Assert.AreEqual ("$Version=1; NAME=VALUE; $Path=/; $Domain=example.com", c4.ToString (), "#A4");
254
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");
258
259                         Cookie c6 = new Cookie ("NAME", "VALUE");
260                         c6.Version = 1;
261                         Assert.AreEqual ("$Version=1; NAME=VALUE", c6.ToString (), "#A6");
262                 }
263         }
264 }