WebRequestTest.cs 12 KB

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