WebRequestTest.cs 12 KB

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