* VBCodeGenerator.cs: Fixed generated code for abstract properties.
[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                                 Assertion.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                         Assertion.AssertEquals ("#1", c.Name, "SomeName");
72                         try
73                         {
74                                 c.Name = null;
75                                 Assertion.Fail ("#2a");
76                         }
77                         catch (CookieException)
78                         {
79                                 Assertion.AssertEquals ("#2b", "SomeName", c.Name);
80                         }
81                         try
82                         {
83                                 c.Name = "";
84                                 Assertion.Fail ("#2c");
85                         }
86                         catch (CookieException)
87                         {
88                                 Assertion.AssertEquals ("#2d", "SomeName", c.Name);
89                         }
90                         try
91                         {
92                                 c.Name = " ";
93                                 Assertion.Fail ("#2e");
94                         }
95                         catch (CookieException)
96                         {
97                                 // bah! this fails, yet the name is changed.. 
98                                 // inconsistent with previous test
99                                 Assertion.AssertEquals ("#2f", String.Empty, c.Name);
100                         }
101                         try
102                         {
103                                 c.Name = "xxx\r\n";
104                                 Assertion.Fail ("#2g");
105                         }
106                         catch (CookieException)
107                         {
108                                 Assertion.AssertEquals ("#2h", String.Empty, c.Name);
109                         }
110                         try
111                         {
112                                 c.Name = "xxx" + (char)0x80;
113                         }
114                         catch (CookieException)
115                         {
116                                 Assertion.Fail ("#2i");
117                         }
118                         try
119                         {
120                                 c.Name = "$omeName";
121                                 Assertion.Fail ("#3a: Name cannot start with '$' character");
122                         }
123                         catch (CookieException)
124                         {
125                                 Assertion.AssertEquals ("#3b", String.Empty, c.Name);
126                         }
127                         c.Name = "SomeName$";
128                         Assertion.AssertEquals ("#4", c.Name, "SomeName$");
129                         try
130                         {
131                                 c.Name = "Some=Name";
132                                 Assertion.Fail ("#5a: Name cannot contain '=' character");
133                         }
134                         catch (CookieException)
135                         {
136                                 Assertion.AssertEquals ("#5b", String.Empty, c.Name);
137                         }
138                         c.Name = "domain";
139                         Assertion.AssertEquals ("#6", c.Name, "domain");
140                 }
141
142                 [Test]
143                 public void Value ()
144                 {
145                         // LAMESPEC: According to .Net specs the Value property should not accept 
146                         // the semicolon and comma characters, yet it does
147                         /*
148                         Cookie c = new Cookie("SomeName", "SomeValue");
149                         try {
150                                 c.Value = "Some;Value";
151                                 Assertion.Fail ("#1: semicolon should not be accepted");
152                         } catch (CookieException) {
153                         }
154                         try {
155                                 c.Value = "Some,Value";
156                                 Assertion.Fail ("#2: comma should not be accepted");
157                         } catch (CookieException) {
158                         }
159                         c.Value = "Some\tValue";
160                         Assertion.AssertEquals ("#3", c.Value, "Some\tValue");
161                         */
162                 }
163
164                 [Test]
165                 public void Port ()
166                 {
167                         Cookie c = new Cookie ("SomeName", "SomeValue");
168                         try
169                         {
170                                 c.Port = "123";
171                                 Assertion.Fail ("#1: port must start and end with double quotes");
172                         }
173                         catch (CookieException)
174                         {
175                         }
176                         try
177                         {
178                                 Assert.AreEqual (0, c.Version, "#6.1");
179                                 c.Port = "\"123\"";
180                                 Assert.AreEqual (1, c.Version, "#6.2");
181                         }
182                         catch (CookieException)
183                         {
184                                 Assertion.Fail ("#2");
185                         }
186                         try
187                         {
188                                 c.Port = "\"123;124\"";
189                                 Assertion.Fail ("#3");
190                         }
191                         catch (CookieException)
192                         {
193                         }
194                         try
195                         {
196                                 c.Port = "\"123,123,124\"";
197                         }
198                         catch (CookieException)
199                         {
200                                 Assertion.Fail ("#4");
201                         }
202                         try
203                         {
204                                 c.Port = "\"123,124\"";
205                         }
206                         catch (CookieException)
207                         {
208                                 Assertion.Fail ("#5");
209                         }
210                 }
211
212                 [Test]
213                 public void Equals ()
214                 {
215                         Cookie c1 = new Cookie ("NAME", "VALUE", "PATH", "DOMAIN");
216                         Cookie c2 = new Cookie ("name", "value", "path", "domain");
217                         Assertion.Assert ("#1", !c1.Equals (c2));
218                         c2.Value = "VALUE";
219                         c2.Path = "PATH";
220                         Assertion.Assert ("#2", c1.Equals (c2));
221                         c2.Version = 1;
222                         Assertion.Assert ("#3", !c1.Equals (c2));
223                 }
224
225                 [Test]
226                 public void ToStringTest ()
227                 {
228                         Cookie c1 = new Cookie ("NAME", "VALUE", "/", "example.com");
229                         Assert.AreEqual ("NAME=VALUE", c1.ToString (), "#A1");
230
231                         Cookie c2 = new Cookie ();
232                         Assert.AreEqual (string.Empty, c2.ToString (), "#A2");
233
234                         Cookie c3 = new Cookie("NAME", "VALUE");
235                         Assert.AreEqual ("NAME=VALUE", c3.ToString (), "#A3");
236
237                         Cookie c4 = new Cookie ("NAME", "VALUE", "/", "example.com");
238                         c4.Version = 1;
239                         Assert.AreEqual ("$Version=1; NAME=VALUE; $Path=/; $Domain=example.com", c4.ToString (), "#A4");
240
241                         Cookie c5 = new Cookie ("NAME", "VALUE", "/", "example.com");
242                         c5.Port = "\"8080\"";
243                         Assert.AreEqual ("$Version=1; NAME=VALUE; $Path=/; $Domain=example.com; $Port=\"8080\"", c5.ToString (), "#A5");
244
245                         Cookie c6 = new Cookie ("NAME", "VALUE");
246                         c6.Version = 1;
247                         Assert.AreEqual ("$Version=1; NAME=VALUE", c6.ToString (), "#A6");
248                 }
249         }
250 }