[bcl] Remove more NET_2_0 checks from class libs
[mono.git] / mcs / class / System / Test / System.Net / FileWebRequestTest.cs
1 //
2 // FileWebRequestTest.cs - NUnit Test Cases for System.Net.FileWebRequest
3 //
4 // Authors:
5 //   Lawrence Pit (loz@cable.a2000.nl)
6 //   Martin Willemoes Hansen (mwh@sysrq.dk)
7 //   Gert Driesen (drieseng@users.sourceforge.net)
8 //
9 // (C) 2003 Martin Willemoes Hansen
10 //
11
12 using System;
13 using System.Collections;
14 using System.IO;
15 using System.Net;
16 using System.Runtime.Serialization;
17 using System.Runtime.Serialization.Formatters;
18 using System.Runtime.Serialization.Formatters.Binary;
19 using System.Security;
20 using System.Security.Permissions;
21
22 using NUnit.Framework;
23
24
25 namespace MonoTests.System.Net
26 {
27         [TestFixture]
28         public class FileWebRequestTest
29         {
30                 private string _tempDirectory;
31                 private string _tempFile;
32                 private Uri _tempFileUri;
33
34                 [SetUp]
35                 public void SetUp ()
36                 {
37                         _tempDirectory = Path.Combine (Path.GetTempPath (), "MonoTests.System.Net.FileWebRequestTest");
38                         _tempFile = Path.Combine (_tempDirectory, "FileWebRequestTest.tmp");
39                         if (!Directory.Exists (_tempDirectory)) {
40                                 Directory.CreateDirectory (_tempDirectory);
41                         } else {
42                                 // ensure no files are left over from previous runs
43                                 string [] files = Directory.GetFiles (_tempDirectory, "*");
44                                 foreach (string file in files)
45                                         File.Delete (file);
46                         }
47                         _tempFileUri = GetTempFileUri ();
48                 }
49
50                 [TearDown]
51                 public void TearDown ()
52                 {
53                         if (Directory.Exists (_tempDirectory))
54                                 Directory.Delete (_tempDirectory, true);
55                 }
56
57                 [Test]
58                 public void Async ()
59                 {
60                         WebResponse res = null;
61
62                         try {
63                                 FileWebRequest req = (FileWebRequest) WebRequest.Create (_tempFileUri);
64                                 req.Method = "PUT";
65                                 req.ContentLength = 1;
66                                 req.ContentType = "image/png";
67
68                                 req.Timeout = 2 * 1000;
69                                 IAsyncResult async = req.BeginGetRequestStream (null, null);
70                                 try {
71                                         req.BeginGetRequestStream (null, null);
72                                         Assert.Fail ("#1 should've failed");
73                                 } catch (InvalidOperationException) {
74                                         // Cannot re-call BeginGetRequestStream/BeginGetResponse while
75                                         // a previous call is still in progress
76                                 }
77
78                                 try {
79                                         req.GetRequestStream ();
80                                         Assert.Fail ("#3 should've failed");
81                                 } catch (InvalidOperationException) {
82                                         // Cannot re-call BeginGetRequestStream/BeginGetResponse while
83                                         // a previous call is still in progress
84                                 }
85
86                                 using (Stream wstream = req.EndGetRequestStream (async)) {
87                                         Assert.IsFalse (wstream.CanRead, "#1r");
88                                         Assert.IsTrue (wstream.CanWrite, "#1w");
89                                         Assert.IsTrue (wstream.CanSeek, "#1s");
90
91                                         wstream.WriteByte (72);
92                                         wstream.WriteByte (101);
93                                         wstream.WriteByte (108);
94                                         wstream.WriteByte (108);
95                                         wstream.WriteByte (111);
96                                         wstream.Close ();
97                                 }
98
99                                 Assert.AreEqual (1, req.ContentLength, "#1cl");
100                                 Assert.AreEqual ("image/png", req.ContentType, "#1ct");
101
102                                 // stream written
103
104                                 req = (FileWebRequest) WebRequest.Create (_tempFileUri);
105                                 res = req.GetResponse ();
106
107                                 try {
108                                         req.BeginGetRequestStream (null, null);
109                                         Assert.Fail ("#20: should've failed");
110                                 } catch (InvalidOperationException) {
111                                         // Cannot send a content-body with this verb-type
112                                 }
113
114                                 try {
115                                         req.Method = "PUT";
116                                         req.BeginGetRequestStream (null, null);
117                                         Assert.Fail ("#21: should've failed");
118                                 } catch (InvalidOperationException) {
119                                         // This operation cannot be perfomed after the request has been submitted.
120                                 }
121
122                                 req.GetResponse ();
123
124                                 IAsyncResult async2 = req.BeginGetResponse (null, null);
125
126                                 // this succeeds !!
127                                 WebResponse res2 = req.EndGetResponse (async2);
128                                 Assert.AreSame (res, res2, "#23");
129
130                                 Assert.AreEqual (5, res.ContentLength, "#2 len");
131                                 Assert.AreEqual ("application/octet-stream", res.ContentType, "#2 type");
132                                 Assert.AreEqual ("file", res.ResponseUri.Scheme, "#2 scheme");
133
134                                 Stream rstream = res.GetResponseStream ();
135                                 Assert.IsTrue (rstream.CanRead, "#3r");
136                                 Assert.IsFalse (rstream.CanWrite, "#3w");
137                                 Assert.IsTrue (rstream.CanSeek, "#3s");
138
139                                 Assert.AreEqual (72, rstream.ReadByte (), "#4a");
140                                 Assert.AreEqual (101, rstream.ReadByte (), "#4b");
141                                 Assert.AreEqual (108, rstream.ReadByte (), "#4c");
142                                 Assert.AreEqual (108, rstream.ReadByte (), "#4d");
143                                 Assert.AreEqual (111, rstream.ReadByte (), "#4e");
144
145                                 rstream.Close ();
146
147                                 try {
148                                         long len = res.ContentLength;
149                                         Assert.AreEqual ((long) 5, len, "#5");
150                                 } catch (ObjectDisposedException) {
151                                         Assert.Fail ("#disposed contentlength");
152                                 }
153                                 try {
154                                         WebHeaderCollection w = res.Headers;
155                                 } catch (ObjectDisposedException) {
156                                         Assert.Fail ("#disposed headers");
157                                 }
158                                 try {
159                                         res.Close ();
160                                 } catch (ObjectDisposedException) {
161                                         Assert.Fail ("#disposed close");
162                                 }
163                         } finally {
164                                 if (res != null)
165                                         res.Close ();
166                         }
167                 }
168
169                 [Test]
170                 [Category ("NotWorking")] // bug #323388
171                 public void Async_GetResponse_Failure ()
172                 {
173                         FileWebRequest req = (FileWebRequest) WebRequest.Create (_tempFileUri);
174                         req.Method = "PUT";
175                         req.ContentLength = 1;
176                         req.ContentType = "image/png";
177                         req.Timeout = 500;
178
179                         IAsyncResult async = req.BeginGetRequestStream (null, null);
180                         try {
181                                 req.GetResponse ();
182                                 Assert.Fail ("#1");
183                         } catch (WebException) {
184                                 // The operation has timed out
185                         }
186
187                         try {
188                                 req.BeginGetResponse (null, null);
189                                 Assert.Fail ("#2");
190                         } catch (InvalidOperationException) {
191                                 // Cannot re-call BeginGetRequestStream/BeginGetResponse while
192                                 // a previous call is still in progress
193                         }
194
195                         using (Stream wstream = req.EndGetRequestStream (async)) {
196                                 wstream.WriteByte (72);
197                         }
198
199                         // the temp file should not be in use
200                         Directory.Delete (_tempDirectory, true);
201                 }
202
203                 [Test]
204                 public void Sync ()
205                 {
206                         WebResponse res = null;
207
208                         try {
209                                 FileWebRequest req = (FileWebRequest) WebRequest.Create (_tempFileUri);
210                                 req.ContentLength = 1;
211                                 req.ContentType = "image/png";
212
213                                 try {
214                                         Stream stream = req.GetRequestStream ();
215                                         Assert.Fail ("should throw exception");
216                                 } catch (ProtocolViolationException) {
217                                 }
218
219                                 req.Method = "PUT";
220
221                                 Stream wstream = req.GetRequestStream ();
222                                 Assert.IsFalse (wstream.CanRead, "#1r");
223                                 Assert.IsTrue (wstream.CanWrite, "#1w");
224                                 Assert.IsTrue (wstream.CanSeek, "#1s");
225
226                                 wstream.WriteByte (72);
227                                 wstream.WriteByte (101);
228                                 wstream.WriteByte (108);
229                                 wstream.WriteByte (108);
230                                 wstream.WriteByte (111);
231                                 wstream.Close ();
232
233                                 Assert.AreEqual (1, req.ContentLength, "#1cl");
234                                 Assert.AreEqual ("image/png", req.ContentType, "#1ct");
235
236                                 // stream written
237
238                                 req = (FileWebRequest) WebRequest.Create (_tempFileUri);
239                                 res = req.GetResponse ();
240                                 Assert.AreEqual ((long) 5, res.ContentLength, "#2 len");
241                                 Assert.AreEqual ("application/octet-stream", res.ContentType, "#2 type");
242                                 Assert.AreEqual ("file", res.ResponseUri.Scheme, "#2 scheme");
243
244                                 Stream rstream = res.GetResponseStream ();
245                                 Assert.IsTrue (rstream.CanRead, "#3r");
246                                 Assert.IsFalse (rstream.CanWrite, "#3w");
247                                 Assert.IsTrue (rstream.CanSeek, "#3s");
248
249                                 Assert.AreEqual (72, rstream.ReadByte (), "#4a");
250                                 Assert.AreEqual (101, rstream.ReadByte (), "#4b");
251                                 Assert.AreEqual (108, rstream.ReadByte (), "#4c");
252                                 Assert.AreEqual (108, rstream.ReadByte (), "#4d");
253                                 Assert.AreEqual (111, rstream.ReadByte (), "#4e");
254
255                                 rstream.Close ();
256
257                                 try {
258                                         long len = res.ContentLength;
259                                         Assert.AreEqual ((long) 5, len, "#5");
260                                 } catch (ObjectDisposedException) {
261                                         Assert.Fail ("#disposed contentlength");
262                                 }
263                                 try {
264                                         WebHeaderCollection w = res.Headers;
265                                 } catch (ObjectDisposedException) {
266                                         Assert.Fail ("#disposed headers");
267                                 }
268                                 try {
269                                         res.Close ();
270                                 } catch (ObjectDisposedException) {
271                                         Assert.Fail ("#disposed close");
272                                 }
273                         } finally {
274                                 if (res != null)
275                                         res.Close ();
276                         }
277                 }
278
279                 [Test]
280                 [Category ("NotWorking")] // bug #323388
281                 public void Sync_GetResponse_Failure ()
282                 {
283                         FileWebRequest req = (FileWebRequest) WebRequest.Create (_tempFileUri);
284                         req.Method = "PUT";
285                         req.ContentLength = 1;
286                         req.ContentType = "image/png";
287                         req.Timeout = 500;
288
289                         using (Stream rs = req.GetRequestStream ()) {
290                                 try {
291                                         req.GetResponse ();
292                                         Assert.Fail ("#1");
293                                 } catch (WebException) {
294                                         // The operation has timed out
295                                 }
296
297                                 try {
298                                         req.BeginGetResponse (null, null);
299                                         Assert.Fail ("#2");
300                                 } catch (InvalidOperationException) {
301                                         // Cannot re-call BeginGetRequestStream/BeginGetResponse while
302                                         // a previous call is still in progress
303                                 }
304                         }
305
306                         // the temp file should not be in use
307                         Directory.Delete (_tempDirectory, true);
308                 }
309
310                 [Test]
311                 public void ConnectionGroupName ()
312                 {
313                         FileWebRequest req = (FileWebRequest) WebRequest.Create (_tempFileUri);
314                         Assert.IsNull (req.ConnectionGroupName, "#A");
315                         req.ConnectionGroupName = "whatever";
316                         Assert.IsNotNull (req.ConnectionGroupName, "#B1");
317                         Assert.AreEqual ("whatever", req.ConnectionGroupName, "#B2");
318                         req.ConnectionGroupName = string.Empty;
319                         Assert.IsNotNull (req.ConnectionGroupName, "#C1");
320                         Assert.AreEqual (string.Empty, req.ConnectionGroupName, "#C2");
321                         req.ConnectionGroupName = null;
322                         Assert.IsNull (req.ConnectionGroupName, "#D");
323                 }
324
325                 [Test]
326                 public void ContentLength ()
327                 {
328                         FileWebRequest req = (FileWebRequest) WebRequest.Create (_tempFileUri);
329                         Assert.AreEqual (0, req.Headers.Count, "#A1");
330                         Assert.AreEqual (0, req.ContentLength, "#A2");
331                         req.ContentLength = 5;
332                         Assert.AreEqual (5, req.ContentLength, "#A3");
333                         Assert.AreEqual (0, req.Headers.Count, "#A4");
334
335                         req.Method = "PUT";
336                         using (Stream s = req.GetRequestStream ()) {
337                                 s.WriteByte (5);
338                                 Assert.AreEqual (5, req.ContentLength, "#B1");
339                                 s.WriteByte (4);
340                                 Assert.AreEqual (5, req.ContentLength, "#B2");
341                                 s.Flush ();
342                                 Assert.AreEqual (5, req.ContentLength, "#B3");
343                         }
344                         Assert.AreEqual (5, req.ContentLength, "#B4");
345                 }
346
347                 [Test]
348                 public void ContentLength_Negative ()
349                 {
350                         FileWebRequest req = (FileWebRequest) WebRequest.Create (_tempFileUri);
351                         try {
352                                 req.ContentLength = -1;
353                                 Assert.Fail ("#1");
354                         } catch (ArgumentException ex) {
355                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
356                                 Assert.IsNotNull (ex.Message, "#3");
357                                 Assert.IsFalse (ex.Message == "value", "#4");
358                                 Assert.IsNotNull (ex.ParamName, "#5");
359                                 Assert.AreEqual ("value", ex.ParamName, "#6");
360                                 Assert.IsNull (ex.InnerException, "#7");
361                         }
362                 }
363
364                 [Test]
365                 public void ContentType ()
366                 {
367                         FileWebRequest req = (FileWebRequest) WebRequest.Create (_tempFileUri);
368                         Assert.AreEqual (0, req.Headers.Count, "#A1");
369                         Assert.IsNull (req.ContentType, "#A2");
370
371                         req.ContentType = "application/x-gzip";
372                         Assert.AreEqual (1, req.Headers.Count, "#B1");
373                         Assert.AreEqual ("Content-Type", req.Headers.GetKey (0), "#B2");
374                         Assert.AreEqual ("application/x-gzip", req.Headers.Get (0), "#B3");
375                         Assert.AreEqual ("application/x-gzip", req.ContentType, "#B4");
376
377                         req.Headers.Set ("Content-Type", "image/png");
378                         Assert.AreEqual ("image/png", req.ContentType, "#C1");
379
380                         req.ContentType = null;
381                         Assert.AreEqual (1, req.Headers.Count, "#D1");
382                         Assert.AreEqual ("Content-Type", req.Headers.GetKey (0), "#D2");
383                         Assert.AreEqual (string.Empty, req.Headers.Get (0), "#D3");
384                         Assert.AreEqual (string.Empty, req.ContentType, "#D4");
385
386                         req.Headers.Remove ("Content-Type");
387                         Assert.AreEqual (0, req.Headers.Count, "#E1");
388                         Assert.IsNull (req.ContentType, "#E2");
389                 }
390
391                 [Test]
392                 public void Credentials ()
393                 {
394                         FileWebRequest req = (FileWebRequest) WebRequest.Create (_tempFileUri);
395                         Assert.IsNull (req.Credentials, "#1");
396                         req.Credentials = new NetworkCredential ();
397                         Assert.IsNotNull (req.Credentials, "#2");
398                         req.Credentials = null;
399                         Assert.IsNull (req.Credentials, "#3");
400                 }
401
402                 [Test]
403                 [Category ("NotWorking")]
404                 public void GetRequestStream ()
405                 {
406                         FileWebRequest req = (FileWebRequest) WebRequest.Create (
407                                 _tempFileUri);
408                         req.Timeout = 1000;
409                         req.Method = "POST";
410                         FileStream fsA = null;
411                         FileStream fsB = null;
412                         try {
413                                 fsA = req.GetRequestStream () as FileStream;
414                                 Assert.IsNotNull (fsA, "#A1");
415                                 try {
416                                         req.GetRequestStream ();
417                                         Assert.Fail ("#A2");
418                                 } catch (WebException) {
419                                         // The operation has timed out
420                                 }
421                                 fsA.Close ();
422                                 try {
423                                         req.GetRequestStream ();
424                                         Assert.Fail ("#A3");
425                                 } catch (InvalidOperationException) {
426                                         // Cannot re-call BeginGetRequestStream/BeginGetResponse 
427                                         // while a previous call is still in progress.
428                                 }
429                         } finally {
430                                 if (fsA != null)
431                                         fsA.Close ();
432                                 if (fsB != null)
433                                         fsB.Close ();
434                         }
435
436                         req = (FileWebRequest) WebRequest.Create (_tempFileUri);
437                         req.Timeout = 1000;
438                         req.Method = "POST";
439                         try {
440                                 fsA = req.GetRequestStream () as FileStream;
441                                 Assert.IsNotNull (fsA, "#B1");
442                                 fsA.Close ();
443                                 try {
444                                         req.GetRequestStream ();
445                                         Assert.Fail ("#B2");
446                                 } catch (WebException) {
447                                         // The operation has timed out
448                                 }
449                                 fsA.Close ();
450                                 try {
451                                         req.GetRequestStream ();
452                                         Assert.Fail ("#B3");
453                                 } catch (InvalidOperationException) {
454                                         // Cannot re-call BeginGetRequestStream/BeginGetResponse 
455                                         // while a previous call is still in progress.
456                                 }
457                         } finally {
458                                 if (fsA != null)
459                                         fsA.Close ();
460                                 if (fsB != null)
461                                         fsB.Close ();
462                         }
463                 }
464
465                 [Test]
466                 public void GetRequestStream_File_Exists ()
467                 {
468                         Stream s = File.Create (_tempFile);
469                         s.Close ();
470                         FileWebRequest req = (FileWebRequest) WebRequest.Create (
471                                 _tempFileUri);
472                         req.Method = "POST";
473                         s = req.GetRequestStream ();
474                         s.Close ();
475                 }
476
477                 [Test]
478                 public void GetRequestStream_Method_Valid ()
479                 {
480                         string [] methods = new string [] { "PUT", "POST", "CHECKOUT",
481                                 "DELETE", "OPTIONS", "TRACE", "GET ", "DUNNO" };
482
483                         foreach (string method in methods) {
484                                 FileWebRequest req = (FileWebRequest) WebRequest.Create (
485                                         _tempFileUri);
486                                 req.Method = method;
487                                 using (Stream s = req.GetRequestStream ()) {
488                                         Assert.IsNotNull (s, "#1:" + method);
489                                         Assert.IsFalse (s.CanRead, "#2:" + method);
490                                         Assert.IsTrue (s.CanSeek, "#3:" + method);
491                                         Assert.IsFalse (s.CanTimeout, "#4:" + method);
492                                         Assert.IsTrue (s.CanWrite, "#5:" + method);
493                                         Assert.AreEqual (0, s.Length, "#6:" + method);
494                                         Assert.AreEqual (0, s.Position, "#7:" + method);
495                                         try {
496                                                 int i = s.ReadTimeout;
497                                                 Assert.Fail ("#8:" + method + "=>" + i);
498                                         } catch (InvalidOperationException) {
499                                         }
500                                         try {
501                                                 int i = s.WriteTimeout;
502                                                 Assert.Fail ("#9:" + method + "=>" + i);
503                                         } catch (InvalidOperationException) {
504                                         }
505                                 }
506                         }
507                 }
508
509                 [Test]
510                 public void GetRequestStream_Method_Invalid ()
511                 {
512                         string [] methods = new string [] { "GET", "get", "HEAD", "head",
513                                 "CONNECT", "connect"};
514                         foreach (string method in methods) {
515                                 FileWebRequest req = (FileWebRequest) WebRequest.Create (
516                                         _tempFileUri);
517                                 req.Method = method;
518                                 try {
519                                         req.GetRequestStream ();
520                                         Assert.Fail ("#1:" + method);
521                                 } catch (ProtocolViolationException ex) {
522                                         Assert.AreEqual (typeof (ProtocolViolationException), ex.GetType (), "#2:" + method);
523                                         Assert.IsNotNull (ex.Message, "#3:" + method);
524                                         Assert.IsNull (ex.InnerException, "#4:" + method);
525                                 }
526                         }
527                 }
528
529                 [Test]
530                 public void GetResponse_File_Exists ()
531                 {
532                         Stream s = File.Create (_tempFile);
533                         s.Close ();
534                         FileWebRequest req = (FileWebRequest) WebRequest.Create (_tempFileUri);
535                         FileWebResponse respA = null;
536                         FileWebResponse respB = null;
537                         try {
538                                 respA = req.GetResponse () as FileWebResponse;
539                                 Assert.IsNotNull (respA, "#1");
540                                 respB = req.GetResponse () as FileWebResponse;
541                                 Assert.IsNotNull (respB, "#2");
542                                 Assert.AreSame (respA, respB, "#3");
543                         } finally {
544                                 if (respA != null)
545                                         respA.Close ();
546                                 if (respB != null)
547                                         respB.Close ();
548                         }
549                 }
550
551                 [Test]
552                 public void GetResponse_File_DoesNotExist ()
553                 {
554                         FileWebRequest req = (FileWebRequest) WebRequest.Create (_tempFileUri);
555                         try {
556                                 req.GetResponse ();
557                                 Assert.Fail ("#1");
558                         } catch (WebException ex) {
559                                 Assert.AreEqual (typeof (WebException), ex.GetType (), "#1");
560                                 Assert.IsNotNull (ex.Message, "#2");
561                                 Assert.IsTrue (ex.Message.IndexOf ("FileWebRequestTest.tmp") != -1, "#3");                              
562                                 Assert.IsNull (ex.Response, "#4");
563                                 Assert.IsNotNull (ex.InnerException, "#5");
564
565 #if ONLY_1_1
566                                 FileNotFoundException fnf = ex.InnerException as FileNotFoundException;
567                                 Assert.IsNotNull (fnf, "#6");
568                                 Assert.AreEqual (typeof (FileNotFoundException), fnf.GetType (), "#7");
569                                 Assert.IsNotNull (fnf.FileName, "#8");
570                                 Assert.IsTrue (fnf.FileName.IndexOf ("FileWebRequestTest.tmp") != -1, "#9");
571                                 Assert.IsNotNull (fnf.Message, "#10");
572                                 Assert.IsTrue (fnf.Message.IndexOf ("FileWebRequestTest.tmp") != -1, "#11");
573                                 Assert.IsNull (fnf.InnerException, "#12");
574 #endif
575                         }
576                 }
577
578                 [Test]
579                 public void Method ()
580                 {
581                         FileWebRequest req = (FileWebRequest) WebRequest.Create (_tempFileUri);
582                         Assert.IsNotNull (req.Method, "#A1");
583                         Assert.AreEqual ("GET", req.Method, "#A2");
584                         req.Method = "whatever";
585                         Assert.IsNotNull (req.Method, "#B1");
586                         Assert.AreEqual ("whatever", req.Method, "#B2");
587                         req.Method = "get ";
588                         Assert.IsNotNull (req.Method, "#C1");
589                         Assert.AreEqual ("get ", req.Method, "#C2");
590                 }
591
592                 [Test]
593                 public void Method_Empty ()
594                 {
595                         FileWebRequest req = (FileWebRequest) WebRequest.Create (_tempFileUri);
596                         try {
597                                 req.Method = string.Empty;
598                                 Assert.Fail ("#1");
599                         } catch (ArgumentException ex) {
600                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
601                                 Assert.IsNotNull (ex.Message, "#3");
602                                 Assert.AreEqual ("value", ex.ParamName, "#4");
603                                 Assert.IsNull (ex.InnerException, "#5");
604                         }
605                 }
606
607                 [Test]
608                 public void Method_Null ()
609                 {
610                         FileWebRequest req = (FileWebRequest) WebRequest.Create (_tempFileUri);
611                         try {
612                                 req.Method = null;
613                                 Assert.Fail ("#1");
614                         } catch (ArgumentException ex) {
615                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
616                                 Assert.IsNotNull (ex.Message, "#3");
617                                 Assert.AreEqual ("value", ex.ParamName, "#4");
618                                 Assert.IsNull (ex.InnerException, "#5");
619                         }
620                 }
621
622                 [Test]
623                 public void PreAuthenticate ()
624                 {
625                         FileWebRequest req = (FileWebRequest) WebRequest.Create (_tempFileUri);
626                         Assert.IsFalse (req.PreAuthenticate, "#1");
627                         req.PreAuthenticate = true;
628                         Assert.IsTrue (req.PreAuthenticate, "#2");
629                 }
630
631                 [Test]
632                 public void Proxy ()
633                 {
634                         FileWebRequest req = (FileWebRequest) WebRequest.Create (_tempFileUri);
635                         Assert.IsNull (req.Proxy, "#1");
636                         req.Proxy = new WebProxy ();
637                         Assert.IsNotNull (req.Proxy, "#2");
638                         req.Proxy = null;
639                         Assert.IsNull (req.Proxy, "#3");
640                 }
641
642                 [Test]
643                 public void RequestUri ()
644                 {
645                         FileWebRequest req = (FileWebRequest) WebRequest.Create (_tempFileUri);
646                         Assert.AreSame (_tempFileUri, req.RequestUri);
647                 }
648
649                 [Test]
650                 public void Timeout ()
651                 {
652                         FileWebRequest req = (FileWebRequest) WebRequest.Create (_tempFileUri);
653                         Assert.AreEqual (100000, req.Timeout, "#1");
654                         req.Timeout = int.MaxValue;
655                         Assert.AreEqual (int.MaxValue, req.Timeout, "#2");
656                         req.Timeout = 0;
657                         Assert.AreEqual (0, req.Timeout, "#3");
658                 }
659
660                 [Test]
661                 public void Timeout_Negative ()
662                 {
663                         FileWebRequest req = (FileWebRequest) WebRequest.Create (_tempFileUri);
664                         req.Timeout = -1;
665                         Assert.AreEqual (-1, req.Timeout, "#1");
666                         try {
667                                 req.Timeout = -2;
668                                 Assert.Fail ("#2");
669                         } catch (ArgumentOutOfRangeException ex) {
670                                 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#3");
671                                 Assert.IsNotNull (ex.Message, "#4");
672                                 Assert.IsNotNull (ex.ParamName, "#5");
673                                 Assert.IsFalse (ex.ParamName == "value", "#6");
674                                 Assert.IsNull (ex.InnerException, "#7");
675                         }
676                 }
677
678                 [Test]
679                 public void GetObjectData ()
680                 {
681                         FileWebRequest fwr = (FileWebRequest) WebRequest.Create ("file:///test.txt");
682                         fwr.ConnectionGroupName = "CGN";
683                         fwr.ContentLength = 10;
684                         fwr.ContentType = "image/png";
685                         fwr.Credentials = new NetworkCredential ("Miguel", "de Icaza", "Novell");
686                         fwr.Headers.Add ("Disposition", "attach");
687                         fwr.Method = "PUT";
688                         fwr.PreAuthenticate = true;
689                         fwr.Proxy = new WebProxy ("proxy.ximian.com");
690                         fwr.Timeout = 20;
691
692                         SerializationInfo si = new SerializationInfo (typeof (FileWebRequest),
693                                 new FormatterConverter ());
694                         ((ISerializable) fwr).GetObjectData (si, new StreamingContext ());
695                         Assert.AreEqual (9, si.MemberCount, "#A1");
696                         int i = 0;
697                         foreach (SerializationEntry entry in si) {
698                                 Assert.IsNotNull (entry.Name, "#B1:" + i);
699                                 Assert.IsNotNull (entry.ObjectType, "#B2:" + i);
700                                 Assert.IsNotNull (entry.Value, "#B3:" + i);
701
702                                 switch (i) {
703                                 case 0:
704                                         Assert.AreEqual ("headers", entry.Name, "#B4:" + i);
705                                         Assert.AreEqual (typeof (WebHeaderCollection), entry.ObjectType, "#B5:" + i);
706                                         break;
707                                 case 1:
708                                         Assert.AreEqual ("proxy", entry.Name, "#B4:" + i);
709                                         Assert.AreEqual (typeof (IWebProxy), entry.ObjectType, "#B5:" + i);
710                                         break;
711                                 case 2:
712                                         Assert.AreEqual ("uri", entry.Name, "#B4:" + i);
713                                         Assert.AreEqual (typeof (Uri), entry.ObjectType, "#B5:" + i);
714                                         break;
715                                 case 3:
716                                         Assert.AreEqual ("connectionGroupName", entry.Name, "#B4:" + i);
717                                         Assert.AreEqual (typeof (string), entry.ObjectType, "#B5:" + i);
718                                         Assert.AreEqual ("CGN", entry.Value, "#B6:" + i);
719                                         break;
720                                 case 4:
721                                         Assert.AreEqual ("method", entry.Name, "#B4:" + i);
722                                         Assert.AreEqual (typeof (string), entry.ObjectType, "#B5:" + i);
723                                         Assert.AreEqual ("PUT", entry.Value, "#B6:" + i);
724                                         break;
725                                 case 5:
726                                         Assert.AreEqual ("contentLength", entry.Name, "#B4:" + i);
727                                         Assert.AreEqual (typeof (long), entry.ObjectType, "#B5:" + i);
728                                         Assert.AreEqual (10, entry.Value, "#B6:" + i);
729                                         break;
730                                 case 6:
731                                         Assert.AreEqual ("timeout", entry.Name, "#B4:" + i);
732                                         Assert.AreEqual (typeof (int), entry.ObjectType, "#B5:" + i);
733                                         Assert.AreEqual (20, entry.Value, "#B6:" + i);
734                                         break;
735                                 case 7:
736                                         Assert.AreEqual ("fileAccess", entry.Name, "#B4:" + i);
737                                         Assert.AreEqual (typeof (FileAccess), entry.ObjectType, "#B5:" + i);
738                                         Assert.AreEqual (FileAccess.Read, entry.Value, "#B6:" + i);
739                                         break;
740                                 case 8:
741                                         Assert.AreEqual ("preauthenticate", entry.Name, "#B4:" + i);
742                                         Assert.AreEqual (typeof (bool), entry.ObjectType, "#B5:" + i);
743                                         Assert.AreEqual (false, entry.Value, "#B6:" + i);
744                                         break;
745                                 }
746                                 i++;
747                         }
748                 }
749
750                 [Test]
751                 [Category ("NotWorking")] // Difference at index 272: 20 instead of 19
752                 public void Serialize ()
753                 {
754                         FileWebRequest fwr = (FileWebRequest) WebRequest.Create ("file://test.txt/");
755                         fwr.ConnectionGroupName = "CGN";
756                         fwr.ContentLength = 10;
757                         fwr.ContentType = "image/png";
758                         fwr.Credentials = new NetworkCredential ("Miguel", "de Icaza", "Novell");
759                         fwr.Headers.Add ("Disposition", "attach");
760                         fwr.Method = "PUT";
761                         fwr.PreAuthenticate = true;
762                         fwr.Proxy = new WebProxy ("proxy.ximian.com");
763                         fwr.Timeout = 20;
764
765                         BinaryFormatter bf = new BinaryFormatter ();
766                         bf.AssemblyFormat = FormatterAssemblyStyle.Full;
767
768                         MemoryStream ms = new MemoryStream ();
769                         bf.Serialize (ms, fwr);
770                         ms.Position = 0;
771
772                         byte [] buffer = new byte [ms.Length];
773                         ms.Read (buffer, 0, buffer.Length);
774                         Assert.AreEqual (_serialized, buffer);
775                 }
776
777                 [Test]
778                 public void Deserialize ()
779                 {
780                         MemoryStream ms = new MemoryStream ();
781                         ms.Write (_serialized, 0, _serialized.Length);
782                         ms.Position = 0;
783
784                         BinaryFormatter bf = new BinaryFormatter ();
785                         FileWebRequest req = (FileWebRequest) bf.Deserialize (ms);
786                         Assert.AreEqual ("CGN", req.ConnectionGroupName, "#A1");
787                         Assert.AreEqual (10, req.ContentLength, "#A2");
788                         Assert.AreEqual ("image/png", req.ContentType, "#A3");
789                         Assert.IsNull (req.Credentials, "#A4");
790                         Assert.AreEqual ("PUT", req.Method, "#A5");
791                         Assert.IsFalse (req.PreAuthenticate, "#A6");
792                         Assert.AreEqual ("file://test.txt/", req.RequestUri.AbsoluteUri, "#A7");
793                         Assert.AreEqual (20, req.Timeout, "#A8");
794
795                         WebHeaderCollection headers = req.Headers;
796                         Assert.IsNotNull (headers, "#C1");
797                         Assert.AreEqual (2, headers.Count, "#C2");
798                         Assert.AreEqual ("Content-Type", req.Headers.GetKey (0), "#C3");
799                         Assert.AreEqual ("image/png", req.Headers.Get (0), "#C4");
800                         Assert.AreEqual ("Disposition", req.Headers.GetKey (1), "#C5");
801                         Assert.AreEqual ("attach", req.Headers.Get (1), "#C6");
802
803                         WebProxy proxy = req.Proxy as WebProxy;
804                         Assert.IsNotNull (proxy, "#D1");
805                         Assert.AreEqual ("http://proxy.ximian.com/", proxy.Address.AbsoluteUri, "#D2");
806                         Assert.IsNotNull (proxy.BypassArrayList, "#D3");
807                         Assert.AreEqual (0, proxy.BypassArrayList.Count, "#D4");
808                         Assert.IsNotNull (proxy.BypassList, "#D5");
809                         Assert.AreEqual (0, proxy.BypassList.Length, "#D6");
810                         Assert.IsFalse (proxy.BypassProxyOnLocal, "#D7");
811                         Assert.IsNull (proxy.Credentials, "#D8");
812                 }
813
814                 private Uri GetTempFileUri ()
815                 {
816                         string tempFile = _tempFile;
817                         if (RunningOnUnix) {
818                                 // remove leading slash for absolute paths
819                                 tempFile = tempFile.TrimStart ('/');
820                         } else {
821                                 tempFile = tempFile.Replace ('\\', '/');
822                         }
823                         return new Uri ("file:///" + tempFile);
824                 }
825
826                 private bool RunningOnUnix {
827                         get {
828                                 // check for Unix platforms - see FAQ for more details
829                                 // http://www.mono-project.com/FAQ:_Technical#How_to_detect_the_execution_platform_.3F
830                                 int platform = (int) Environment.OSVersion.Platform;
831                                 return ((platform == 4) || (platform == 128) || (platform == 6));
832                         }
833                 }
834
835                 private static readonly byte [] _serialized = new byte [] {
836                         0x00, 0x01, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x01, 0x00,
837                         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0c, 0x02, 0x00, 0x00, 0x00,
838                         0x49, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x2c, 0x20, 0x56, 0x65,
839                         0x72, 0x73, 0x69, 0x6f, 0x6e, 0x3d, 0x32, 0x2e, 0x30, 0x2e, 0x30,
840                         0x2e, 0x30, 0x2c, 0x20, 0x43, 0x75, 0x6c, 0x74, 0x75, 0x72, 0x65,
841                         0x3d, 0x6e, 0x65, 0x75, 0x74, 0x72, 0x61, 0x6c, 0x2c, 0x20, 0x50,
842                         0x75, 0x62, 0x6c, 0x69, 0x63, 0x4b, 0x65, 0x79, 0x54, 0x6f, 0x6b,
843                         0x65, 0x6e, 0x3d, 0x62, 0x37, 0x37, 0x61, 0x35, 0x63, 0x35, 0x36,
844                         0x31, 0x39, 0x33, 0x34, 0x65, 0x30, 0x38, 0x39, 0x05, 0x01, 0x00,
845                         0x00, 0x00, 0x19, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x2e, 0x4e,
846                         0x65, 0x74, 0x2e, 0x46, 0x69, 0x6c, 0x65, 0x57, 0x65, 0x62, 0x52,
847                         0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x09, 0x00, 0x00, 0x00, 0x07,
848                         0x68, 0x65, 0x61, 0x64, 0x65, 0x72, 0x73, 0x05, 0x70, 0x72, 0x6f,
849                         0x78, 0x79, 0x03, 0x75, 0x72, 0x69, 0x13, 0x63, 0x6f, 0x6e, 0x6e,
850                         0x65, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x47, 0x72, 0x6f, 0x75, 0x70,
851                         0x4e, 0x61, 0x6d, 0x65, 0x06, 0x6d, 0x65, 0x74, 0x68, 0x6f, 0x64,
852                         0x0d, 0x63, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x4c, 0x65, 0x6e,
853                         0x67, 0x74, 0x68, 0x07, 0x74, 0x69, 0x6d, 0x65, 0x6f, 0x75, 0x74,
854                         0x0a, 0x66, 0x69, 0x6c, 0x65, 0x41, 0x63, 0x63, 0x65, 0x73, 0x73,
855                         0x0f, 0x70, 0x72, 0x65, 0x61, 0x75, 0x74, 0x68, 0x65, 0x6e, 0x74,
856                         0x69, 0x63, 0x61, 0x74, 0x65, 0x04, 0x04, 0x04, 0x01, 0x01, 0x00,
857                         0x00, 0x03, 0x00, 0x1e, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x2e,
858                         0x4e, 0x65, 0x74, 0x2e, 0x57, 0x65, 0x62, 0x48, 0x65, 0x61, 0x64,
859                         0x65, 0x72, 0x43, 0x6f, 0x6c, 0x6c, 0x65, 0x63, 0x74, 0x69, 0x6f,
860                         0x6e, 0x02, 0x00, 0x00, 0x00, 0x13, 0x53, 0x79, 0x73, 0x74, 0x65,
861                         0x6d, 0x2e, 0x4e, 0x65, 0x74, 0x2e, 0x57, 0x65, 0x62, 0x50, 0x72,
862                         0x6f, 0x78, 0x79, 0x02, 0x00, 0x00, 0x00, 0x0a, 0x53, 0x79, 0x73,
863                         0x74, 0x65, 0x6d, 0x2e, 0x55, 0x72, 0x69, 0x02, 0x00, 0x00, 0x00,
864                         0x09, 0x08, 0x14, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x2e, 0x49,
865                         0x4f, 0x2e, 0x46, 0x69, 0x6c, 0x65, 0x41, 0x63, 0x63, 0x65, 0x73,
866                         0x73, 0x01, 0x02, 0x00, 0x00, 0x00, 0x09, 0x03, 0x00, 0x00, 0x00,
867                         0x09, 0x04, 0x00, 0x00, 0x00, 0x09, 0x05, 0x00, 0x00, 0x00, 0x06,
868                         0x06, 0x00, 0x00, 0x00, 0x03, 0x43, 0x47, 0x4e, 0x06, 0x07, 0x00,
869                         0x00, 0x00, 0x03, 0x50, 0x55, 0x54, 0x0a, 0x00, 0x00, 0x00, 0x00,
870                         0x00, 0x00, 0x00, 0x14, 0x00, 0x00, 0x00, 0x04, 0xf8, 0xff, 0xff,
871                         0xff, 0x14, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x2e, 0x49, 0x4f,
872                         0x2e, 0x46, 0x69, 0x6c, 0x65, 0x41, 0x63, 0x63, 0x65, 0x73, 0x73,
873                         0x01, 0x00, 0x00, 0x00, 0x07, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x5f,
874                         0x5f, 0x00, 0x08, 0x01, 0x00, 0x00, 0x00, 0x00, 0x05, 0x03, 0x00,
875                         0x00, 0x00, 0x1e, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x2e, 0x4e,
876                         0x65, 0x74, 0x2e, 0x57, 0x65, 0x62, 0x48, 0x65, 0x61, 0x64, 0x65,
877                         0x72, 0x43, 0x6f, 0x6c, 0x6c, 0x65, 0x63, 0x74, 0x69, 0x6f, 0x6e,
878                         0x05, 0x00, 0x00, 0x00, 0x05, 0x43, 0x6f, 0x75, 0x6e, 0x74, 0x01,
879                         0x30, 0x01, 0x32, 0x01, 0x31, 0x01, 0x33, 0x00, 0x01, 0x01, 0x01,
880                         0x01, 0x08, 0x02, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x06,
881                         0x09, 0x00, 0x00, 0x00, 0x0c, 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x6e,
882                         0x74, 0x2d, 0x54, 0x79, 0x70, 0x65, 0x06, 0x0a, 0x00, 0x00, 0x00,
883                         0x09, 0x69, 0x6d, 0x61, 0x67, 0x65, 0x2f, 0x70, 0x6e, 0x67, 0x06,
884                         0x0b, 0x00, 0x00, 0x00, 0x0b, 0x44, 0x69, 0x73, 0x70, 0x6f, 0x73,
885                         0x69, 0x74, 0x69, 0x6f, 0x6e, 0x06, 0x0c, 0x00, 0x00, 0x00, 0x06,
886                         0x61, 0x74, 0x74, 0x61, 0x63, 0x68, 0x05, 0x04, 0x00, 0x00, 0x00,
887                         0x13, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x2e, 0x4e, 0x65, 0x74,
888                         0x2e, 0x57, 0x65, 0x62, 0x50, 0x72, 0x6f, 0x78, 0x79, 0x04, 0x00,
889                         0x00, 0x00, 0x0e, 0x5f, 0x42, 0x79, 0x70, 0x61, 0x73, 0x73, 0x4f,
890                         0x6e, 0x4c, 0x6f, 0x63, 0x61, 0x6c, 0x0d, 0x5f, 0x50, 0x72, 0x6f,
891                         0x78, 0x79, 0x41, 0x64, 0x64, 0x72, 0x65, 0x73, 0x73, 0x0b, 0x5f,
892                         0x42, 0x79, 0x70, 0x61, 0x73, 0x73, 0x4c, 0x69, 0x73, 0x74, 0x16,
893                         0x5f, 0x55, 0x73, 0x65, 0x44, 0x65, 0x66, 0x61, 0x75, 0x6c, 0x74,
894                         0x43, 0x72, 0x65, 0x64, 0x65, 0x6e, 0x74, 0x69, 0x61, 0x6c, 0x73,
895                         0x00, 0x04, 0x02, 0x00, 0x01, 0x0a, 0x53, 0x79, 0x73, 0x74, 0x65,
896                         0x6d, 0x2e, 0x55, 0x72, 0x69, 0x02, 0x00, 0x00, 0x00, 0x01, 0x02,
897                         0x00, 0x00, 0x00, 0x00, 0x09, 0x0d, 0x00, 0x00, 0x00, 0x0a, 0x00,
898                         0x05, 0x05, 0x00, 0x00, 0x00, 0x0a, 0x53, 0x79, 0x73, 0x74, 0x65,
899                         0x6d, 0x2e, 0x55, 0x72, 0x69, 0x01, 0x00, 0x00, 0x00, 0x0b, 0x41,
900                         0x62, 0x73, 0x6f, 0x6c, 0x75, 0x74, 0x65, 0x55, 0x72, 0x69, 0x01,
901                         0x02, 0x00, 0x00, 0x00, 0x06, 0x0e, 0x00, 0x00, 0x00, 0x10, 0x66,
902                         0x69, 0x6c, 0x65, 0x3a, 0x2f, 0x2f, 0x74, 0x65, 0x73, 0x74, 0x2e,
903                         0x74, 0x78, 0x74, 0x2f, 0x01, 0x0d, 0x00, 0x00, 0x00, 0x05, 0x00,
904                         0x00, 0x00, 0x06, 0x0f, 0x00, 0x00, 0x00, 0x18, 0x68, 0x74, 0x74,
905                         0x70, 0x3a, 0x2f, 0x2f, 0x70, 0x72, 0x6f, 0x78, 0x79, 0x2e, 0x78,
906                         0x69, 0x6d, 0x69, 0x61, 0x6e, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x0b
907                 };
908         }
909 }