WebRequestTest.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. //
  2. // WebRequestTest.cs - NUnit Test Cases for System.Net.WebRequest
  3. //
  4. // Authors:
  5. // Lawrence Pit ([email protected])
  6. // Martin Willemoes Hansen ([email protected])
  7. // Sebastien Pouliot <[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 MonoTests.Helpers;
  14. using System;
  15. using System.Net;
  16. using System.Threading;
  17. using System.Collections;
  18. using System.Runtime.Serialization;
  19. using Socks = System.Net.Sockets;
  20. namespace MonoTests.System.Net {
  21. // WebRequest is abstract
  22. public class NonAbstractWebRequest : WebRequest
  23. {
  24. public NonAbstractWebRequest ()
  25. {
  26. }
  27. public NonAbstractWebRequest (SerializationInfo si, StreamingContext sc)
  28. : base (si, sc)
  29. {
  30. }
  31. }
  32. [TestFixture]
  33. public class WebRequestTest {
  34. private void Callback (IAsyncResult ar)
  35. {
  36. Assert.Fail ("Callback");
  37. }
  38. [Test]
  39. public void SerializationConstructor ()
  40. {
  41. NonAbstractWebRequest w = new NonAbstractWebRequest (null, new StreamingContext ());
  42. Assert.IsNotNull (w);
  43. }
  44. // properties (only test 'get'ter)
  45. [Test]
  46. [ExpectedException (typeof (NotImplementedException))]
  47. public void ConnectionGroupName ()
  48. {
  49. NonAbstractWebRequest w = new NonAbstractWebRequest ();
  50. Assert.IsNull (w.ConnectionGroupName);
  51. }
  52. [Test]
  53. [ExpectedException (typeof (NotImplementedException))]
  54. public void ContentLength ()
  55. {
  56. NonAbstractWebRequest w = new NonAbstractWebRequest ();
  57. Assert.IsNull (w.ContentLength);
  58. }
  59. [Test]
  60. [ExpectedException (typeof (NotImplementedException))]
  61. public void ContentType ()
  62. {
  63. NonAbstractWebRequest w = new NonAbstractWebRequest ();
  64. Assert.IsNull (w.ContentType);
  65. }
  66. [Test]
  67. [ExpectedException (typeof (NotImplementedException))]
  68. public void Credentials ()
  69. {
  70. NonAbstractWebRequest w = new NonAbstractWebRequest ();
  71. Assert.IsNull (w.Credentials);
  72. }
  73. [Test]
  74. [ExpectedException (typeof (NotImplementedException))]
  75. public void Headers ()
  76. {
  77. NonAbstractWebRequest w = new NonAbstractWebRequest ();
  78. Assert.IsNull (w.Headers);
  79. }
  80. [Test]
  81. [ExpectedException (typeof (NotImplementedException))]
  82. public void Method ()
  83. {
  84. NonAbstractWebRequest w = new NonAbstractWebRequest ();
  85. Assert.IsNull (w.Method);
  86. }
  87. [Test]
  88. [ExpectedException (typeof (NotImplementedException))]
  89. public void PreAuthenticate ()
  90. {
  91. NonAbstractWebRequest w = new NonAbstractWebRequest ();
  92. Assert.IsTrue (w.PreAuthenticate);
  93. }
  94. [Test]
  95. [ExpectedException (typeof (NotImplementedException))]
  96. public void Proxy ()
  97. {
  98. NonAbstractWebRequest w = new NonAbstractWebRequest ();
  99. Assert.IsNull (w.Proxy);
  100. }
  101. [Test]
  102. [ExpectedException (typeof (NotImplementedException))]
  103. public void RequestUri ()
  104. {
  105. NonAbstractWebRequest w = new NonAbstractWebRequest ();
  106. Assert.IsNull (w.RequestUri);
  107. }
  108. [Test]
  109. [ExpectedException (typeof (NotImplementedException))]
  110. public void Timeout ()
  111. {
  112. NonAbstractWebRequest w = new NonAbstractWebRequest ();
  113. Assert.IsNull (w.Timeout);
  114. }
  115. // methods
  116. [Test]
  117. [ExpectedException (typeof (NotImplementedException))]
  118. public void Abort ()
  119. {
  120. NonAbstractWebRequest w = new NonAbstractWebRequest ();
  121. w.Abort ();
  122. }
  123. [Test]
  124. [ExpectedException (typeof (NotImplementedException))]
  125. public void BeginGetRequestStream ()
  126. {
  127. NonAbstractWebRequest w = new NonAbstractWebRequest ();
  128. IAsyncResult r = w.BeginGetRequestStream (new AsyncCallback (Callback), w);
  129. }
  130. [Test]
  131. [ExpectedException (typeof (NotImplementedException))]
  132. public void BeginGetResponse ()
  133. {
  134. NonAbstractWebRequest w = new NonAbstractWebRequest ();
  135. IAsyncResult r = w.BeginGetResponse (new AsyncCallback (Callback), w);
  136. }
  137. [Test]
  138. [ExpectedException (typeof (NotImplementedException))]
  139. public void EndGetRequestStream ()
  140. {
  141. NonAbstractWebRequest w = new NonAbstractWebRequest ();
  142. w.EndGetRequestStream (null);
  143. }
  144. [Test]
  145. [ExpectedException (typeof (NotImplementedException))]
  146. public void EndGetResponse ()
  147. {
  148. NonAbstractWebRequest w = new NonAbstractWebRequest ();
  149. w.EndGetResponse (null);
  150. }
  151. [Test]
  152. [ExpectedException (typeof (NotImplementedException))]
  153. public void GetRequestStream ()
  154. {
  155. NonAbstractWebRequest w = new NonAbstractWebRequest ();
  156. w.GetRequestStream ();
  157. }
  158. [Test]
  159. [ExpectedException (typeof (NotImplementedException))]
  160. public void GetResponse ()
  161. {
  162. NonAbstractWebRequest w = new NonAbstractWebRequest ();
  163. w.GetResponse ();
  164. }
  165. [Test]
  166. #if FEATURE_NO_BSD_SOCKETS
  167. [ExpectedException (typeof (PlatformNotSupportedException))]
  168. #endif
  169. public void All ()
  170. {
  171. WebRequest req = WebRequest.Create ("http://www.contoso.com");
  172. Assert.IsTrue (req is HttpWebRequest, "#1");
  173. req = WebRequest.Create ("https://www.contoso.com");
  174. Assert.IsTrue (req is HttpWebRequest, "#2");
  175. req = WebRequest.Create ("file://www.contoso.com");
  176. Assert.IsTrue (req is FileWebRequest, "#3");
  177. req = WebRequest.Create ("ftp://www.contoso.com");
  178. Assert.IsTrue (req is FtpWebRequest, "#4");
  179. WebRequest.RegisterPrefix ("http://www.contoso.com", new TestWebRequestCreator ());
  180. bool ret = WebRequest.RegisterPrefix ("http://WWW.contoso.com", new TestWebRequestCreator ());
  181. Assert.AreEqual (false, ret, "#5a");
  182. ret = WebRequest.RegisterPrefix ("http://www.contoso.com/foo/bar", new TestWebRequestCreator2 ());
  183. Assert.AreEqual (true, ret, "#5b");
  184. ret = WebRequest.RegisterPrefix ("http://www", new TestWebRequestCreator3 ());
  185. Assert.AreEqual (true, ret, "#5c");
  186. req = WebRequest.Create ("http://WWW.contoso.com");
  187. Assert.IsTrue (req is TestWebRequest, "#6");
  188. req = WebRequest.Create ("http://WWW.contoso.com/foo/bar/index.html");
  189. Assert.IsTrue (req is TestWebRequest2, "#7");
  190. req = WebRequest.Create ("http://WWW.x.com");
  191. Assert.IsTrue (req is TestWebRequest3, "#8");
  192. req = WebRequest.Create ("http://WWW.c");
  193. Assert.IsTrue (req is TestWebRequest3, "#9");
  194. req = WebRequest.CreateDefault (new Uri("http://WWW.contoso.com"));
  195. Assert.IsTrue (req is HttpWebRequest, "#10");
  196. try {
  197. req = WebRequest.Create ("tcp://www.contoso.com");
  198. Assert.Fail ("#11 should have failed with NotSupportedException");
  199. } catch (NotSupportedException) {
  200. }
  201. }
  202. [Test]
  203. public void Create_RequestUriString_Null ()
  204. {
  205. try {
  206. WebRequest.Create ((string) null);
  207. Assert.Fail ("#1");
  208. } catch (ArgumentNullException ex) {
  209. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  210. Assert.IsNull (ex.InnerException, "#3");
  211. Assert.IsNotNull (ex.Message, "#4");
  212. Assert.IsNotNull (ex.ParamName, "#5");
  213. Assert.AreEqual ("requestUriString", ex.ParamName, "#6");
  214. }
  215. }
  216. [Test]
  217. public void CreateDefault_RequestUri_Null ()
  218. {
  219. try {
  220. WebRequest.CreateDefault ((Uri) null);
  221. Assert.Fail ("#1");
  222. } catch (ArgumentNullException ex) {
  223. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  224. Assert.IsNull (ex.InnerException, "#3");
  225. Assert.IsNotNull (ex.Message, "#4");
  226. Assert.IsNotNull (ex.ParamName, "#5");
  227. Assert.AreEqual ("requestUri", ex.ParamName, "#6");
  228. }
  229. }
  230. [Test]
  231. #if FEATURE_NO_BSD_SOCKETS
  232. [ExpectedException (typeof (PlatformNotSupportedException))]
  233. #endif
  234. public void DefaultWebProxy ()
  235. {
  236. WebProxy proxy = new WebProxy ("proxy.intern.com", 83);
  237. WebRequest.DefaultWebProxy = proxy;
  238. Assert.IsNotNull (WebRequest.DefaultWebProxy, "#A1");
  239. Assert.AreSame (proxy, WebRequest.DefaultWebProxy, "#A2");
  240. HttpWebRequest req = (HttpWebRequest) WebRequest.CreateDefault (
  241. new Uri ("http://www.mono-project.com"));
  242. Assert.IsNotNull (req.Proxy, "#B1");
  243. Assert.AreSame (proxy, req.Proxy, "#B2");
  244. WebRequest.DefaultWebProxy = null;
  245. Assert.IsNull (WebRequest.DefaultWebProxy, "#C1");
  246. Assert.IsNotNull (req.Proxy, "#C2");
  247. Assert.AreSame (proxy, req.Proxy, "#C3");
  248. req = (HttpWebRequest) WebRequest.CreateDefault (
  249. new Uri ("http://www.mono-project.com"));
  250. Assert.IsNull (req.Proxy, "#D");
  251. }
  252. [Test]
  253. public void RegisterPrefix_Creator_Null ()
  254. {
  255. try {
  256. WebRequest.RegisterPrefix ("http://www.mono-project.com", (IWebRequestCreate) null);
  257. Assert.Fail ("#1");
  258. } catch (ArgumentNullException ex) {
  259. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  260. Assert.IsNull (ex.InnerException, "#3");
  261. Assert.IsNotNull (ex.Message, "#4");
  262. Assert.IsNotNull (ex.ParamName, "#5");
  263. Assert.AreEqual ("creator", ex.ParamName, "#6");
  264. }
  265. }
  266. [Test]
  267. public void RegisterPrefix_Prefix_Null ()
  268. {
  269. try {
  270. WebRequest.RegisterPrefix ((string) null, new TestWebRequestCreator ());
  271. Assert.Fail ("#1");
  272. } catch (ArgumentNullException ex) {
  273. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  274. Assert.IsNull (ex.InnerException, "#3");
  275. Assert.IsNotNull (ex.Message, "#4");
  276. Assert.IsNotNull (ex.ParamName, "#5");
  277. Assert.AreEqual ("prefix", ex.ParamName, "#6");
  278. }
  279. }
  280. [Test] //BNC#323452
  281. // Throws exception with Status == Timeout. The same code behaves as the test expects when run from a regular app.
  282. // Might be an issue with the test suite. To investigate.
  283. [Category("AndroidNotWorking")]
  284. public void TestFailedConnection ()
  285. {
  286. try {
  287. WebRequest.Create ("http://127.0.0.1:0/non-existant.txt").GetResponse ();
  288. Assert.Fail ("Should have raised an exception");
  289. #if FEATURE_NO_BSD_SOCKETS
  290. } catch (PlatformNotSupportedException) {
  291. // Expected
  292. #endif
  293. } catch (Exception e) {
  294. Assert.IsTrue (e is WebException, "Got " + e.GetType ().Name + ": " + e.Message);
  295. //#if NET_2_0 e.Message == "Unable to connect to the remote server"
  296. //#if NET_1_1 e.Message == "The underlying connection was closed: Unable to connect to the remote server."
  297. Assert.AreEqual (WebExceptionStatus.ConnectFailure, ((WebException)e).Status);
  298. //#if !NET_1_1 (this is not true in .NET 1.x)
  299. Assert.IsNotNull (e.InnerException);
  300. Assert.IsTrue (e.InnerException is Socks.SocketException, "InnerException should be SocketException");
  301. //e.Message == "The requested address is not valid in its context 127.0.0.1:0"
  302. //#endif
  303. }
  304. }
  305. [Test] //BNC#323452
  306. [Category ("AndroidNotWorking")] // Fails when ran as part of the entire BCL test suite. Works when only this fixture is ran
  307. public void TestFailedResolution ()
  308. {
  309. try {
  310. var req = WebRequest.Create ("http://thisdomaindoesnotexist.monotestcase.x/non-existant.txt").GetResponse ();
  311. /*
  312. * Work around broken t-online.de DNS Server.
  313. *
  314. * T-Online's DNS Server for DSL Customers resolves
  315. * non-exisitng domain names to
  316. * http://navigationshilfe1.t-online.de/dnserror?url=....
  317. * instead of reporting an error.
  318. */
  319. if (req.ResponseUri.DnsSafeHost.Equals ("navigationshilfe1.t-online.de"))
  320. Assert.Ignore ("Misbehaving DNS server.");
  321. Assert.Fail ("Should have raised an exception");
  322. #if FEATURE_NO_BSD_SOCKETS
  323. } catch (PlatformNotSupportedException) {
  324. // Expected
  325. #endif
  326. } catch (Exception e) {
  327. Assert.IsTrue (e is WebException);
  328. //#if NET_2_0 e.Message == "The underlying connection was closed: The remote name could not be resolved."
  329. //#if NET_1_1 e.Message == "The remote name could not be resolved: 'thisdomaindoesnotexist.monotestcase.x'"
  330. Assert.AreEqual (((WebException)e).Status, WebExceptionStatus.NameResolutionFailure);
  331. Assert.IsNull (e.InnerException);
  332. }
  333. }
  334. internal class TestWebRequestCreator : IWebRequestCreate
  335. {
  336. internal TestWebRequestCreator () { }
  337. public WebRequest Create (Uri uri)
  338. {
  339. return new TestWebRequest ();
  340. }
  341. }
  342. internal class TestWebRequest : WebRequest
  343. {
  344. internal TestWebRequest () { }
  345. }
  346. internal class TestWebRequestCreator2 : IWebRequestCreate
  347. {
  348. internal TestWebRequestCreator2 () { }
  349. public WebRequest Create (Uri uri)
  350. {
  351. return new TestWebRequest2 ();
  352. }
  353. }
  354. internal class TestWebRequest2 : WebRequest
  355. {
  356. internal TestWebRequest2 () { }
  357. }
  358. internal class TestWebRequestCreator3 : IWebRequestCreate
  359. {
  360. internal TestWebRequestCreator3 () { }
  361. public WebRequest Create (Uri uri)
  362. {
  363. return new TestWebRequest3 ();
  364. }
  365. }
  366. internal class TestWebRequest3 : WebRequest
  367. {
  368. internal TestWebRequest3 () { }
  369. }
  370. [Test] // Covers #41477
  371. #if FEATURE_NO_BSD_SOCKETS
  372. [ExpectedException (typeof (PlatformNotSupportedException))]
  373. #endif
  374. public void TestReceiveCancelation ()
  375. {
  376. var uri = "http://localhost:" + NetworkHelpers.FindFreePort () + "/";
  377. HttpListener listener = new HttpListener ();
  378. listener.Prefixes.Add (uri);
  379. listener.Start ();
  380. try {
  381. for (var i = 0; i < 10; i++) {
  382. var request = WebRequest.CreateHttp (uri);
  383. request.Method = "GET";
  384. var tokenSource = new CancellationTokenSource ();
  385. tokenSource.Token.Register(() => request.Abort ());
  386. var responseTask = request.GetResponseAsync ();
  387. var context = listener.GetContext ();
  388. byte[] outBuffer = new byte[8 * 1024];
  389. context.Response.OutputStream.WriteAsync (outBuffer, 0, outBuffer.Length);
  390. Assert.IsTrue (responseTask.Wait (1000), "Timeout #1");
  391. WebResponse response = responseTask.Result;
  392. var stream = response.GetResponseStream ();
  393. byte[] buffer = new byte[8 * 1024];
  394. var taskRead = stream.ReadAsync (buffer, 0, buffer.Length, tokenSource.Token);
  395. tokenSource.Cancel ();
  396. Assert.IsTrue (taskRead.Wait (1000), "Timeout #2");
  397. var byteRead = taskRead.Result;
  398. }
  399. } catch (AggregateException ex) {
  400. var webEx = ex.InnerException as WebException;
  401. Assert.IsNotNull(webEx, "Inner exception is not a WebException");
  402. Assert.AreEqual (webEx.Status, WebExceptionStatus.RequestCanceled);
  403. }
  404. listener.Close ();
  405. }
  406. }
  407. }