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