HttpWebRequestTest.cs 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980
  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] // bug #429200
  240. public void GetRequestStream ()
  241. {
  242. IPEndPoint ep = new IPEndPoint (IPAddress.Loopback, 8000);
  243. string url = "http://" + IPAddress.Loopback.ToString () + ":8000/test/";
  244. using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (EchoRequestHandler))) {
  245. responder.Start ();
  246. HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
  247. req.Method = "POST";
  248. req.Timeout = 2000;
  249. req.ReadWriteTimeout = 2000;
  250. Stream rs1 = req.GetRequestStream ();
  251. Stream rs2 = req.GetRequestStream ();
  252. Assert.IsNotNull (rs1, "#1");
  253. Assert.AreSame (rs1, rs2, "#2");
  254. rs1.Close ();
  255. responder.Stop ();
  256. }
  257. }
  258. [Test]
  259. [Ignore ("This test asserts that our code violates RFC 2616")]
  260. public void GetRequestStream_Body_NotAllowed ()
  261. {
  262. string [] methods = new string [] { "GET", "HEAD", "CONNECT",
  263. "get", "HeAd", "ConNect" };
  264. foreach (string method in methods) {
  265. HttpWebRequest req = (HttpWebRequest) WebRequest.Create (
  266. "http://localhost:8000");
  267. req.Method = method;
  268. try {
  269. req.GetRequestStream ();
  270. Assert.Fail ("#1:" + method);
  271. } catch (ProtocolViolationException ex) {
  272. Assert.AreEqual (typeof (ProtocolViolationException), ex.GetType (), "#2:" + method);
  273. Assert.IsNull (ex.InnerException, "#3:" + method);
  274. Assert.IsNotNull (ex.Message, "#4:" + method);
  275. }
  276. }
  277. }
  278. [Test]
  279. #if TARGET_JVM
  280. [Category("NotWorking")]
  281. #endif
  282. [Ignore ("This does not timeout any more. That's how MS works when reading small responses")]
  283. public void ReadTimeout ()
  284. {
  285. IPEndPoint localEP = new IPEndPoint (IPAddress.Loopback, 8764);
  286. string url = "http://" + localEP.ToString () + "/original/";
  287. using (SocketResponder responder = new SocketResponder (localEP, new SocketRequestHandler (RedirectRequestHandler))) {
  288. responder.Start ();
  289. HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
  290. req.Method = "POST";
  291. req.AllowAutoRedirect = false;
  292. req.Timeout = 200;
  293. req.ReadWriteTimeout = 100;
  294. req.KeepAlive = false;
  295. Stream rs = req.GetRequestStream ();
  296. rs.Close ();
  297. using (HttpWebResponse resp = (HttpWebResponse) req.GetResponse ()) {
  298. try {
  299. Stream s = resp.GetResponseStream ();
  300. s.ReadByte ();
  301. Assert.Fail ("#1");
  302. } catch (WebException ex) {
  303. Assert.AreEqual (typeof (WebException), ex.GetType (), "#2");
  304. Assert.IsNull (ex.InnerException, "#3");
  305. Assert.IsNull (ex.Response, "#4");
  306. Assert.AreEqual (WebExceptionStatus.Timeout, ex.Status, "#5");
  307. }
  308. }
  309. responder.Stop ();
  310. }
  311. }
  312. [Test] // bug #81624
  313. #if TARGET_JVM
  314. [Category("NotWorking")]
  315. #endif
  316. [Ignore ("Fails randomly. Need to do more debugging.")]
  317. public void AllowAutoRedirect ()
  318. {
  319. IPEndPoint localEP = new IPEndPoint (IPAddress.Loopback, 8764);
  320. string url = "http://" + localEP.ToString () + "/original/";
  321. // allow autoredirect
  322. using (SocketResponder responder = new SocketResponder (localEP, new SocketRequestHandler (RedirectRequestHandler))) {
  323. responder.Start ();
  324. HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
  325. req.Method = "POST";
  326. req.Timeout = 2000;
  327. req.ReadWriteTimeout = 2000;
  328. req.KeepAlive = false;
  329. Stream rs = req.GetRequestStream ();
  330. rs.Close ();
  331. using (HttpWebResponse resp = (HttpWebResponse) req.GetResponse ()) {
  332. StreamReader sr = new StreamReader (resp.GetResponseStream (),
  333. Encoding.UTF8);
  334. string body = sr.ReadToEnd ();
  335. Assert.AreEqual (resp.StatusCode, HttpStatusCode.OK, "#A1");
  336. Assert.AreEqual (resp.ResponseUri.ToString (), "http://" +
  337. localEP.ToString () + "/moved/", "#A2");
  338. Assert.AreEqual ("GET", resp.Method, "#A3");
  339. Assert.AreEqual ("LOOKS OK", body, "#A4");
  340. }
  341. responder.Stop ();
  342. }
  343. // do not allow autoredirect
  344. using (SocketResponder responder = new SocketResponder (localEP, new SocketRequestHandler (RedirectRequestHandler))) {
  345. responder.Start ();
  346. HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
  347. req.Method = "POST";
  348. req.AllowAutoRedirect = false;
  349. req.Timeout = 1000;
  350. req.ReadWriteTimeout = 1000;
  351. req.KeepAlive = false;
  352. Stream rs = req.GetRequestStream ();
  353. rs.Close ();
  354. using (HttpWebResponse resp = (HttpWebResponse) req.GetResponse ()) {
  355. Assert.AreEqual (resp.StatusCode, HttpStatusCode.Found, "#B1");
  356. Assert.AreEqual (url, resp.ResponseUri.ToString (), "#B2");
  357. Assert.AreEqual ("POST", resp.Method, "#B3");
  358. }
  359. responder.Stop ();
  360. }
  361. }
  362. [Test] // bug #81671
  363. [Category ("NotWorking")]
  364. public void InternalServerError ()
  365. {
  366. IPEndPoint localEP = new IPEndPoint (IPAddress.Loopback, 8764);
  367. string url = "http://" + localEP.ToString () + "/original/";
  368. // POST
  369. using (SocketResponder responder = new SocketResponder (localEP, new SocketRequestHandler (InternalErrorHandler))) {
  370. responder.Start ();
  371. HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
  372. req.Method = "POST";
  373. req.Timeout = 2000;
  374. req.ReadWriteTimeout = 2000;
  375. req.KeepAlive = false;
  376. Stream rs = req.GetRequestStream ();
  377. rs.Close ();
  378. try {
  379. req.GetResponse ();
  380. Assert.Fail ("#A1");
  381. } catch (WebException ex) {
  382. Assert.AreEqual (typeof (WebException), ex.GetType (), "#A2");
  383. Assert.IsNull (ex.InnerException, "#A3");
  384. Assert.IsNotNull (ex.Message, "#A4");
  385. Assert.AreEqual (WebExceptionStatus.ProtocolError, ex.Status, "#A5");
  386. HttpWebResponse webResponse = ex.Response as HttpWebResponse;
  387. Assert.IsNotNull (webResponse, "#A6");
  388. Assert.AreEqual ("POST", webResponse.Method, "#A7");
  389. webResponse.Close ();
  390. }
  391. responder.Stop ();
  392. }
  393. // GET
  394. using (SocketResponder responder = new SocketResponder (localEP, new SocketRequestHandler (InternalErrorHandler))) {
  395. responder.Start ();
  396. HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
  397. req.Method = "GET";
  398. req.Timeout = 2000;
  399. req.ReadWriteTimeout = 2000;
  400. req.KeepAlive = false;
  401. try {
  402. req.GetResponse ();
  403. Assert.Fail ("#B1");
  404. } catch (WebException ex) {
  405. Assert.AreEqual (typeof (WebException), ex.GetType (), "#B2");
  406. Assert.IsNull (ex.InnerException, "#B3");
  407. Assert.AreEqual (WebExceptionStatus.ProtocolError, ex.Status, "#B4");
  408. HttpWebResponse webResponse = ex.Response as HttpWebResponse;
  409. Assert.IsNotNull (webResponse, "#B5");
  410. Assert.AreEqual ("GET", webResponse.Method, "#B6");
  411. webResponse.Close ();
  412. }
  413. responder.Stop ();
  414. }
  415. }
  416. [Test]
  417. [Category ("NotWorking")] // we report a timeout
  418. public void NoContentLength ()
  419. {
  420. IPEndPoint localEP = new IPEndPoint (IPAddress.Loopback, 8764);
  421. string url = "http://" + localEP.ToString () + "/original/";
  422. // POST
  423. using (SocketResponder responder = new SocketResponder (localEP, new SocketRequestHandler (NoContentLengthHandler))) {
  424. responder.Start ();
  425. HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
  426. req.Method = "POST";
  427. req.Timeout = 2000;
  428. req.ReadWriteTimeout = 2000;
  429. req.KeepAlive = false;
  430. Stream rs = req.GetRequestStream ();
  431. rs.Close ();
  432. try {
  433. req.GetResponse ();
  434. Assert.Fail ("#A1");
  435. } catch (WebException ex) {
  436. Assert.AreEqual (typeof (WebException), ex.GetType (), "#A2");
  437. #if NET_2_0
  438. //Assert.IsNotNull (ex.InnerException, "#A3");
  439. Assert.AreEqual (WebExceptionStatus.ReceiveFailure, ex.Status, "#A4");
  440. Assert.AreEqual (typeof (IOException), ex.InnerException.GetType (), "#A5");
  441. // Unable to read data from the transport connection:
  442. // A connection attempt failed because the connected party
  443. // did not properly respond after a period of time, or
  444. // established connection failed because connected host has
  445. // failed to respond
  446. IOException ioe = (IOException) ex.InnerException;
  447. Assert.IsNotNull (ioe.InnerException, "#A6");
  448. Assert.IsNotNull (ioe.Message, "#A7");
  449. Assert.AreEqual (typeof (SocketException), ioe.InnerException.GetType (), "#A8");
  450. // A connection attempt failed because the connected party
  451. // did not properly respond after a period of time, or
  452. // established connection failed because connected host has
  453. // failed to respond
  454. SocketException soe = (SocketException) ioe.InnerException;
  455. Assert.IsNull (soe.InnerException, "#A9");
  456. Assert.IsNotNull (soe.Message, "#A10");
  457. HttpWebResponse webResponse = ex.Response as HttpWebResponse;
  458. Assert.IsNull (webResponse, "#A11");
  459. #else
  460. Assert.IsNull (ex.InnerException, "#A3");
  461. Assert.AreEqual (WebExceptionStatus.ProtocolError, ex.Status, "#A4");
  462. HttpWebResponse webResponse = ex.Response as HttpWebResponse;
  463. Assert.IsNotNull (webResponse, "#A5");
  464. Assert.AreEqual ("POST", webResponse.Method, "#A6");
  465. webResponse.Close ();
  466. #endif
  467. }
  468. responder.Stop ();
  469. }
  470. // GET
  471. using (SocketResponder responder = new SocketResponder (localEP, new SocketRequestHandler (NoContentLengthHandler))) {
  472. responder.Start ();
  473. HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
  474. req.Method = "GET";
  475. req.Timeout = 2000;
  476. req.ReadWriteTimeout = 2000;
  477. req.KeepAlive = false;
  478. try {
  479. req.GetResponse ();
  480. Assert.Fail ("#B1");
  481. } catch (WebException ex) {
  482. Assert.AreEqual (typeof (WebException), ex.GetType (), "#B2");
  483. Assert.IsNull (ex.InnerException, "#B3");
  484. Assert.AreEqual (WebExceptionStatus.ProtocolError, ex.Status, "#B4");
  485. HttpWebResponse webResponse = ex.Response as HttpWebResponse;
  486. Assert.IsNotNull (webResponse, "#B5");
  487. Assert.AreEqual ("GET", webResponse.Method, "#B6");
  488. webResponse.Close ();
  489. }
  490. responder.Stop ();
  491. }
  492. }
  493. #if NET_2_0
  494. [Test] // bug #81504
  495. #if TARGET_JVM
  496. [Category ("NotWorking")]
  497. #endif
  498. public void Stream_CanTimeout ()
  499. {
  500. IPEndPoint localEP = new IPEndPoint (IPAddress.Loopback, 8764);
  501. string url = "http://" + localEP.ToString () + "/original/";
  502. // allow autoredirect
  503. using (SocketResponder responder = new SocketResponder (localEP, new SocketRequestHandler (RedirectRequestHandler))) {
  504. responder.Start ();
  505. HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
  506. req.Method = "POST";
  507. req.Timeout = 2000;
  508. req.ReadWriteTimeout = 2000;
  509. req.KeepAlive = false;
  510. Stream rs = req.GetRequestStream ();
  511. Assert.IsTrue (rs.CanTimeout, "#1");
  512. rs.Close ();
  513. using (HttpWebResponse resp = (HttpWebResponse) req.GetResponse ()) {
  514. Stream os = resp.GetResponseStream ();
  515. Assert.IsTrue (os.CanTimeout, "#2");
  516. os.Close ();
  517. }
  518. responder.Stop ();
  519. }
  520. }
  521. #endif
  522. [Test] // bug #353495
  523. [Category ("NotWorking")]
  524. public void LastModifiedKind ()
  525. {
  526. const string reqURL = "http://coffeefaq.com/site/node/25";
  527. HttpWebRequest req = (HttpWebRequest) WebRequest.Create (reqURL);
  528. HttpWebResponse resp = (HttpWebResponse) req.GetResponse ();
  529. DateTime lastMod = resp.LastModified;
  530. string rawLastMod = resp.Headers ["Last-Modified"];
  531. resp.Close ();
  532. //Assert.AreEqual ("Tue, 15 Jan 2008 08:59:59 GMT", rawLastMod, "#1");
  533. #if NET_2_0
  534. Assert.AreEqual (DateTimeKind.Local, lastMod.Kind, "#2");
  535. #endif
  536. req = (HttpWebRequest) WebRequest.Create (reqURL);
  537. req.IfModifiedSince = lastMod;
  538. try {
  539. resp = (HttpWebResponse) req.GetResponse ();
  540. resp.Close ();
  541. Assert.Fail ("Should result in 304");
  542. } catch (WebException ex) {
  543. Assert.AreEqual (WebExceptionStatus.ProtocolError, ex.Status, "#3");
  544. Assert.AreEqual (((HttpWebResponse) ex.Response).StatusCode, HttpStatusCode.NotModified, "#4");
  545. }
  546. }
  547. static byte [] EchoRequestHandler (Socket socket)
  548. {
  549. MemoryStream ms = new MemoryStream ();
  550. byte [] buffer = new byte [4096];
  551. int bytesReceived = socket.Receive (buffer);
  552. while (bytesReceived > 0) {
  553. ms.Write (buffer, 0, bytesReceived);
  554. if (socket.Available > 0) {
  555. bytesReceived = socket.Receive (buffer);
  556. } else {
  557. bytesReceived = 0;
  558. }
  559. }
  560. ms.Flush ();
  561. ms.Position = 0;
  562. StreamReader sr = new StreamReader (ms, Encoding.UTF8);
  563. string request = sr.ReadToEnd ();
  564. StringWriter sw = new StringWriter ();
  565. sw.WriteLine ("HTTP/1.1 200 OK");
  566. sw.WriteLine ("Content-Type: text/xml");
  567. sw.WriteLine ("Content-Length: " + request.Length.ToString (CultureInfo.InvariantCulture));
  568. sw.WriteLine ();
  569. sw.Write (request);
  570. sw.Flush ();
  571. return Encoding.UTF8.GetBytes (sw.ToString ());
  572. }
  573. static byte [] RedirectRequestHandler (Socket socket)
  574. {
  575. MemoryStream ms = new MemoryStream ();
  576. byte [] buffer = new byte [4096];
  577. int bytesReceived = socket.Receive (buffer);
  578. while (bytesReceived > 0) {
  579. ms.Write (buffer, 0, bytesReceived);
  580. if (socket.Available > 0) {
  581. bytesReceived = socket.Receive (buffer);
  582. } else {
  583. bytesReceived = 0;
  584. }
  585. }
  586. ms.Flush ();
  587. ms.Position = 0;
  588. string statusLine = null;
  589. using (StreamReader sr = new StreamReader (ms, Encoding.UTF8)) {
  590. statusLine = sr.ReadLine ();
  591. }
  592. StringWriter sw = new StringWriter ();
  593. if (statusLine.StartsWith ("POST /original/")) {
  594. sw.WriteLine ("HTTP/1.0 302 Found");
  595. sw.WriteLine ("Location: " + "http://" + IPAddress.Loopback.ToString () + ":8764/moved/");
  596. sw.WriteLine ();
  597. sw.Flush ();
  598. } else if (statusLine.StartsWith ("GET /moved/")) {
  599. sw.WriteLine ("HTTP/1.0 200 OK");
  600. sw.WriteLine ("Content-Type: text/plain");
  601. sw.WriteLine ("Content-Length: 8");
  602. sw.WriteLine ();
  603. sw.Write ("LOOKS OK");
  604. sw.Flush ();
  605. } else {
  606. sw.WriteLine ("HTTP/1.0 500 Too Lazy");
  607. sw.WriteLine ();
  608. sw.Flush ();
  609. }
  610. return Encoding.UTF8.GetBytes (sw.ToString ());
  611. }
  612. static byte [] InternalErrorHandler (Socket socket)
  613. {
  614. StringWriter sw = new StringWriter ();
  615. sw.WriteLine ("HTTP/1.1 500 Too Lazy");
  616. sw.WriteLine ("Content-Length: 0");
  617. sw.WriteLine ();
  618. sw.Flush ();
  619. return Encoding.UTF8.GetBytes (sw.ToString ());
  620. }
  621. static byte [] NoContentLengthHandler (Socket socket)
  622. {
  623. StringWriter sw = new StringWriter ();
  624. sw.WriteLine ("HTTP/1.1 500 Too Lazy");
  625. sw.WriteLine ();
  626. sw.Flush ();
  627. return Encoding.UTF8.GetBytes (sw.ToString ());
  628. }
  629. [Test]
  630. public void NtlmAuthentication ()
  631. {
  632. NtlmServer server = new NtlmServer ();
  633. server.Start ();
  634. string url = String.Format ("http://{0}:{1}/nothing.html", server.IPAddress, server.Port);
  635. HttpWebRequest request = (HttpWebRequest) WebRequest.Create (url);
  636. request.Timeout = 5000;
  637. request.Credentials = new NetworkCredential ("user", "password", "domain");
  638. HttpWebResponse resp = (HttpWebResponse) request.GetResponse ();
  639. string res = null;
  640. using (StreamReader reader = new StreamReader (resp.GetResponseStream ())) {
  641. res = reader.ReadToEnd ();
  642. }
  643. resp.Close ();
  644. server.Stop ();
  645. Assert.AreEqual ("OK", res);
  646. }
  647. class NtlmServer : HttpServer {
  648. public string Where = "";
  649. protected override void Run ()
  650. {
  651. Where = "before accept";
  652. Socket client = sock.Accept ();
  653. NetworkStream ns = new NetworkStream (client, false);
  654. StreamReader reader = new StreamReader (ns, Encoding.ASCII);
  655. string line;
  656. Where = "first read";
  657. while ((line = reader.ReadLine ()) != null) {
  658. if (line.Trim () == String.Empty) {
  659. break;
  660. }
  661. }
  662. Where = "first write";
  663. StreamWriter writer = new StreamWriter (ns, Encoding.ASCII);
  664. writer.Write ( "HTTP/1.1 401 Unauthorized\r\n" +
  665. "WWW-Authenticate: NTLM\r\n" +
  666. "Content-Length: 5\r\n\r\nWRONG");
  667. writer.Flush ();
  668. Where = "second read";
  669. while ((line = reader.ReadLine ()) != null) {
  670. if (line.Trim () == String.Empty) {
  671. break;
  672. }
  673. }
  674. Where = "second write";
  675. writer.Write ( "HTTP/1.1 401 Unauthorized\r\n" +
  676. "WWW-Authenticate: NTLM TlRMTVNTUAACAAAAAAAAADgAAAABggAC8GDhqIONH3sAAAAAAAAAAAAAAAA4AAAABQLODgAAAA8=\r\n" +
  677. "Content-Length: 5\r\n\r\nWRONG");
  678. writer.Flush ();
  679. Where = "third read";
  680. while ((line = reader.ReadLine ()) != null) {
  681. if (line.Trim () == String.Empty) {
  682. break;
  683. }
  684. }
  685. Where = "third write";
  686. writer.Write ( "HTTP/1.1 200 OK\r\n" +
  687. "Keep-Alive: true\r\n" +
  688. "Content-Length: 2\r\n\r\nOK");
  689. writer.Flush ();
  690. Thread.Sleep (1000);
  691. writer.Close ();
  692. reader.Close ();
  693. client.Close ();
  694. }
  695. }
  696. class BadChunkedServer : HttpServer {
  697. protected override void Run ()
  698. {
  699. Socket client = sock.Accept ();
  700. NetworkStream ns = new NetworkStream (client, true);
  701. StreamWriter writer = new StreamWriter (ns, Encoding.ASCII);
  702. writer.Write ( "HTTP/1.1 200 OK\r\n" +
  703. "Transfer-Encoding: chunked\r\n" +
  704. "Connection: close\r\n" +
  705. "Content-Type: text/plain; charset=UTF-8\r\n\r\n");
  706. // This body lacks a 'last-chunk' (see RFC 2616)
  707. writer.Write ("10\r\n1234567890123456\r\n");
  708. writer.Flush ();
  709. client.Shutdown (SocketShutdown.Send);
  710. Thread.Sleep (1000);
  711. writer.Close ();
  712. }
  713. }
  714. class AcceptAllPolicy : ICertificatePolicy {
  715. public bool CheckValidationResult (ServicePoint sp, X509Certificate certificate, WebRequest request, int error)
  716. {
  717. return true;
  718. }
  719. }
  720. abstract class HttpServer
  721. {
  722. protected Socket sock;
  723. protected Exception error;
  724. protected ManualResetEvent evt;
  725. public HttpServer ()
  726. {
  727. sock = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  728. sock.Bind (new IPEndPoint (IPAddress.Loopback, 0));
  729. sock.Listen (1);
  730. }
  731. public void Start ()
  732. {
  733. evt = new ManualResetEvent (false);
  734. Thread th = new Thread (new ThreadStart (Run));
  735. th.Start ();
  736. }
  737. public void Stop ()
  738. {
  739. evt.Set ();
  740. sock.Close ();
  741. }
  742. public IPAddress IPAddress {
  743. get { return ((IPEndPoint) sock.LocalEndPoint).Address; }
  744. }
  745. public int Port {
  746. get { return ((IPEndPoint) sock.LocalEndPoint).Port; }
  747. }
  748. public Exception Error {
  749. get { return error; }
  750. }
  751. protected abstract void Run ();
  752. }
  753. #if !TARGET_JVM
  754. class SslHttpServer : HttpServer {
  755. X509Certificate _certificate;
  756. protected override void Run ()
  757. {
  758. try {
  759. Socket client = sock.Accept ();
  760. NetworkStream ns = new NetworkStream (client, true);
  761. SslServerStream s = new SslServerStream (ns, Certificate, false, false);
  762. s.PrivateKeyCertSelectionDelegate += new PrivateKeySelectionCallback (GetPrivateKey);
  763. StreamReader reader = new StreamReader (s);
  764. StreamWriter writer = new StreamWriter (s, Encoding.ASCII);
  765. string line;
  766. string hello = "<html><body><h1>Hello World!</h1></body></html>";
  767. string answer = "HTTP/1.0 200\r\n" +
  768. "Connection: close\r\n" +
  769. "Content-Type: text/html\r\n" +
  770. "Content-Encoding: " + Encoding.ASCII.WebName + "\r\n" +
  771. "Content-Length: " + hello.Length + "\r\n" +
  772. "\r\n" + hello;
  773. // Read the headers
  774. do {
  775. line = reader.ReadLine ();
  776. } while (line != "" && line != null && line.Length > 0);
  777. // Now the content. We know it's 100 bytes.
  778. // This makes BeginRead in sslclientstream block.
  779. char [] cs = new char [100];
  780. reader.Read (cs, 0, 100);
  781. writer.Write (answer);
  782. writer.Flush ();
  783. evt.WaitOne (50000, false);
  784. } catch (Exception e) {
  785. error = e;
  786. }
  787. }
  788. X509Certificate Certificate {
  789. get {
  790. if (_certificate == null)
  791. _certificate = new X509Certificate (CertData.Certificate);
  792. return _certificate;
  793. }
  794. }
  795. AsymmetricAlgorithm GetPrivateKey (X509Certificate certificate, string targetHost)
  796. {
  797. PrivateKey key = new PrivateKey (CertData.PrivateKey, null);
  798. return key.RSA;
  799. }
  800. }
  801. class CertData {
  802. public readonly static byte [] Certificate = {
  803. 48, 130, 1, 191, 48, 130, 1, 40, 160, 3, 2, 1, 2, 2, 16, 36,
  804. 14, 97, 190, 146, 132, 208, 71, 175, 6, 87, 168, 185, 175, 55, 43, 48,
  805. 13, 6, 9, 42, 134, 72, 134, 247, 13, 1, 1, 4, 5, 0, 48, 18,
  806. 49, 16, 48, 14, 6, 3, 85, 4, 3, 19, 7, 103, 111, 110, 122, 97,
  807. 108, 111, 48, 30, 23, 13, 48, 53, 48, 54, 50, 50, 49, 57, 51, 48,
  808. 52, 54, 90, 23, 13, 51, 57, 49, 50, 51, 49, 50, 51, 53, 57, 53,
  809. 57, 90, 48, 18, 49, 16, 48, 14, 6, 3, 85, 4, 3, 19, 7, 103,
  810. 111, 110, 122, 97, 108, 111, 48, 129, 158, 48, 13, 6, 9, 42, 134, 72,
  811. 134, 247, 13, 1, 1, 1, 5, 0, 3, 129, 140, 0, 48, 129, 136, 2,
  812. 129, 129, 0, 138, 9, 38, 25, 166, 252, 59, 26, 39, 184, 128, 216, 38,
  813. 73, 41, 86, 30, 228, 160, 205, 41, 135, 115, 223, 44, 62, 42, 198, 178,
  814. 190, 81, 11, 25, 21, 216, 49, 179, 130, 246, 52, 97, 175, 212, 94, 157,
  815. 231, 162, 66, 161, 103, 63, 204, 83, 141, 172, 119, 97, 225, 206, 98, 101,
  816. 210, 106, 2, 206, 81, 90, 173, 47, 41, 199, 209, 241, 177, 177, 96, 207,
  817. 254, 220, 190, 66, 180, 153, 0, 209, 14, 178, 69, 194, 3, 37, 116, 239,
  818. 49, 23, 185, 245, 255, 126, 35, 85, 246, 56, 244, 107, 117, 24, 14, 57,
  819. 9, 111, 147, 189, 220, 142, 57, 104, 153, 193, 205, 19, 14, 22, 157, 16,
  820. 24, 80, 201, 2, 2, 0, 17, 163, 23, 48, 21, 48, 19, 6, 3, 85,
  821. 29, 37, 4, 12, 48, 10, 6, 8, 43, 6, 1, 5, 5, 7, 3, 1,
  822. 48, 13, 6, 9, 42, 134, 72, 134, 247, 13, 1, 1, 4, 5, 0, 3,
  823. 129, 129, 0, 64, 49, 57, 253, 218, 198, 229, 51, 189, 12, 154, 225, 183,
  824. 160, 147, 90, 113, 172, 69, 122, 28, 77, 97, 215, 231, 194, 150, 29, 196,
  825. 65, 95, 218, 99, 142, 111, 79, 205, 109, 76, 32, 92, 220, 76, 88, 53,
  826. 237, 80, 11, 85, 44, 91, 21, 210, 12, 34, 223, 234, 18, 187, 136, 62,
  827. 26, 240, 103, 180, 12, 226, 221, 250, 247, 129, 51, 23, 129, 165, 56, 67,
  828. 43, 83, 244, 110, 207, 24, 253, 195, 16, 46, 80, 113, 80, 18, 2, 254,
  829. 120, 147, 151, 164, 23, 210, 230, 100, 19, 197, 179, 28, 194, 48, 106, 159,
  830. 155, 144, 37, 82, 44, 160, 40, 52, 146, 174, 77, 188, 160, 230, 75, 172,
  831. 123, 3, 254,
  832. };
  833. public readonly static byte [] PrivateKey = {
  834. 30, 241, 181, 176, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0,
  835. 0, 0, 0, 0, 84, 2, 0, 0, 7, 2, 0, 0, 0, 36, 0, 0,
  836. 82, 83, 65, 50, 0, 4, 0, 0, 17, 0, 0, 0, 201, 80, 24, 16,
  837. 157, 22, 14, 19, 205, 193, 153, 104, 57, 142, 220, 189, 147, 111, 9, 57,
  838. 14, 24, 117, 107, 244, 56, 246, 85, 35, 126, 255, 245, 185, 23, 49, 239,
  839. 116, 37, 3, 194, 69, 178, 14, 209, 0, 153, 180, 66, 190, 220, 254, 207,
  840. 96, 177, 177, 241, 209, 199, 41, 47, 173, 90, 81, 206, 2, 106, 210, 101,
  841. 98, 206, 225, 97, 119, 172, 141, 83, 204, 63, 103, 161, 66, 162, 231, 157,
  842. 94, 212, 175, 97, 52, 246, 130, 179, 49, 216, 21, 25, 11, 81, 190, 178,
  843. 198, 42, 62, 44, 223, 115, 135, 41, 205, 160, 228, 30, 86, 41, 73, 38,
  844. 216, 128, 184, 39, 26, 59, 252, 166, 25, 38, 9, 138, 175, 88, 190, 223,
  845. 27, 24, 224, 123, 190, 69, 164, 234, 129, 59, 108, 229, 248, 62, 187, 15,
  846. 235, 147, 162, 83, 47, 123, 170, 190, 224, 31, 215, 110, 143, 31, 227, 216,
  847. 85, 88, 154, 83, 207, 229, 41, 28, 237, 116, 181, 17, 37, 141, 224, 185,
  848. 164, 144, 141, 233, 164, 138, 177, 241, 115, 181, 230, 150, 7, 92, 139, 141,
  849. 113, 95, 57, 191, 211, 165, 217, 250, 197, 68, 164, 184, 168, 43, 48, 65,
  850. 177, 237, 173, 144, 148, 221, 62, 189, 147, 63, 216, 188, 206, 103, 226, 171,
  851. 32, 20, 230, 116, 144, 192, 1, 39, 202, 87, 74, 250, 6, 142, 188, 23,
  852. 45, 4, 112, 191, 253, 67, 69, 70, 128, 143, 44, 234, 41, 96, 195, 82,
  853. 202, 35, 158, 149, 240, 151, 23, 25, 166, 179, 85, 144, 58, 120, 149, 229,
  854. 205, 34, 8, 110, 86, 119, 130, 210, 37, 173, 65, 71, 169, 67, 8, 51,
  855. 20, 96, 51, 155, 3, 39, 85, 187, 40, 193, 57, 19, 99, 78, 173, 28,
  856. 129, 154, 108, 175, 8, 138, 237, 71, 27, 148, 129, 35, 47, 57, 101, 237,
  857. 168, 178, 227, 221, 212, 63, 124, 254, 253, 215, 183, 159, 49, 103, 74, 49,
  858. 67, 160, 171, 72, 194, 215, 108, 251, 178, 18, 184, 100, 211, 105, 21, 186,
  859. 39, 66, 218, 154, 72, 222, 90, 237, 179, 251, 51, 224, 212, 56, 251, 6,
  860. 209, 151, 198, 176, 89, 110, 35, 141, 248, 237, 223, 68, 135, 206, 207, 169,
  861. 254, 219, 243, 130, 71, 11, 94, 113, 233, 92, 63, 156, 169, 72, 215, 110,
  862. 95, 94, 191, 50, 59, 89, 187, 59, 183, 99, 161, 146, 233, 245, 219, 80,
  863. 87, 113, 251, 50, 144, 195, 158, 46, 189, 232, 119, 91, 75, 22, 6, 176,
  864. 39, 206, 25, 196, 213, 195, 219, 24, 28, 103, 104, 36, 137, 128, 4, 119,
  865. 163, 40, 126, 87, 18, 86, 128, 243, 213, 101, 2, 237, 78, 64, 160, 55,
  866. 199, 93, 90, 126, 175, 199, 55, 89, 234, 190, 5, 16, 196, 88, 28, 208,
  867. 28, 92, 32, 115, 204, 9, 202, 101, 15, 123, 43, 75, 90, 144, 95, 179,
  868. 102, 249, 57, 150, 204, 99, 147, 203, 16, 63, 81, 244, 226, 237, 82, 204,
  869. 20, 200, 140, 65, 83, 217, 161, 23, 123, 37, 115, 12, 100, 73, 70, 190,
  870. 32, 235, 174, 140, 148, 157, 47, 238, 40, 208, 228, 80, 54, 187, 156, 252,
  871. 253, 230, 231, 156, 138, 125, 96, 79, 3, 27, 143, 55, 146, 169, 165, 61,
  872. 238, 60, 227, 77, 217, 93, 117, 122, 111, 46, 173, 113,
  873. };
  874. }
  875. #endif
  876. }
  877. }