Revert "Merge pull request #5330 from alexanderkyte/dedup_mkbundle"
[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 ("identity", 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: identity");
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                 internal static byte [] GzipResponseHandler (Socket socket)
526                 {
527                         StringWriter sw = new StringWriter ();
528                         sw.NewLine = "\r\n";
529                         sw.WriteLine ("HTTP/1.0 200 OK");
530                         sw.WriteLine ("Server: Mono/Test");
531                         sw.WriteLine ("Content-Encoding: gzip");
532                         sw.WriteLine ("Content-Type: text/xml; charset=UTF-8");
533                         sw.WriteLine ();
534                         sw.Flush ();
535
536                         var gzipDummyXml = new byte[] {
537                                 0x1f, 0x8b, 0x08, 0x08, 0xb6, 0xb1, 0xd3, 0x58, 0x00, 0x03, 0x74, 0x65, 0x73, 0x74, 0x67, 0x7a,
538                                 0x00, 0xb3, 0x49, 0x29, 0xcd, 0xcd, 0xad, 0x54, 0xd0, 0xb7, 0x03, 0x00, 0xed, 0x55, 0x32, 0xec,
539                                 0x09, 0x00, 0x00, 0x00 };
540                         var header = Encoding.UTF8.GetBytes (sw.ToString ());
541                         
542                         var response = new byte[gzipDummyXml.Length + header.Length];
543                         header.CopyTo(response, 0);
544                         gzipDummyXml.CopyTo(response, header.Length);
545
546                         return response;
547                 }
548         }
549
550         [TestFixture]
551         public class HttpResponseStreamTest
552         {
553                 [Test]
554 #if FEATURE_NO_BSD_SOCKETS
555                 [ExpectedException (typeof (PlatformNotSupportedException))]
556 #endif
557                 public void BeginRead_Buffer_Null ()
558                 {
559                         IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint();
560                         string url = "http://" + ep.ToString () + "/test/";
561
562                         using (SocketResponder responder = new SocketResponder (ep, s => HttpWebResponseTest.FullResponseHandler (s))) {
563                                 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
564                                 req.Method = "GET";
565                                 req.Timeout = 2000;
566                                 req.ReadWriteTimeout = 2000;
567                                 req.KeepAlive = false;
568
569                                 using (HttpWebResponse resp = (HttpWebResponse) req.GetResponse ()) {
570                                         Stream rs = resp.GetResponseStream ();
571                                         byte [] buffer = null;
572                                         try {
573                                                 try {
574                                                         rs.BeginRead (buffer, 0, 0, null, null);
575                                                         Assert.Fail ("#A1");
576                                                 } catch (ArgumentNullException ex) {
577                                                         Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#A2");
578                                                         Assert.IsNull (ex.InnerException, "#A3");
579                                                         Assert.IsNotNull (ex.Message, "#A4");
580                                                         Assert.AreEqual ("buffer", ex.ParamName, "#A5");
581                                                 }
582
583                                                 // read full response
584                                                 buffer = new byte [24];
585                                                 Assert.AreEqual (9, rs.Read (buffer, 0, buffer.Length));
586
587                                                 buffer = null;
588                                                 try {
589                                                         rs.BeginRead (buffer, 0, 0, null, null);
590                                                         Assert.Fail ("#B1");
591                                                 } catch (ArgumentNullException ex) {
592                                                         Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#B2");
593                                                         Assert.IsNull (ex.InnerException, "#B3");
594                                                         Assert.IsNotNull (ex.Message, "#B4");
595                                                         Assert.AreEqual ("buffer", ex.ParamName, "#B5");
596                                                 }
597                                         } finally {
598                                                 rs.Close ();
599                                                 req.Abort ();
600                                         }
601                                 }
602                         }
603                 }
604
605                 [Test]
606 #if FEATURE_NO_BSD_SOCKETS
607                 [ExpectedException (typeof (PlatformNotSupportedException))]
608 #endif
609                 public void BeginWrite ()
610                 {
611                         IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint();
612                         string url = "http://" + ep.ToString () + "/test/";
613
614                         using (SocketResponder responder = new SocketResponder (ep, s => HttpWebResponseTest.FullResponseHandler (s))) {
615                                 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
616                                 req.Method = "GET";
617                                 req.Timeout = 2000;
618                                 req.ReadWriteTimeout = 2000;
619                                 req.KeepAlive = false;
620
621                                 using (HttpWebResponse resp = (HttpWebResponse) req.GetResponse ()) {
622                                         Stream rs = resp.GetResponseStream ();
623                                         byte [] buffer = new byte [5];
624                                         try {
625                                                 rs.BeginWrite (buffer, 0, buffer.Length, null, null);
626                                                 Assert.Fail ("#1");
627                                         } catch (NotSupportedException ex) {
628                                                 // The stream does not support writing
629                                                 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#2");
630                                                 Assert.IsNull (ex.InnerException, "#3");
631                                                 Assert.IsNotNull (ex.Message, "#4");
632                                         } finally {
633                                                 rs.Close ();
634                                                 req.Abort ();
635                                         }
636                                 }
637                         }
638                 }
639
640                 [Test]
641                 [Category ("NotWorking")]
642                 public void CanRead ()
643                 {
644                         IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint();
645                         string url = "http://" + ep.ToString () + "/test/";
646
647                         using (SocketResponder responder = new SocketResponder (ep, s => HttpWebResponseTest.FullResponseHandler (s))) {
648                                 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
649                                 req.Method = "GET";
650                                 req.Timeout = 2000;
651                                 req.ReadWriteTimeout = 2000;
652                                 req.KeepAlive = false;
653
654                                 using (HttpWebResponse resp = (HttpWebResponse) req.GetResponse ()) {
655                                         Stream rs = resp.GetResponseStream ();
656                                         try {
657                                                 Assert.IsTrue (rs.CanRead, "#1");
658                                                 rs.Close ();
659                                                 Assert.IsFalse (rs.CanRead, "#2");
660                                         } finally {
661                                                 rs.Close ();
662                                                 req.Abort ();
663                                         }
664                                 }
665                         }
666                 }
667
668                 [Test]
669 #if FEATURE_NO_BSD_SOCKETS
670                 [ExpectedException (typeof (PlatformNotSupportedException))]
671 #endif
672                 public void CanSeek ()
673                 {
674                         IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint();
675                         string url = "http://" + ep.ToString () + "/test/";
676
677                         using (SocketResponder responder = new SocketResponder (ep, s => HttpWebResponseTest.FullResponseHandler (s))) {
678                                 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
679                                 req.Method = "GET";
680                                 req.Timeout = 2000;
681                                 req.ReadWriteTimeout = 2000;
682                                 req.KeepAlive = false;
683
684                                 using (HttpWebResponse resp = (HttpWebResponse) req.GetResponse ()) {
685                                         Stream rs = resp.GetResponseStream ();
686                                         try {
687                                                 Assert.IsFalse (rs.CanSeek, "#1");
688                                                 rs.Close ();
689                                                 Assert.IsFalse (rs.CanSeek, "#2");
690                                         } finally {
691                                                 rs.Close ();
692                                                 req.Abort ();
693                                         }
694                                 }
695                         }
696                 }
697
698                 [Test] // bug #324182
699 #if FEATURE_NO_BSD_SOCKETS
700                 [ExpectedException (typeof (PlatformNotSupportedException))]
701 #endif
702                 public void CanTimeout ()
703                 {
704                         IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint();
705                         string url = "http://" + ep.ToString () + "/test/";
706
707                         using (SocketResponder responder = new SocketResponder (ep, s => HttpWebResponseTest.FullResponseHandler (s))) {
708                                 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
709                                 req.Method = "GET";
710                                 req.Timeout = 2000;
711                                 req.ReadWriteTimeout = 2000;
712                                 req.KeepAlive = false;
713
714                                 using (HttpWebResponse resp = (HttpWebResponse) req.GetResponse ()) {
715                                         Stream rs = resp.GetResponseStream ();
716                                         try {
717                                                 Assert.IsTrue (rs.CanTimeout, "#1");
718                                                 rs.Close ();
719                                                 Assert.IsTrue (rs.CanTimeout, "#2");
720                                         } finally {
721                                                 rs.Close ();
722                                                 req.Abort ();
723                                         }
724                                 }
725                         }
726                 }
727
728                 [Test]
729 #if FEATURE_NO_BSD_SOCKETS
730                 [ExpectedException (typeof (PlatformNotSupportedException))]
731 #endif
732                 public void CanWrite ()
733                 {
734                         IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint();
735                         string url = "http://" + ep.ToString () + "/test/";
736
737                         using (SocketResponder responder = new SocketResponder (ep, s => HttpWebResponseTest.FullResponseHandler (s))) {
738                                 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
739                                 req.Method = "GET";
740                                 req.Timeout = 2000;
741                                 req.ReadWriteTimeout = 2000;
742                                 req.KeepAlive = false;
743
744                                 using (HttpWebResponse resp = (HttpWebResponse) req.GetResponse ()) {
745                                         Stream rs = resp.GetResponseStream ();
746                                         try {
747                                                 Assert.IsFalse (rs.CanWrite, "#1");
748                                                 rs.Close ();
749                                                 Assert.IsFalse (rs.CanWrite, "#2");
750                                         } finally {
751                                                 rs.Close ();
752                                                 req.Abort ();
753                                         }
754                                 }
755                         }
756                 }
757
758                 [Test]
759 #if FEATURE_NO_BSD_SOCKETS
760                 [ExpectedException (typeof (PlatformNotSupportedException))]
761 #endif
762                 public void Read ()
763                 {
764                         IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint();
765                         string url = "http://" + ep.ToString () + "/test/";
766
767                         using (SocketResponder responder = new SocketResponder (ep, s => HttpWebResponseTest.FullResponseHandler (s))) {
768                                 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
769                                 req.Method = "GET";
770                                 req.Timeout = 2000;
771                                 req.ReadWriteTimeout = 2000;
772                                 req.KeepAlive = false;
773
774                                 using (HttpWebResponse resp = (HttpWebResponse) req.GetResponse ()) {
775                                         Stream rs = resp.GetResponseStream ();
776                                         byte [] buffer = new byte [5];
777                                         try {
778                                                 Assert.AreEqual (1, rs.Read (buffer, 4, 1), "#A1");
779                                                 Assert.AreEqual (new byte [] { 0x00, 0x00, 0x00, 0x00, 0x3c }, buffer, "#A2");
780                                                 Assert.AreEqual (2, rs.Read (buffer, 0, 2), "#B1");
781                                                 Assert.AreEqual (new byte [] { 0x64, 0x75, 0x00, 0x00, 0x3c }, buffer, "#B2");
782                                                 Assert.AreEqual (4, rs.Read (buffer, 1, 4), "#C1");
783                                                 Assert.AreEqual (new byte [] { 0x64, 0x6d, 0x6d, 0x79, 0x20 }, buffer, "#C2");
784                                                 Assert.AreEqual (2, rs.Read (buffer, 0, 3), "#D1");
785                                                 Assert.AreEqual (new byte [] { 0x2f, 0x3e, 0x6d, 0x79, 0x20 }, buffer, "#D2");
786                                                 Assert.AreEqual (0, rs.Read (buffer, 1, 3), "#E1");
787                                                 Assert.AreEqual (new byte [] { 0x2f, 0x3e, 0x6d, 0x79, 0x20 }, buffer, "#E2");
788                                                 Assert.AreEqual (0, rs.Read (buffer, buffer.Length, 0), "#G1");
789                                                 Assert.AreEqual (new byte [] { 0x2f, 0x3e, 0x6d, 0x79, 0x20 }, buffer, "#G2");
790                                         } finally {
791                                                 rs.Close ();
792                                                 req.Abort ();
793                                         }
794                                 }
795                         }
796                 }
797
798                 [Test]
799 #if FEATURE_NO_BSD_SOCKETS
800                 [ExpectedException (typeof (PlatformNotSupportedException))]
801 #endif
802                 public void Read_Buffer_Null ()
803                 {
804                         IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint();
805                         string url = "http://" + ep.ToString () + "/test/";
806
807                         using (SocketResponder responder = new SocketResponder (ep, s => HttpWebResponseTest.FullResponseHandler (s))) {
808                                 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
809                                 req.Method = "GET";
810                                 req.Timeout = 2000;
811                                 req.ReadWriteTimeout = 2000;
812                                 req.KeepAlive = false;
813
814                                 using (HttpWebResponse resp = (HttpWebResponse) req.GetResponse ()) {
815                                         Stream rs = resp.GetResponseStream ();
816                                         byte [] buffer = null;
817                                         try {
818                                                 try {
819                                                         rs.Read (buffer, 0, 0);
820                                                         Assert.Fail ("#A1");
821                                                 } catch (ArgumentNullException ex) {
822                                                         Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#A2");
823                                                         Assert.IsNull (ex.InnerException, "#A3");
824                                                         Assert.IsNotNull (ex.Message, "#A4");
825                                                         Assert.AreEqual ("buffer", ex.ParamName, "#A5");
826                                                 }
827
828                                                 // read full response
829                                                 buffer = new byte [24];
830                                                 Assert.AreEqual (9, rs.Read (buffer, 0, buffer.Length));
831
832                                                 buffer = null;
833                                                 try {
834                                                         rs.Read (buffer, 0, 0);
835                                                         Assert.Fail ("#B1");
836                                                 } catch (ArgumentNullException ex) {
837                                                         Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#B2");
838                                                         Assert.IsNull (ex.InnerException, "#B3");
839                                                         Assert.IsNotNull (ex.Message, "#B4");
840                                                         Assert.AreEqual ("buffer", ex.ParamName, "#B5");
841                                                 }
842                                         } finally {
843                                                 rs.Close ();
844                                                 req.Abort ();
845                                         }
846                                 }
847                         }
848                 }
849
850                 [Test]
851 #if FEATURE_NO_BSD_SOCKETS
852                 [ExpectedException (typeof (PlatformNotSupportedException))]
853 #endif
854                 public void Read_Count_Negative ()
855                 {
856                         IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint();
857                         string url = "http://" + ep.ToString () + "/test/";
858
859                         using (SocketResponder responder = new SocketResponder (ep, s => HttpWebResponseTest.FullResponseHandler (s))) {
860                                 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
861                                 req.Method = "GET";
862                                 req.Timeout = 2000;
863                                 req.ReadWriteTimeout = 2000;
864                                 req.KeepAlive = false;
865
866                                 using (HttpWebResponse resp = (HttpWebResponse) req.GetResponse ()) {
867                                         Stream rs = resp.GetResponseStream ();
868                                         byte [] buffer = new byte [5];
869                                         try {
870                                                 try {
871                                                         rs.Read (buffer, 1, -1);
872                                                         Assert.Fail ("#A1");
873                                                 } catch (ArgumentOutOfRangeException ex) {
874                                                         // Specified argument was out of the range of valid values
875                                                         Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2");
876                                                         Assert.IsNull (ex.InnerException, "#A3");
877                                                         Assert.IsNotNull (ex.Message, "#A4");
878                                                         Assert.AreEqual ("size", ex.ParamName, "#A5");
879                                                 }
880
881                                                 // read full response
882                                                 buffer = new byte [24];
883                                                 Assert.AreEqual (9, rs.Read (buffer, 0, buffer.Length));
884
885                                                 try {
886                                                         rs.Read (buffer, 1, -1);
887                                                         Assert.Fail ("#B1");
888                                                 } catch (ArgumentOutOfRangeException ex) {
889                                                         // Specified argument was out of the range of valid values
890                                                         Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#B2");
891                                                         Assert.IsNull (ex.InnerException, "#B3");
892                                                         Assert.IsNotNull (ex.Message, "#B4");
893                                                         Assert.AreEqual ("size", ex.ParamName, "#B5");
894                                                 }
895                                         } finally {
896                                                 rs.Close ();
897                                                 req.Abort ();
898                                         }
899                                 }
900                         }
901                 }
902
903                 [Test]
904 #if FEATURE_NO_BSD_SOCKETS
905                 [ExpectedException (typeof (PlatformNotSupportedException))]
906 #endif
907                 public void Read_Count_Overflow ()
908                 {
909                         IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint();
910                         string url = "http://" + ep.ToString () + "/test/";
911
912                         using (SocketResponder responder = new SocketResponder (ep, s => HttpWebResponseTest.FullResponseHandler (s))) {
913                                 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
914                                 req.Method = "GET";
915                                 req.Timeout = 2000;
916                                 req.ReadWriteTimeout = 2000;
917                                 req.KeepAlive = false;
918
919                                 using (HttpWebResponse resp = (HttpWebResponse) req.GetResponse ()) {
920                                         Stream rs = resp.GetResponseStream ();
921                                         byte [] buffer = new byte [5];
922                                         try {
923                                                 try {
924                                                         rs.Read (buffer, buffer.Length - 2, 3);
925                                                         Assert.Fail ("#A1");
926                                                 } catch (ArgumentOutOfRangeException ex) {
927                                                         // Specified argument was out of the range of valid values
928                                                         Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2");
929                                                         Assert.IsNull (ex.InnerException, "#A3");
930                                                         Assert.IsNotNull (ex.Message, "#A4");
931                                                         Assert.AreEqual ("size", ex.ParamName, "#A5");
932                                                 }
933
934                                                 // read full response
935                                                 buffer = new byte [24];
936                                                 Assert.AreEqual (9, rs.Read (buffer, 0, buffer.Length));
937
938                                                 try {
939                                                         rs.Read (buffer, buffer.Length - 2, 3);
940                                                         Assert.Fail ("#B1");
941                                                 } catch (ArgumentOutOfRangeException ex) {
942                                                         // Specified argument was out of the range of valid values
943                                                         Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#B2");
944                                                         Assert.IsNull (ex.InnerException, "#B3");
945                                                         Assert.IsNotNull (ex.Message, "#B4");
946                                                         Assert.AreEqual ("size", ex.ParamName, "#B5");
947                                                 }
948                                         } finally {
949                                                 rs.Close ();
950                                                 req.Abort ();
951                                         }
952                                 }
953                         }
954                 }
955
956                 [Test]
957 #if FEATURE_NO_BSD_SOCKETS
958                 [ExpectedException (typeof (PlatformNotSupportedException))]
959 #endif
960                 public void Read_Offset_Negative ()
961                 {
962                         IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint();
963                         string url = "http://" + ep.ToString () + "/test/";
964
965                         using (SocketResponder responder = new SocketResponder (ep, s => HttpWebResponseTest.FullResponseHandler (s))) {
966                                 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
967                                 req.Method = "GET";
968                                 req.Timeout = 2000;
969                                 req.ReadWriteTimeout = 2000;
970                                 req.KeepAlive = false;
971
972                                 using (HttpWebResponse resp = (HttpWebResponse) req.GetResponse ()) {
973                                         Stream rs = resp.GetResponseStream ();
974                                         byte [] buffer = new byte [5];
975                                         try {
976                                                 try {
977                                                         rs.Read (buffer, -1, 0);
978                                                         Assert.Fail ("#A1");
979                                                 } catch (ArgumentOutOfRangeException ex) {
980                                                         // Specified argument was out of the range of valid values
981                                                         Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2");
982                                                         Assert.IsNull (ex.InnerException, "#A3");
983                                                         Assert.IsNotNull (ex.Message, "#A4");
984                                                         Assert.AreEqual ("offset", ex.ParamName, "#A5");
985                                                 }
986
987                                                 // read full response
988                                                 buffer = new byte [24];
989                                                 Assert.AreEqual (9, rs.Read (buffer, 0, buffer.Length));
990
991                                                 try {
992                                                         rs.Read (buffer, -1, 0);
993                                                         Assert.Fail ("#B1");
994                                                 } catch (ArgumentOutOfRangeException ex) {
995                                                         // Specified argument was out of the range of valid values
996                                                         Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#B2");
997                                                         Assert.IsNull (ex.InnerException, "#B3");
998                                                         Assert.IsNotNull (ex.Message, "#B4");
999                                                         Assert.AreEqual ("offset", ex.ParamName, "#B5");
1000                                                 }
1001                                         } finally {
1002                                                 rs.Close ();
1003                                                 req.Abort ();
1004                                         }
1005                                 }
1006                         }
1007                 }
1008
1009                 [Test]
1010 #if FEATURE_NO_BSD_SOCKETS
1011                 [ExpectedException (typeof (PlatformNotSupportedException))]
1012 #endif
1013                 public void Read_Offset_Overflow ()
1014                 {
1015                         IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint();
1016                         string url = "http://" + ep.ToString () + "/test/";
1017
1018                         using (SocketResponder responder = new SocketResponder (ep, s => HttpWebResponseTest.FullResponseHandler (s))) {
1019                                 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
1020                                 req.Method = "GET";
1021                                 req.Timeout = 2000;
1022                                 req.ReadWriteTimeout = 2000;
1023                                 req.KeepAlive = false;
1024
1025                                 using (HttpWebResponse resp = (HttpWebResponse) req.GetResponse ()) {
1026                                         Stream rs = resp.GetResponseStream ();
1027                                         byte [] buffer = new byte [5];
1028                                         try {
1029                                                 try {
1030                                                         rs.Read (buffer, buffer.Length + 1, 0);
1031                                                         Assert.Fail ("#A1");
1032                                                 } catch (ArgumentOutOfRangeException ex) {
1033                                                         // Specified argument was out of the range of valid values
1034                                                         Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2");
1035                                                         Assert.IsNull (ex.InnerException, "#A3");
1036                                                         Assert.IsNotNull (ex.Message, "#A4");
1037                                                         Assert.AreEqual ("offset", ex.ParamName, "#A5");
1038                                                 }
1039
1040                                                 // read full response
1041                                                 buffer = new byte [24];
1042                                                 Assert.AreEqual (9, rs.Read (buffer, 0, buffer.Length));
1043
1044                                                 try {
1045                                                         rs.Read (buffer, buffer.Length + 1, 0);
1046                                                         Assert.Fail ("#B1");
1047                                                 } catch (ArgumentOutOfRangeException ex) {
1048                                                         // Specified argument was out of the range of valid values
1049                                                         Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#B2");
1050                                                         Assert.IsNull (ex.InnerException, "#B3");
1051                                                         Assert.IsNotNull (ex.Message, "#B4");
1052                                                         Assert.AreEqual ("offset", ex.ParamName, "#B5");
1053                                                 }
1054                                         } finally {
1055                                                 rs.Close ();
1056                                                 req.Abort ();
1057                                         }
1058                                 }
1059                         }
1060                 }
1061
1062                 [Test]
1063                 [Category ("NotWorking")]
1064                 public void Read_Stream_Closed ()
1065                 {
1066                         IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint();
1067                         string url = "http://" + ep.ToString () + "/test/";
1068
1069                         using (SocketResponder responder = new SocketResponder (ep, s => HttpWebResponseTest.FullResponseHandler (s))) {
1070                                 HttpWebRequest req;
1071                                 
1072                                 req = (HttpWebRequest) WebRequest.Create (url);
1073                                 req.Method = "GET";
1074                                 req.Timeout = 2000;
1075                                 req.ReadWriteTimeout = 2000;
1076                                 req.KeepAlive = false;
1077
1078                                 using (HttpWebResponse resp = (HttpWebResponse) req.GetResponse ()) {
1079                                         Stream rs = resp.GetResponseStream ();
1080                                         rs.Close ();
1081                                         try {
1082                                                 rs.Read (new byte [0], 0, 0);
1083                                                 Assert.Fail ("#A1");
1084                                         } catch (WebException ex) {
1085                                                 // The request was aborted: The connection was closed unexpectedly
1086                                                 Assert.AreEqual (typeof (WebException), ex.GetType (), "#A2");
1087                                                 Assert.IsNull (ex.InnerException, "#A3");
1088                                                 Assert.IsNotNull (ex.Message, "#A4");
1089                                                 Assert.IsNull (ex.Response, "#A5");
1090                                                 Assert.AreEqual (WebExceptionStatus.ConnectionClosed, ex.Status, "#A6");
1091                                         } finally {
1092                                                 rs.Close ();
1093                                                 req.Abort ();
1094                                         }
1095                                 }
1096
1097                                 req = (HttpWebRequest) WebRequest.Create (url);
1098                                 req.Method = "GET";
1099                                 req.Timeout = 2000;
1100                                 req.ReadWriteTimeout = 2000;
1101                                 req.KeepAlive = false;
1102
1103                                 using (HttpWebResponse resp = (HttpWebResponse) req.GetResponse ()) {
1104                                         Stream rs = resp.GetResponseStream ();
1105                                         byte [] buffer = new byte [24];
1106                                         Assert.AreEqual (9, rs.Read (buffer, 0, buffer.Length));
1107                                         rs.Close ();
1108                                         try {
1109                                                 rs.Read (new byte [0], 0, 0);
1110                                                 Assert.Fail ("#B1");
1111                                         } catch (WebException ex) {
1112                                                 // The request was aborted: The connection was closed unexpectedly
1113                                                 Assert.AreEqual (typeof (WebException), ex.GetType (), "#B2");
1114                                                 Assert.IsNull (ex.InnerException, "#B3");
1115                                                 Assert.IsNotNull (ex.Message, "#B4");
1116                                                 Assert.IsNull (ex.Response, "#B5");
1117                                                 Assert.AreEqual (WebExceptionStatus.ConnectionClosed, ex.Status, "#B6");
1118                                         } finally {
1119                                                 rs.Close ();
1120                                                 req.Abort ();
1121                                         }
1122                                 }
1123                         }
1124                 }
1125
1126                 [Test]
1127 #if FEATURE_NO_BSD_SOCKETS
1128                 [ExpectedException (typeof (PlatformNotSupportedException))]
1129 #endif
1130                 public void ReadTimeout ()
1131                 {
1132                         IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint();
1133                         string url = "http://" + ep.ToString () + "/test/";
1134
1135                         using (SocketResponder responder = new SocketResponder (ep, s => HttpWebResponseTest.FullResponseHandler (s))) {
1136                                 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
1137                                 req.Method = "GET";
1138                                 req.Timeout = 2000;
1139                                 req.ReadWriteTimeout = 2000;
1140                                 req.KeepAlive = false;
1141
1142                                 using (HttpWebResponse resp = (HttpWebResponse) req.GetResponse ()) {
1143                                         Stream rs = resp.GetResponseStream ();
1144                                         try {
1145                                                 Assert.AreEqual (2000, rs.ReadTimeout, "#1");
1146                                                 rs.Close ();
1147                                                 Assert.AreEqual (2000, rs.ReadTimeout, "#2");
1148                                         } finally {
1149                                                 rs.Close ();
1150                                                 req.Abort ();
1151                                         }
1152                                 }
1153                         }
1154                 }
1155
1156                 [Test]
1157 #if FEATURE_NO_BSD_SOCKETS
1158                 [ExpectedException (typeof (PlatformNotSupportedException))]
1159 #endif
1160                 public void Write ()
1161                 {
1162                         IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint();
1163                         string url = "http://" + ep.ToString () + "/test/";
1164
1165                         using (SocketResponder responder = new SocketResponder (ep, s => HttpWebResponseTest.FullResponseHandler (s))) {
1166                                 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
1167                                 req.Method = "GET";
1168                                 req.Timeout = 2000;
1169                                 req.ReadWriteTimeout = 2000;
1170                                 req.KeepAlive = false;
1171
1172                                 using (HttpWebResponse resp = (HttpWebResponse) req.GetResponse ()) {
1173                                         Stream rs = resp.GetResponseStream ();
1174                                         byte [] buffer = new byte [5];
1175                                         try {
1176                                                 rs.Write (buffer, 0, buffer.Length);
1177                                                 Assert.Fail ("#1");
1178                                         } catch (NotSupportedException ex) {
1179                                                 // The stream does not support writing
1180                                                 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#2");
1181                                                 Assert.IsNull (ex.InnerException, "#3");
1182                                                 Assert.IsNotNull (ex.Message, "#4");
1183                                         } finally {
1184                                                 rs.Close ();
1185                                                 req.Abort ();
1186                                         }
1187                                 }
1188                         }
1189                 }
1190
1191                 [Test]
1192 #if FEATURE_NO_BSD_SOCKETS
1193                 [ExpectedException (typeof (PlatformNotSupportedException))]
1194 #endif
1195                 public void WriteTimeout ()
1196                 {
1197                         IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint();
1198                         string url = "http://" + ep.ToString () + "/test/";
1199
1200                         using (SocketResponder responder = new SocketResponder (ep, s => HttpWebResponseTest.FullResponseHandler (s))) {
1201                                 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
1202                                 req.Method = "GET";
1203                                 req.Timeout = 2000;
1204                                 req.ReadWriteTimeout = 2000;
1205                                 req.KeepAlive = false;
1206
1207                                 using (HttpWebResponse resp = (HttpWebResponse) req.GetResponse ()) {
1208                                         Stream rs = resp.GetResponseStream ();
1209                                         try {
1210                                                 Assert.AreEqual (2000, rs.WriteTimeout, "#1");
1211                                                 rs.Close ();
1212                                                 Assert.AreEqual (2000, rs.WriteTimeout, "#2");
1213                                         } finally {
1214                                                 rs.Close ();
1215                                                 req.Abort ();
1216                                         }
1217                                 }
1218                         }
1219                 }
1220
1221
1222                 [Test]
1223 #if FEATURE_NO_BSD_SOCKETS
1224                 [ExpectedException (typeof (PlatformNotSupportedException))]
1225 #endif
1226                 public void AutomaticDecompression ()
1227                 {
1228                         IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint();
1229                         string url = "http://" + ep.ToString () + "/test/";
1230
1231                         using (SocketResponder responder = new SocketResponder (ep, s => HttpWebResponseTest.GzipResponseHandler (s))) {
1232                                 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
1233                                 req.Method = "GET";
1234                                 req.Timeout = 2000;
1235                                 req.ReadWriteTimeout = 2000;
1236                                 req.KeepAlive = false;
1237                                 req.AutomaticDecompression = DecompressionMethods.GZip;
1238
1239                                 using (HttpWebResponse resp = (HttpWebResponse) req.GetResponse ()) {
1240                                         Stream rs = resp.GetResponseStream ();
1241                                         byte [] buffer = new byte [24];
1242                                         try {
1243                                                 // read full response
1244                                                 Assert.AreEqual (9, rs.Read (buffer, 0, buffer.Length));
1245                                                 Assert.IsNull (resp.Headers[HttpRequestHeader.ContentEncoding]);
1246                                         } finally {
1247                                                 rs.Close ();
1248                                                 req.Abort ();
1249                                         }
1250                                 }
1251                         }
1252                 }
1253         }
1254 }