HttpWebResponseTest.cs 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197
  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 ("utf-8", 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: " + Encoding.UTF8.WebName);
  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. }
  456. [TestFixture]
  457. public class HttpResponseStreamTest
  458. {
  459. [Test]
  460. #if FEATURE_NO_BSD_SOCKETS
  461. [ExpectedException (typeof (PlatformNotSupportedException))]
  462. #endif
  463. public void BeginRead_Buffer_Null ()
  464. {
  465. IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint();
  466. string url = "http://" + ep.ToString () + "/test/";
  467. using (SocketResponder responder = new SocketResponder (ep, s => HttpWebResponseTest.FullResponseHandler (s))) {
  468. HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
  469. req.Method = "GET";
  470. req.Timeout = 2000;
  471. req.ReadWriteTimeout = 2000;
  472. req.KeepAlive = false;
  473. using (HttpWebResponse resp = (HttpWebResponse) req.GetResponse ()) {
  474. Stream rs = resp.GetResponseStream ();
  475. byte [] buffer = null;
  476. try {
  477. try {
  478. rs.BeginRead (buffer, 0, 0, null, null);
  479. Assert.Fail ("#A1");
  480. } catch (ArgumentNullException ex) {
  481. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#A2");
  482. Assert.IsNull (ex.InnerException, "#A3");
  483. Assert.IsNotNull (ex.Message, "#A4");
  484. Assert.AreEqual ("buffer", ex.ParamName, "#A5");
  485. }
  486. // read full response
  487. buffer = new byte [24];
  488. Assert.AreEqual (9, rs.Read (buffer, 0, buffer.Length));
  489. buffer = null;
  490. try {
  491. rs.BeginRead (buffer, 0, 0, null, null);
  492. Assert.Fail ("#B1");
  493. } catch (ArgumentNullException ex) {
  494. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#B2");
  495. Assert.IsNull (ex.InnerException, "#B3");
  496. Assert.IsNotNull (ex.Message, "#B4");
  497. Assert.AreEqual ("buffer", ex.ParamName, "#B5");
  498. }
  499. } finally {
  500. rs.Close ();
  501. req.Abort ();
  502. }
  503. }
  504. }
  505. }
  506. [Test]
  507. #if FEATURE_NO_BSD_SOCKETS
  508. [ExpectedException (typeof (PlatformNotSupportedException))]
  509. #endif
  510. public void BeginWrite ()
  511. {
  512. IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint();
  513. string url = "http://" + ep.ToString () + "/test/";
  514. using (SocketResponder responder = new SocketResponder (ep, s => HttpWebResponseTest.FullResponseHandler (s))) {
  515. HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
  516. req.Method = "GET";
  517. req.Timeout = 2000;
  518. req.ReadWriteTimeout = 2000;
  519. req.KeepAlive = false;
  520. using (HttpWebResponse resp = (HttpWebResponse) req.GetResponse ()) {
  521. Stream rs = resp.GetResponseStream ();
  522. byte [] buffer = new byte [5];
  523. try {
  524. rs.BeginWrite (buffer, 0, buffer.Length, null, null);
  525. Assert.Fail ("#1");
  526. } catch (NotSupportedException ex) {
  527. // The stream does not support writing
  528. Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#2");
  529. Assert.IsNull (ex.InnerException, "#3");
  530. Assert.IsNotNull (ex.Message, "#4");
  531. } finally {
  532. rs.Close ();
  533. req.Abort ();
  534. }
  535. }
  536. }
  537. }
  538. [Test]
  539. [Category ("NotWorking")]
  540. public void CanRead ()
  541. {
  542. IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint();
  543. string url = "http://" + ep.ToString () + "/test/";
  544. using (SocketResponder responder = new SocketResponder (ep, s => HttpWebResponseTest.FullResponseHandler (s))) {
  545. HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
  546. req.Method = "GET";
  547. req.Timeout = 2000;
  548. req.ReadWriteTimeout = 2000;
  549. req.KeepAlive = false;
  550. using (HttpWebResponse resp = (HttpWebResponse) req.GetResponse ()) {
  551. Stream rs = resp.GetResponseStream ();
  552. try {
  553. Assert.IsTrue (rs.CanRead, "#1");
  554. rs.Close ();
  555. Assert.IsFalse (rs.CanRead, "#2");
  556. } finally {
  557. rs.Close ();
  558. req.Abort ();
  559. }
  560. }
  561. }
  562. }
  563. [Test]
  564. #if FEATURE_NO_BSD_SOCKETS
  565. [ExpectedException (typeof (PlatformNotSupportedException))]
  566. #endif
  567. public void CanSeek ()
  568. {
  569. IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint();
  570. string url = "http://" + ep.ToString () + "/test/";
  571. using (SocketResponder responder = new SocketResponder (ep, s => HttpWebResponseTest.FullResponseHandler (s))) {
  572. HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
  573. req.Method = "GET";
  574. req.Timeout = 2000;
  575. req.ReadWriteTimeout = 2000;
  576. req.KeepAlive = false;
  577. using (HttpWebResponse resp = (HttpWebResponse) req.GetResponse ()) {
  578. Stream rs = resp.GetResponseStream ();
  579. try {
  580. Assert.IsFalse (rs.CanSeek, "#1");
  581. rs.Close ();
  582. Assert.IsFalse (rs.CanSeek, "#2");
  583. } finally {
  584. rs.Close ();
  585. req.Abort ();
  586. }
  587. }
  588. }
  589. }
  590. [Test] // bug #324182
  591. #if FEATURE_NO_BSD_SOCKETS
  592. [ExpectedException (typeof (PlatformNotSupportedException))]
  593. #endif
  594. public void CanTimeout ()
  595. {
  596. IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint();
  597. string url = "http://" + ep.ToString () + "/test/";
  598. using (SocketResponder responder = new SocketResponder (ep, s => HttpWebResponseTest.FullResponseHandler (s))) {
  599. HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
  600. req.Method = "GET";
  601. req.Timeout = 2000;
  602. req.ReadWriteTimeout = 2000;
  603. req.KeepAlive = false;
  604. using (HttpWebResponse resp = (HttpWebResponse) req.GetResponse ()) {
  605. Stream rs = resp.GetResponseStream ();
  606. try {
  607. Assert.IsTrue (rs.CanTimeout, "#1");
  608. rs.Close ();
  609. Assert.IsTrue (rs.CanTimeout, "#2");
  610. } finally {
  611. rs.Close ();
  612. req.Abort ();
  613. }
  614. }
  615. }
  616. }
  617. [Test]
  618. #if FEATURE_NO_BSD_SOCKETS
  619. [ExpectedException (typeof (PlatformNotSupportedException))]
  620. #endif
  621. public void CanWrite ()
  622. {
  623. IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint();
  624. string url = "http://" + ep.ToString () + "/test/";
  625. using (SocketResponder responder = new SocketResponder (ep, s => HttpWebResponseTest.FullResponseHandler (s))) {
  626. HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
  627. req.Method = "GET";
  628. req.Timeout = 2000;
  629. req.ReadWriteTimeout = 2000;
  630. req.KeepAlive = false;
  631. using (HttpWebResponse resp = (HttpWebResponse) req.GetResponse ()) {
  632. Stream rs = resp.GetResponseStream ();
  633. try {
  634. Assert.IsFalse (rs.CanWrite, "#1");
  635. rs.Close ();
  636. Assert.IsFalse (rs.CanWrite, "#2");
  637. } finally {
  638. rs.Close ();
  639. req.Abort ();
  640. }
  641. }
  642. }
  643. }
  644. [Test]
  645. #if FEATURE_NO_BSD_SOCKETS
  646. [ExpectedException (typeof (PlatformNotSupportedException))]
  647. #endif
  648. public void Read ()
  649. {
  650. IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint();
  651. string url = "http://" + ep.ToString () + "/test/";
  652. using (SocketResponder responder = new SocketResponder (ep, s => HttpWebResponseTest.FullResponseHandler (s))) {
  653. HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
  654. req.Method = "GET";
  655. req.Timeout = 2000;
  656. req.ReadWriteTimeout = 2000;
  657. req.KeepAlive = false;
  658. using (HttpWebResponse resp = (HttpWebResponse) req.GetResponse ()) {
  659. Stream rs = resp.GetResponseStream ();
  660. byte [] buffer = new byte [5];
  661. try {
  662. Assert.AreEqual (1, rs.Read (buffer, 4, 1), "#A1");
  663. Assert.AreEqual (new byte [] { 0x00, 0x00, 0x00, 0x00, 0x3c }, buffer, "#A2");
  664. Assert.AreEqual (2, rs.Read (buffer, 0, 2), "#B1");
  665. Assert.AreEqual (new byte [] { 0x64, 0x75, 0x00, 0x00, 0x3c }, buffer, "#B2");
  666. Assert.AreEqual (4, rs.Read (buffer, 1, 4), "#C1");
  667. Assert.AreEqual (new byte [] { 0x64, 0x6d, 0x6d, 0x79, 0x20 }, buffer, "#C2");
  668. Assert.AreEqual (2, rs.Read (buffer, 0, 3), "#D1");
  669. Assert.AreEqual (new byte [] { 0x2f, 0x3e, 0x6d, 0x79, 0x20 }, buffer, "#D2");
  670. Assert.AreEqual (0, rs.Read (buffer, 1, 3), "#E1");
  671. Assert.AreEqual (new byte [] { 0x2f, 0x3e, 0x6d, 0x79, 0x20 }, buffer, "#E2");
  672. Assert.AreEqual (0, rs.Read (buffer, buffer.Length, 0), "#G1");
  673. Assert.AreEqual (new byte [] { 0x2f, 0x3e, 0x6d, 0x79, 0x20 }, buffer, "#G2");
  674. } finally {
  675. rs.Close ();
  676. req.Abort ();
  677. }
  678. }
  679. }
  680. }
  681. [Test]
  682. #if FEATURE_NO_BSD_SOCKETS
  683. [ExpectedException (typeof (PlatformNotSupportedException))]
  684. #endif
  685. public void Read_Buffer_Null ()
  686. {
  687. IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint();
  688. string url = "http://" + ep.ToString () + "/test/";
  689. using (SocketResponder responder = new SocketResponder (ep, s => HttpWebResponseTest.FullResponseHandler (s))) {
  690. HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
  691. req.Method = "GET";
  692. req.Timeout = 2000;
  693. req.ReadWriteTimeout = 2000;
  694. req.KeepAlive = false;
  695. using (HttpWebResponse resp = (HttpWebResponse) req.GetResponse ()) {
  696. Stream rs = resp.GetResponseStream ();
  697. byte [] buffer = null;
  698. try {
  699. try {
  700. rs.Read (buffer, 0, 0);
  701. Assert.Fail ("#A1");
  702. } catch (ArgumentNullException ex) {
  703. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#A2");
  704. Assert.IsNull (ex.InnerException, "#A3");
  705. Assert.IsNotNull (ex.Message, "#A4");
  706. Assert.AreEqual ("buffer", ex.ParamName, "#A5");
  707. }
  708. // read full response
  709. buffer = new byte [24];
  710. Assert.AreEqual (9, rs.Read (buffer, 0, buffer.Length));
  711. buffer = null;
  712. try {
  713. rs.Read (buffer, 0, 0);
  714. Assert.Fail ("#B1");
  715. } catch (ArgumentNullException ex) {
  716. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#B2");
  717. Assert.IsNull (ex.InnerException, "#B3");
  718. Assert.IsNotNull (ex.Message, "#B4");
  719. Assert.AreEqual ("buffer", ex.ParamName, "#B5");
  720. }
  721. } finally {
  722. rs.Close ();
  723. req.Abort ();
  724. }
  725. }
  726. }
  727. }
  728. [Test]
  729. #if FEATURE_NO_BSD_SOCKETS
  730. [ExpectedException (typeof (PlatformNotSupportedException))]
  731. #endif
  732. public void Read_Count_Negative ()
  733. {
  734. IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint();
  735. string url = "http://" + ep.ToString () + "/test/";
  736. using (SocketResponder responder = new SocketResponder (ep, s => HttpWebResponseTest.FullResponseHandler (s))) {
  737. HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
  738. req.Method = "GET";
  739. req.Timeout = 2000;
  740. req.ReadWriteTimeout = 2000;
  741. req.KeepAlive = false;
  742. using (HttpWebResponse resp = (HttpWebResponse) req.GetResponse ()) {
  743. Stream rs = resp.GetResponseStream ();
  744. byte [] buffer = new byte [5];
  745. try {
  746. try {
  747. rs.Read (buffer, 1, -1);
  748. Assert.Fail ("#A1");
  749. } catch (ArgumentOutOfRangeException ex) {
  750. // Specified argument was out of the range of valid values
  751. Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2");
  752. Assert.IsNull (ex.InnerException, "#A3");
  753. Assert.IsNotNull (ex.Message, "#A4");
  754. Assert.AreEqual ("size", ex.ParamName, "#A5");
  755. }
  756. // read full response
  757. buffer = new byte [24];
  758. Assert.AreEqual (9, rs.Read (buffer, 0, buffer.Length));
  759. try {
  760. rs.Read (buffer, 1, -1);
  761. Assert.Fail ("#B1");
  762. } catch (ArgumentOutOfRangeException ex) {
  763. // Specified argument was out of the range of valid values
  764. Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#B2");
  765. Assert.IsNull (ex.InnerException, "#B3");
  766. Assert.IsNotNull (ex.Message, "#B4");
  767. Assert.AreEqual ("size", ex.ParamName, "#B5");
  768. }
  769. } finally {
  770. rs.Close ();
  771. req.Abort ();
  772. }
  773. }
  774. }
  775. }
  776. [Test]
  777. #if FEATURE_NO_BSD_SOCKETS
  778. [ExpectedException (typeof (PlatformNotSupportedException))]
  779. #endif
  780. public void Read_Count_Overflow ()
  781. {
  782. IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint();
  783. string url = "http://" + ep.ToString () + "/test/";
  784. using (SocketResponder responder = new SocketResponder (ep, s => HttpWebResponseTest.FullResponseHandler (s))) {
  785. HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
  786. req.Method = "GET";
  787. req.Timeout = 2000;
  788. req.ReadWriteTimeout = 2000;
  789. req.KeepAlive = false;
  790. using (HttpWebResponse resp = (HttpWebResponse) req.GetResponse ()) {
  791. Stream rs = resp.GetResponseStream ();
  792. byte [] buffer = new byte [5];
  793. try {
  794. try {
  795. rs.Read (buffer, buffer.Length - 2, 3);
  796. Assert.Fail ("#A1");
  797. } catch (ArgumentOutOfRangeException ex) {
  798. // Specified argument was out of the range of valid values
  799. Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2");
  800. Assert.IsNull (ex.InnerException, "#A3");
  801. Assert.IsNotNull (ex.Message, "#A4");
  802. Assert.AreEqual ("size", ex.ParamName, "#A5");
  803. }
  804. // read full response
  805. buffer = new byte [24];
  806. Assert.AreEqual (9, rs.Read (buffer, 0, buffer.Length));
  807. try {
  808. rs.Read (buffer, buffer.Length - 2, 3);
  809. Assert.Fail ("#B1");
  810. } catch (ArgumentOutOfRangeException ex) {
  811. // Specified argument was out of the range of valid values
  812. Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#B2");
  813. Assert.IsNull (ex.InnerException, "#B3");
  814. Assert.IsNotNull (ex.Message, "#B4");
  815. Assert.AreEqual ("size", ex.ParamName, "#B5");
  816. }
  817. } finally {
  818. rs.Close ();
  819. req.Abort ();
  820. }
  821. }
  822. }
  823. }
  824. [Test]
  825. #if FEATURE_NO_BSD_SOCKETS
  826. [ExpectedException (typeof (PlatformNotSupportedException))]
  827. #endif
  828. public void Read_Offset_Negative ()
  829. {
  830. IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint();
  831. string url = "http://" + ep.ToString () + "/test/";
  832. using (SocketResponder responder = new SocketResponder (ep, s => HttpWebResponseTest.FullResponseHandler (s))) {
  833. HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
  834. req.Method = "GET";
  835. req.Timeout = 2000;
  836. req.ReadWriteTimeout = 2000;
  837. req.KeepAlive = false;
  838. using (HttpWebResponse resp = (HttpWebResponse) req.GetResponse ()) {
  839. Stream rs = resp.GetResponseStream ();
  840. byte [] buffer = new byte [5];
  841. try {
  842. try {
  843. rs.Read (buffer, -1, 0);
  844. Assert.Fail ("#A1");
  845. } catch (ArgumentOutOfRangeException ex) {
  846. // Specified argument was out of the range of valid values
  847. Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2");
  848. Assert.IsNull (ex.InnerException, "#A3");
  849. Assert.IsNotNull (ex.Message, "#A4");
  850. Assert.AreEqual ("offset", ex.ParamName, "#A5");
  851. }
  852. // read full response
  853. buffer = new byte [24];
  854. Assert.AreEqual (9, rs.Read (buffer, 0, buffer.Length));
  855. try {
  856. rs.Read (buffer, -1, 0);
  857. Assert.Fail ("#B1");
  858. } catch (ArgumentOutOfRangeException ex) {
  859. // Specified argument was out of the range of valid values
  860. Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#B2");
  861. Assert.IsNull (ex.InnerException, "#B3");
  862. Assert.IsNotNull (ex.Message, "#B4");
  863. Assert.AreEqual ("offset", ex.ParamName, "#B5");
  864. }
  865. } finally {
  866. rs.Close ();
  867. req.Abort ();
  868. }
  869. }
  870. }
  871. }
  872. [Test]
  873. #if FEATURE_NO_BSD_SOCKETS
  874. [ExpectedException (typeof (PlatformNotSupportedException))]
  875. #endif
  876. public void Read_Offset_Overflow ()
  877. {
  878. IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint();
  879. string url = "http://" + ep.ToString () + "/test/";
  880. using (SocketResponder responder = new SocketResponder (ep, s => HttpWebResponseTest.FullResponseHandler (s))) {
  881. HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
  882. req.Method = "GET";
  883. req.Timeout = 2000;
  884. req.ReadWriteTimeout = 2000;
  885. req.KeepAlive = false;
  886. using (HttpWebResponse resp = (HttpWebResponse) req.GetResponse ()) {
  887. Stream rs = resp.GetResponseStream ();
  888. byte [] buffer = new byte [5];
  889. try {
  890. try {
  891. rs.Read (buffer, buffer.Length + 1, 0);
  892. Assert.Fail ("#A1");
  893. } catch (ArgumentOutOfRangeException ex) {
  894. // Specified argument was out of the range of valid values
  895. Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2");
  896. Assert.IsNull (ex.InnerException, "#A3");
  897. Assert.IsNotNull (ex.Message, "#A4");
  898. Assert.AreEqual ("offset", ex.ParamName, "#A5");
  899. }
  900. // read full response
  901. buffer = new byte [24];
  902. Assert.AreEqual (9, rs.Read (buffer, 0, buffer.Length));
  903. try {
  904. rs.Read (buffer, buffer.Length + 1, 0);
  905. Assert.Fail ("#B1");
  906. } catch (ArgumentOutOfRangeException ex) {
  907. // Specified argument was out of the range of valid values
  908. Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#B2");
  909. Assert.IsNull (ex.InnerException, "#B3");
  910. Assert.IsNotNull (ex.Message, "#B4");
  911. Assert.AreEqual ("offset", ex.ParamName, "#B5");
  912. }
  913. } finally {
  914. rs.Close ();
  915. req.Abort ();
  916. }
  917. }
  918. }
  919. }
  920. [Test]
  921. [Category ("NotWorking")]
  922. public void Read_Stream_Closed ()
  923. {
  924. IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint();
  925. string url = "http://" + ep.ToString () + "/test/";
  926. using (SocketResponder responder = new SocketResponder (ep, s => HttpWebResponseTest.FullResponseHandler (s))) {
  927. HttpWebRequest req;
  928. req = (HttpWebRequest) WebRequest.Create (url);
  929. req.Method = "GET";
  930. req.Timeout = 2000;
  931. req.ReadWriteTimeout = 2000;
  932. req.KeepAlive = false;
  933. using (HttpWebResponse resp = (HttpWebResponse) req.GetResponse ()) {
  934. Stream rs = resp.GetResponseStream ();
  935. rs.Close ();
  936. try {
  937. rs.Read (new byte [0], 0, 0);
  938. Assert.Fail ("#A1");
  939. } catch (WebException ex) {
  940. // The request was aborted: The connection was closed unexpectedly
  941. Assert.AreEqual (typeof (WebException), ex.GetType (), "#A2");
  942. Assert.IsNull (ex.InnerException, "#A3");
  943. Assert.IsNotNull (ex.Message, "#A4");
  944. Assert.IsNull (ex.Response, "#A5");
  945. Assert.AreEqual (WebExceptionStatus.ConnectionClosed, ex.Status, "#A6");
  946. } finally {
  947. rs.Close ();
  948. req.Abort ();
  949. }
  950. }
  951. req = (HttpWebRequest) WebRequest.Create (url);
  952. req.Method = "GET";
  953. req.Timeout = 2000;
  954. req.ReadWriteTimeout = 2000;
  955. req.KeepAlive = false;
  956. using (HttpWebResponse resp = (HttpWebResponse) req.GetResponse ()) {
  957. Stream rs = resp.GetResponseStream ();
  958. byte [] buffer = new byte [24];
  959. Assert.AreEqual (9, rs.Read (buffer, 0, buffer.Length));
  960. rs.Close ();
  961. try {
  962. rs.Read (new byte [0], 0, 0);
  963. Assert.Fail ("#B1");
  964. } catch (WebException ex) {
  965. // The request was aborted: The connection was closed unexpectedly
  966. Assert.AreEqual (typeof (WebException), ex.GetType (), "#B2");
  967. Assert.IsNull (ex.InnerException, "#B3");
  968. Assert.IsNotNull (ex.Message, "#B4");
  969. Assert.IsNull (ex.Response, "#B5");
  970. Assert.AreEqual (WebExceptionStatus.ConnectionClosed, ex.Status, "#B6");
  971. } finally {
  972. rs.Close ();
  973. req.Abort ();
  974. }
  975. }
  976. }
  977. }
  978. [Test]
  979. #if FEATURE_NO_BSD_SOCKETS
  980. [ExpectedException (typeof (PlatformNotSupportedException))]
  981. #endif
  982. public void ReadTimeout ()
  983. {
  984. IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint();
  985. string url = "http://" + ep.ToString () + "/test/";
  986. using (SocketResponder responder = new SocketResponder (ep, s => HttpWebResponseTest.FullResponseHandler (s))) {
  987. HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
  988. req.Method = "GET";
  989. req.Timeout = 2000;
  990. req.ReadWriteTimeout = 2000;
  991. req.KeepAlive = false;
  992. using (HttpWebResponse resp = (HttpWebResponse) req.GetResponse ()) {
  993. Stream rs = resp.GetResponseStream ();
  994. try {
  995. Assert.AreEqual (2000, rs.ReadTimeout, "#1");
  996. rs.Close ();
  997. Assert.AreEqual (2000, rs.ReadTimeout, "#2");
  998. } finally {
  999. rs.Close ();
  1000. req.Abort ();
  1001. }
  1002. }
  1003. }
  1004. }
  1005. [Test]
  1006. #if FEATURE_NO_BSD_SOCKETS
  1007. [ExpectedException (typeof (PlatformNotSupportedException))]
  1008. #endif
  1009. public void Write ()
  1010. {
  1011. IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint();
  1012. string url = "http://" + ep.ToString () + "/test/";
  1013. using (SocketResponder responder = new SocketResponder (ep, s => HttpWebResponseTest.FullResponseHandler (s))) {
  1014. HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
  1015. req.Method = "GET";
  1016. req.Timeout = 2000;
  1017. req.ReadWriteTimeout = 2000;
  1018. req.KeepAlive = false;
  1019. using (HttpWebResponse resp = (HttpWebResponse) req.GetResponse ()) {
  1020. Stream rs = resp.GetResponseStream ();
  1021. byte [] buffer = new byte [5];
  1022. try {
  1023. rs.Write (buffer, 0, buffer.Length);
  1024. Assert.Fail ("#1");
  1025. } catch (NotSupportedException ex) {
  1026. // The stream does not support writing
  1027. Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#2");
  1028. Assert.IsNull (ex.InnerException, "#3");
  1029. Assert.IsNotNull (ex.Message, "#4");
  1030. } finally {
  1031. rs.Close ();
  1032. req.Abort ();
  1033. }
  1034. }
  1035. }
  1036. }
  1037. [Test]
  1038. #if FEATURE_NO_BSD_SOCKETS
  1039. [ExpectedException (typeof (PlatformNotSupportedException))]
  1040. #endif
  1041. public void WriteTimeout ()
  1042. {
  1043. IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint();
  1044. string url = "http://" + ep.ToString () + "/test/";
  1045. using (SocketResponder responder = new SocketResponder (ep, s => HttpWebResponseTest.FullResponseHandler (s))) {
  1046. HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
  1047. req.Method = "GET";
  1048. req.Timeout = 2000;
  1049. req.ReadWriteTimeout = 2000;
  1050. req.KeepAlive = false;
  1051. using (HttpWebResponse resp = (HttpWebResponse) req.GetResponse ()) {
  1052. Stream rs = resp.GetResponseStream ();
  1053. try {
  1054. Assert.AreEqual (2000, rs.WriteTimeout, "#1");
  1055. rs.Close ();
  1056. Assert.AreEqual (2000, rs.WriteTimeout, "#2");
  1057. } finally {
  1058. rs.Close ();
  1059. req.Abort ();
  1060. }
  1061. }
  1062. }
  1063. }
  1064. }
  1065. }