HttpWebResponseTest.cs 40 KB

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