[System*] Throw a PlatformNotSupported exception when using the networking stack...
[mono.git] / mcs / class / System / Test / System.Net / HttpWebResponseTest.cs
1 //
2 // HttpWebResponseTest.cs - NUnit Test Cases for System.Net.HttpWebResponse
3 //
4 // Authors:
5 //      Gert Driesen (drieseng@users.sourceforge.net)
6 //
7 // Copyright (c) 2008 Gert Driesen
8 //
9
10 using System;
11 using System.Globalization;
12 using System.IO;
13 using System.Net;
14 using System.Net.Sockets;
15 using System.Text;
16
17 using MonoTests.Helpers;
18
19 using NUnit.Framework;
20
21 namespace MonoTests.System.Net
22 {
23         [TestFixture]
24         public class HttpWebResponseTest
25         {
26                 [Test]
27 #if FEATURE_NO_BSD_SOCKETS
28                 [ExpectedException (typeof (PlatformNotSupportedException))]
29 #endif
30                 public void CharacterSet_Disposed ()
31                 {
32                         IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint();
33                         string url = "http://" + ep.ToString () + "/test/";
34
35                         using (SocketResponder responder = new SocketResponder (ep, s => FullResponseHandler (s))) {
36                                 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
37                                 req.Method = "GET";
38                                 req.Timeout = 2000;
39                                 req.ReadWriteTimeout = 2000;
40                                 req.KeepAlive = false;
41
42                                 HttpWebResponse resp = (HttpWebResponse) req.GetResponse ();
43                                 ((IDisposable) resp).Dispose ();
44
45                                 try {
46                                         string charset = resp.CharacterSet;
47                                         Assert.Fail ("#1:" + charset);
48                                 } catch (ObjectDisposedException ex) {
49                                         Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
50                                         Assert.IsNull (ex.InnerException, "#3");
51                                         Assert.IsNotNull (ex.Message, "#4");
52                                         Assert.AreEqual (typeof (HttpWebResponse).FullName, ex.ObjectName, "#5");
53                                 }
54                         }
55                 }
56
57                 [Test]
58 #if FEATURE_NO_BSD_SOCKETS
59                 [ExpectedException (typeof (PlatformNotSupportedException))]
60 #endif
61                 public void Close_Disposed ()
62                 {
63                         IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint();
64                         string url = "http://" + ep.ToString () + "/test/";
65
66                         using (SocketResponder responder = new SocketResponder (ep, s => FullResponseHandler (s))) {
67                                 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
68                                 req.Method = "GET";
69                                 req.Timeout = 2000;
70                                 req.ReadWriteTimeout = 2000;
71                                 req.KeepAlive = false;
72
73                                 HttpWebResponse resp = (HttpWebResponse) req.GetResponse ();
74                                 ((IDisposable) resp).Dispose ();
75                                 resp.Close ();
76                         }
77                 }
78
79                 [Test]
80 #if FEATURE_NO_BSD_SOCKETS
81                 [ExpectedException (typeof (PlatformNotSupportedException))]
82 #endif
83                 public void ContentEncoding_Disposed ()
84                 {
85                         IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint();
86                         string url = "http://" + ep.ToString () + "/test/";
87
88                         using (SocketResponder responder = new SocketResponder (ep, s => FullResponseHandler (s))) {
89                                 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
90                                 req.Method = "GET";
91                                 req.Timeout = 2000;
92                                 req.ReadWriteTimeout = 2000;
93                                 req.KeepAlive = false;
94
95                                 HttpWebResponse resp = (HttpWebResponse) req.GetResponse ();
96                                 ((IDisposable) resp).Dispose ();
97
98                                 try {
99                                         string enc = resp.ContentEncoding;
100                                         Assert.Fail ("#1:" + enc);
101                                 } catch (ObjectDisposedException ex) {
102                                         Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
103                                         Assert.IsNull (ex.InnerException, "#3");
104                                         Assert.IsNotNull (ex.Message, "#4");
105                                         Assert.AreEqual (typeof (HttpWebResponse).FullName, ex.ObjectName, "#5");
106                                 }
107                         }
108                 }
109
110                 [Test]
111 #if FEATURE_NO_BSD_SOCKETS
112                 [ExpectedException (typeof (PlatformNotSupportedException))]
113 #endif
114                 public void ContentLength_Disposed ()
115                 {
116                         IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint();
117                         string url = "http://" + ep.ToString () + "/test/";
118
119                         using (SocketResponder responder = new SocketResponder (ep, s => FullResponseHandler (s))) {
120                                 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
121                                 req.Method = "GET";
122                                 req.Timeout = 2000;
123                                 req.ReadWriteTimeout = 2000;
124                                 req.KeepAlive = false;
125
126                                 HttpWebResponse resp = (HttpWebResponse) req.GetResponse ();
127                                 ((IDisposable) resp).Dispose ();
128
129                                 Assert.AreEqual (9, resp.ContentLength);
130                         }
131                 }
132
133                 [Test]
134 #if FEATURE_NO_BSD_SOCKETS
135                 [ExpectedException (typeof (PlatformNotSupportedException))]
136 #endif
137                 public void ContentType_Disposed ()
138                 {
139                         IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint();
140                         string url = "http://" + ep.ToString () + "/test/";
141
142                         using (SocketResponder responder = new SocketResponder (ep, s => FullResponseHandler (s))) {
143                                 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
144                                 req.Method = "GET";
145                                 req.Timeout = 2000;
146                                 req.ReadWriteTimeout = 2000;
147                                 req.KeepAlive = false;
148
149                                 HttpWebResponse resp = (HttpWebResponse) req.GetResponse ();
150                                 ((IDisposable) resp).Dispose ();
151
152                                 try {
153                                         string contentType = resp.ContentType;
154                                         Assert.Fail ("#1:" + contentType);
155                                 } catch (ObjectDisposedException ex) {
156                                         Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
157                                         Assert.IsNull (ex.InnerException, "#3");
158                                         Assert.IsNotNull (ex.Message, "#4");
159                                         Assert.AreEqual (typeof (HttpWebResponse).FullName, ex.ObjectName, "#5");
160                                 }
161                         }
162                 }
163
164                 [Test]
165 #if FEATURE_NO_BSD_SOCKETS
166                 [ExpectedException (typeof (PlatformNotSupportedException))]
167 #endif
168                 public void Cookies_Disposed ()
169                 {
170                         IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint();
171                         string url = "http://" + ep.ToString () + "/test/";
172
173                         using (SocketResponder responder = new SocketResponder (ep, s => FullResponseHandler (s))) {
174                                 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
175                                 req.Method = "GET";
176                                 req.Timeout = 2000;
177                                 req.ReadWriteTimeout = 2000;
178                                 req.KeepAlive = false;
179
180                                 HttpWebResponse resp = (HttpWebResponse) req.GetResponse ();
181                                 ((IDisposable) resp).Dispose ();
182
183                                 try {
184                                         CookieCollection cookies = resp.Cookies;
185                                         Assert.Fail ("#A1:" + cookies);
186                                 } catch (ObjectDisposedException ex) {
187                                         Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#A2");
188                                         Assert.IsNull (ex.InnerException, "#A3");
189                                         Assert.IsNotNull (ex.Message, "#A4");
190                                         Assert.AreEqual (typeof (HttpWebResponse).FullName, ex.ObjectName, "#A5");
191                                 }
192
193                                 try {
194                                         resp.Cookies = new CookieCollection ();
195                                         Assert.Fail ("#B1");
196                                 } catch (ObjectDisposedException ex) {
197                                         Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#B2");
198                                         Assert.IsNull (ex.InnerException, "#B3");
199                                         Assert.IsNotNull (ex.Message, "#B4");
200                                         Assert.AreEqual (typeof (HttpWebResponse).FullName, ex.ObjectName, "#B5");
201                                 }
202                         }
203                 }
204
205                 [Test]
206 #if FEATURE_NO_BSD_SOCKETS
207                 [ExpectedException (typeof (PlatformNotSupportedException))]
208 #endif
209                 public void GetResponseHeader_Disposed ()
210                 {
211                         IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint();
212                         string url = "http://" + ep.ToString () + "/test/";
213
214                         using (SocketResponder responder = new SocketResponder (ep, s => FullResponseHandler (s))) {
215                                 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
216                                 req.Method = "GET";
217                                 req.Timeout = 2000;
218                                 req.ReadWriteTimeout = 2000;
219                                 req.KeepAlive = false;
220
221                                 HttpWebResponse resp = (HttpWebResponse) req.GetResponse ();
222                                 ((IDisposable) resp).Dispose ();
223
224                                 try {
225                                         string server = resp.GetResponseHeader ("Server");
226                                         Assert.Fail ("#1:" + server);
227                                 } catch (ObjectDisposedException ex) {
228                                         Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
229                                         Assert.IsNull (ex.InnerException, "#3");
230                                         Assert.IsNotNull (ex.Message, "#4");
231                                         Assert.AreEqual (typeof (HttpWebResponse).FullName, ex.ObjectName, "#5");
232                                 }
233                         }
234                 }
235
236                 [Test]
237 #if FEATURE_NO_BSD_SOCKETS
238                 [ExpectedException (typeof (PlatformNotSupportedException))]
239 #endif
240                 public void GetResponseStream_Disposed ()
241                 {
242                         IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint();
243                         string url = "http://" + ep.ToString () + "/test/";
244
245                         using (SocketResponder responder = new SocketResponder (ep, s => FullResponseHandler (s))) {
246                                 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
247                                 req.Method = "GET";
248                                 req.Timeout = 2000;
249                                 req.ReadWriteTimeout = 2000;
250                                 req.KeepAlive = false;
251
252                                 HttpWebResponse resp = (HttpWebResponse) req.GetResponse ();
253                                 ((IDisposable) resp).Dispose ();
254
255                                 try {
256                                         Stream s = resp.GetResponseStream ();
257                                         Assert.Fail ("#1:" + s);
258                                 } catch (ObjectDisposedException ex) {
259                                         Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
260                                         Assert.IsNull (ex.InnerException, "#3");
261                                         Assert.IsNotNull (ex.Message, "#4");
262                                         Assert.AreEqual (typeof (HttpWebResponse).FullName, ex.ObjectName, "#5");
263                                 }
264                         }
265                 }
266
267                 [Test]
268 #if FEATURE_NO_BSD_SOCKETS
269                 [ExpectedException (typeof (PlatformNotSupportedException))]
270 #endif
271                 public void Headers_Disposed ()
272                 {
273                         IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint();
274                         string url = "http://" + ep.ToString () + "/test/";
275
276                         using (SocketResponder responder = new SocketResponder (ep, s => FullResponseHandler (s))) {
277                                 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
278                                 req.Method = "GET";
279                                 req.Timeout = 2000;
280                                 req.ReadWriteTimeout = 2000;
281                                 req.KeepAlive = false;
282
283                                 HttpWebResponse resp = (HttpWebResponse) req.GetResponse ();
284                                 ((IDisposable) resp).Dispose ();
285
286                                 WebHeaderCollection headers = resp.Headers;
287                                 Assert.AreEqual (6, headers.Count, "#1");
288                                 Assert.AreEqual ("9", headers ["Content-Length"], "#2");
289                                 Assert.AreEqual ("utf-8", headers ["Content-Encoding"], "#3");
290                                 Assert.AreEqual ("text/xml; charset=UTF-8", headers ["Content-Type"], "#4");
291                                 Assert.AreEqual ("Wed, 08 Jan 2003 23:11:55 GMT", headers ["Last-Modified"], "#5");
292                                 Assert.AreEqual ("UserID=Miguel,StoreProfile=true", headers ["Set-Cookie"], "#6");
293                                 Assert.AreEqual ("Mono/Test", headers ["Server"], "#7");
294                         }
295                 }
296
297                 [Test]
298 #if FEATURE_NO_BSD_SOCKETS
299                 [ExpectedException (typeof (PlatformNotSupportedException))]
300 #endif
301                 public void LastModified_Disposed ()
302                 {
303                         IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint();
304                         string url = "http://" + ep.ToString () + "/test/";
305
306                         using (SocketResponder responder = new SocketResponder (ep, s => FullResponseHandler (s))) {
307                                 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
308                                 req.Method = "GET";
309                                 req.Timeout = 2000;
310                                 req.ReadWriteTimeout = 2000;
311                                 req.KeepAlive = false;
312
313                                 HttpWebResponse resp = (HttpWebResponse) req.GetResponse ();
314                                 ((IDisposable) resp).Dispose ();
315
316                                 try {
317                                         DateTime lastMod = resp.LastModified;
318                                         Assert.Fail ("#1:" + lastMod);
319                                 } catch (ObjectDisposedException ex) {
320                                         Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
321                                         Assert.IsNull (ex.InnerException, "#3");
322                                         Assert.IsNotNull (ex.Message, "#4");
323                                         Assert.AreEqual (typeof (HttpWebResponse).FullName, ex.ObjectName, "#5");
324                                 }
325                         }
326                 }
327
328                 [Test]
329 #if FEATURE_NO_BSD_SOCKETS
330                 [ExpectedException (typeof (PlatformNotSupportedException))]
331 #endif
332                 public void Method_Disposed ()
333                 {
334                         IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint();
335                         string url = "http://" + ep.ToString () + "/test/";
336
337                         using (SocketResponder responder = new SocketResponder (ep, s => FullResponseHandler (s))) {
338                                 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
339                                 req.Method = "GET";
340                                 req.Timeout = 2000;
341                                 req.ReadWriteTimeout = 2000;
342                                 req.KeepAlive = false;
343
344                                 HttpWebResponse resp = (HttpWebResponse) req.GetResponse ();
345                                 ((IDisposable) resp).Dispose ();
346
347                                 try {
348                                         string method = resp.Method;
349                                         Assert.Fail ("#1:" + method);
350                                 } catch (ObjectDisposedException ex) {
351                                         Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
352                                         Assert.IsNull (ex.InnerException, "#3");
353                                         Assert.IsNotNull (ex.Message, "#4");
354                                         Assert.AreEqual (typeof (HttpWebResponse).FullName, ex.ObjectName, "#5");
355                                 }
356                         }
357                 }
358
359                 [Test]
360 #if FEATURE_NO_BSD_SOCKETS
361                 [ExpectedException (typeof (PlatformNotSupportedException))]
362 #endif
363                 public void ProtocolVersion_Disposed ()
364                 {
365                         IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint();
366                         string url = "http://" + ep.ToString () + "/test/";
367
368                         using (SocketResponder responder = new SocketResponder (ep, s => FullResponseHandler (s))) {
369                                 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
370                                 req.Method = "GET";
371                                 req.Timeout = 2000;
372                                 req.ReadWriteTimeout = 2000;
373                                 req.KeepAlive = false;
374
375                                 HttpWebResponse resp = (HttpWebResponse) req.GetResponse ();
376                                 ((IDisposable) resp).Dispose ();
377
378                                 try {
379                                         Version protocolVersion = resp.ProtocolVersion;
380                                         Assert.Fail ("#1:" + protocolVersion);
381                                 } catch (ObjectDisposedException ex) {
382                                         Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
383                                         Assert.IsNull (ex.InnerException, "#3");
384                                         Assert.IsNotNull (ex.Message, "#4");
385                                         Assert.AreEqual (typeof (HttpWebResponse).FullName, ex.ObjectName, "#5");
386                                 }
387                         }
388                 }
389
390                 [Test]
391 #if FEATURE_NO_BSD_SOCKETS
392                 [ExpectedException (typeof (PlatformNotSupportedException))]
393 #endif
394                 public void ResponseUri_Disposed ()
395                 {
396                         IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint();
397                         string url = "http://" + ep.ToString () + "/test/";
398
399                         using (SocketResponder responder = new SocketResponder (ep, s => FullResponseHandler (s))) {
400                                 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
401                                 req.Method = "GET";
402                                 req.Timeout = 2000;
403                                 req.ReadWriteTimeout = 2000;
404                                 req.KeepAlive = false;
405
406                                 HttpWebResponse resp = (HttpWebResponse) req.GetResponse ();
407                                 ((IDisposable) resp).Dispose ();
408
409                                 try {
410                                         Uri respUri = resp.ResponseUri;
411                                         Assert.Fail ("#1:" + respUri);
412                                 } catch (ObjectDisposedException ex) {
413                                         Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
414                                         Assert.IsNull (ex.InnerException, "#3");
415                                         Assert.IsNotNull (ex.Message, "#4");
416                                         Assert.AreEqual (typeof (HttpWebResponse).FullName, ex.ObjectName, "#5");
417                                 }
418                         }
419                 }
420
421                 [Test]
422 #if FEATURE_NO_BSD_SOCKETS
423                 [ExpectedException (typeof (PlatformNotSupportedException))]
424 #endif
425                 public void Server_Disposed ()
426                 {
427                         IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint();
428                         string url = "http://" + ep.ToString () + "/test/";
429
430                         using (SocketResponder responder = new SocketResponder (ep, s => FullResponseHandler (s))) {
431                                 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
432                                 req.Method = "GET";
433                                 req.Timeout = 2000;
434                                 req.ReadWriteTimeout = 2000;
435                                 req.KeepAlive = false;
436
437                                 HttpWebResponse resp = (HttpWebResponse) req.GetResponse ();
438                                 ((IDisposable) resp).Dispose ();
439
440                                 try {
441                                         string server = resp.Server;
442                                         Assert.Fail ("#1:" + server);
443                                 } catch (ObjectDisposedException ex) {
444                                         Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
445                                         Assert.IsNull (ex.InnerException, "#3");
446                                         Assert.IsNotNull (ex.Message, "#4");
447                                         Assert.AreEqual (typeof (HttpWebResponse).FullName, ex.ObjectName, "#5");
448                                 }
449                         }
450                 }
451
452                 [Test]
453 #if FEATURE_NO_BSD_SOCKETS
454                 [ExpectedException (typeof (PlatformNotSupportedException))]
455 #endif
456                 public void StatusCode_Disposed ()
457                 {
458                         IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint();
459                         string url = "http://" + ep.ToString () + "/test/";
460
461                         using (SocketResponder responder = new SocketResponder (ep, s => FullResponseHandler (s))) {
462                                 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
463                                 req.Method = "GET";
464                                 req.Timeout = 2000;
465                                 req.ReadWriteTimeout = 2000;
466                                 req.KeepAlive = false;
467
468                                 HttpWebResponse resp = (HttpWebResponse) req.GetResponse ();
469                                 ((IDisposable) resp).Dispose ();
470
471                                 Assert.AreEqual (HttpStatusCode.OK, resp.StatusCode);
472                         }
473                 }
474
475                 [Test]
476 #if FEATURE_NO_BSD_SOCKETS
477                 [ExpectedException (typeof (PlatformNotSupportedException))]
478 #endif
479                 public void StatusDescription_Disposed ()
480                 {
481                         IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint();
482                         string url = "http://" + ep.ToString () + "/test/";
483
484                         using (SocketResponder responder = new SocketResponder (ep, s => FullResponseHandler (s))) {
485                                 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
486                                 req.Method = "GET";
487                                 req.Timeout = 2000;
488                                 req.ReadWriteTimeout = 2000;
489                                 req.KeepAlive = false;
490
491                                 HttpWebResponse resp = (HttpWebResponse) req.GetResponse ();
492                                 ((IDisposable) resp).Dispose ();
493
494                                 try {
495                                         string statusDesc = resp.StatusDescription;
496                                         Assert.Fail ("#1:" + statusDesc);
497                                 } catch (ObjectDisposedException ex) {
498                                         Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
499                                         Assert.IsNull (ex.InnerException, "#3");
500                                         Assert.IsNotNull (ex.Message, "#4");
501                                         Assert.AreEqual (typeof (HttpWebResponse).FullName, ex.ObjectName, "#5");
502                                 }
503                         }
504                 }
505
506                 internal static byte [] FullResponseHandler (Socket socket)
507                 {
508                         StringWriter sw = new StringWriter ();
509                         sw.NewLine = "\r\n";
510                         sw.WriteLine ("HTTP/1.0 200 OK");
511                         sw.WriteLine ("Server: Mono/Test");
512                         sw.WriteLine ("Last-Modified: Wed, 08 Jan 2003 23:11:55 GMT");
513                         sw.WriteLine ("Content-Encoding: " + Encoding.UTF8.WebName);
514                         sw.WriteLine ("Content-Type: text/xml; charset=UTF-8");
515                         sw.WriteLine ("Content-Length: 9");
516                         sw.WriteLine ("Set-Cookie: UserID=Miguel");
517                         sw.WriteLine ("Set-Cookie: StoreProfile=true");
518                         sw.WriteLine ();
519                         sw.Write ("<dummy />");
520                         sw.Flush ();
521
522                         return Encoding.UTF8.GetBytes (sw.ToString ());
523                 }
524         }
525
526         [TestFixture]
527         public class HttpResponseStreamTest
528         {
529                 [Test]
530 #if FEATURE_NO_BSD_SOCKETS
531                 [ExpectedException (typeof (PlatformNotSupportedException))]
532 #endif
533                 public void BeginRead_Buffer_Null ()
534                 {
535                         IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint();
536                         string url = "http://" + ep.ToString () + "/test/";
537
538                         using (SocketResponder responder = new SocketResponder (ep, s => HttpWebResponseTest.FullResponseHandler (s))) {
539                                 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
540                                 req.Method = "GET";
541                                 req.Timeout = 2000;
542                                 req.ReadWriteTimeout = 2000;
543                                 req.KeepAlive = false;
544
545                                 using (HttpWebResponse resp = (HttpWebResponse) req.GetResponse ()) {
546                                         Stream rs = resp.GetResponseStream ();
547                                         byte [] buffer = null;
548                                         try {
549                                                 try {
550                                                         rs.BeginRead (buffer, 0, 0, null, null);
551                                                         Assert.Fail ("#A1");
552                                                 } catch (ArgumentNullException ex) {
553                                                         Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#A2");
554                                                         Assert.IsNull (ex.InnerException, "#A3");
555                                                         Assert.IsNotNull (ex.Message, "#A4");
556                                                         Assert.AreEqual ("buffer", ex.ParamName, "#A5");
557                                                 }
558
559                                                 // read full response
560                                                 buffer = new byte [24];
561                                                 Assert.AreEqual (9, rs.Read (buffer, 0, buffer.Length));
562
563                                                 buffer = null;
564                                                 try {
565                                                         rs.BeginRead (buffer, 0, 0, null, null);
566                                                         Assert.Fail ("#B1");
567                                                 } catch (ArgumentNullException ex) {
568                                                         Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#B2");
569                                                         Assert.IsNull (ex.InnerException, "#B3");
570                                                         Assert.IsNotNull (ex.Message, "#B4");
571                                                         Assert.AreEqual ("buffer", ex.ParamName, "#B5");
572                                                 }
573                                         } finally {
574                                                 rs.Close ();
575                                                 req.Abort ();
576                                         }
577                                 }
578                         }
579                 }
580
581                 [Test]
582 #if FEATURE_NO_BSD_SOCKETS
583                 [ExpectedException (typeof (PlatformNotSupportedException))]
584 #endif
585                 public void BeginWrite ()
586                 {
587                         IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint();
588                         string url = "http://" + ep.ToString () + "/test/";
589
590                         using (SocketResponder responder = new SocketResponder (ep, s => HttpWebResponseTest.FullResponseHandler (s))) {
591                                 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
592                                 req.Method = "GET";
593                                 req.Timeout = 2000;
594                                 req.ReadWriteTimeout = 2000;
595                                 req.KeepAlive = false;
596
597                                 using (HttpWebResponse resp = (HttpWebResponse) req.GetResponse ()) {
598                                         Stream rs = resp.GetResponseStream ();
599                                         byte [] buffer = new byte [5];
600                                         try {
601                                                 rs.BeginWrite (buffer, 0, buffer.Length, null, null);
602                                                 Assert.Fail ("#1");
603                                         } catch (NotSupportedException ex) {
604                                                 // The stream does not support writing
605                                                 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#2");
606                                                 Assert.IsNull (ex.InnerException, "#3");
607                                                 Assert.IsNotNull (ex.Message, "#4");
608                                         } finally {
609                                                 rs.Close ();
610                                                 req.Abort ();
611                                         }
612                                 }
613                         }
614                 }
615
616                 [Test]
617                 [Category ("NotWorking")]
618                 public void CanRead ()
619                 {
620                         IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint();
621                         string url = "http://" + ep.ToString () + "/test/";
622
623                         using (SocketResponder responder = new SocketResponder (ep, s => HttpWebResponseTest.FullResponseHandler (s))) {
624                                 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
625                                 req.Method = "GET";
626                                 req.Timeout = 2000;
627                                 req.ReadWriteTimeout = 2000;
628                                 req.KeepAlive = false;
629
630                                 using (HttpWebResponse resp = (HttpWebResponse) req.GetResponse ()) {
631                                         Stream rs = resp.GetResponseStream ();
632                                         try {
633                                                 Assert.IsTrue (rs.CanRead, "#1");
634                                                 rs.Close ();
635                                                 Assert.IsFalse (rs.CanRead, "#2");
636                                         } finally {
637                                                 rs.Close ();
638                                                 req.Abort ();
639                                         }
640                                 }
641                         }
642                 }
643
644                 [Test]
645 #if FEATURE_NO_BSD_SOCKETS
646                 [ExpectedException (typeof (PlatformNotSupportedException))]
647 #endif
648                 public void CanSeek ()
649                 {
650                         IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint();
651                         string url = "http://" + ep.ToString () + "/test/";
652
653                         using (SocketResponder responder = new SocketResponder (ep, s => HttpWebResponseTest.FullResponseHandler (s))) {
654                                 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
655                                 req.Method = "GET";
656                                 req.Timeout = 2000;
657                                 req.ReadWriteTimeout = 2000;
658                                 req.KeepAlive = false;
659
660                                 using (HttpWebResponse resp = (HttpWebResponse) req.GetResponse ()) {
661                                         Stream rs = resp.GetResponseStream ();
662                                         try {
663                                                 Assert.IsFalse (rs.CanSeek, "#1");
664                                                 rs.Close ();
665                                                 Assert.IsFalse (rs.CanSeek, "#2");
666                                         } finally {
667                                                 rs.Close ();
668                                                 req.Abort ();
669                                         }
670                                 }
671                         }
672                 }
673
674                 [Test] // bug #324182
675 #if FEATURE_NO_BSD_SOCKETS
676                 [ExpectedException (typeof (PlatformNotSupportedException))]
677 #endif
678                 public void CanTimeout ()
679                 {
680                         IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint();
681                         string url = "http://" + ep.ToString () + "/test/";
682
683                         using (SocketResponder responder = new SocketResponder (ep, s => HttpWebResponseTest.FullResponseHandler (s))) {
684                                 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
685                                 req.Method = "GET";
686                                 req.Timeout = 2000;
687                                 req.ReadWriteTimeout = 2000;
688                                 req.KeepAlive = false;
689
690                                 using (HttpWebResponse resp = (HttpWebResponse) req.GetResponse ()) {
691                                         Stream rs = resp.GetResponseStream ();
692                                         try {
693                                                 Assert.IsTrue (rs.CanTimeout, "#1");
694                                                 rs.Close ();
695                                                 Assert.IsTrue (rs.CanTimeout, "#2");
696                                         } finally {
697                                                 rs.Close ();
698                                                 req.Abort ();
699                                         }
700                                 }
701                         }
702                 }
703
704                 [Test]
705 #if FEATURE_NO_BSD_SOCKETS
706                 [ExpectedException (typeof (PlatformNotSupportedException))]
707 #endif
708                 public void CanWrite ()
709                 {
710                         IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint();
711                         string url = "http://" + ep.ToString () + "/test/";
712
713                         using (SocketResponder responder = new SocketResponder (ep, s => HttpWebResponseTest.FullResponseHandler (s))) {
714                                 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
715                                 req.Method = "GET";
716                                 req.Timeout = 2000;
717                                 req.ReadWriteTimeout = 2000;
718                                 req.KeepAlive = false;
719
720                                 using (HttpWebResponse resp = (HttpWebResponse) req.GetResponse ()) {
721                                         Stream rs = resp.GetResponseStream ();
722                                         try {
723                                                 Assert.IsFalse (rs.CanWrite, "#1");
724                                                 rs.Close ();
725                                                 Assert.IsFalse (rs.CanWrite, "#2");
726                                         } finally {
727                                                 rs.Close ();
728                                                 req.Abort ();
729                                         }
730                                 }
731                         }
732                 }
733
734                 [Test]
735 #if FEATURE_NO_BSD_SOCKETS
736                 [ExpectedException (typeof (PlatformNotSupportedException))]
737 #endif
738                 public void Read ()
739                 {
740                         IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint();
741                         string url = "http://" + ep.ToString () + "/test/";
742
743                         using (SocketResponder responder = new SocketResponder (ep, s => HttpWebResponseTest.FullResponseHandler (s))) {
744                                 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
745                                 req.Method = "GET";
746                                 req.Timeout = 2000;
747                                 req.ReadWriteTimeout = 2000;
748                                 req.KeepAlive = false;
749
750                                 using (HttpWebResponse resp = (HttpWebResponse) req.GetResponse ()) {
751                                         Stream rs = resp.GetResponseStream ();
752                                         byte [] buffer = new byte [5];
753                                         try {
754                                                 Assert.AreEqual (1, rs.Read (buffer, 4, 1), "#A1");
755                                                 Assert.AreEqual (new byte [] { 0x00, 0x00, 0x00, 0x00, 0x3c }, buffer, "#A2");
756                                                 Assert.AreEqual (2, rs.Read (buffer, 0, 2), "#B1");
757                                                 Assert.AreEqual (new byte [] { 0x64, 0x75, 0x00, 0x00, 0x3c }, buffer, "#B2");
758                                                 Assert.AreEqual (4, rs.Read (buffer, 1, 4), "#C1");
759                                                 Assert.AreEqual (new byte [] { 0x64, 0x6d, 0x6d, 0x79, 0x20 }, buffer, "#C2");
760                                                 Assert.AreEqual (2, rs.Read (buffer, 0, 3), "#D1");
761                                                 Assert.AreEqual (new byte [] { 0x2f, 0x3e, 0x6d, 0x79, 0x20 }, buffer, "#D2");
762                                                 Assert.AreEqual (0, rs.Read (buffer, 1, 3), "#E1");
763                                                 Assert.AreEqual (new byte [] { 0x2f, 0x3e, 0x6d, 0x79, 0x20 }, buffer, "#E2");
764                                                 Assert.AreEqual (0, rs.Read (buffer, buffer.Length, 0), "#G1");
765                                                 Assert.AreEqual (new byte [] { 0x2f, 0x3e, 0x6d, 0x79, 0x20 }, buffer, "#G2");
766                                         } finally {
767                                                 rs.Close ();
768                                                 req.Abort ();
769                                         }
770                                 }
771                         }
772                 }
773
774                 [Test]
775 #if FEATURE_NO_BSD_SOCKETS
776                 [ExpectedException (typeof (PlatformNotSupportedException))]
777 #endif
778                 public void Read_Buffer_Null ()
779                 {
780                         IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint();
781                         string url = "http://" + ep.ToString () + "/test/";
782
783                         using (SocketResponder responder = new SocketResponder (ep, s => HttpWebResponseTest.FullResponseHandler (s))) {
784                                 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
785                                 req.Method = "GET";
786                                 req.Timeout = 2000;
787                                 req.ReadWriteTimeout = 2000;
788                                 req.KeepAlive = false;
789
790                                 using (HttpWebResponse resp = (HttpWebResponse) req.GetResponse ()) {
791                                         Stream rs = resp.GetResponseStream ();
792                                         byte [] buffer = null;
793                                         try {
794                                                 try {
795                                                         rs.Read (buffer, 0, 0);
796                                                         Assert.Fail ("#A1");
797                                                 } catch (ArgumentNullException ex) {
798                                                         Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#A2");
799                                                         Assert.IsNull (ex.InnerException, "#A3");
800                                                         Assert.IsNotNull (ex.Message, "#A4");
801                                                         Assert.AreEqual ("buffer", ex.ParamName, "#A5");
802                                                 }
803
804                                                 // read full response
805                                                 buffer = new byte [24];
806                                                 Assert.AreEqual (9, rs.Read (buffer, 0, buffer.Length));
807
808                                                 buffer = null;
809                                                 try {
810                                                         rs.Read (buffer, 0, 0);
811                                                         Assert.Fail ("#B1");
812                                                 } catch (ArgumentNullException ex) {
813                                                         Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#B2");
814                                                         Assert.IsNull (ex.InnerException, "#B3");
815                                                         Assert.IsNotNull (ex.Message, "#B4");
816                                                         Assert.AreEqual ("buffer", ex.ParamName, "#B5");
817                                                 }
818                                         } finally {
819                                                 rs.Close ();
820                                                 req.Abort ();
821                                         }
822                                 }
823                         }
824                 }
825
826                 [Test]
827 #if FEATURE_NO_BSD_SOCKETS
828                 [ExpectedException (typeof (PlatformNotSupportedException))]
829 #endif
830                 public void Read_Count_Negative ()
831                 {
832                         IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint();
833                         string url = "http://" + ep.ToString () + "/test/";
834
835                         using (SocketResponder responder = new SocketResponder (ep, s => HttpWebResponseTest.FullResponseHandler (s))) {
836                                 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
837                                 req.Method = "GET";
838                                 req.Timeout = 2000;
839                                 req.ReadWriteTimeout = 2000;
840                                 req.KeepAlive = false;
841
842                                 using (HttpWebResponse resp = (HttpWebResponse) req.GetResponse ()) {
843                                         Stream rs = resp.GetResponseStream ();
844                                         byte [] buffer = new byte [5];
845                                         try {
846                                                 try {
847                                                         rs.Read (buffer, 1, -1);
848                                                         Assert.Fail ("#A1");
849                                                 } catch (ArgumentOutOfRangeException ex) {
850                                                         // Specified argument was out of the range of valid values
851                                                         Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2");
852                                                         Assert.IsNull (ex.InnerException, "#A3");
853                                                         Assert.IsNotNull (ex.Message, "#A4");
854                                                         Assert.AreEqual ("size", ex.ParamName, "#A5");
855                                                 }
856
857                                                 // read full response
858                                                 buffer = new byte [24];
859                                                 Assert.AreEqual (9, rs.Read (buffer, 0, buffer.Length));
860
861                                                 try {
862                                                         rs.Read (buffer, 1, -1);
863                                                         Assert.Fail ("#B1");
864                                                 } catch (ArgumentOutOfRangeException ex) {
865                                                         // Specified argument was out of the range of valid values
866                                                         Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#B2");
867                                                         Assert.IsNull (ex.InnerException, "#B3");
868                                                         Assert.IsNotNull (ex.Message, "#B4");
869                                                         Assert.AreEqual ("size", ex.ParamName, "#B5");
870                                                 }
871                                         } finally {
872                                                 rs.Close ();
873                                                 req.Abort ();
874                                         }
875                                 }
876                         }
877                 }
878
879                 [Test]
880 #if FEATURE_NO_BSD_SOCKETS
881                 [ExpectedException (typeof (PlatformNotSupportedException))]
882 #endif
883                 public void Read_Count_Overflow ()
884                 {
885                         IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint();
886                         string url = "http://" + ep.ToString () + "/test/";
887
888                         using (SocketResponder responder = new SocketResponder (ep, s => HttpWebResponseTest.FullResponseHandler (s))) {
889                                 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
890                                 req.Method = "GET";
891                                 req.Timeout = 2000;
892                                 req.ReadWriteTimeout = 2000;
893                                 req.KeepAlive = false;
894
895                                 using (HttpWebResponse resp = (HttpWebResponse) req.GetResponse ()) {
896                                         Stream rs = resp.GetResponseStream ();
897                                         byte [] buffer = new byte [5];
898                                         try {
899                                                 try {
900                                                         rs.Read (buffer, buffer.Length - 2, 3);
901                                                         Assert.Fail ("#A1");
902                                                 } catch (ArgumentOutOfRangeException ex) {
903                                                         // Specified argument was out of the range of valid values
904                                                         Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2");
905                                                         Assert.IsNull (ex.InnerException, "#A3");
906                                                         Assert.IsNotNull (ex.Message, "#A4");
907                                                         Assert.AreEqual ("size", ex.ParamName, "#A5");
908                                                 }
909
910                                                 // read full response
911                                                 buffer = new byte [24];
912                                                 Assert.AreEqual (9, rs.Read (buffer, 0, buffer.Length));
913
914                                                 try {
915                                                         rs.Read (buffer, buffer.Length - 2, 3);
916                                                         Assert.Fail ("#B1");
917                                                 } catch (ArgumentOutOfRangeException ex) {
918                                                         // Specified argument was out of the range of valid values
919                                                         Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#B2");
920                                                         Assert.IsNull (ex.InnerException, "#B3");
921                                                         Assert.IsNotNull (ex.Message, "#B4");
922                                                         Assert.AreEqual ("size", ex.ParamName, "#B5");
923                                                 }
924                                         } finally {
925                                                 rs.Close ();
926                                                 req.Abort ();
927                                         }
928                                 }
929                         }
930                 }
931
932                 [Test]
933 #if FEATURE_NO_BSD_SOCKETS
934                 [ExpectedException (typeof (PlatformNotSupportedException))]
935 #endif
936                 public void Read_Offset_Negative ()
937                 {
938                         IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint();
939                         string url = "http://" + ep.ToString () + "/test/";
940
941                         using (SocketResponder responder = new SocketResponder (ep, s => HttpWebResponseTest.FullResponseHandler (s))) {
942                                 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
943                                 req.Method = "GET";
944                                 req.Timeout = 2000;
945                                 req.ReadWriteTimeout = 2000;
946                                 req.KeepAlive = false;
947
948                                 using (HttpWebResponse resp = (HttpWebResponse) req.GetResponse ()) {
949                                         Stream rs = resp.GetResponseStream ();
950                                         byte [] buffer = new byte [5];
951                                         try {
952                                                 try {
953                                                         rs.Read (buffer, -1, 0);
954                                                         Assert.Fail ("#A1");
955                                                 } catch (ArgumentOutOfRangeException ex) {
956                                                         // Specified argument was out of the range of valid values
957                                                         Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2");
958                                                         Assert.IsNull (ex.InnerException, "#A3");
959                                                         Assert.IsNotNull (ex.Message, "#A4");
960                                                         Assert.AreEqual ("offset", ex.ParamName, "#A5");
961                                                 }
962
963                                                 // read full response
964                                                 buffer = new byte [24];
965                                                 Assert.AreEqual (9, rs.Read (buffer, 0, buffer.Length));
966
967                                                 try {
968                                                         rs.Read (buffer, -1, 0);
969                                                         Assert.Fail ("#B1");
970                                                 } catch (ArgumentOutOfRangeException ex) {
971                                                         // Specified argument was out of the range of valid values
972                                                         Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#B2");
973                                                         Assert.IsNull (ex.InnerException, "#B3");
974                                                         Assert.IsNotNull (ex.Message, "#B4");
975                                                         Assert.AreEqual ("offset", ex.ParamName, "#B5");
976                                                 }
977                                         } finally {
978                                                 rs.Close ();
979                                                 req.Abort ();
980                                         }
981                                 }
982                         }
983                 }
984
985                 [Test]
986 #if FEATURE_NO_BSD_SOCKETS
987                 [ExpectedException (typeof (PlatformNotSupportedException))]
988 #endif
989                 public void Read_Offset_Overflow ()
990                 {
991                         IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint();
992                         string url = "http://" + ep.ToString () + "/test/";
993
994                         using (SocketResponder responder = new SocketResponder (ep, s => HttpWebResponseTest.FullResponseHandler (s))) {
995                                 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
996                                 req.Method = "GET";
997                                 req.Timeout = 2000;
998                                 req.ReadWriteTimeout = 2000;
999                                 req.KeepAlive = false;
1000
1001                                 using (HttpWebResponse resp = (HttpWebResponse) req.GetResponse ()) {
1002                                         Stream rs = resp.GetResponseStream ();
1003                                         byte [] buffer = new byte [5];
1004                                         try {
1005                                                 try {
1006                                                         rs.Read (buffer, buffer.Length + 1, 0);
1007                                                         Assert.Fail ("#A1");
1008                                                 } catch (ArgumentOutOfRangeException ex) {
1009                                                         // Specified argument was out of the range of valid values
1010                                                         Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2");
1011                                                         Assert.IsNull (ex.InnerException, "#A3");
1012                                                         Assert.IsNotNull (ex.Message, "#A4");
1013                                                         Assert.AreEqual ("offset", ex.ParamName, "#A5");
1014                                                 }
1015
1016                                                 // read full response
1017                                                 buffer = new byte [24];
1018                                                 Assert.AreEqual (9, rs.Read (buffer, 0, buffer.Length));
1019
1020                                                 try {
1021                                                         rs.Read (buffer, buffer.Length + 1, 0);
1022                                                         Assert.Fail ("#B1");
1023                                                 } catch (ArgumentOutOfRangeException ex) {
1024                                                         // Specified argument was out of the range of valid values
1025                                                         Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#B2");
1026                                                         Assert.IsNull (ex.InnerException, "#B3");
1027                                                         Assert.IsNotNull (ex.Message, "#B4");
1028                                                         Assert.AreEqual ("offset", ex.ParamName, "#B5");
1029                                                 }
1030                                         } finally {
1031                                                 rs.Close ();
1032                                                 req.Abort ();
1033                                         }
1034                                 }
1035                         }
1036                 }
1037
1038                 [Test]
1039                 [Category ("NotWorking")]
1040                 public void Read_Stream_Closed ()
1041                 {
1042                         IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint();
1043                         string url = "http://" + ep.ToString () + "/test/";
1044
1045                         using (SocketResponder responder = new SocketResponder (ep, s => HttpWebResponseTest.FullResponseHandler (s))) {
1046                                 HttpWebRequest req;
1047                                 
1048                                 req = (HttpWebRequest) WebRequest.Create (url);
1049                                 req.Method = "GET";
1050                                 req.Timeout = 2000;
1051                                 req.ReadWriteTimeout = 2000;
1052                                 req.KeepAlive = false;
1053
1054                                 using (HttpWebResponse resp = (HttpWebResponse) req.GetResponse ()) {
1055                                         Stream rs = resp.GetResponseStream ();
1056                                         rs.Close ();
1057                                         try {
1058                                                 rs.Read (new byte [0], 0, 0);
1059                                                 Assert.Fail ("#A1");
1060                                         } catch (WebException ex) {
1061                                                 // The request was aborted: The connection was closed unexpectedly
1062                                                 Assert.AreEqual (typeof (WebException), ex.GetType (), "#A2");
1063                                                 Assert.IsNull (ex.InnerException, "#A3");
1064                                                 Assert.IsNotNull (ex.Message, "#A4");
1065                                                 Assert.IsNull (ex.Response, "#A5");
1066                                                 Assert.AreEqual (WebExceptionStatus.ConnectionClosed, ex.Status, "#A6");
1067                                         } finally {
1068                                                 rs.Close ();
1069                                                 req.Abort ();
1070                                         }
1071                                 }
1072
1073                                 req = (HttpWebRequest) WebRequest.Create (url);
1074                                 req.Method = "GET";
1075                                 req.Timeout = 2000;
1076                                 req.ReadWriteTimeout = 2000;
1077                                 req.KeepAlive = false;
1078
1079                                 using (HttpWebResponse resp = (HttpWebResponse) req.GetResponse ()) {
1080                                         Stream rs = resp.GetResponseStream ();
1081                                         byte [] buffer = new byte [24];
1082                                         Assert.AreEqual (9, rs.Read (buffer, 0, buffer.Length));
1083                                         rs.Close ();
1084                                         try {
1085                                                 rs.Read (new byte [0], 0, 0);
1086                                                 Assert.Fail ("#B1");
1087                                         } catch (WebException ex) {
1088                                                 // The request was aborted: The connection was closed unexpectedly
1089                                                 Assert.AreEqual (typeof (WebException), ex.GetType (), "#B2");
1090                                                 Assert.IsNull (ex.InnerException, "#B3");
1091                                                 Assert.IsNotNull (ex.Message, "#B4");
1092                                                 Assert.IsNull (ex.Response, "#B5");
1093                                                 Assert.AreEqual (WebExceptionStatus.ConnectionClosed, ex.Status, "#B6");
1094                                         } finally {
1095                                                 rs.Close ();
1096                                                 req.Abort ();
1097                                         }
1098                                 }
1099                         }
1100                 }
1101
1102                 [Test]
1103 #if FEATURE_NO_BSD_SOCKETS
1104                 [ExpectedException (typeof (PlatformNotSupportedException))]
1105 #endif
1106                 public void ReadTimeout ()
1107                 {
1108                         IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint();
1109                         string url = "http://" + ep.ToString () + "/test/";
1110
1111                         using (SocketResponder responder = new SocketResponder (ep, s => HttpWebResponseTest.FullResponseHandler (s))) {
1112                                 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
1113                                 req.Method = "GET";
1114                                 req.Timeout = 2000;
1115                                 req.ReadWriteTimeout = 2000;
1116                                 req.KeepAlive = false;
1117
1118                                 using (HttpWebResponse resp = (HttpWebResponse) req.GetResponse ()) {
1119                                         Stream rs = resp.GetResponseStream ();
1120                                         try {
1121                                                 Assert.AreEqual (2000, rs.ReadTimeout, "#1");
1122                                                 rs.Close ();
1123                                                 Assert.AreEqual (2000, rs.ReadTimeout, "#2");
1124                                         } finally {
1125                                                 rs.Close ();
1126                                                 req.Abort ();
1127                                         }
1128                                 }
1129                         }
1130                 }
1131
1132                 [Test]
1133 #if FEATURE_NO_BSD_SOCKETS
1134                 [ExpectedException (typeof (PlatformNotSupportedException))]
1135 #endif
1136                 public void Write ()
1137                 {
1138                         IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint();
1139                         string url = "http://" + ep.ToString () + "/test/";
1140
1141                         using (SocketResponder responder = new SocketResponder (ep, s => HttpWebResponseTest.FullResponseHandler (s))) {
1142                                 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
1143                                 req.Method = "GET";
1144                                 req.Timeout = 2000;
1145                                 req.ReadWriteTimeout = 2000;
1146                                 req.KeepAlive = false;
1147
1148                                 using (HttpWebResponse resp = (HttpWebResponse) req.GetResponse ()) {
1149                                         Stream rs = resp.GetResponseStream ();
1150                                         byte [] buffer = new byte [5];
1151                                         try {
1152                                                 rs.Write (buffer, 0, buffer.Length);
1153                                                 Assert.Fail ("#1");
1154                                         } catch (NotSupportedException ex) {
1155                                                 // The stream does not support writing
1156                                                 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#2");
1157                                                 Assert.IsNull (ex.InnerException, "#3");
1158                                                 Assert.IsNotNull (ex.Message, "#4");
1159                                         } finally {
1160                                                 rs.Close ();
1161                                                 req.Abort ();
1162                                         }
1163                                 }
1164                         }
1165                 }
1166
1167                 [Test]
1168 #if FEATURE_NO_BSD_SOCKETS
1169                 [ExpectedException (typeof (PlatformNotSupportedException))]
1170 #endif
1171                 public void WriteTimeout ()
1172                 {
1173                         IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint();
1174                         string url = "http://" + ep.ToString () + "/test/";
1175
1176                         using (SocketResponder responder = new SocketResponder (ep, s => HttpWebResponseTest.FullResponseHandler (s))) {
1177                                 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
1178                                 req.Method = "GET";
1179                                 req.Timeout = 2000;
1180                                 req.ReadWriteTimeout = 2000;
1181                                 req.KeepAlive = false;
1182
1183                                 using (HttpWebResponse resp = (HttpWebResponse) req.GetResponse ()) {
1184                                         Stream rs = resp.GetResponseStream ();
1185                                         try {
1186                                                 Assert.AreEqual (2000, rs.WriteTimeout, "#1");
1187                                                 rs.Close ();
1188                                                 Assert.AreEqual (2000, rs.WriteTimeout, "#2");
1189                                         } finally {
1190                                                 rs.Close ();
1191                                                 req.Abort ();
1192                                         }
1193                                 }
1194                         }
1195                 }
1196         }
1197 }