HttpWebRequestTest.cs 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882
  1. //
  2. // HttpWebRequestTest.cs - NUnit Test Cases for System.Net.HttpWebRequest
  3. //
  4. // Authors:
  5. // Lawrence Pit ([email protected])
  6. // Martin Willemoes Hansen ([email protected])
  7. // Gonzalo Paniagua Javier ([email protected])
  8. //
  9. // (C) 2003 Martin Willemoes Hansen
  10. // Copyright (c) 2005 Novell, Inc. (http://www.novell.com
  11. //
  12. using NUnit.Framework;
  13. using System;
  14. using System.Collections;
  15. using System.Collections.Specialized;
  16. using System.Globalization;
  17. using System.IO;
  18. using System.Net;
  19. using System.Net.Sockets;
  20. using System.Security.Cryptography;
  21. using System.Security.Cryptography.X509Certificates;
  22. using System.Text;
  23. using System.Threading;
  24. #if !TARGET_JVM
  25. using Mono.Security.Authenticode;
  26. using Mono.Security.Protocol.Tls;
  27. #endif
  28. namespace MonoTests.System.Net
  29. {
  30. [TestFixture]
  31. public class HttpWebRequestTest
  32. {
  33. [Test]
  34. #if TARGET_JVM
  35. [Ignore ("Ignore failures in Sys.Net")]
  36. #endif
  37. public void Proxy_Null ()
  38. {
  39. HttpWebRequest req = (HttpWebRequest) WebRequest.Create ("http://www.google.com");
  40. Assert.IsNotNull (req.Proxy, "#1");
  41. #if NET_2_0
  42. req.Proxy = null;
  43. Assert.IsNull (req.Proxy, "#2");
  44. #else
  45. try {
  46. req.Proxy = null;
  47. Assert.Fail ("#2");
  48. } catch (ArgumentNullException ex) {
  49. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#3");
  50. Assert.IsNull (ex.InnerException, "#4");
  51. Assert.IsNotNull (ex.Message, "#5");
  52. Assert.IsNotNull (ex.ParamName, "#6");
  53. Assert.AreEqual ("value", ex.ParamName, "#7");
  54. }
  55. #endif
  56. }
  57. [Test]
  58. [Category("InetAccess")]
  59. #if TARGET_JVM
  60. [Ignore ("NMA - wrong cookies number returned")]
  61. #endif
  62. public void Sync ()
  63. {
  64. HttpWebRequest req = (HttpWebRequest) WebRequest.Create ("http://www.google.com");
  65. Assertion.AssertNotNull ("req:If Modified Since: ", req.IfModifiedSince);
  66. req.UserAgent = "MonoClient v1.0";
  67. Assert.AreEqual ("User-Agent", req.Headers.GetKey (0), "#A1");
  68. Assert.AreEqual ("MonoClient v1.0", req.Headers.Get (0), "#A2");
  69. HttpWebResponse res = (HttpWebResponse) req.GetResponse ();
  70. Assert.AreEqual ("OK", res.StatusCode.ToString (), "#B1");
  71. Assert.AreEqual ("OK", res.StatusDescription, "#B2");
  72. Assert.AreEqual ("text/html; charset=ISO-8859-1", res.Headers.Get ("Content-Type"), "#C1");
  73. Assert.IsNotNull (res.LastModified, "#C2");
  74. Assert.AreEqual (0, res.Cookies.Count, "#C3");
  75. res.Close ();
  76. }
  77. [Test]
  78. public void AddRange ()
  79. {
  80. HttpWebRequest req = (HttpWebRequest) WebRequest.Create ("http://www.google.com");
  81. req.AddRange (10);
  82. req.AddRange (50, 90);
  83. req.AddRange ("bytes", 100);
  84. req.AddRange ("bytes", 100, 120);
  85. Assertion.AssertEquals ("#1", "bytes=10-,50-90,100-,100-120", req.Headers ["Range"]);
  86. try {
  87. req.AddRange ("bits", 2000);
  88. Assertion.Fail ("#2");
  89. } catch (InvalidOperationException) {}
  90. }
  91. [Test]
  92. [Category("InetAccess")]
  93. public void Cookies1 ()
  94. {
  95. // The purpose of this test is to ensure that the cookies we get from a request
  96. // are stored in both, the CookieCollection in HttpWebResponse and the CookieContainer
  97. // in HttpWebRequest.
  98. // If this URL stops sending *one* and only one cookie, replace it.
  99. string url = "http://www.elmundo.es";
  100. CookieContainer cookies = new CookieContainer ();
  101. HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
  102. req.KeepAlive = false;
  103. req.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv; 1.7.6) Gecko/20050317 Firefox/1.0.2";
  104. req.CookieContainer = cookies;
  105. Assertion.AssertEquals ("#01", 0, cookies.Count);
  106. using (HttpWebResponse res = (HttpWebResponse) req.GetResponse()) {
  107. CookieCollection coll = req.CookieContainer.GetCookies (new Uri (url));
  108. Assertion.AssertEquals ("#02", 1, coll.Count);
  109. Assertion.AssertEquals ("#03", 1, res.Cookies.Count);
  110. Cookie one = coll [0];
  111. Cookie two = res.Cookies [0];
  112. Assertion.AssertEquals ("#04", true, object.ReferenceEquals (one, two));
  113. }
  114. }
  115. #if !TARGET_JVM //NotWorking
  116. [Test]
  117. public void SslClientBlock ()
  118. {
  119. // This tests that the write request/initread/write body sequence does not hang
  120. // when using SSL.
  121. // If there's a regression for this, the test will hang.
  122. ServicePointManager.CertificatePolicy = new AcceptAllPolicy ();
  123. try {
  124. SslHttpServer server = new SslHttpServer ();
  125. server.Start ();
  126. string url = String.Format ("https://{0}:{1}/nothing.html", server.IPAddress, server.Port);
  127. HttpWebRequest request = (HttpWebRequest) WebRequest.Create (url);
  128. request.Method = "POST";
  129. Stream stream = request.GetRequestStream ();
  130. byte [] bytes = new byte [100];
  131. stream.Write (bytes, 0, bytes.Length);
  132. stream.Close ();
  133. HttpWebResponse resp = (HttpWebResponse) request.GetResponse ();
  134. Assertion.AssertEquals ("StatusCode", 200, (int) resp.StatusCode);
  135. StreamReader sr = new StreamReader (resp.GetResponseStream (), Encoding.UTF8);
  136. string x = sr.ReadToEnd ();
  137. sr.Close ();
  138. resp.Close ();
  139. server.Stop ();
  140. if (server.Error != null)
  141. throw server.Error;
  142. } finally {
  143. ServicePointManager.CertificatePolicy = null;
  144. }
  145. }
  146. #endif
  147. [Test]
  148. #if TARGET_JVM
  149. [Category("NotWorking")]
  150. #endif
  151. public void Missing_ContentEncoding ()
  152. {
  153. ServicePointManager.CertificatePolicy = new AcceptAllPolicy ();
  154. try {
  155. BadChunkedServer server = new BadChunkedServer ();
  156. server.Start ();
  157. string url = String.Format ("http://{0}:{1}/nothing.html", server.IPAddress, server.Port);
  158. HttpWebRequest request = (HttpWebRequest) WebRequest.Create (url);
  159. request.Method = "GET";
  160. HttpWebResponse resp = (HttpWebResponse) request.GetResponse ();
  161. Assert.AreEqual ("", resp.ContentEncoding);
  162. resp.Close ();
  163. server.Stop ();
  164. if (server.Error != null)
  165. throw server.Error;
  166. } finally {
  167. ServicePointManager.CertificatePolicy = null;
  168. }
  169. }
  170. [Test]
  171. #if TARGET_JVM
  172. [Category ("NotWorking")]
  173. #endif
  174. public void BadServer_ChunkedClose ()
  175. {
  176. // The server will send a chunked response without a 'last-chunked' mark
  177. // and then shutdown the socket for sending.
  178. BadChunkedServer server = new BadChunkedServer ();
  179. server.Start ();
  180. string url = String.Format ("http://{0}:{1}/nothing.html", server.IPAddress, server.Port);
  181. HttpWebRequest request = (HttpWebRequest) WebRequest.Create (url);
  182. HttpWebResponse resp = (HttpWebResponse) request.GetResponse ();
  183. string x = null;
  184. try {
  185. byte [] bytes = new byte [32];
  186. // Using StreamReader+UTF8Encoding here fails on MS runtime
  187. Stream stream = resp.GetResponseStream ();
  188. int nread = stream.Read (bytes, 0, 32);
  189. Assertion.AssertEquals ("#01", 16, nread);
  190. x = Encoding.ASCII.GetString (bytes, 0, 16);
  191. } finally {
  192. resp.Close ();
  193. server.Stop ();
  194. }
  195. if (server.Error != null)
  196. throw server.Error;
  197. Assertion.AssertEquals ("1234567890123456", x);
  198. }
  199. [Test]
  200. [Ignore ("This test asserts that our code violates RFC 2616")]
  201. public void MethodCase ()
  202. {
  203. ListDictionary methods = new ListDictionary ();
  204. #if NET_2_0
  205. methods.Add ("post", "POST");
  206. methods.Add ("puT", "PUT");
  207. #else
  208. methods.Add ("post", "post");
  209. methods.Add ("puT", "puT");
  210. #endif
  211. methods.Add ("POST", "POST");
  212. methods.Add ("whatever", "whatever");
  213. methods.Add ("PUT", "PUT");
  214. IPEndPoint ep = new IPEndPoint (IPAddress.Loopback, 8000);
  215. string url = "http://" + IPAddress.Loopback.ToString () + ":8000/test/";
  216. foreach (DictionaryEntry de in methods) {
  217. SocketResponder responder = new SocketResponder (new IPEndPoint (IPAddress.Loopback, 8000),
  218. new SocketRequestHandler (EchoRequestHandler));
  219. responder.Start ();
  220. HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
  221. req.Method = (string) de.Key;
  222. req.Timeout = 2000;
  223. req.ReadWriteTimeout = 2000;
  224. req.KeepAlive = false;
  225. Stream rs = req.GetRequestStream ();
  226. rs.Close ();
  227. using (HttpWebResponse resp = (HttpWebResponse) req.GetResponse ()) {
  228. StreamReader sr = new StreamReader (resp.GetResponseStream (),
  229. Encoding.UTF8);
  230. string line = sr.ReadLine ();
  231. sr.Close ();
  232. Assert.AreEqual (((string) de.Value) + " /test/ HTTP/1.1",
  233. line, req.Method);
  234. resp.Close ();
  235. }
  236. responder.Stop ();
  237. }
  238. }
  239. [Test]
  240. [Ignore ("This test asserts that our code violates RFC 2616")]
  241. public void GetRequestStream_Body_NotAllowed ()
  242. {
  243. string [] methods = new string [] { "GET", "HEAD", "CONNECT",
  244. "get", "HeAd", "ConNect" };
  245. foreach (string method in methods) {
  246. HttpWebRequest req = (HttpWebRequest) WebRequest.Create (
  247. "http://localhost:8000");
  248. req.Method = method;
  249. try {
  250. req.GetRequestStream ();
  251. Assert.Fail ("#1:" + method);
  252. } catch (ProtocolViolationException ex) {
  253. Assert.AreEqual (typeof (ProtocolViolationException), ex.GetType (), "#2:" + method);
  254. Assert.IsNull (ex.InnerException, "#3:" + method);
  255. Assert.IsNotNull (ex.Message, "#4:" + method);
  256. }
  257. }
  258. }
  259. [Test]
  260. #if TARGET_JVM
  261. [Category("NotWorking")]
  262. #endif
  263. public void ReadTimeout ()
  264. {
  265. IPEndPoint localEP = new IPEndPoint (IPAddress.Loopback, 8764);
  266. string url = "http://" + localEP.ToString () + "/original/";
  267. using (SocketResponder responder = new SocketResponder (localEP, new SocketRequestHandler (RedirectRequestHandler))) {
  268. responder.Start ();
  269. HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
  270. req.Method = "POST";
  271. req.AllowAutoRedirect = false;
  272. req.Timeout = 200;
  273. req.ReadWriteTimeout = 100;
  274. req.KeepAlive = false;
  275. Stream rs = req.GetRequestStream ();
  276. rs.Close ();
  277. using (HttpWebResponse resp = (HttpWebResponse) req.GetResponse ()) {
  278. try {
  279. Stream s = resp.GetResponseStream ();
  280. s.ReadByte ();
  281. Assert.Fail ("#1");
  282. } catch (WebException ex) {
  283. Assert.AreEqual (typeof (WebException), ex.GetType (), "#2");
  284. Assert.IsNull (ex.InnerException, "#3");
  285. Assert.IsNull (ex.Response, "#4");
  286. Assert.AreEqual (WebExceptionStatus.Timeout, ex.Status, "#5");
  287. }
  288. }
  289. responder.Stop ();
  290. }
  291. }
  292. [Test] // bug #81624
  293. #if TARGET_JVM
  294. [Category("NotWorking")]
  295. #endif
  296. public void AllowAutoRedirect ()
  297. {
  298. IPEndPoint localEP = new IPEndPoint (IPAddress.Loopback, 8764);
  299. string url = "http://" + localEP.ToString () + "/original/";
  300. // allow autoredirect
  301. using (SocketResponder responder = new SocketResponder (localEP, new SocketRequestHandler (RedirectRequestHandler))) {
  302. responder.Start ();
  303. HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
  304. req.Method = "POST";
  305. req.Timeout = 2000;
  306. req.ReadWriteTimeout = 2000;
  307. req.KeepAlive = false;
  308. Stream rs = req.GetRequestStream ();
  309. rs.Close ();
  310. using (HttpWebResponse resp = (HttpWebResponse) req.GetResponse ()) {
  311. StreamReader sr = new StreamReader (resp.GetResponseStream (),
  312. Encoding.UTF8);
  313. string body = sr.ReadToEnd ();
  314. Assert.AreEqual (resp.StatusCode, HttpStatusCode.OK, "#A1");
  315. Assert.AreEqual (resp.ResponseUri.ToString (), "http://" +
  316. localEP.ToString () + "/moved/", "#A2");
  317. Assert.AreEqual ("GET", resp.Method, "#A3");
  318. Assert.AreEqual ("LOOKS OK", body, "#A4");
  319. }
  320. responder.Stop ();
  321. }
  322. // do not allow autoredirect
  323. using (SocketResponder responder = new SocketResponder (localEP, new SocketRequestHandler (RedirectRequestHandler))) {
  324. responder.Start ();
  325. HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
  326. req.Method = "POST";
  327. req.AllowAutoRedirect = false;
  328. req.Timeout = 1000;
  329. req.ReadWriteTimeout = 1000;
  330. req.KeepAlive = false;
  331. Stream rs = req.GetRequestStream ();
  332. rs.Close ();
  333. using (HttpWebResponse resp = (HttpWebResponse) req.GetResponse ()) {
  334. Assert.AreEqual (resp.StatusCode, HttpStatusCode.Found, "#B1");
  335. Assert.AreEqual (url, resp.ResponseUri.ToString (), "#B2");
  336. Assert.AreEqual ("POST", resp.Method, "#B3");
  337. }
  338. responder.Stop ();
  339. }
  340. }
  341. [Test] // bug #81671
  342. [Category ("NotWorking")]
  343. public void InternalServerError ()
  344. {
  345. IPEndPoint localEP = new IPEndPoint (IPAddress.Loopback, 8764);
  346. string url = "http://" + localEP.ToString () + "/original/";
  347. // POST
  348. using (SocketResponder responder = new SocketResponder (localEP, new SocketRequestHandler (InternalErrorHandler))) {
  349. responder.Start ();
  350. HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
  351. req.Method = "POST";
  352. req.Timeout = 2000;
  353. req.ReadWriteTimeout = 2000;
  354. req.KeepAlive = false;
  355. Stream rs = req.GetRequestStream ();
  356. rs.Close ();
  357. try {
  358. req.GetResponse ();
  359. Assert.Fail ("#A1");
  360. } catch (WebException ex) {
  361. Assert.AreEqual (typeof (WebException), ex.GetType (), "#A2");
  362. Assert.IsNull (ex.InnerException, "#A3");
  363. Assert.IsNotNull (ex.Message, "#A4");
  364. Assert.AreEqual (WebExceptionStatus.ProtocolError, ex.Status, "#A5");
  365. HttpWebResponse webResponse = ex.Response as HttpWebResponse;
  366. Assert.IsNotNull (webResponse, "#A6");
  367. Assert.AreEqual ("POST", webResponse.Method, "#A7");
  368. webResponse.Close ();
  369. }
  370. responder.Stop ();
  371. }
  372. // GET
  373. using (SocketResponder responder = new SocketResponder (localEP, new SocketRequestHandler (InternalErrorHandler))) {
  374. responder.Start ();
  375. HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
  376. req.Method = "GET";
  377. req.Timeout = 2000;
  378. req.ReadWriteTimeout = 2000;
  379. req.KeepAlive = false;
  380. try {
  381. req.GetResponse ();
  382. Assert.Fail ("#B1");
  383. } catch (WebException ex) {
  384. Assert.AreEqual (typeof (WebException), ex.GetType (), "#B2");
  385. Assert.IsNull (ex.InnerException, "#B3");
  386. Assert.AreEqual (WebExceptionStatus.ProtocolError, ex.Status, "#B4");
  387. HttpWebResponse webResponse = ex.Response as HttpWebResponse;
  388. Assert.IsNotNull (webResponse, "#B5");
  389. Assert.AreEqual ("GET", webResponse.Method, "#B6");
  390. webResponse.Close ();
  391. }
  392. responder.Stop ();
  393. }
  394. }
  395. [Test]
  396. [Category ("NotWorking")] // we report a timeout
  397. public void NoContentLength ()
  398. {
  399. IPEndPoint localEP = new IPEndPoint (IPAddress.Loopback, 8764);
  400. string url = "http://" + localEP.ToString () + "/original/";
  401. // POST
  402. using (SocketResponder responder = new SocketResponder (localEP, new SocketRequestHandler (NoContentLengthHandler))) {
  403. responder.Start ();
  404. HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
  405. req.Method = "POST";
  406. req.Timeout = 2000;
  407. req.ReadWriteTimeout = 2000;
  408. req.KeepAlive = false;
  409. Stream rs = req.GetRequestStream ();
  410. rs.Close ();
  411. try {
  412. req.GetResponse ();
  413. Assert.Fail ("#A1");
  414. } catch (WebException ex) {
  415. Assert.AreEqual (typeof (WebException), ex.GetType (), "#A2");
  416. #if NET_2_0
  417. //Assert.IsNotNull (ex.InnerException, "#A3");
  418. Assert.AreEqual (WebExceptionStatus.ReceiveFailure, ex.Status, "#A4");
  419. Assert.AreEqual (typeof (IOException), ex.InnerException.GetType (), "#A5");
  420. // Unable to read data from the transport connection:
  421. // A connection attempt failed because the connected party
  422. // did not properly respond after a period of time, or
  423. // established connection failed because connected host has
  424. // failed to respond
  425. IOException ioe = (IOException) ex.InnerException;
  426. Assert.IsNotNull (ioe.InnerException, "#A6");
  427. Assert.IsNotNull (ioe.Message, "#A7");
  428. Assert.AreEqual (typeof (SocketException), ioe.InnerException.GetType (), "#A8");
  429. // A connection attempt failed because the connected party
  430. // did not properly respond after a period of time, or
  431. // established connection failed because connected host has
  432. // failed to respond
  433. SocketException soe = (SocketException) ioe.InnerException;
  434. Assert.IsNull (soe.InnerException, "#A9");
  435. Assert.IsNotNull (soe.Message, "#A10");
  436. HttpWebResponse webResponse = ex.Response as HttpWebResponse;
  437. Assert.IsNull (webResponse, "#A11");
  438. #else
  439. Assert.IsNull (ex.InnerException, "#A3");
  440. Assert.AreEqual (WebExceptionStatus.ProtocolError, ex.Status, "#A4");
  441. HttpWebResponse webResponse = ex.Response as HttpWebResponse;
  442. Assert.IsNotNull (webResponse, "#A5");
  443. Assert.AreEqual ("POST", webResponse.Method, "#A6");
  444. webResponse.Close ();
  445. #endif
  446. }
  447. responder.Stop ();
  448. }
  449. // GET
  450. using (SocketResponder responder = new SocketResponder (localEP, new SocketRequestHandler (NoContentLengthHandler))) {
  451. responder.Start ();
  452. HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
  453. req.Method = "GET";
  454. req.Timeout = 2000;
  455. req.ReadWriteTimeout = 2000;
  456. req.KeepAlive = false;
  457. try {
  458. req.GetResponse ();
  459. Assert.Fail ("#B1");
  460. } catch (WebException ex) {
  461. Assert.AreEqual (typeof (WebException), ex.GetType (), "#B2");
  462. Assert.IsNull (ex.InnerException, "#B3");
  463. Assert.AreEqual (WebExceptionStatus.ProtocolError, ex.Status, "#B4");
  464. HttpWebResponse webResponse = ex.Response as HttpWebResponse;
  465. Assert.IsNotNull (webResponse, "#B5");
  466. Assert.AreEqual ("GET", webResponse.Method, "#B6");
  467. webResponse.Close ();
  468. }
  469. responder.Stop ();
  470. }
  471. }
  472. #if NET_2_0
  473. [Test] // bug #81504
  474. #if TARGET_JVM
  475. [Category ("NotWorking")]
  476. #endif
  477. public void Stream_CanTimeout ()
  478. {
  479. IPEndPoint localEP = new IPEndPoint (IPAddress.Loopback, 8764);
  480. string url = "http://" + localEP.ToString () + "/original/";
  481. // allow autoredirect
  482. using (SocketResponder responder = new SocketResponder (localEP, new SocketRequestHandler (RedirectRequestHandler))) {
  483. responder.Start ();
  484. HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
  485. req.Method = "POST";
  486. req.Timeout = 2000;
  487. req.ReadWriteTimeout = 2000;
  488. req.KeepAlive = false;
  489. Stream rs = req.GetRequestStream ();
  490. Assert.IsTrue (rs.CanTimeout, "#1");
  491. rs.Close ();
  492. using (HttpWebResponse resp = (HttpWebResponse) req.GetResponse ()) {
  493. Stream os = resp.GetResponseStream ();
  494. Assert.IsTrue (os.CanTimeout, "#2");
  495. os.Close ();
  496. }
  497. responder.Stop ();
  498. }
  499. }
  500. #endif
  501. [Test]
  502. [Category ("NotWorking")] // #353495
  503. public void LastModifiedKind () // bug #353495
  504. {
  505. const string reqURL = "http://coffeefaq.com/site/node/25";
  506. const string dtFmt = "ddd, dd MMM yyyy HH:mm:ss";
  507. HttpWebRequest req = (HttpWebRequest) WebRequest.Create (reqURL);
  508. HttpWebResponse resp = (HttpWebResponse) req.GetResponse ();
  509. DateTime lastMod = resp.LastModified;
  510. string rawLastMod = resp.Headers ["Last-Modified"];
  511. resp.Close ();
  512. //Assert.AreEqual ("Tue, 15 Jan 2008 08:59:59 GMT", rawLastMod, "#1");
  513. #if NET_2_0
  514. Assert.AreEqual (DateTimeKind.Local, lastMod.Kind, "#2");
  515. #endif
  516. req = (HttpWebRequest) WebRequest.Create (reqURL);
  517. req.IfModifiedSince = lastMod;
  518. try {
  519. resp = (HttpWebResponse) req.GetResponse ();
  520. resp.Close ();
  521. Assert.Fail ("Should result in 304");
  522. } catch (WebException ex) {
  523. Assert.AreEqual (WebExceptionStatus.ProtocolError, ex.Status, "#3");
  524. Assert.AreEqual (((HttpWebResponse) ex.Response).StatusCode, HttpStatusCode.NotModified, "#4");
  525. }
  526. }
  527. static byte [] EchoRequestHandler (Socket socket)
  528. {
  529. MemoryStream ms = new MemoryStream ();
  530. byte [] buffer = new byte [4096];
  531. int bytesReceived = socket.Receive (buffer);
  532. while (bytesReceived > 0) {
  533. ms.Write (buffer, 0, bytesReceived);
  534. if (socket.Available > 0) {
  535. bytesReceived = socket.Receive (buffer);
  536. } else {
  537. bytesReceived = 0;
  538. }
  539. }
  540. ms.Flush ();
  541. ms.Position = 0;
  542. StreamReader sr = new StreamReader (ms, Encoding.UTF8);
  543. string request = sr.ReadToEnd ();
  544. StringWriter sw = new StringWriter ();
  545. sw.WriteLine ("HTTP/1.1 200 OK");
  546. sw.WriteLine ("Content-Type: text/xml");
  547. sw.WriteLine ("Content-Length: " + request.Length.ToString (CultureInfo.InvariantCulture));
  548. sw.WriteLine ();
  549. sw.Write (request);
  550. sw.Flush ();
  551. return Encoding.UTF8.GetBytes (sw.ToString ());
  552. }
  553. static byte [] RedirectRequestHandler (Socket socket)
  554. {
  555. MemoryStream ms = new MemoryStream ();
  556. byte [] buffer = new byte [4096];
  557. int bytesReceived = socket.Receive (buffer);
  558. while (bytesReceived > 0) {
  559. ms.Write (buffer, 0, bytesReceived);
  560. if (socket.Available > 0) {
  561. bytesReceived = socket.Receive (buffer);
  562. } else {
  563. bytesReceived = 0;
  564. }
  565. }
  566. ms.Flush ();
  567. ms.Position = 0;
  568. string statusLine = null;
  569. using (StreamReader sr = new StreamReader (ms, Encoding.UTF8)) {
  570. statusLine = sr.ReadLine ();
  571. }
  572. StringWriter sw = new StringWriter ();
  573. if (statusLine.StartsWith ("POST /original/")) {
  574. sw.WriteLine ("HTTP/1.1 302 Found");
  575. sw.WriteLine ("Location: " + "http://" + IPAddress.Loopback.ToString () + ":8764/moved/");
  576. sw.WriteLine ();
  577. sw.Flush ();
  578. } else if (statusLine.StartsWith ("GET /moved/")) {
  579. sw.WriteLine ("HTTP/1.1 200 OK");
  580. sw.WriteLine ("Content-Type: text/plain");
  581. sw.WriteLine ("Content-Length: 8");
  582. sw.WriteLine ();
  583. sw.Write ("LOOKS OK");
  584. sw.Flush ();
  585. } else {
  586. sw.WriteLine ("HTTP/1.1 500 Too Lazy");
  587. sw.WriteLine ();
  588. sw.Flush ();
  589. }
  590. return Encoding.UTF8.GetBytes (sw.ToString ());
  591. }
  592. static byte [] InternalErrorHandler (Socket socket)
  593. {
  594. StringWriter sw = new StringWriter ();
  595. sw.WriteLine ("HTTP/1.1 500 Too Lazy");
  596. sw.WriteLine ("Content-Length: 0");
  597. sw.WriteLine ();
  598. sw.Flush ();
  599. return Encoding.UTF8.GetBytes (sw.ToString ());
  600. }
  601. static byte [] NoContentLengthHandler (Socket socket)
  602. {
  603. StringWriter sw = new StringWriter ();
  604. sw.WriteLine ("HTTP/1.1 500 Too Lazy");
  605. sw.WriteLine ();
  606. sw.Flush ();
  607. return Encoding.UTF8.GetBytes (sw.ToString ());
  608. }
  609. class BadChunkedServer : HttpServer {
  610. protected override void Run ()
  611. {
  612. Socket client = sock.Accept ();
  613. NetworkStream ns = new NetworkStream (client, true);
  614. StreamWriter writer = new StreamWriter (ns, Encoding.ASCII);
  615. writer.Write ( "HTTP/1.1 200 OK\r\n" +
  616. "Transfer-Encoding: chunked\r\n" +
  617. "Connection: close\r\n" +
  618. "Content-Type: text/plain; charset=UTF-8\r\n\r\n");
  619. // This body lacks a 'last-chunk' (see RFC 2616)
  620. writer.Write ("10\r\n1234567890123456\r\n");
  621. writer.Flush ();
  622. client.Shutdown (SocketShutdown.Send);
  623. Thread.Sleep (1000);
  624. writer.Close ();
  625. }
  626. }
  627. class AcceptAllPolicy : ICertificatePolicy {
  628. public bool CheckValidationResult (ServicePoint sp, X509Certificate certificate, WebRequest request, int error)
  629. {
  630. return true;
  631. }
  632. }
  633. abstract class HttpServer
  634. {
  635. protected Socket sock;
  636. protected Exception error;
  637. protected ManualResetEvent evt;
  638. public HttpServer ()
  639. {
  640. sock = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  641. sock.Bind (new IPEndPoint (IPAddress.Loopback, 0));
  642. sock.Listen (1);
  643. }
  644. public void Start ()
  645. {
  646. evt = new ManualResetEvent (false);
  647. Thread th = new Thread (new ThreadStart (Run));
  648. th.Start ();
  649. }
  650. public void Stop ()
  651. {
  652. evt.Set ();
  653. sock.Close ();
  654. }
  655. public IPAddress IPAddress {
  656. get { return ((IPEndPoint) sock.LocalEndPoint).Address; }
  657. }
  658. public int Port {
  659. get { return ((IPEndPoint) sock.LocalEndPoint).Port; }
  660. }
  661. public Exception Error {
  662. get { return error; }
  663. }
  664. protected abstract void Run ();
  665. }
  666. #if !TARGET_JVM
  667. class SslHttpServer : HttpServer {
  668. X509Certificate _certificate;
  669. protected override void Run ()
  670. {
  671. try {
  672. Socket client = sock.Accept ();
  673. NetworkStream ns = new NetworkStream (client, true);
  674. SslServerStream s = new SslServerStream (ns, Certificate, false, false);
  675. s.PrivateKeyCertSelectionDelegate += new PrivateKeySelectionCallback (GetPrivateKey);
  676. StreamReader reader = new StreamReader (s);
  677. StreamWriter writer = new StreamWriter (s, Encoding.ASCII);
  678. string line;
  679. string hello = "<html><body><h1>Hello World!</h1></body></html>";
  680. string answer = "HTTP/1.0 200\r\n" +
  681. "Connection: close\r\n" +
  682. "Content-Type: text/html\r\n" +
  683. "Content-Encoding: " + Encoding.ASCII.WebName + "\r\n" +
  684. "Content-Length: " + hello.Length + "\r\n" +
  685. "\r\n" + hello;
  686. // Read the headers
  687. do {
  688. line = reader.ReadLine ();
  689. } while (line != "" && line != null && line.Length > 0);
  690. // Now the content. We know it's 100 bytes.
  691. // This makes BeginRead in sslclientstream block.
  692. char [] cs = new char [100];
  693. reader.Read (cs, 0, 100);
  694. writer.Write (answer);
  695. writer.Flush ();
  696. evt.WaitOne (50000, false);
  697. } catch (Exception e) {
  698. error = e;
  699. }
  700. }
  701. X509Certificate Certificate {
  702. get {
  703. if (_certificate == null)
  704. _certificate = new X509Certificate (CertData.Certificate);
  705. return _certificate;
  706. }
  707. }
  708. AsymmetricAlgorithm GetPrivateKey (X509Certificate certificate, string targetHost)
  709. {
  710. PrivateKey key = new PrivateKey (CertData.PrivateKey, null);
  711. return key.RSA;
  712. }
  713. }
  714. class CertData {
  715. public readonly static byte [] Certificate = {
  716. 48, 130, 1, 191, 48, 130, 1, 40, 160, 3, 2, 1, 2, 2, 16, 36,
  717. 14, 97, 190, 146, 132, 208, 71, 175, 6, 87, 168, 185, 175, 55, 43, 48,
  718. 13, 6, 9, 42, 134, 72, 134, 247, 13, 1, 1, 4, 5, 0, 48, 18,
  719. 49, 16, 48, 14, 6, 3, 85, 4, 3, 19, 7, 103, 111, 110, 122, 97,
  720. 108, 111, 48, 30, 23, 13, 48, 53, 48, 54, 50, 50, 49, 57, 51, 48,
  721. 52, 54, 90, 23, 13, 51, 57, 49, 50, 51, 49, 50, 51, 53, 57, 53,
  722. 57, 90, 48, 18, 49, 16, 48, 14, 6, 3, 85, 4, 3, 19, 7, 103,
  723. 111, 110, 122, 97, 108, 111, 48, 129, 158, 48, 13, 6, 9, 42, 134, 72,
  724. 134, 247, 13, 1, 1, 1, 5, 0, 3, 129, 140, 0, 48, 129, 136, 2,
  725. 129, 129, 0, 138, 9, 38, 25, 166, 252, 59, 26, 39, 184, 128, 216, 38,
  726. 73, 41, 86, 30, 228, 160, 205, 41, 135, 115, 223, 44, 62, 42, 198, 178,
  727. 190, 81, 11, 25, 21, 216, 49, 179, 130, 246, 52, 97, 175, 212, 94, 157,
  728. 231, 162, 66, 161, 103, 63, 204, 83, 141, 172, 119, 97, 225, 206, 98, 101,
  729. 210, 106, 2, 206, 81, 90, 173, 47, 41, 199, 209, 241, 177, 177, 96, 207,
  730. 254, 220, 190, 66, 180, 153, 0, 209, 14, 178, 69, 194, 3, 37, 116, 239,
  731. 49, 23, 185, 245, 255, 126, 35, 85, 246, 56, 244, 107, 117, 24, 14, 57,
  732. 9, 111, 147, 189, 220, 142, 57, 104, 153, 193, 205, 19, 14, 22, 157, 16,
  733. 24, 80, 201, 2, 2, 0, 17, 163, 23, 48, 21, 48, 19, 6, 3, 85,
  734. 29, 37, 4, 12, 48, 10, 6, 8, 43, 6, 1, 5, 5, 7, 3, 1,
  735. 48, 13, 6, 9, 42, 134, 72, 134, 247, 13, 1, 1, 4, 5, 0, 3,
  736. 129, 129, 0, 64, 49, 57, 253, 218, 198, 229, 51, 189, 12, 154, 225, 183,
  737. 160, 147, 90, 113, 172, 69, 122, 28, 77, 97, 215, 231, 194, 150, 29, 196,
  738. 65, 95, 218, 99, 142, 111, 79, 205, 109, 76, 32, 92, 220, 76, 88, 53,
  739. 237, 80, 11, 85, 44, 91, 21, 210, 12, 34, 223, 234, 18, 187, 136, 62,
  740. 26, 240, 103, 180, 12, 226, 221, 250, 247, 129, 51, 23, 129, 165, 56, 67,
  741. 43, 83, 244, 110, 207, 24, 253, 195, 16, 46, 80, 113, 80, 18, 2, 254,
  742. 120, 147, 151, 164, 23, 210, 230, 100, 19, 197, 179, 28, 194, 48, 106, 159,
  743. 155, 144, 37, 82, 44, 160, 40, 52, 146, 174, 77, 188, 160, 230, 75, 172,
  744. 123, 3, 254,
  745. };
  746. public readonly static byte [] PrivateKey = {
  747. 30, 241, 181, 176, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0,
  748. 0, 0, 0, 0, 84, 2, 0, 0, 7, 2, 0, 0, 0, 36, 0, 0,
  749. 82, 83, 65, 50, 0, 4, 0, 0, 17, 0, 0, 0, 201, 80, 24, 16,
  750. 157, 22, 14, 19, 205, 193, 153, 104, 57, 142, 220, 189, 147, 111, 9, 57,
  751. 14, 24, 117, 107, 244, 56, 246, 85, 35, 126, 255, 245, 185, 23, 49, 239,
  752. 116, 37, 3, 194, 69, 178, 14, 209, 0, 153, 180, 66, 190, 220, 254, 207,
  753. 96, 177, 177, 241, 209, 199, 41, 47, 173, 90, 81, 206, 2, 106, 210, 101,
  754. 98, 206, 225, 97, 119, 172, 141, 83, 204, 63, 103, 161, 66, 162, 231, 157,
  755. 94, 212, 175, 97, 52, 246, 130, 179, 49, 216, 21, 25, 11, 81, 190, 178,
  756. 198, 42, 62, 44, 223, 115, 135, 41, 205, 160, 228, 30, 86, 41, 73, 38,
  757. 216, 128, 184, 39, 26, 59, 252, 166, 25, 38, 9, 138, 175, 88, 190, 223,
  758. 27, 24, 224, 123, 190, 69, 164, 234, 129, 59, 108, 229, 248, 62, 187, 15,
  759. 235, 147, 162, 83, 47, 123, 170, 190, 224, 31, 215, 110, 143, 31, 227, 216,
  760. 85, 88, 154, 83, 207, 229, 41, 28, 237, 116, 181, 17, 37, 141, 224, 185,
  761. 164, 144, 141, 233, 164, 138, 177, 241, 115, 181, 230, 150, 7, 92, 139, 141,
  762. 113, 95, 57, 191, 211, 165, 217, 250, 197, 68, 164, 184, 168, 43, 48, 65,
  763. 177, 237, 173, 144, 148, 221, 62, 189, 147, 63, 216, 188, 206, 103, 226, 171,
  764. 32, 20, 230, 116, 144, 192, 1, 39, 202, 87, 74, 250, 6, 142, 188, 23,
  765. 45, 4, 112, 191, 253, 67, 69, 70, 128, 143, 44, 234, 41, 96, 195, 82,
  766. 202, 35, 158, 149, 240, 151, 23, 25, 166, 179, 85, 144, 58, 120, 149, 229,
  767. 205, 34, 8, 110, 86, 119, 130, 210, 37, 173, 65, 71, 169, 67, 8, 51,
  768. 20, 96, 51, 155, 3, 39, 85, 187, 40, 193, 57, 19, 99, 78, 173, 28,
  769. 129, 154, 108, 175, 8, 138, 237, 71, 27, 148, 129, 35, 47, 57, 101, 237,
  770. 168, 178, 227, 221, 212, 63, 124, 254, 253, 215, 183, 159, 49, 103, 74, 49,
  771. 67, 160, 171, 72, 194, 215, 108, 251, 178, 18, 184, 100, 211, 105, 21, 186,
  772. 39, 66, 218, 154, 72, 222, 90, 237, 179, 251, 51, 224, 212, 56, 251, 6,
  773. 209, 151, 198, 176, 89, 110, 35, 141, 248, 237, 223, 68, 135, 206, 207, 169,
  774. 254, 219, 243, 130, 71, 11, 94, 113, 233, 92, 63, 156, 169, 72, 215, 110,
  775. 95, 94, 191, 50, 59, 89, 187, 59, 183, 99, 161, 146, 233, 245, 219, 80,
  776. 87, 113, 251, 50, 144, 195, 158, 46, 189, 232, 119, 91, 75, 22, 6, 176,
  777. 39, 206, 25, 196, 213, 195, 219, 24, 28, 103, 104, 36, 137, 128, 4, 119,
  778. 163, 40, 126, 87, 18, 86, 128, 243, 213, 101, 2, 237, 78, 64, 160, 55,
  779. 199, 93, 90, 126, 175, 199, 55, 89, 234, 190, 5, 16, 196, 88, 28, 208,
  780. 28, 92, 32, 115, 204, 9, 202, 101, 15, 123, 43, 75, 90, 144, 95, 179,
  781. 102, 249, 57, 150, 204, 99, 147, 203, 16, 63, 81, 244, 226, 237, 82, 204,
  782. 20, 200, 140, 65, 83, 217, 161, 23, 123, 37, 115, 12, 100, 73, 70, 190,
  783. 32, 235, 174, 140, 148, 157, 47, 238, 40, 208, 228, 80, 54, 187, 156, 252,
  784. 253, 230, 231, 156, 138, 125, 96, 79, 3, 27, 143, 55, 146, 169, 165, 61,
  785. 238, 60, 227, 77, 217, 93, 117, 122, 111, 46, 173, 113,
  786. };
  787. }
  788. #endif
  789. }
  790. }