2 // FileWebRequestTest.cs - NUnit Test Cases for System.Net.FileWebRequest
5 // Lawrence Pit (loz@cable.a2000.nl)
6 // Martin Willemoes Hansen (mwh@sysrq.dk)
7 // Gert Driesen (drieseng@users.sourceforge.net)
9 // (C) 2003 Martin Willemoes Hansen
13 using System.Collections;
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;
22 using NUnit.Framework;
25 using System.Globalization;
26 using System.Reflection;
29 namespace MonoTests.System.Net
32 public class FileWebRequestTest
34 private string _tempDirectory;
35 private string _tempFile;
36 private Uri _tempFileUri;
41 _tempDirectory = Path.Combine (Path.GetTempPath (), "MonoTests.System.Net.FileWebRequestTest");
42 _tempFile = Path.Combine (_tempDirectory, "FileWebRequestTest.tmp");
43 if (!Directory.Exists (_tempDirectory)) {
44 Directory.CreateDirectory (_tempDirectory);
46 // ensure no files are left over from previous runs
47 string [] files = Directory.GetFiles (_tempDirectory, "*");
48 foreach (string file in files)
51 _tempFileUri = GetTempFileUri ();
55 public void TearDown ()
57 if (Directory.Exists (_tempDirectory))
58 Directory.Delete (_tempDirectory, true);
64 WebResponse res = null;
67 FileWebRequest req = (FileWebRequest) WebRequest.Create (_tempFileUri);
69 req.ContentLength = 1;
70 req.ContentType = "image/png";
72 req.Timeout = 2 * 1000;
73 IAsyncResult async = req.BeginGetRequestStream (null, null);
75 req.BeginGetRequestStream (null, null);
76 Assert.Fail ("#1 should've failed");
77 } catch (InvalidOperationException) {
78 // Cannot re-call BeginGetRequestStream/BeginGetResponse while
79 // a previous call is still in progress
83 req.GetRequestStream ();
84 Assert.Fail ("#3 should've failed");
85 } catch (InvalidOperationException) {
86 // Cannot re-call BeginGetRequestStream/BeginGetResponse while
87 // a previous call is still in progress
90 using (Stream wstream = req.EndGetRequestStream (async)) {
91 Assert.IsFalse (wstream.CanRead, "#1r");
92 Assert.IsTrue (wstream.CanWrite, "#1w");
93 Assert.IsTrue (wstream.CanSeek, "#1s");
95 wstream.WriteByte (72);
96 wstream.WriteByte (101);
97 wstream.WriteByte (108);
98 wstream.WriteByte (108);
99 wstream.WriteByte (111);
103 Assert.AreEqual (1, req.ContentLength, "#1cl");
104 Assert.AreEqual ("image/png", req.ContentType, "#1ct");
108 req = (FileWebRequest) WebRequest.Create (_tempFileUri);
109 res = req.GetResponse ();
112 req.BeginGetRequestStream (null, null);
113 Assert.Fail ("#20: should've failed");
114 } catch (InvalidOperationException) {
115 // Cannot send a content-body with this verb-type
120 req.BeginGetRequestStream (null, null);
121 Assert.Fail ("#21: should've failed");
122 } catch (InvalidOperationException) {
123 // This operation cannot be perfomed after the request has been submitted.
128 IAsyncResult async2 = req.BeginGetResponse (null, null);
131 WebResponse res2 = req.EndGetResponse (async2);
132 Assert.AreSame (res, res2, "#23");
134 Assert.AreEqual (5, res.ContentLength, "#2 len");
135 Assert.AreEqual ("application/octet-stream", res.ContentType, "#2 type");
136 Assert.AreEqual ("file", res.ResponseUri.Scheme, "#2 scheme");
138 Stream rstream = res.GetResponseStream ();
139 Assert.IsTrue (rstream.CanRead, "#3r");
140 Assert.IsFalse (rstream.CanWrite, "#3w");
141 Assert.IsTrue (rstream.CanSeek, "#3s");
143 Assert.AreEqual (72, rstream.ReadByte (), "#4a");
144 Assert.AreEqual (101, rstream.ReadByte (), "#4b");
145 Assert.AreEqual (108, rstream.ReadByte (), "#4c");
146 Assert.AreEqual (108, rstream.ReadByte (), "#4d");
147 Assert.AreEqual (111, rstream.ReadByte (), "#4e");
152 long len = res.ContentLength;
153 Assert.AreEqual ((long) 5, len, "#5");
154 } catch (ObjectDisposedException) {
155 Assert.Fail ("#disposed contentlength");
158 WebHeaderCollection w = res.Headers;
159 } catch (ObjectDisposedException) {
160 Assert.Fail ("#disposed headers");
164 } catch (ObjectDisposedException) {
165 Assert.Fail ("#disposed close");
174 [Category ("NotWorking")] // bug #323388
175 public void Async_GetResponse_Failure ()
177 FileWebRequest req = (FileWebRequest) WebRequest.Create (_tempFileUri);
179 req.ContentLength = 1;
180 req.ContentType = "image/png";
183 IAsyncResult async = req.BeginGetRequestStream (null, null);
187 } catch (WebException) {
188 // The operation has timed out
192 req.BeginGetResponse (null, null);
194 } catch (InvalidOperationException) {
195 // Cannot re-call BeginGetRequestStream/BeginGetResponse while
196 // a previous call is still in progress
199 using (Stream wstream = req.EndGetRequestStream (async)) {
200 wstream.WriteByte (72);
203 // the temp file should not be in use
204 Directory.Delete (_tempDirectory, true);
210 WebResponse res = null;
213 FileWebRequest req = (FileWebRequest) WebRequest.Create (_tempFileUri);
214 req.ContentLength = 1;
215 req.ContentType = "image/png";
218 Stream stream = req.GetRequestStream ();
219 Assert.Fail ("should throw exception");
220 } catch (ProtocolViolationException) {
225 Stream wstream = req.GetRequestStream ();
226 Assert.IsFalse (wstream.CanRead, "#1r");
227 Assert.IsTrue (wstream.CanWrite, "#1w");
228 Assert.IsTrue (wstream.CanSeek, "#1s");
230 wstream.WriteByte (72);
231 wstream.WriteByte (101);
232 wstream.WriteByte (108);
233 wstream.WriteByte (108);
234 wstream.WriteByte (111);
237 Assert.AreEqual (1, req.ContentLength, "#1cl");
238 Assert.AreEqual ("image/png", req.ContentType, "#1ct");
242 req = (FileWebRequest) WebRequest.Create (_tempFileUri);
243 res = req.GetResponse ();
244 Assert.AreEqual ((long) 5, res.ContentLength, "#2 len");
245 Assert.AreEqual ("application/octet-stream", res.ContentType, "#2 type");
246 Assert.AreEqual ("file", res.ResponseUri.Scheme, "#2 scheme");
248 Stream rstream = res.GetResponseStream ();
249 Assert.IsTrue (rstream.CanRead, "#3r");
250 Assert.IsFalse (rstream.CanWrite, "#3w");
251 Assert.IsTrue (rstream.CanSeek, "#3s");
253 Assert.AreEqual (72, rstream.ReadByte (), "#4a");
254 Assert.AreEqual (101, rstream.ReadByte (), "#4b");
255 Assert.AreEqual (108, rstream.ReadByte (), "#4c");
256 Assert.AreEqual (108, rstream.ReadByte (), "#4d");
257 Assert.AreEqual (111, rstream.ReadByte (), "#4e");
262 long len = res.ContentLength;
263 Assert.AreEqual ((long) 5, len, "#5");
264 } catch (ObjectDisposedException) {
265 Assert.Fail ("#disposed contentlength");
268 WebHeaderCollection w = res.Headers;
269 } catch (ObjectDisposedException) {
270 Assert.Fail ("#disposed headers");
274 } catch (ObjectDisposedException) {
275 Assert.Fail ("#disposed close");
284 [Category ("NotWorking")] // bug #323388
285 public void Sync_GetResponse_Failure ()
287 FileWebRequest req = (FileWebRequest) WebRequest.Create (_tempFileUri);
289 req.ContentLength = 1;
290 req.ContentType = "image/png";
293 using (Stream rs = req.GetRequestStream ()) {
297 } catch (WebException) {
298 // The operation has timed out
302 req.BeginGetResponse (null, null);
304 } catch (InvalidOperationException) {
305 // Cannot re-call BeginGetRequestStream/BeginGetResponse while
306 // a previous call is still in progress
310 // the temp file should not be in use
311 Directory.Delete (_tempDirectory, true);
315 public void ConnectionGroupName ()
317 FileWebRequest req = (FileWebRequest) WebRequest.Create (_tempFileUri);
318 Assert.IsNull (req.ConnectionGroupName, "#A");
319 req.ConnectionGroupName = "whatever";
320 Assert.IsNotNull (req.ConnectionGroupName, "#B1");
321 Assert.AreEqual ("whatever", req.ConnectionGroupName, "#B2");
322 req.ConnectionGroupName = string.Empty;
323 Assert.IsNotNull (req.ConnectionGroupName, "#C1");
324 Assert.AreEqual (string.Empty, req.ConnectionGroupName, "#C2");
325 req.ConnectionGroupName = null;
326 Assert.IsNull (req.ConnectionGroupName, "#D");
330 public void ContentLength ()
332 FileWebRequest req = (FileWebRequest) WebRequest.Create (_tempFileUri);
333 Assert.AreEqual (0, req.Headers.Count, "#A1");
334 Assert.AreEqual (0, req.ContentLength, "#A2");
335 req.ContentLength = 5;
336 Assert.AreEqual (5, req.ContentLength, "#A3");
337 Assert.AreEqual (0, req.Headers.Count, "#A4");
340 using (Stream s = req.GetRequestStream ()) {
342 Assert.AreEqual (5, req.ContentLength, "#B1");
344 Assert.AreEqual (5, req.ContentLength, "#B2");
346 Assert.AreEqual (5, req.ContentLength, "#B3");
348 Assert.AreEqual (5, req.ContentLength, "#B4");
352 public void ContentLength_Negative ()
354 FileWebRequest req = (FileWebRequest) WebRequest.Create (_tempFileUri);
356 req.ContentLength = -1;
358 } catch (ArgumentException ex) {
359 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
360 Assert.IsNotNull (ex.Message, "#3");
362 Assert.IsFalse (ex.Message == "value", "#4");
364 Assert.AreEqual ("value", ex.Message, "#4");
368 Assert.IsNotNull (ex.ParamName, "#5");
369 Assert.AreEqual ("value", ex.ParamName, "#6");
371 Assert.IsNull (ex.ParamName, "#5");
374 Assert.IsNull (ex.InnerException, "#7");
379 public void ContentType ()
381 FileWebRequest req = (FileWebRequest) WebRequest.Create (_tempFileUri);
382 Assert.AreEqual (0, req.Headers.Count, "#A1");
383 Assert.IsNull (req.ContentType, "#A2");
385 req.ContentType = "application/x-gzip";
386 Assert.AreEqual (1, req.Headers.Count, "#B1");
387 Assert.AreEqual ("Content-Type", req.Headers.GetKey (0), "#B2");
388 Assert.AreEqual ("application/x-gzip", req.Headers.Get (0), "#B3");
389 Assert.AreEqual ("application/x-gzip", req.ContentType, "#B4");
391 req.Headers.Set ("Content-Type", "image/png");
392 Assert.AreEqual ("image/png", req.ContentType, "#C1");
394 req.ContentType = null;
395 Assert.AreEqual (1, req.Headers.Count, "#D1");
396 Assert.AreEqual ("Content-Type", req.Headers.GetKey (0), "#D2");
397 Assert.AreEqual (string.Empty, req.Headers.Get (0), "#D3");
398 Assert.AreEqual (string.Empty, req.ContentType, "#D4");
400 req.Headers.Remove ("Content-Type");
401 Assert.AreEqual (0, req.Headers.Count, "#E1");
402 Assert.IsNull (req.ContentType, "#E2");
406 public void Credentials ()
408 FileWebRequest req = (FileWebRequest) WebRequest.Create (_tempFileUri);
409 Assert.IsNull (req.Credentials, "#1");
410 req.Credentials = new NetworkCredential ();
411 Assert.IsNotNull (req.Credentials, "#2");
412 req.Credentials = null;
413 Assert.IsNull (req.Credentials, "#3");
417 [Category ("NotWorking")]
418 public void GetRequestStream ()
420 FileWebRequest req = (FileWebRequest) WebRequest.Create (
424 FileStream fsA = null;
425 FileStream fsB = null;
427 fsA = req.GetRequestStream () as FileStream;
428 Assert.IsNotNull (fsA, "#A1");
431 req.GetRequestStream ();
433 } catch (WebException) {
434 // The operation has timed out
438 req.GetRequestStream ();
440 } catch (InvalidOperationException) {
441 // Cannot re-call BeginGetRequestStream/BeginGetResponse
442 // while a previous call is still in progress.
445 fsB = req.GetRequestStream () as FileStream;
446 Assert.IsNotNull (fsB, "#A2");
447 Assert.AreSame (fsA, fsB, "#A3");
456 req = (FileWebRequest) WebRequest.Create (_tempFileUri);
460 fsA = req.GetRequestStream () as FileStream;
461 Assert.IsNotNull (fsA, "#B1");
465 req.GetRequestStream ();
467 } catch (WebException) {
468 // The operation has timed out
472 req.GetRequestStream ();
474 } catch (InvalidOperationException) {
475 // Cannot re-call BeginGetRequestStream/BeginGetResponse
476 // while a previous call is still in progress.
479 fsB = req.GetRequestStream () as FileStream;
480 Assert.IsNotNull (fsB, "#B2");
481 Assert.AreSame (fsA, fsB, "#B3");
492 public void GetRequestStream_File_Exists ()
494 Stream s = File.Create (_tempFile);
496 FileWebRequest req = (FileWebRequest) WebRequest.Create (
499 s = req.GetRequestStream ();
504 public void GetRequestStream_Method_Valid ()
506 string [] methods = new string [] { "PUT", "POST", "CHECKOUT",
507 "DELETE", "OPTIONS", "TRACE", "GET ", "DUNNO" };
509 foreach (string method in methods) {
510 FileWebRequest req = (FileWebRequest) WebRequest.Create (
513 using (Stream s = req.GetRequestStream ()) {
514 Assert.IsNotNull (s, "#1:" + method);
515 Assert.IsFalse (s.CanRead, "#2:" + method);
516 Assert.IsTrue (s.CanSeek, "#3:" + method);
518 Assert.IsFalse (s.CanTimeout, "#4:" + method);
520 Assert.IsTrue (s.CanWrite, "#5:" + method);
521 Assert.AreEqual (0, s.Length, "#6:" + method);
522 Assert.AreEqual (0, s.Position, "#7:" + method);
525 int i = s.ReadTimeout;
526 Assert.Fail ("#8:" + method + "=>" + i);
527 } catch (InvalidOperationException) {
530 int i = s.WriteTimeout;
531 Assert.Fail ("#9:" + method + "=>" + i);
532 } catch (InvalidOperationException) {
540 public void GetRequestStream_Method_Invalid ()
542 string [] methods = new string [] { "GET", "get", "HEAD", "head",
543 "CONNECT", "connect"};
544 foreach (string method in methods) {
545 FileWebRequest req = (FileWebRequest) WebRequest.Create (
549 req.GetRequestStream ();
550 Assert.Fail ("#1:" + method);
551 } catch (ProtocolViolationException ex) {
552 Assert.AreEqual (typeof (ProtocolViolationException), ex.GetType (), "#2:" + method);
553 Assert.IsNotNull (ex.Message, "#3:" + method);
554 Assert.IsNull (ex.InnerException, "#4:" + method);
560 public void GetResponse_File_Exists ()
562 Stream s = File.Create (_tempFile);
564 FileWebRequest req = (FileWebRequest) WebRequest.Create (_tempFileUri);
565 FileWebResponse respA = null;
566 FileWebResponse respB = null;
568 respA = req.GetResponse () as FileWebResponse;
569 Assert.IsNotNull (respA, "#1");
570 respB = req.GetResponse () as FileWebResponse;
571 Assert.IsNotNull (respB, "#2");
572 Assert.AreSame (respA, respB, "#3");
582 public void GetResponse_File_DoesNotExist ()
584 FileWebRequest req = (FileWebRequest) WebRequest.Create (_tempFileUri);
588 } catch (WebException ex) {
589 Assert.AreEqual (typeof (WebException), ex.GetType (), "#1");
590 Assert.IsNotNull (ex.Message, "#2");
592 Assert.IsTrue (ex.Message.IndexOf ("FileWebRequestTest.tmp") != -1, "#3");
593 Assert.IsNull (ex.Response, "#4");
594 Assert.IsNotNull (ex.InnerException, "#5");
598 FileNotFoundException fnf = ex.InnerException as FileNotFoundException;
599 Assert.IsNotNull (fnf, "#6");
600 Assert.AreEqual (typeof (FileNotFoundException), fnf.GetType (), "#7");
601 Assert.IsNotNull (fnf.FileName, "#8");
602 Assert.IsTrue (fnf.FileName.IndexOf ("FileWebRequestTest.tmp") != -1, "#9");
603 Assert.IsNotNull (fnf.Message, "#10");
604 Assert.IsTrue (fnf.Message.IndexOf ("FileWebRequestTest.tmp") != -1, "#11");
605 Assert.IsNull (fnf.InnerException, "#12");
611 public void Method ()
613 FileWebRequest req = (FileWebRequest) WebRequest.Create (_tempFileUri);
614 Assert.IsNotNull (req.Method, "#A1");
615 Assert.AreEqual ("GET", req.Method, "#A2");
616 req.Method = "whatever";
617 Assert.IsNotNull (req.Method, "#B1");
618 Assert.AreEqual ("whatever", req.Method, "#B2");
620 Assert.IsNotNull (req.Method, "#C1");
621 Assert.AreEqual ("get ", req.Method, "#C2");
625 public void Method_Empty ()
627 FileWebRequest req = (FileWebRequest) WebRequest.Create (_tempFileUri);
629 req.Method = string.Empty;
631 } catch (ArgumentException ex) {
632 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
633 Assert.IsNotNull (ex.Message, "#3");
636 Assert.AreEqual ("value", ex.ParamName, "#4");
638 Assert.IsNull (ex.ParamName, "#4");
641 Assert.IsNull (ex.InnerException, "#5");
646 public void Method_Null ()
648 FileWebRequest req = (FileWebRequest) WebRequest.Create (_tempFileUri);
652 } catch (ArgumentException ex) {
653 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
654 Assert.IsNotNull (ex.Message, "#3");
657 Assert.AreEqual ("value", ex.ParamName, "#4");
659 Assert.IsNull (ex.ParamName, "#4");
662 Assert.IsNull (ex.InnerException, "#5");
667 public void PreAuthenticate ()
669 FileWebRequest req = (FileWebRequest) WebRequest.Create (_tempFileUri);
670 Assert.IsFalse (req.PreAuthenticate, "#1");
671 req.PreAuthenticate = true;
672 Assert.IsTrue (req.PreAuthenticate, "#2");
678 FileWebRequest req = (FileWebRequest) WebRequest.Create (_tempFileUri);
679 Assert.IsNull (req.Proxy, "#1");
680 req.Proxy = new WebProxy ();
681 Assert.IsNotNull (req.Proxy, "#2");
683 Assert.IsNull (req.Proxy, "#3");
687 public void RequestUri ()
689 FileWebRequest req = (FileWebRequest) WebRequest.Create (_tempFileUri);
690 Assert.AreSame (_tempFileUri, req.RequestUri);
694 public void Timeout ()
696 FileWebRequest req = (FileWebRequest) WebRequest.Create (_tempFileUri);
697 Assert.AreEqual (100000, req.Timeout, "#1");
698 req.Timeout = int.MaxValue;
699 Assert.AreEqual (int.MaxValue, req.Timeout, "#2");
701 Assert.AreEqual (0, req.Timeout, "#3");
705 public void Timeout_Negative ()
707 FileWebRequest req = (FileWebRequest) WebRequest.Create (_tempFileUri);
709 Assert.AreEqual (-1, req.Timeout, "#1");
713 } catch (ArgumentOutOfRangeException ex) {
714 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#3");
715 Assert.IsNotNull (ex.Message, "#4");
717 Assert.IsNotNull (ex.ParamName, "#5");
719 Assert.IsFalse (ex.ParamName == "value", "#6");
721 Assert.AreEqual ("value", ex.ParamName, "#6");
724 Assert.IsNull (ex.InnerException, "#7");
730 //FIXME: include Java serialization compliant tests
731 [Ignore ("The MS compliant binary serialization is not supported")]
733 public void GetObjectData ()
735 FileWebRequest fwr = (FileWebRequest) WebRequest.Create ("file:///test.txt");
736 fwr.ConnectionGroupName = "CGN";
737 fwr.ContentLength = 10;
738 fwr.ContentType = "image/png";
739 fwr.Credentials = new NetworkCredential ("Miguel", "de Icaza", "Novell");
740 fwr.Headers.Add ("Disposition", "attach");
742 fwr.PreAuthenticate = true;
743 fwr.Proxy = new WebProxy ("proxy.ximian.com");
746 SerializationInfo si = new SerializationInfo (typeof (FileWebRequest),
747 new FormatterConverter ());
748 ((ISerializable) fwr).GetObjectData (si, new StreamingContext ());
749 Assert.AreEqual (9, si.MemberCount, "#A1");
751 foreach (SerializationEntry entry in si) {
752 Assert.IsNotNull (entry.Name, "#B1:" + i);
753 Assert.IsNotNull (entry.ObjectType, "#B2:" + i);
754 Assert.IsNotNull (entry.Value, "#B3:" + i);
758 Assert.AreEqual ("headers", entry.Name, "#B4:" + i);
759 Assert.AreEqual (typeof (WebHeaderCollection), entry.ObjectType, "#B5:" + i);
762 Assert.AreEqual ("proxy", entry.Name, "#B4:" + i);
763 Assert.AreEqual (typeof (IWebProxy), entry.ObjectType, "#B5:" + i);
766 Assert.AreEqual ("uri", entry.Name, "#B4:" + i);
767 Assert.AreEqual (typeof (Uri), entry.ObjectType, "#B5:" + i);
770 Assert.AreEqual ("connectionGroupName", entry.Name, "#B4:" + i);
771 Assert.AreEqual (typeof (string), entry.ObjectType, "#B5:" + i);
772 Assert.AreEqual ("CGN", entry.Value, "#B6:" + i);
775 Assert.AreEqual ("method", entry.Name, "#B4:" + i);
776 Assert.AreEqual (typeof (string), entry.ObjectType, "#B5:" + i);
777 Assert.AreEqual ("PUT", entry.Value, "#B6:" + i);
780 Assert.AreEqual ("contentLength", entry.Name, "#B4:" + i);
781 Assert.AreEqual (typeof (long), entry.ObjectType, "#B5:" + i);
782 Assert.AreEqual (10, entry.Value, "#B6:" + i);
785 Assert.AreEqual ("timeout", entry.Name, "#B4:" + i);
786 Assert.AreEqual (typeof (int), entry.ObjectType, "#B5:" + i);
787 Assert.AreEqual (20, entry.Value, "#B6:" + i);
790 Assert.AreEqual ("fileAccess", entry.Name, "#B4:" + i);
791 Assert.AreEqual (typeof (FileAccess), entry.ObjectType, "#B5:" + i);
792 Assert.AreEqual (FileAccess.Read, entry.Value, "#B6:" + i);
795 Assert.AreEqual ("preauthenticate", entry.Name, "#B4:" + i);
796 Assert.AreEqual (typeof (bool), entry.ObjectType, "#B5:" + i);
798 Assert.AreEqual (false, entry.Value, "#B6:" + i);
800 Assert.AreEqual (true, entry.Value, "#B6:" + i);
809 [Category ("NotWorking")] // Difference at index 272: 20 instead of 19
810 public void Serialize ()
812 FileWebRequest fwr = (FileWebRequest) WebRequest.Create ("file://test.txt/");
813 fwr.ConnectionGroupName = "CGN";
814 fwr.ContentLength = 10;
815 fwr.ContentType = "image/png";
816 fwr.Credentials = new NetworkCredential ("Miguel", "de Icaza", "Novell");
817 fwr.Headers.Add ("Disposition", "attach");
819 fwr.PreAuthenticate = true;
820 fwr.Proxy = new WebProxy ("proxy.ximian.com");
823 BinaryFormatter bf = new BinaryFormatter ();
824 bf.AssemblyFormat = FormatterAssemblyStyle.Full;
826 MemoryStream ms = new MemoryStream ();
827 bf.Serialize (ms, fwr);
830 byte [] buffer = new byte [ms.Length];
831 ms.Read (buffer, 0, buffer.Length);
832 Assert.AreEqual (_serialized, buffer);
837 [Ignore ("The MS compliant binary serialization is not supported")]
839 public void Deserialize ()
841 MemoryStream ms = new MemoryStream ();
842 ms.Write (_serialized, 0, _serialized.Length);
845 BinaryFormatter bf = new BinaryFormatter ();
846 FileWebRequest req = (FileWebRequest) bf.Deserialize (ms);
847 Assert.AreEqual ("CGN", req.ConnectionGroupName, "#A1");
848 Assert.AreEqual (10, req.ContentLength, "#A2");
849 Assert.AreEqual ("image/png", req.ContentType, "#A3");
850 Assert.IsNull (req.Credentials, "#A4");
851 Assert.AreEqual ("PUT", req.Method, "#A5");
853 Assert.IsFalse (req.PreAuthenticate, "#A6");
855 Assert.IsTrue (req.PreAuthenticate, "#A6");
857 Assert.AreEqual ("file://test.txt/", req.RequestUri.AbsoluteUri, "#A7");
858 Assert.AreEqual (20, req.Timeout, "#A8");
860 WebHeaderCollection headers = req.Headers;
861 Assert.IsNotNull (headers, "#C1");
862 Assert.AreEqual (2, headers.Count, "#C2");
863 Assert.AreEqual ("Content-Type", req.Headers.GetKey (0), "#C3");
864 Assert.AreEqual ("image/png", req.Headers.Get (0), "#C4");
865 Assert.AreEqual ("Disposition", req.Headers.GetKey (1), "#C5");
866 Assert.AreEqual ("attach", req.Headers.Get (1), "#C6");
868 WebProxy proxy = req.Proxy as WebProxy;
869 Assert.IsNotNull (proxy, "#D1");
870 Assert.AreEqual ("http://proxy.ximian.com/", proxy.Address.AbsoluteUri, "#D2");
871 Assert.IsNotNull (proxy.BypassArrayList, "#D3");
872 Assert.AreEqual (0, proxy.BypassArrayList.Count, "#D4");
873 Assert.IsNotNull (proxy.BypassList, "#D5");
874 Assert.AreEqual (0, proxy.BypassList.Length, "#D6");
875 Assert.IsFalse (proxy.BypassProxyOnLocal, "#D7");
876 Assert.IsNull (proxy.Credentials, "#D8");
879 private Uri GetTempFileUri ()
881 string tempFile = _tempFile;
883 // remove leading slash for absolute paths
884 tempFile = tempFile.TrimStart ('/');
886 tempFile = tempFile.Replace ('\\', '/');
888 return new Uri ("file:///" + tempFile);
893 private bool RunningOnUnix {
895 Type t = Type.GetType("java.lang.System");
896 MethodInfo mi = t.GetMethod("getProperty", new Type[] { typeof(string) });
897 string osName = (string) mi.Invoke(null, new object[] { "os.name" });
903 return !osName.StartsWith("win", true, CultureInfo.InvariantCulture);
908 private bool RunningOnUnix {
910 // check for Unix platforms - see FAQ for more details
911 // http://www.mono-project.com/FAQ:_Technical#How_to_detect_the_execution_platform_.3F
912 int platform = (int) Environment.OSVersion.Platform;
913 return ((platform == 4) || (platform == 128) || (platform == 6));
918 private static readonly byte [] _serialized = new byte [] {
920 0x00, 0x01, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x01, 0x00,
921 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0c, 0x02, 0x00, 0x00, 0x00,
922 0x49, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x2c, 0x20, 0x56, 0x65,
923 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x3d, 0x32, 0x2e, 0x30, 0x2e, 0x30,
924 0x2e, 0x30, 0x2c, 0x20, 0x43, 0x75, 0x6c, 0x74, 0x75, 0x72, 0x65,
925 0x3d, 0x6e, 0x65, 0x75, 0x74, 0x72, 0x61, 0x6c, 0x2c, 0x20, 0x50,
926 0x75, 0x62, 0x6c, 0x69, 0x63, 0x4b, 0x65, 0x79, 0x54, 0x6f, 0x6b,
927 0x65, 0x6e, 0x3d, 0x62, 0x37, 0x37, 0x61, 0x35, 0x63, 0x35, 0x36,
928 0x31, 0x39, 0x33, 0x34, 0x65, 0x30, 0x38, 0x39, 0x05, 0x01, 0x00,
929 0x00, 0x00, 0x19, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x2e, 0x4e,
930 0x65, 0x74, 0x2e, 0x46, 0x69, 0x6c, 0x65, 0x57, 0x65, 0x62, 0x52,
931 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x09, 0x00, 0x00, 0x00, 0x07,
932 0x68, 0x65, 0x61, 0x64, 0x65, 0x72, 0x73, 0x05, 0x70, 0x72, 0x6f,
933 0x78, 0x79, 0x03, 0x75, 0x72, 0x69, 0x13, 0x63, 0x6f, 0x6e, 0x6e,
934 0x65, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x47, 0x72, 0x6f, 0x75, 0x70,
935 0x4e, 0x61, 0x6d, 0x65, 0x06, 0x6d, 0x65, 0x74, 0x68, 0x6f, 0x64,
936 0x0d, 0x63, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x4c, 0x65, 0x6e,
937 0x67, 0x74, 0x68, 0x07, 0x74, 0x69, 0x6d, 0x65, 0x6f, 0x75, 0x74,
938 0x0a, 0x66, 0x69, 0x6c, 0x65, 0x41, 0x63, 0x63, 0x65, 0x73, 0x73,
939 0x0f, 0x70, 0x72, 0x65, 0x61, 0x75, 0x74, 0x68, 0x65, 0x6e, 0x74,
940 0x69, 0x63, 0x61, 0x74, 0x65, 0x04, 0x04, 0x04, 0x01, 0x01, 0x00,
941 0x00, 0x03, 0x00, 0x1e, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x2e,
942 0x4e, 0x65, 0x74, 0x2e, 0x57, 0x65, 0x62, 0x48, 0x65, 0x61, 0x64,
943 0x65, 0x72, 0x43, 0x6f, 0x6c, 0x6c, 0x65, 0x63, 0x74, 0x69, 0x6f,
944 0x6e, 0x02, 0x00, 0x00, 0x00, 0x13, 0x53, 0x79, 0x73, 0x74, 0x65,
945 0x6d, 0x2e, 0x4e, 0x65, 0x74, 0x2e, 0x57, 0x65, 0x62, 0x50, 0x72,
946 0x6f, 0x78, 0x79, 0x02, 0x00, 0x00, 0x00, 0x0a, 0x53, 0x79, 0x73,
947 0x74, 0x65, 0x6d, 0x2e, 0x55, 0x72, 0x69, 0x02, 0x00, 0x00, 0x00,
948 0x09, 0x08, 0x14, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x2e, 0x49,
949 0x4f, 0x2e, 0x46, 0x69, 0x6c, 0x65, 0x41, 0x63, 0x63, 0x65, 0x73,
950 0x73, 0x01, 0x02, 0x00, 0x00, 0x00, 0x09, 0x03, 0x00, 0x00, 0x00,
951 0x09, 0x04, 0x00, 0x00, 0x00, 0x09, 0x05, 0x00, 0x00, 0x00, 0x06,
952 0x06, 0x00, 0x00, 0x00, 0x03, 0x43, 0x47, 0x4e, 0x06, 0x07, 0x00,
953 0x00, 0x00, 0x03, 0x50, 0x55, 0x54, 0x0a, 0x00, 0x00, 0x00, 0x00,
954 0x00, 0x00, 0x00, 0x14, 0x00, 0x00, 0x00, 0x04, 0xf8, 0xff, 0xff,
955 0xff, 0x14, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x2e, 0x49, 0x4f,
956 0x2e, 0x46, 0x69, 0x6c, 0x65, 0x41, 0x63, 0x63, 0x65, 0x73, 0x73,
957 0x01, 0x00, 0x00, 0x00, 0x07, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x5f,
958 0x5f, 0x00, 0x08, 0x01, 0x00, 0x00, 0x00, 0x00, 0x05, 0x03, 0x00,
959 0x00, 0x00, 0x1e, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x2e, 0x4e,
960 0x65, 0x74, 0x2e, 0x57, 0x65, 0x62, 0x48, 0x65, 0x61, 0x64, 0x65,
961 0x72, 0x43, 0x6f, 0x6c, 0x6c, 0x65, 0x63, 0x74, 0x69, 0x6f, 0x6e,
962 0x05, 0x00, 0x00, 0x00, 0x05, 0x43, 0x6f, 0x75, 0x6e, 0x74, 0x01,
963 0x30, 0x01, 0x32, 0x01, 0x31, 0x01, 0x33, 0x00, 0x01, 0x01, 0x01,
964 0x01, 0x08, 0x02, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x06,
965 0x09, 0x00, 0x00, 0x00, 0x0c, 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x6e,
966 0x74, 0x2d, 0x54, 0x79, 0x70, 0x65, 0x06, 0x0a, 0x00, 0x00, 0x00,
967 0x09, 0x69, 0x6d, 0x61, 0x67, 0x65, 0x2f, 0x70, 0x6e, 0x67, 0x06,
968 0x0b, 0x00, 0x00, 0x00, 0x0b, 0x44, 0x69, 0x73, 0x70, 0x6f, 0x73,
969 0x69, 0x74, 0x69, 0x6f, 0x6e, 0x06, 0x0c, 0x00, 0x00, 0x00, 0x06,
970 0x61, 0x74, 0x74, 0x61, 0x63, 0x68, 0x05, 0x04, 0x00, 0x00, 0x00,
971 0x13, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x2e, 0x4e, 0x65, 0x74,
972 0x2e, 0x57, 0x65, 0x62, 0x50, 0x72, 0x6f, 0x78, 0x79, 0x04, 0x00,
973 0x00, 0x00, 0x0e, 0x5f, 0x42, 0x79, 0x70, 0x61, 0x73, 0x73, 0x4f,
974 0x6e, 0x4c, 0x6f, 0x63, 0x61, 0x6c, 0x0d, 0x5f, 0x50, 0x72, 0x6f,
975 0x78, 0x79, 0x41, 0x64, 0x64, 0x72, 0x65, 0x73, 0x73, 0x0b, 0x5f,
976 0x42, 0x79, 0x70, 0x61, 0x73, 0x73, 0x4c, 0x69, 0x73, 0x74, 0x16,
977 0x5f, 0x55, 0x73, 0x65, 0x44, 0x65, 0x66, 0x61, 0x75, 0x6c, 0x74,
978 0x43, 0x72, 0x65, 0x64, 0x65, 0x6e, 0x74, 0x69, 0x61, 0x6c, 0x73,
979 0x00, 0x04, 0x02, 0x00, 0x01, 0x0a, 0x53, 0x79, 0x73, 0x74, 0x65,
980 0x6d, 0x2e, 0x55, 0x72, 0x69, 0x02, 0x00, 0x00, 0x00, 0x01, 0x02,
981 0x00, 0x00, 0x00, 0x00, 0x09, 0x0d, 0x00, 0x00, 0x00, 0x0a, 0x00,
982 0x05, 0x05, 0x00, 0x00, 0x00, 0x0a, 0x53, 0x79, 0x73, 0x74, 0x65,
983 0x6d, 0x2e, 0x55, 0x72, 0x69, 0x01, 0x00, 0x00, 0x00, 0x0b, 0x41,
984 0x62, 0x73, 0x6f, 0x6c, 0x75, 0x74, 0x65, 0x55, 0x72, 0x69, 0x01,
985 0x02, 0x00, 0x00, 0x00, 0x06, 0x0e, 0x00, 0x00, 0x00, 0x10, 0x66,
986 0x69, 0x6c, 0x65, 0x3a, 0x2f, 0x2f, 0x74, 0x65, 0x73, 0x74, 0x2e,
987 0x74, 0x78, 0x74, 0x2f, 0x01, 0x0d, 0x00, 0x00, 0x00, 0x05, 0x00,
988 0x00, 0x00, 0x06, 0x0f, 0x00, 0x00, 0x00, 0x18, 0x68, 0x74, 0x74,
989 0x70, 0x3a, 0x2f, 0x2f, 0x70, 0x72, 0x6f, 0x78, 0x79, 0x2e, 0x78,
990 0x69, 0x6d, 0x69, 0x61, 0x6e, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x0b
992 0x00, 0x01, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x01, 0x00,
993 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0c, 0x02, 0x00, 0x00, 0x00,
994 0x4c, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x2c, 0x20, 0x56, 0x65,
995 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x3d, 0x31, 0x2e, 0x30, 0x2e, 0x35,
996 0x30, 0x30, 0x30, 0x2e, 0x30, 0x2c, 0x20, 0x43, 0x75, 0x6c, 0x74,
997 0x75, 0x72, 0x65, 0x3d, 0x6e, 0x65, 0x75, 0x74, 0x72, 0x61, 0x6c,
998 0x2c, 0x20, 0x50, 0x75, 0x62, 0x6c, 0x69, 0x63, 0x4b, 0x65, 0x79,
999 0x54, 0x6f, 0x6b, 0x65, 0x6e, 0x3d, 0x62, 0x37, 0x37, 0x61, 0x35,
1000 0x63, 0x35, 0x36, 0x31, 0x39, 0x33, 0x34, 0x65, 0x30, 0x38, 0x39,
1001 0x05, 0x01, 0x00, 0x00, 0x00, 0x19, 0x53, 0x79, 0x73, 0x74, 0x65,
1002 0x6d, 0x2e, 0x4e, 0x65, 0x74, 0x2e, 0x46, 0x69, 0x6c, 0x65, 0x57,
1003 0x65, 0x62, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x09, 0x00,
1004 0x00, 0x00, 0x07, 0x68, 0x65, 0x61, 0x64, 0x65, 0x72, 0x73, 0x05,
1005 0x70, 0x72, 0x6f, 0x78, 0x79, 0x03, 0x75, 0x72, 0x69, 0x13, 0x63,
1006 0x6f, 0x6e, 0x6e, 0x65, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x47, 0x72,
1007 0x6f, 0x75, 0x70, 0x4e, 0x61, 0x6d, 0x65, 0x06, 0x6d, 0x65, 0x74,
1008 0x68, 0x6f, 0x64, 0x0d, 0x63, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74,
1009 0x4c, 0x65, 0x6e, 0x67, 0x74, 0x68, 0x07, 0x74, 0x69, 0x6d, 0x65,
1010 0x6f, 0x75, 0x74, 0x0a, 0x66, 0x69, 0x6c, 0x65, 0x41, 0x63, 0x63,
1011 0x65, 0x73, 0x73, 0x0f, 0x70, 0x72, 0x65, 0x61, 0x75, 0x74, 0x68,
1012 0x65, 0x6e, 0x74, 0x69, 0x63, 0x61, 0x74, 0x65, 0x04, 0x04, 0x04,
1013 0x01, 0x01, 0x00, 0x00, 0x03, 0x00, 0x1e, 0x53, 0x79, 0x73, 0x74,
1014 0x65, 0x6d, 0x2e, 0x4e, 0x65, 0x74, 0x2e, 0x57, 0x65, 0x62, 0x48,
1015 0x65, 0x61, 0x64, 0x65, 0x72, 0x43, 0x6f, 0x6c, 0x6c, 0x65, 0x63,
1016 0x74, 0x69, 0x6f, 0x6e, 0x02, 0x00, 0x00, 0x00, 0x13, 0x53, 0x79,
1017 0x73, 0x74, 0x65, 0x6d, 0x2e, 0x4e, 0x65, 0x74, 0x2e, 0x57, 0x65,
1018 0x62, 0x50, 0x72, 0x6f, 0x78, 0x79, 0x02, 0x00, 0x00, 0x00, 0x0a,
1019 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x2e, 0x55, 0x72, 0x69, 0x02,
1020 0x00, 0x00, 0x00, 0x09, 0x08, 0x14, 0x53, 0x79, 0x73, 0x74, 0x65,
1021 0x6d, 0x2e, 0x49, 0x4f, 0x2e, 0x46, 0x69, 0x6c, 0x65, 0x41, 0x63,
1022 0x63, 0x65, 0x73, 0x73, 0x01, 0x02, 0x00, 0x00, 0x00, 0x09, 0x03,
1023 0x00, 0x00, 0x00, 0x09, 0x04, 0x00, 0x00, 0x00, 0x09, 0x05, 0x00,
1024 0x00, 0x00, 0x06, 0x06, 0x00, 0x00, 0x00, 0x03, 0x43, 0x47, 0x4e,
1025 0x06, 0x07, 0x00, 0x00, 0x00, 0x03, 0x50, 0x55, 0x54, 0x0a, 0x00,
1026 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x14, 0x00, 0x00, 0x00, 0x04,
1027 0xf8, 0xff, 0xff, 0xff, 0x14, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d,
1028 0x2e, 0x49, 0x4f, 0x2e, 0x46, 0x69, 0x6c, 0x65, 0x41, 0x63, 0x63,
1029 0x65, 0x73, 0x73, 0x01, 0x00, 0x00, 0x00, 0x07, 0x76, 0x61, 0x6c,
1030 0x75, 0x65, 0x5f, 0x5f, 0x00, 0x08, 0x01, 0x00, 0x00, 0x00, 0x01,
1031 0x05, 0x03, 0x00, 0x00, 0x00, 0x1e, 0x53, 0x79, 0x73, 0x74, 0x65,
1032 0x6d, 0x2e, 0x4e, 0x65, 0x74, 0x2e, 0x57, 0x65, 0x62, 0x48, 0x65,
1033 0x61, 0x64, 0x65, 0x72, 0x43, 0x6f, 0x6c, 0x6c, 0x65, 0x63, 0x74,
1034 0x69, 0x6f, 0x6e, 0x05, 0x00, 0x00, 0x00, 0x05, 0x43, 0x6f, 0x75,
1035 0x6e, 0x74, 0x01, 0x30, 0x01, 0x32, 0x01, 0x31, 0x01, 0x33, 0x00,
1036 0x01, 0x01, 0x01, 0x01, 0x08, 0x02, 0x00, 0x00, 0x00, 0x02, 0x00,
1037 0x00, 0x00, 0x06, 0x09, 0x00, 0x00, 0x00, 0x0c, 0x43, 0x6f, 0x6e,
1038 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x54, 0x79, 0x70, 0x65, 0x06, 0x0a,
1039 0x00, 0x00, 0x00, 0x09, 0x69, 0x6d, 0x61, 0x67, 0x65, 0x2f, 0x70,
1040 0x6e, 0x67, 0x06, 0x0b, 0x00, 0x00, 0x00, 0x0b, 0x44, 0x69, 0x73,
1041 0x70, 0x6f, 0x73, 0x69, 0x74, 0x69, 0x6f, 0x6e, 0x06, 0x0c, 0x00,
1042 0x00, 0x00, 0x06, 0x61, 0x74, 0x74, 0x61, 0x63, 0x68, 0x05, 0x04,
1043 0x00, 0x00, 0x00, 0x13, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x2e,
1044 0x4e, 0x65, 0x74, 0x2e, 0x57, 0x65, 0x62, 0x50, 0x72, 0x6f, 0x78,
1045 0x79, 0x03, 0x00, 0x00, 0x00, 0x0e, 0x5f, 0x42, 0x79, 0x70, 0x61,
1046 0x73, 0x73, 0x4f, 0x6e, 0x4c, 0x6f, 0x63, 0x61, 0x6c, 0x0d, 0x5f,
1047 0x50, 0x72, 0x6f, 0x78, 0x79, 0x41, 0x64, 0x64, 0x72, 0x65, 0x73,
1048 0x73, 0x0b, 0x5f, 0x42, 0x79, 0x70, 0x61, 0x73, 0x73, 0x4c, 0x69,
1049 0x73, 0x74, 0x00, 0x04, 0x02, 0x01, 0x0a, 0x53, 0x79, 0x73, 0x74,
1050 0x65, 0x6d, 0x2e, 0x55, 0x72, 0x69, 0x02, 0x00, 0x00, 0x00, 0x02,
1051 0x00, 0x00, 0x00, 0x00, 0x09, 0x0d, 0x00, 0x00, 0x00, 0x0a, 0x05,
1052 0x05, 0x00, 0x00, 0x00, 0x0a, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d,
1053 0x2e, 0x55, 0x72, 0x69, 0x01, 0x00, 0x00, 0x00, 0x0b, 0x41, 0x62,
1054 0x73, 0x6f, 0x6c, 0x75, 0x74, 0x65, 0x55, 0x72, 0x69, 0x01, 0x02,
1055 0x00, 0x00, 0x00, 0x06, 0x0e, 0x00, 0x00, 0x00, 0x10, 0x66, 0x69,
1056 0x6c, 0x65, 0x3a, 0x2f, 0x2f, 0x74, 0x65, 0x73, 0x74, 0x2e, 0x74,
1057 0x78, 0x74, 0x2f, 0x01, 0x0d, 0x00, 0x00, 0x00, 0x05, 0x00, 0x00,
1058 0x00, 0x06, 0x0f, 0x00, 0x00, 0x00, 0x18, 0x68, 0x74, 0x74, 0x70,
1059 0x3a, 0x2f, 0x2f, 0x70, 0x72, 0x6f, 0x78, 0x79, 0x2e, 0x78, 0x69,
1060 0x6d, 0x69, 0x61, 0x6e, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x0b