WebClientTest.cs 62 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936
  1. //
  2. // WebClientTest.cs - NUnit Test Cases for System.Net.WebClient
  3. //
  4. // Copyright (C) 2007 Novell, Inc (http://www.novell.com)
  5. //
  6. using System;
  7. using System.Collections;
  8. using System.Collections.Specialized;
  9. using System.Globalization;
  10. using System.IO;
  11. using System.Net;
  12. using System.Net.Sockets;
  13. using System.Runtime.Serialization;
  14. using System.Text;
  15. using System.Threading;
  16. using NUnit.Framework;
  17. using MonoTests.Helpers;
  18. namespace MonoTests.System.Net
  19. {
  20. [TestFixture]
  21. public class WebClientTest
  22. {
  23. private string _tempFolder;
  24. [SetUp]
  25. public void SetUp ()
  26. {
  27. _tempFolder = Path.Combine (Path.GetTempPath (),
  28. GetType ().FullName);
  29. if (Directory.Exists (_tempFolder))
  30. Directory.Delete (_tempFolder, true);
  31. Directory.CreateDirectory (_tempFolder);
  32. }
  33. [TearDown]
  34. public void TearDown ()
  35. {
  36. if (Directory.Exists (_tempFolder))
  37. Directory.Delete (_tempFolder, true);
  38. }
  39. [Test]
  40. #if FEATURE_NO_BSD_SOCKETS
  41. [ExpectedException (typeof (WebException))] // Something catches the PlatformNotSupportedException and re-throws an WebException
  42. #endif
  43. [Category ("InetAccess")]
  44. public void DownloadTwice ()
  45. {
  46. WebClient wc = new WebClient();
  47. string filename = Path.GetTempFileName();
  48. // A new, but empty file has been created. This is a test case
  49. // for bug 81005
  50. wc.DownloadFile("http://google.com/", filename);
  51. // Now, remove the file and attempt to download again.
  52. File.Delete(filename);
  53. wc.DownloadFile("http://google.com/", filename);
  54. }
  55. [Test]
  56. public void DownloadData1_Address_Null ()
  57. {
  58. WebClient wc = new WebClient ();
  59. try {
  60. wc.DownloadData ((string) null);
  61. Assert.Fail ("#1");
  62. } catch (ArgumentNullException ex) {
  63. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  64. Assert.IsNull (ex.InnerException, "#3");
  65. Assert.IsNotNull (ex.Message, "#4");
  66. Assert.IsNotNull (ex.ParamName, "#5");
  67. Assert.AreEqual ("address", ex.ParamName, "#6");
  68. }
  69. }
  70. [Test] // DownloadData (string)
  71. public void DownloadData1_Address_SchemeNotSupported ()
  72. {
  73. WebClient wc = new WebClient ();
  74. try {
  75. wc.DownloadData ("tp://scheme.notsupported");
  76. Assert.Fail ("#1");
  77. } catch (WebException ex) {
  78. // An error occurred performing a WebClient request
  79. Assert.AreEqual (typeof (WebException), ex.GetType (), "#2");
  80. Assert.IsNotNull (ex.InnerException, "#3");
  81. Assert.IsNotNull (ex.Message, "#4");
  82. Assert.IsNull (ex.Response, "#5");
  83. Assert.AreEqual (WebExceptionStatus.UnknownError, ex.Status, "#6");
  84. // The URI prefix is not recognized
  85. Exception inner = ex.InnerException;
  86. Assert.AreEqual (typeof (NotSupportedException), inner.GetType (), "#7");
  87. Assert.IsNull (inner.InnerException, "#8");
  88. Assert.IsNotNull (inner.Message, "#9");
  89. }
  90. }
  91. [Test] // DownloadData (Uri)
  92. public void DownloadData2_Address_Null ()
  93. {
  94. WebClient wc = new WebClient ();
  95. try {
  96. wc.DownloadData ((Uri) null);
  97. Assert.Fail ("#1");
  98. } catch (ArgumentNullException ex) {
  99. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  100. Assert.IsNull (ex.InnerException, "#3");
  101. Assert.IsNotNull (ex.Message, "#4");
  102. Assert.IsNotNull (ex.ParamName, "#5");
  103. Assert.AreEqual ("address", ex.ParamName, "#6");
  104. }
  105. }
  106. [Test] // DownloadData (Uri)
  107. public void DownloadData2_Address_SchemeNotSupported ()
  108. {
  109. WebClient wc = new WebClient ();
  110. try {
  111. wc.DownloadData (new Uri ("tp://scheme.notsupported"));
  112. Assert.Fail ("#1");
  113. } catch (WebException ex) {
  114. // An error occurred performing a WebClient request
  115. Assert.AreEqual (typeof (WebException), ex.GetType (), "#2");
  116. Assert.IsNotNull (ex.InnerException, "#3");
  117. Assert.IsNotNull (ex.Message, "#4");
  118. Assert.IsNull (ex.Response, "#5");
  119. Assert.AreEqual (WebExceptionStatus.UnknownError, ex.Status, "#6");
  120. // The URI prefix is not recognized
  121. Exception inner = ex.InnerException;
  122. Assert.AreEqual (typeof (NotSupportedException), inner.GetType (), "#7");
  123. Assert.IsNull (inner.InnerException, "#8");
  124. Assert.IsNotNull (inner.Message, "#9");
  125. }
  126. }
  127. [Test]
  128. public void DownloadFile1_Address_Null ()
  129. {
  130. WebClient wc = new WebClient ();
  131. try {
  132. wc.DownloadFile ((string) null, "tmp.out");
  133. Assert.Fail ("#1");
  134. } catch (ArgumentNullException ex) {
  135. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  136. Assert.IsNull (ex.InnerException, "#3");
  137. Assert.IsNotNull (ex.Message, "#4");
  138. Assert.IsNotNull (ex.ParamName, "#5");
  139. Assert.AreEqual ("address", ex.ParamName, "#6");
  140. }
  141. }
  142. [Test] // DownloadFile (string, string)
  143. public void DownloadFile1_Address_SchemeNotSupported ()
  144. {
  145. string file = Path.Combine (Path.GetTempPath (), "tmp.out");
  146. WebClient wc = new WebClient ();
  147. try {
  148. wc.DownloadFile ("tp://scheme.notsupported", file);
  149. Assert.Fail ("#1");
  150. } catch (WebException ex) {
  151. // An error occurred performing a WebClient request
  152. Assert.AreEqual (typeof (WebException), ex.GetType (), "#2");
  153. Assert.IsNotNull (ex.InnerException, "#3");
  154. Assert.IsNotNull (ex.Message, "#4");
  155. Assert.IsNull (ex.Response, "#5");
  156. Assert.AreEqual (WebExceptionStatus.UnknownError, ex.Status, "#6");
  157. // The URI prefix is not recognized
  158. Exception inner = ex.InnerException;
  159. Assert.AreEqual (typeof (NotSupportedException), inner.GetType (), "#7");
  160. Assert.IsNull (inner.InnerException, "#8");
  161. Assert.IsNotNull (inner.Message, "#9");
  162. }
  163. finally {
  164. if (File.Exists (file))
  165. File.Delete (file);
  166. }
  167. }
  168. [Test] // DownloadFile (string, string)
  169. public void DownloadFile1_FileName_Null ()
  170. {
  171. WebClient wc = new WebClient ();
  172. try {
  173. wc.DownloadFile ("tp://scheme.notsupported",
  174. (string) null);
  175. Assert.Fail ("#1");
  176. } catch (ArgumentNullException ex) {
  177. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  178. Assert.IsNull (ex.InnerException, "#3");
  179. Assert.IsNotNull (ex.Message, "#4");
  180. Assert.IsNotNull (ex.ParamName, "#5");
  181. Assert.AreEqual ("fileName", ex.ParamName, "#6");
  182. }
  183. }
  184. [Test] // DownloadFile (Uri, string)
  185. public void DownloadFile2_Address_Null ()
  186. {
  187. WebClient wc = new WebClient ();
  188. try {
  189. wc.DownloadFile ((Uri) null, "tmp.out");
  190. Assert.Fail ("#1");
  191. } catch (ArgumentNullException ex) {
  192. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  193. Assert.IsNull (ex.InnerException, "#3");
  194. Assert.IsNotNull (ex.Message, "#4");
  195. Assert.IsNotNull (ex.ParamName, "#5");
  196. Assert.AreEqual ("address", ex.ParamName, "#6");
  197. }
  198. }
  199. [Test] // DownloadFile (Uri, string)
  200. public void DownloadFile2_Address_SchemeNotSupported ()
  201. {
  202. string file = Path.Combine (Path.GetTempPath (), "tmp.out");
  203. WebClient wc = new WebClient ();
  204. try {
  205. wc.DownloadFile (new Uri ("tp://scheme.notsupported"), file);
  206. Assert.Fail ("#1");
  207. } catch (WebException ex) {
  208. // An error occurred performing a WebClient request
  209. Assert.AreEqual (typeof (WebException), ex.GetType (), "#2");
  210. Assert.IsNotNull (ex.InnerException, "#3");
  211. Assert.IsNotNull (ex.Message, "#4");
  212. Assert.IsNull (ex.Response, "#5");
  213. Assert.AreEqual (WebExceptionStatus.UnknownError, ex.Status, "#6");
  214. // The URI prefix is not recognized
  215. Exception inner = ex.InnerException;
  216. Assert.AreEqual (typeof (NotSupportedException), inner.GetType (), "#7");
  217. Assert.IsNull (inner.InnerException, "#8");
  218. Assert.IsNotNull (inner.Message, "#9");
  219. }
  220. finally {
  221. if (File.Exists (file))
  222. File.Delete (file);
  223. }
  224. }
  225. [Test] // DownloadFile (Uri, string)
  226. public void DownloadFile2_FileName_Null ()
  227. {
  228. WebClient wc = new WebClient ();
  229. try {
  230. wc.DownloadFile (new Uri ("tp://scheme.notsupported"),
  231. (string) null);
  232. Assert.Fail ("#1");
  233. } catch (ArgumentNullException ex) {
  234. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  235. Assert.IsNull (ex.InnerException, "#3");
  236. Assert.IsNotNull (ex.Message, "#4");
  237. Assert.IsNotNull (ex.ParamName, "#5");
  238. Assert.AreEqual ("fileName", ex.ParamName, "#6");
  239. }
  240. }
  241. [Test] // DownloadString (string)
  242. public void DownloadString1_Address_Null ()
  243. {
  244. WebClient wc = new WebClient ();
  245. try {
  246. wc.DownloadString ((string) null);
  247. Assert.Fail ("#1");
  248. } catch (ArgumentNullException ex) {
  249. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  250. Assert.IsNull (ex.InnerException, "#3");
  251. Assert.IsNotNull (ex.Message, "#4");
  252. Assert.IsNotNull (ex.ParamName, "#5");
  253. Assert.AreEqual ("address", ex.ParamName, "#6");
  254. }
  255. }
  256. [Test] // DownloadString (string)
  257. public void DownloadString1_Address_SchemeNotSupported ()
  258. {
  259. WebClient wc = new WebClient ();
  260. try {
  261. wc.DownloadString ("tp://scheme.notsupported");
  262. Assert.Fail ("#1");
  263. } catch (WebException ex) {
  264. // An error occurred performing a WebClient request
  265. Assert.AreEqual (typeof (WebException), ex.GetType (), "#2");
  266. Assert.IsNotNull (ex.InnerException, "#3");
  267. Assert.IsNotNull (ex.Message, "#4");
  268. Assert.IsNull (ex.Response, "#5");
  269. Assert.AreEqual (WebExceptionStatus.UnknownError, ex.Status, "#6");
  270. // The URI prefix is not recognized
  271. Exception inner = ex.InnerException;
  272. Assert.AreEqual (typeof (NotSupportedException), inner.GetType (), "#7");
  273. Assert.IsNull (inner.InnerException, "#8");
  274. Assert.IsNotNull (inner.Message, "#9");
  275. }
  276. }
  277. [Test] // DownloadString (Uri)
  278. public void DownloadString2_Address_Null ()
  279. {
  280. WebClient wc = new WebClient ();
  281. try {
  282. wc.DownloadString ((Uri) null);
  283. Assert.Fail ("#1");
  284. } catch (ArgumentNullException ex) {
  285. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  286. Assert.IsNull (ex.InnerException, "#3");
  287. Assert.IsNotNull (ex.Message, "#4");
  288. Assert.IsNotNull (ex.ParamName, "#5");
  289. Assert.AreEqual ("address", ex.ParamName, "#6");
  290. }
  291. }
  292. [Test] // DownloadString (Uri)
  293. public void DownloadString2_Address_SchemeNotSupported ()
  294. {
  295. WebClient wc = new WebClient ();
  296. try {
  297. wc.DownloadString (new Uri ("tp://scheme.notsupported"));
  298. Assert.Fail ("#1");
  299. } catch (WebException ex) {
  300. // An error occurred performing a WebClient request
  301. Assert.AreEqual (typeof (WebException), ex.GetType (), "#2");
  302. Assert.IsNotNull (ex.InnerException, "#3");
  303. Assert.IsNotNull (ex.Message, "#4");
  304. Assert.IsNull (ex.Response, "#5");
  305. Assert.AreEqual (WebExceptionStatus.UnknownError, ex.Status, "#6");
  306. // The URI prefix is not recognized
  307. Exception inner = ex.InnerException;
  308. Assert.AreEqual (typeof (NotSupportedException), inner.GetType (), "#7");
  309. Assert.IsNull (inner.InnerException, "#8");
  310. Assert.IsNotNull (inner.Message, "#9");
  311. }
  312. }
  313. [Test]
  314. public void EncodingTest ()
  315. {
  316. WebClient wc = new WebClient ();
  317. Assert.AreSame (Encoding.Default, wc.Encoding, "#1");
  318. wc.Encoding = Encoding.ASCII;
  319. Assert.AreSame (Encoding.ASCII, wc.Encoding, "#2");
  320. }
  321. [Test]
  322. public void Encoding_Value_Null ()
  323. {
  324. WebClient wc = new WebClient ();
  325. try {
  326. wc.Encoding = null;
  327. Assert.Fail ("#1");
  328. } catch (ArgumentNullException ex) {
  329. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  330. Assert.IsNull (ex.InnerException, "#3");
  331. Assert.IsNotNull (ex.Message, "#4");
  332. Assert.AreEqual ("Encoding", ex.ParamName, "#6");
  333. }
  334. }
  335. [Test] // OpenRead (string)
  336. public void OpenRead1_Address_Null ()
  337. {
  338. WebClient wc = new WebClient ();
  339. try {
  340. wc.OpenRead ((string) null);
  341. Assert.Fail ("#1");
  342. } catch (ArgumentNullException ex) {
  343. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  344. Assert.IsNull (ex.InnerException, "#3");
  345. Assert.IsNotNull (ex.Message, "#4");
  346. Assert.IsNotNull (ex.ParamName, "#5");
  347. Assert.AreEqual ("address", ex.ParamName, "#6");
  348. }
  349. }
  350. [Test] // OpenRead (string)
  351. public void OpenRead1_Address_SchemeNotSupported ()
  352. {
  353. WebClient wc = new WebClient ();
  354. try {
  355. wc.OpenRead ("tp://scheme.notsupported");
  356. Assert.Fail ("#1");
  357. } catch (WebException ex) {
  358. // An error occurred performing a WebClient request
  359. Assert.AreEqual (typeof (WebException), ex.GetType (), "#2");
  360. Assert.IsNotNull (ex.InnerException, "#3");
  361. Assert.IsNotNull (ex.Message, "#4");
  362. Assert.IsNull (ex.Response, "#5");
  363. Assert.AreEqual (WebExceptionStatus.UnknownError, ex.Status, "#6");
  364. // The URI prefix is not recognized
  365. Exception inner = ex.InnerException;
  366. Assert.AreEqual (typeof (NotSupportedException), inner.GetType (), "#7");
  367. Assert.IsNull (inner.InnerException, "#8");
  368. Assert.IsNotNull (inner.Message, "#9");
  369. }
  370. }
  371. [Test] // OpenRead (Uri)
  372. public void OpenRead2_Address_Null ()
  373. {
  374. WebClient wc = new WebClient ();
  375. try {
  376. wc.OpenRead ((Uri) null);
  377. Assert.Fail ("#1");
  378. } catch (ArgumentNullException ex) {
  379. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  380. Assert.IsNull (ex.InnerException, "#3");
  381. Assert.IsNotNull (ex.Message, "#4");
  382. Assert.IsNotNull (ex.ParamName, "#5");
  383. Assert.AreEqual ("address", ex.ParamName, "#6");
  384. }
  385. }
  386. [Test] // OpenRead (Uri)
  387. public void OpenRead2_Address_SchemeNotSupported ()
  388. {
  389. WebClient wc = new WebClient ();
  390. try {
  391. wc.OpenRead (new Uri ("tp://scheme.notsupported"));
  392. Assert.Fail ("#1");
  393. } catch (WebException ex) {
  394. // An error occurred performing a WebClient request
  395. Assert.AreEqual (typeof (WebException), ex.GetType (), "#2");
  396. Assert.IsNotNull (ex.InnerException, "#3");
  397. Assert.IsNotNull (ex.Message, "#4");
  398. Assert.IsNull (ex.Response, "#5");
  399. Assert.AreEqual (WebExceptionStatus.UnknownError, ex.Status, "#6");
  400. // The URI prefix is not recognized
  401. Exception inner = ex.InnerException;
  402. Assert.AreEqual (typeof (NotSupportedException), inner.GetType (), "#7");
  403. Assert.IsNull (inner.InnerException, "#8");
  404. Assert.IsNotNull (inner.Message, "#9");
  405. }
  406. }
  407. [Test]
  408. public void OpenReadTaskAsyncOnFile ()
  409. {
  410. var tmp = Path.GetTempFileName ();
  411. string url = "file://" + tmp;
  412. var client = new WebClient ();
  413. var task = client.OpenReadTaskAsync (url);
  414. Assert.IsTrue (task.Wait (2000));
  415. }
  416. [Test] // OpenWrite (string)
  417. public void OpenWrite1_Address_Null ()
  418. {
  419. WebClient wc = new WebClient ();
  420. try {
  421. wc.OpenWrite ((string) null);
  422. Assert.Fail ("#1");
  423. } catch (ArgumentNullException ex) {
  424. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  425. Assert.IsNull (ex.InnerException, "#3");
  426. Assert.IsNotNull (ex.Message, "#4");
  427. Assert.IsNotNull (ex.ParamName, "#5");
  428. Assert.AreEqual ("address", ex.ParamName, "#6");
  429. }
  430. }
  431. [Test] // OpenWrite (string)
  432. public void OpenWrite1_Address_SchemeNotSupported ()
  433. {
  434. WebClient wc = new WebClient ();
  435. try {
  436. wc.OpenWrite ("tp://scheme.notsupported");
  437. Assert.Fail ("#1");
  438. } catch (WebException ex) {
  439. // An error occurred performing a WebClient request
  440. Assert.AreEqual (typeof (WebException), ex.GetType (), "#2");
  441. Assert.IsNotNull (ex.InnerException, "#3");
  442. Assert.IsNotNull (ex.Message, "#4");
  443. Assert.IsNull (ex.Response, "#5");
  444. Assert.AreEqual (WebExceptionStatus.UnknownError, ex.Status, "#6");
  445. // The URI prefix is not recognized
  446. Exception inner = ex.InnerException;
  447. Assert.AreEqual (typeof (NotSupportedException), inner.GetType (), "#7");
  448. Assert.IsNull (inner.InnerException, "#8");
  449. Assert.IsNotNull (inner.Message, "#9");
  450. }
  451. }
  452. [Test] // OpenWrite (string, string)
  453. public void OpenWrite2_Address_Null ()
  454. {
  455. WebClient wc = new WebClient ();
  456. try {
  457. wc.OpenWrite ((string) null, "PUT");
  458. Assert.Fail ("#1");
  459. } catch (ArgumentNullException ex) {
  460. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  461. Assert.IsNull (ex.InnerException, "#3");
  462. Assert.IsNotNull (ex.Message, "#4");
  463. Assert.IsNotNull (ex.ParamName, "#5");
  464. Assert.AreEqual ("address", ex.ParamName, "#6");
  465. }
  466. }
  467. [Test] // OpenWrite (string, string)
  468. public void OpenWrite2_Address_SchemeNotSupported ()
  469. {
  470. WebClient wc = new WebClient ();
  471. try {
  472. wc.OpenWrite ("tp://scheme.notsupported", "PUT");
  473. Assert.Fail ("#1");
  474. } catch (WebException ex) {
  475. // An error occurred performing a WebClient request
  476. Assert.AreEqual (typeof (WebException), ex.GetType (), "#2");
  477. Assert.IsNotNull (ex.InnerException, "#3");
  478. Assert.IsNotNull (ex.Message, "#4");
  479. Assert.IsNull (ex.Response, "#5");
  480. Assert.AreEqual (WebExceptionStatus.UnknownError, ex.Status, "#6");
  481. // The URI prefix is not recognized
  482. Exception inner = ex.InnerException;
  483. Assert.AreEqual (typeof (NotSupportedException), inner.GetType (), "#7");
  484. Assert.IsNull (inner.InnerException, "#8");
  485. Assert.IsNotNull (inner.Message, "#9");
  486. }
  487. }
  488. [Test] // OpenWrite (Uri)
  489. public void OpenWrite3_Address_Null ()
  490. {
  491. WebClient wc = new WebClient ();
  492. try {
  493. wc.OpenWrite ((Uri) null);
  494. Assert.Fail ("#1");
  495. } catch (ArgumentNullException ex) {
  496. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  497. Assert.IsNull (ex.InnerException, "#3");
  498. Assert.IsNotNull (ex.Message, "#4");
  499. Assert.IsNotNull (ex.ParamName, "#5");
  500. Assert.AreEqual ("address", ex.ParamName, "#6");
  501. }
  502. }
  503. [Test] // OpenWrite (Uri)
  504. public void OpenWrite3_Address_SchemeNotSupported ()
  505. {
  506. WebClient wc = new WebClient ();
  507. try {
  508. wc.OpenWrite (new Uri ("tp://scheme.notsupported"));
  509. Assert.Fail ("#1");
  510. } catch (WebException ex) {
  511. // An error occurred performing a WebClient request
  512. Assert.AreEqual (typeof (WebException), ex.GetType (), "#2");
  513. Assert.IsNotNull (ex.InnerException, "#3");
  514. Assert.IsNotNull (ex.Message, "#4");
  515. Assert.IsNull (ex.Response, "#5");
  516. Assert.AreEqual (WebExceptionStatus.UnknownError, ex.Status, "#6");
  517. // The URI prefix is not recognized
  518. Exception inner = ex.InnerException;
  519. Assert.AreEqual (typeof (NotSupportedException), inner.GetType (), "#7");
  520. Assert.IsNull (inner.InnerException, "#8");
  521. Assert.IsNotNull (inner.Message, "#9");
  522. }
  523. }
  524. [Test] // OpenWrite (Uri, string)
  525. public void OpenWrite4_Address_Null ()
  526. {
  527. WebClient wc = new WebClient ();
  528. try {
  529. wc.OpenWrite ((Uri) null, "POST");
  530. Assert.Fail ("#1");
  531. } catch (ArgumentNullException ex) {
  532. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  533. Assert.IsNull (ex.InnerException, "#3");
  534. Assert.IsNotNull (ex.Message, "#4");
  535. Assert.IsNotNull (ex.ParamName, "#5");
  536. Assert.AreEqual ("address", ex.ParamName, "#6");
  537. }
  538. }
  539. [Test] // OpenWrite (Uri, string)
  540. public void OpenWrite4_Address_SchemeNotSupported ()
  541. {
  542. WebClient wc = new WebClient ();
  543. try {
  544. wc.OpenWrite (new Uri ("tp://scheme.notsupported"),
  545. "POST");
  546. Assert.Fail ("#1");
  547. } catch (WebException ex) {
  548. // An error occurred performing a WebClient request
  549. Assert.AreEqual (typeof (WebException), ex.GetType (), "#2");
  550. Assert.IsNotNull (ex.InnerException, "#3");
  551. Assert.IsNotNull (ex.Message, "#4");
  552. Assert.IsNull (ex.Response, "#5");
  553. Assert.AreEqual (WebExceptionStatus.UnknownError, ex.Status, "#6");
  554. // The URI prefix is not recognized
  555. Exception inner = ex.InnerException;
  556. Assert.AreEqual (typeof (NotSupportedException), inner.GetType (), "#7");
  557. Assert.IsNull (inner.InnerException, "#8");
  558. Assert.IsNotNull (inner.Message, "#9");
  559. }
  560. }
  561. [Test] // UploadData (string, byte [])
  562. public void UploadData1_Address_Null ()
  563. {
  564. WebClient wc = new WebClient ();
  565. try {
  566. wc.UploadData ((string) null, new byte [] { 0x1a });
  567. Assert.Fail ("#1");
  568. } catch (ArgumentNullException ex) {
  569. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  570. Assert.IsNull (ex.InnerException, "#3");
  571. Assert.IsNotNull (ex.Message, "#4");
  572. Assert.IsNotNull (ex.ParamName, "#5");
  573. Assert.AreEqual ("address", ex.ParamName, "#6");
  574. }
  575. }
  576. [Test] // UploadData (string, byte [])
  577. public void UploadData1_Address_SchemeNotSupported ()
  578. {
  579. WebClient wc = new WebClient ();
  580. try {
  581. wc.UploadData ("tp://scheme.notsupported", new byte [] { 0x1a });
  582. Assert.Fail ("#1");
  583. } catch (WebException ex) {
  584. // An error occurred performing a WebClient request
  585. Assert.AreEqual (typeof (WebException), ex.GetType (), "#2");
  586. Assert.IsNotNull (ex.InnerException, "#3");
  587. Assert.IsNotNull (ex.Message, "#4");
  588. Assert.IsNull (ex.Response, "#5");
  589. Assert.AreEqual (WebExceptionStatus.UnknownError, ex.Status, "#6");
  590. // The URI prefix is not recognized
  591. Exception inner = ex.InnerException;
  592. Assert.AreEqual (typeof (NotSupportedException), inner.GetType (), "#7");
  593. Assert.IsNull (inner.InnerException, "#8");
  594. Assert.IsNotNull (inner.Message, "#9");
  595. }
  596. }
  597. [Test] // UploadData (string, byte [])
  598. public void UploadData1_Data_Null ()
  599. {
  600. WebClient wc = new WebClient ();
  601. try {
  602. wc.UploadData ("http://www.mono-project.com",
  603. (byte []) null);
  604. Assert.Fail ("#1");
  605. } catch (ArgumentNullException ex) {
  606. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  607. Assert.IsNull (ex.InnerException, "#3");
  608. Assert.IsNotNull (ex.Message, "#4");
  609. Assert.IsNotNull (ex.ParamName, "#5");
  610. Assert.AreEqual ("data", ex.ParamName, "#6");
  611. }
  612. }
  613. [Test] // UploadData (Uri, byte [])
  614. public void UploadData2_Address_Null ()
  615. {
  616. WebClient wc = new WebClient ();
  617. try {
  618. wc.UploadData ((Uri) null, new byte [] { 0x1a });
  619. Assert.Fail ("#1");
  620. } catch (ArgumentNullException ex) {
  621. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  622. Assert.IsNull (ex.InnerException, "#3");
  623. Assert.IsNotNull (ex.Message, "#4");
  624. Assert.IsNotNull (ex.ParamName, "#5");
  625. Assert.AreEqual ("address", ex.ParamName, "#6");
  626. }
  627. }
  628. [Test] // UploadData (Uri, byte [])
  629. public void UploadData2_Address_SchemeNotSupported ()
  630. {
  631. WebClient wc = new WebClient ();
  632. try {
  633. wc.UploadData (new Uri ("tp://scheme.notsupported"),
  634. new byte [] { 0x1a });
  635. Assert.Fail ("#1");
  636. } catch (WebException ex) {
  637. // An error occurred performing a WebClient request
  638. Assert.AreEqual (typeof (WebException), ex.GetType (), "#2");
  639. Assert.IsNotNull (ex.InnerException, "#3");
  640. Assert.IsNotNull (ex.Message, "#4");
  641. Assert.IsNull (ex.Response, "#5");
  642. Assert.AreEqual (WebExceptionStatus.UnknownError, ex.Status, "#6");
  643. // The URI prefix is not recognized
  644. Exception inner = ex.InnerException;
  645. Assert.AreEqual (typeof (NotSupportedException), inner.GetType (), "#7");
  646. Assert.IsNull (inner.InnerException, "#8");
  647. Assert.IsNotNull (inner.Message, "#9");
  648. }
  649. }
  650. [Test] // UploadData (Uri, byte [])
  651. public void UploadData2_Data_Null ()
  652. {
  653. WebClient wc = new WebClient ();
  654. try {
  655. wc.UploadData (new Uri ("http://www.mono-project.com"),
  656. (byte []) null);
  657. Assert.Fail ("#1");
  658. } catch (ArgumentNullException ex) {
  659. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  660. Assert.IsNull (ex.InnerException, "#3");
  661. Assert.IsNotNull (ex.Message, "#4");
  662. Assert.IsNotNull (ex.ParamName, "#5");
  663. Assert.AreEqual ("data", ex.ParamName, "#6");
  664. }
  665. }
  666. [Test] // UploadData (string, string, byte [])
  667. public void UploadData3_Address_Null ()
  668. {
  669. WebClient wc = new WebClient ();
  670. try {
  671. wc.UploadData ((string) null, "POST",
  672. new byte [] { 0x1a });
  673. Assert.Fail ("#1");
  674. } catch (ArgumentNullException ex) {
  675. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  676. Assert.IsNull (ex.InnerException, "#3");
  677. Assert.IsNotNull (ex.Message, "#4");
  678. Assert.IsNotNull (ex.ParamName, "#5");
  679. Assert.AreEqual ("address", ex.ParamName, "#6");
  680. }
  681. }
  682. [Test] // UploadData (string, string, byte [])
  683. public void UploadData3_Address_SchemeNotSupported ()
  684. {
  685. WebClient wc = new WebClient ();
  686. try {
  687. wc.UploadData ("tp://scheme.notsupported",
  688. "POST", new byte [] { 0x1a });
  689. Assert.Fail ("#1");
  690. } catch (WebException ex) {
  691. // An error occurred performing a WebClient request
  692. Assert.AreEqual (typeof (WebException), ex.GetType (), "#2");
  693. Assert.IsNotNull (ex.InnerException, "#3");
  694. Assert.IsNotNull (ex.Message, "#4");
  695. Assert.IsNull (ex.Response, "#5");
  696. Assert.AreEqual (WebExceptionStatus.UnknownError, ex.Status, "#6");
  697. // The URI prefix is not recognized
  698. Exception inner = ex.InnerException;
  699. Assert.AreEqual (typeof (NotSupportedException), inner.GetType (), "#7");
  700. Assert.IsNull (inner.InnerException, "#8");
  701. Assert.IsNotNull (inner.Message, "#9");
  702. }
  703. }
  704. [Test] // UploadData (string, string, byte [])
  705. public void UploadData3_Data_Null ()
  706. {
  707. WebClient wc = new WebClient ();
  708. try {
  709. wc.UploadData ("http://www.mono-project.com",
  710. "POST", (byte []) null);
  711. Assert.Fail ("#1");
  712. } catch (ArgumentNullException ex) {
  713. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  714. Assert.IsNull (ex.InnerException, "#3");
  715. Assert.IsNotNull (ex.Message, "#4");
  716. Assert.IsNotNull (ex.ParamName, "#5");
  717. Assert.AreEqual ("data", ex.ParamName, "#6");
  718. }
  719. }
  720. [Test] // UploadData (Uri, string, byte [])
  721. public void UploadData4_Address_Null ()
  722. {
  723. WebClient wc = new WebClient ();
  724. try {
  725. wc.UploadData ((Uri) null, "POST", new byte [] { 0x1a });
  726. Assert.Fail ("#1");
  727. } catch (ArgumentNullException ex) {
  728. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  729. Assert.IsNull (ex.InnerException, "#3");
  730. Assert.IsNotNull (ex.Message, "#4");
  731. Assert.IsNotNull (ex.ParamName, "#5");
  732. Assert.AreEqual ("address", ex.ParamName, "#6");
  733. }
  734. }
  735. [Test] // UploadData (Uri, string, byte [])
  736. public void UploadData4_Address_SchemeNotSupported ()
  737. {
  738. WebClient wc = new WebClient ();
  739. try {
  740. wc.UploadData (new Uri ("tp://scheme.notsupported"),
  741. "POST", new byte [] { 0x1a });
  742. Assert.Fail ("#1");
  743. } catch (WebException ex) {
  744. // An error occurred performing a WebClient request
  745. Assert.AreEqual (typeof (WebException), ex.GetType (), "#2");
  746. Assert.IsNotNull (ex.InnerException, "#3");
  747. Assert.IsNotNull (ex.Message, "#4");
  748. Assert.IsNull (ex.Response, "#5");
  749. Assert.AreEqual (WebExceptionStatus.UnknownError, ex.Status, "#6");
  750. // The URI prefix is not recognized
  751. Exception inner = ex.InnerException;
  752. Assert.AreEqual (typeof (NotSupportedException), inner.GetType (), "#7");
  753. Assert.IsNull (inner.InnerException, "#8");
  754. Assert.IsNotNull (inner.Message, "#9");
  755. }
  756. }
  757. [Test] // UploadData (Uri, string, byte [])
  758. public void UploadData4_Data_Null ()
  759. {
  760. WebClient wc = new WebClient ();
  761. try {
  762. wc.UploadData (new Uri ("http://www.mono-project.com"),
  763. "POST", (byte []) null);
  764. Assert.Fail ("#1");
  765. } catch (ArgumentNullException ex) {
  766. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  767. Assert.IsNull (ex.InnerException, "#3");
  768. Assert.IsNotNull (ex.Message, "#4");
  769. Assert.IsNotNull (ex.ParamName, "#5");
  770. Assert.AreEqual ("data", ex.ParamName, "#6");
  771. }
  772. }
  773. [Test] // UploadFile (string, string)
  774. public void UploadFile1_Address_Null ()
  775. {
  776. string tempFile = Path.Combine (_tempFolder, "upload.tmp");
  777. File.Create (tempFile).Close ();
  778. WebClient wc = new WebClient ();
  779. try {
  780. wc.UploadFile ((string) null, tempFile);
  781. Assert.Fail ("#1");
  782. } catch (ArgumentNullException ex) {
  783. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  784. Assert.IsNull (ex.InnerException, "#3");
  785. Assert.IsNotNull (ex.Message, "#4");
  786. Assert.IsNotNull (ex.ParamName, "#5");
  787. Assert.AreEqual ("address", ex.ParamName, "#6");
  788. }
  789. }
  790. [Test] // UploadFile (string, string)
  791. public void UploadFile1_Address_SchemeNotSupported ()
  792. {
  793. string tempFile = Path.Combine (_tempFolder, "upload.tmp");
  794. File.Create (tempFile).Close ();
  795. WebClient wc = new WebClient ();
  796. try {
  797. wc.UploadFile ("tp://scheme.notsupported",
  798. tempFile);
  799. Assert.Fail ("#1");
  800. } catch (WebException ex) {
  801. // An error occurred performing a WebClient request
  802. Assert.AreEqual (typeof (WebException), ex.GetType (), "#2");
  803. Assert.IsNotNull (ex.InnerException, "#3");
  804. Assert.IsNotNull (ex.Message, "#4");
  805. Assert.IsNull (ex.Response, "#5");
  806. Assert.AreEqual (WebExceptionStatus.UnknownError, ex.Status, "#6");
  807. // The URI prefix is not recognized
  808. Exception inner = ex.InnerException;
  809. Assert.AreEqual (typeof (NotSupportedException), inner.GetType (), "#7");
  810. Assert.IsNull (inner.InnerException, "#8");
  811. Assert.IsNotNull (inner.Message, "#9");
  812. }
  813. }
  814. [Test] // UploadFile (string, string)
  815. public void UploadFile1_FileName_NotFound ()
  816. {
  817. string tempFile = Path.Combine (_tempFolder, "upload.tmp");
  818. WebClient wc = new WebClient ();
  819. try {
  820. wc.UploadFile ("tp://scheme.notsupported",
  821. tempFile);
  822. Assert.Fail ("#1");
  823. } catch (WebException ex) {
  824. // An error occurred performing a WebClient request
  825. Assert.AreEqual (typeof (WebException), ex.GetType (), "#2");
  826. Assert.IsNotNull (ex.Message, "#3");
  827. Assert.IsNull (ex.Response, "#4");
  828. Assert.AreEqual (WebExceptionStatus.UnknownError, ex.Status, "#5");
  829. // Could not find file "..."
  830. FileNotFoundException inner = ex.InnerException
  831. as FileNotFoundException;
  832. Assert.IsNotNull (inner, "#6");
  833. Assert.AreEqual (typeof (FileNotFoundException), inner.GetType (), "#7");
  834. Assert.IsNotNull (inner.FileName, "#8");
  835. Assert.AreEqual (tempFile, inner.FileName, "#9");
  836. Assert.IsNull (inner.InnerException, "#10");
  837. Assert.IsNotNull (inner.Message, "#11");
  838. }
  839. }
  840. [Test] // UploadFile (string, string)
  841. public void UploadFile1_FileName_Null ()
  842. {
  843. WebClient wc = new WebClient ();
  844. try {
  845. wc.UploadFile ("tp://scheme.notsupported",
  846. (string) null);
  847. Assert.Fail ("#1");
  848. } catch (ArgumentNullException ex) {
  849. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  850. Assert.IsNull (ex.InnerException, "#3");
  851. Assert.IsNotNull (ex.Message, "#4");
  852. Assert.IsNotNull (ex.ParamName, "#5");
  853. Assert.AreEqual ("fileName", ex.ParamName, "#6");
  854. }
  855. }
  856. [Test] // UploadFile (Uri, string)
  857. public void UploadFile2_Address_Null ()
  858. {
  859. string tempFile = Path.Combine (_tempFolder, "upload.tmp");
  860. WebClient wc = new WebClient ();
  861. try {
  862. wc.UploadFile ((Uri) null, tempFile);
  863. Assert.Fail ("#1");
  864. } catch (ArgumentNullException ex) {
  865. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  866. Assert.IsNull (ex.InnerException, "#3");
  867. Assert.IsNotNull (ex.Message, "#4");
  868. Assert.IsNotNull (ex.ParamName, "#5");
  869. Assert.AreEqual ("address", ex.ParamName, "#6");
  870. }
  871. }
  872. [Test] // UploadFile (Uri, string)
  873. public void UploadFile2_Address_SchemeNotSupported ()
  874. {
  875. string tempFile = Path.Combine (_tempFolder, "upload.tmp");
  876. File.Create (tempFile).Close ();
  877. WebClient wc = new WebClient ();
  878. try {
  879. wc.UploadFile (new Uri ("tp://scheme.notsupported"),
  880. tempFile);
  881. Assert.Fail ("#1");
  882. } catch (WebException ex) {
  883. // An error occurred performing a WebClient request
  884. Assert.AreEqual (typeof (WebException), ex.GetType (), "#2");
  885. Assert.IsNotNull (ex.InnerException, "#3");
  886. Assert.IsNotNull (ex.Message, "#4");
  887. Assert.IsNull (ex.Response, "#5");
  888. Assert.AreEqual (WebExceptionStatus.UnknownError, ex.Status, "#6");
  889. // The URI prefix is not recognized
  890. Exception inner = ex.InnerException;
  891. Assert.AreEqual (typeof (NotSupportedException), inner.GetType (), "#7");
  892. Assert.IsNull (inner.InnerException, "#8");
  893. Assert.IsNotNull (inner.Message, "#9");
  894. }
  895. }
  896. [Test] // UploadFile (Uri, string)
  897. public void UploadFile2_FileName_NotFound ()
  898. {
  899. string tempFile = Path.Combine (_tempFolder, "upload.tmp");
  900. WebClient wc = new WebClient ();
  901. try {
  902. wc.UploadFile (new Uri ("tp://scheme.notsupported"),
  903. tempFile);
  904. Assert.Fail ("#1");
  905. } catch (WebException ex) {
  906. // An error occurred performing a WebClient request
  907. Assert.AreEqual (typeof (WebException), ex.GetType (), "#2");
  908. Assert.IsNotNull (ex.Message, "#3");
  909. Assert.IsNull (ex.Response, "#4");
  910. Assert.AreEqual (WebExceptionStatus.UnknownError, ex.Status, "#5");
  911. // Could not find file "..."
  912. FileNotFoundException inner = ex.InnerException
  913. as FileNotFoundException;
  914. Assert.IsNotNull (inner, "#6");
  915. Assert.AreEqual (typeof (FileNotFoundException), inner.GetType (), "#7");
  916. Assert.IsNotNull (inner.FileName, "#8");
  917. Assert.AreEqual (tempFile, inner.FileName, "#9");
  918. Assert.IsNull (inner.InnerException, "#10");
  919. Assert.IsNotNull (inner.Message, "#11");
  920. }
  921. }
  922. [Test] // UploadFile (Uri, string)
  923. public void UploadFile2_FileName_Null ()
  924. {
  925. WebClient wc = new WebClient ();
  926. try {
  927. wc.UploadFile (new Uri ("tp://scheme.notsupported"),
  928. null);
  929. Assert.Fail ("#1");
  930. } catch (ArgumentNullException ex) {
  931. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  932. Assert.IsNull (ex.InnerException, "#3");
  933. Assert.IsNotNull (ex.Message, "#4");
  934. Assert.IsNotNull (ex.ParamName, "#5");
  935. Assert.AreEqual ("fileName", ex.ParamName, "#6");
  936. }
  937. }
  938. [Test] // UploadFile (string, string, string)
  939. public void UploadFile3_Address_Null ()
  940. {
  941. string tempFile = Path.Combine (_tempFolder, "upload.tmp");
  942. File.Create (tempFile).Close ();
  943. WebClient wc = new WebClient ();
  944. try {
  945. wc.UploadFile ((string) null, "POST", tempFile);
  946. Assert.Fail ("#1");
  947. } catch (ArgumentNullException ex) {
  948. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  949. Assert.IsNull (ex.InnerException, "#3");
  950. Assert.IsNotNull (ex.Message, "#4");
  951. Assert.IsNotNull (ex.ParamName, "#5");
  952. Assert.AreEqual ("path", ex.ParamName, "#6");
  953. }
  954. }
  955. [Test] // UploadFile (string, string, string)
  956. public void UploadFile3_Address_SchemeNotSupported ()
  957. {
  958. string tempFile = Path.Combine (_tempFolder, "upload.tmp");
  959. File.Create (tempFile).Close ();
  960. WebClient wc = new WebClient ();
  961. try {
  962. wc.UploadFile ("tp://scheme.notsupported",
  963. "POST", tempFile);
  964. Assert.Fail ("#1");
  965. } catch (WebException ex) {
  966. // An error occurred performing a WebClient request
  967. Assert.AreEqual (typeof (WebException), ex.GetType (), "#2");
  968. Assert.IsNotNull (ex.InnerException, "#3");
  969. Assert.IsNotNull (ex.Message, "#4");
  970. Assert.IsNull (ex.Response, "#5");
  971. Assert.AreEqual (WebExceptionStatus.UnknownError, ex.Status, "#6");
  972. // The URI prefix is not recognized
  973. Exception inner = ex.InnerException;
  974. Assert.AreEqual (typeof (NotSupportedException), inner.GetType (), "#7");
  975. Assert.IsNull (inner.InnerException, "#8");
  976. Assert.IsNotNull (inner.Message, "#9");
  977. }
  978. }
  979. [Test] // UploadFile (string, string, string)
  980. public void UploadFile3_FileName_NotFound ()
  981. {
  982. string tempFile = Path.Combine (_tempFolder, "upload.tmp");
  983. WebClient wc = new WebClient ();
  984. try {
  985. wc.UploadFile ("tp://scheme.notsupported",
  986. "POST", tempFile);
  987. Assert.Fail ("#1");
  988. } catch (WebException ex) {
  989. // An error occurred performing a WebClient request
  990. Assert.AreEqual (typeof (WebException), ex.GetType (), "#2");
  991. Assert.IsNotNull (ex.Message, "#3");
  992. Assert.IsNull (ex.Response, "#4");
  993. Assert.AreEqual (WebExceptionStatus.UnknownError, ex.Status, "#5");
  994. // Could not find file "..."
  995. FileNotFoundException inner = ex.InnerException
  996. as FileNotFoundException;
  997. Assert.IsNotNull (inner, "#6");
  998. Assert.AreEqual (typeof (FileNotFoundException), inner.GetType (), "#7");
  999. Assert.IsNotNull (inner.FileName, "#8");
  1000. Assert.AreEqual (tempFile, inner.FileName, "#9");
  1001. Assert.IsNull (inner.InnerException, "#10");
  1002. Assert.IsNotNull (inner.Message, "#11");
  1003. }
  1004. }
  1005. [Test] // UploadFile (string, string, string)
  1006. public void UploadFile3_FileName_Null ()
  1007. {
  1008. WebClient wc = new WebClient ();
  1009. try {
  1010. wc.UploadFile ("tp://scheme.notsupported",
  1011. "POST", (string) null);
  1012. Assert.Fail ("#1");
  1013. } catch (ArgumentNullException ex) {
  1014. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  1015. Assert.IsNull (ex.InnerException, "#3");
  1016. Assert.IsNotNull (ex.Message, "#4");
  1017. Assert.IsNotNull (ex.ParamName, "#5");
  1018. Assert.AreEqual ("fileName", ex.ParamName, "#6");
  1019. }
  1020. }
  1021. [Test] // UploadFile (Uri, string, string)
  1022. public void UploadFile4_Address_Null ()
  1023. {
  1024. string tempFile = Path.Combine (_tempFolder, "upload.tmp");
  1025. WebClient wc = new WebClient ();
  1026. try {
  1027. wc.UploadFile ((Uri) null, "POST", tempFile);
  1028. Assert.Fail ("#1");
  1029. } catch (ArgumentNullException ex) {
  1030. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  1031. Assert.IsNull (ex.InnerException, "#3");
  1032. Assert.IsNotNull (ex.Message, "#4");
  1033. Assert.IsNotNull (ex.ParamName, "#5");
  1034. Assert.AreEqual ("address", ex.ParamName, "#6");
  1035. }
  1036. }
  1037. [Test] // UploadFile (Uri, string, string)
  1038. public void UploadFile4_Address_SchemeNotSupported ()
  1039. {
  1040. string tempFile = Path.Combine (_tempFolder, "upload.tmp");
  1041. File.Create (tempFile).Close ();
  1042. WebClient wc = new WebClient ();
  1043. try {
  1044. wc.UploadFile (new Uri ("tp://scheme.notsupported"),
  1045. "POST", tempFile);
  1046. Assert.Fail ("#1");
  1047. } catch (WebException ex) {
  1048. // An error occurred performing a WebClient request
  1049. Assert.AreEqual (typeof (WebException), ex.GetType (), "#2");
  1050. Assert.IsNotNull (ex.InnerException, "#3");
  1051. Assert.IsNotNull (ex.Message, "#4");
  1052. Assert.IsNull (ex.Response, "#5");
  1053. Assert.AreEqual (WebExceptionStatus.UnknownError, ex.Status, "#6");
  1054. // The URI prefix is not recognized
  1055. Exception inner = ex.InnerException;
  1056. Assert.AreEqual (typeof (NotSupportedException), inner.GetType (), "#7");
  1057. Assert.IsNull (inner.InnerException, "#8");
  1058. Assert.IsNotNull (inner.Message, "#9");
  1059. }
  1060. }
  1061. [Test] // UploadFile (Uri, string, string)
  1062. public void UploadFile4_FileName_NotFound ()
  1063. {
  1064. string tempFile = Path.Combine (_tempFolder, "upload.tmp");
  1065. WebClient wc = new WebClient ();
  1066. try {
  1067. wc.UploadFile (new Uri ("tp://scheme.notsupported"),
  1068. "POST", tempFile);
  1069. Assert.Fail ("#1");
  1070. } catch (WebException ex) {
  1071. // An error occurred performing a WebClient request
  1072. Assert.AreEqual (typeof (WebException), ex.GetType (), "#2");
  1073. Assert.IsNotNull (ex.Message, "#3");
  1074. Assert.IsNull (ex.Response, "#4");
  1075. Assert.AreEqual (WebExceptionStatus.UnknownError, ex.Status, "#5");
  1076. // Could not find file "..."
  1077. FileNotFoundException inner = ex.InnerException
  1078. as FileNotFoundException;
  1079. Assert.IsNotNull (inner, "#6");
  1080. Assert.AreEqual (typeof (FileNotFoundException), inner.GetType (), "#7");
  1081. Assert.IsNotNull (inner.FileName, "#8");
  1082. Assert.AreEqual (tempFile, inner.FileName, "#9");
  1083. Assert.IsNull (inner.InnerException, "#10");
  1084. Assert.IsNotNull (inner.Message, "#11");
  1085. }
  1086. }
  1087. [Test] // UploadFile (Uri, string, string)
  1088. public void UploadFile4_FileName_Null ()
  1089. {
  1090. WebClient wc = new WebClient ();
  1091. try {
  1092. wc.UploadFile (new Uri ("tp://scheme.notsupported"),
  1093. "POST", (string) null);
  1094. Assert.Fail ("#1");
  1095. } catch (ArgumentNullException ex) {
  1096. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  1097. Assert.IsNull (ex.InnerException, "#3");
  1098. Assert.IsNotNull (ex.Message, "#4");
  1099. Assert.IsNotNull (ex.ParamName, "#5");
  1100. Assert.AreEqual ("fileName", ex.ParamName, "#6");
  1101. }
  1102. }
  1103. [Test] // UploadString (string, string)
  1104. public void UploadString1_Address_Null ()
  1105. {
  1106. WebClient wc = new WebClient ();
  1107. try {
  1108. wc.UploadString ((string) null, (string) null);
  1109. Assert.Fail ("#1");
  1110. } catch (ArgumentNullException ex) {
  1111. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  1112. Assert.IsNull (ex.InnerException, "#3");
  1113. Assert.IsNotNull (ex.Message, "#4");
  1114. Assert.IsNotNull (ex.ParamName, "#5");
  1115. Assert.AreEqual ("address", ex.ParamName, "#6");
  1116. }
  1117. }
  1118. [Test] // UploadString (string, string)
  1119. public void UploadString1_Address_SchemeNotSupported ()
  1120. {
  1121. WebClient wc = new WebClient ();
  1122. try {
  1123. wc.UploadString ("tp://scheme.notsupported", "abc");
  1124. Assert.Fail ("#1");
  1125. } catch (WebException ex) {
  1126. // An error occurred performing a WebClient request
  1127. Assert.AreEqual (typeof (WebException), ex.GetType (), "#2");
  1128. Assert.IsNotNull (ex.InnerException, "#3");
  1129. Assert.IsNotNull (ex.Message, "#4");
  1130. Assert.IsNull (ex.Response, "#5");
  1131. Assert.AreEqual (WebExceptionStatus.UnknownError, ex.Status, "#6");
  1132. // The URI prefix is not recognized
  1133. Exception inner = ex.InnerException;
  1134. Assert.AreEqual (typeof (NotSupportedException), inner.GetType (), "#7");
  1135. Assert.IsNull (inner.InnerException, "#8");
  1136. Assert.IsNotNull (inner.Message, "#9");
  1137. }
  1138. }
  1139. [Test] // UploadString (string, string)
  1140. public void UploadString1_Data_Null ()
  1141. {
  1142. WebClient wc = new WebClient ();
  1143. try {
  1144. wc.UploadString ("tp://scheme.notsupported", (string) null);
  1145. Assert.Fail ("#1");
  1146. } catch (ArgumentNullException ex) {
  1147. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  1148. Assert.IsNull (ex.InnerException, "#3");
  1149. Assert.IsNotNull (ex.Message, "#4");
  1150. Assert.IsNotNull (ex.ParamName, "#5");
  1151. Assert.AreEqual ("data", ex.ParamName, "#6");
  1152. }
  1153. }
  1154. [Test] // UploadString (Uri, string)
  1155. public void UploadString2_Address_Null ()
  1156. {
  1157. WebClient wc = new WebClient ();
  1158. try {
  1159. wc.UploadString ((Uri) null, (string) null);
  1160. Assert.Fail ("#1");
  1161. } catch (ArgumentNullException ex) {
  1162. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  1163. Assert.IsNull (ex.InnerException, "#3");
  1164. Assert.IsNotNull (ex.Message, "#4");
  1165. Assert.IsNotNull (ex.ParamName, "#5");
  1166. Assert.AreEqual ("address", ex.ParamName, "#6");
  1167. }
  1168. }
  1169. [Test] // UploadString (Uri, string)
  1170. public void UploadString2_Address_SchemeNotSupported ()
  1171. {
  1172. WebClient wc = new WebClient ();
  1173. try {
  1174. wc.UploadString (new Uri ("tp://scheme.notsupported"), "abc");
  1175. Assert.Fail ("#1");
  1176. } catch (WebException ex) {
  1177. // An error occurred performing a WebClient request
  1178. Assert.AreEqual (typeof (WebException), ex.GetType (), "#2");
  1179. Assert.IsNotNull (ex.InnerException, "#3");
  1180. Assert.IsNotNull (ex.Message, "#4");
  1181. Assert.IsNull (ex.Response, "#5");
  1182. Assert.AreEqual (WebExceptionStatus.UnknownError, ex.Status, "#6");
  1183. // The URI prefix is not recognized
  1184. Exception inner = ex.InnerException;
  1185. Assert.AreEqual (typeof (NotSupportedException), inner.GetType (), "#7");
  1186. Assert.IsNull (inner.InnerException, "#8");
  1187. Assert.IsNotNull (inner.Message, "#9");
  1188. }
  1189. }
  1190. [Test] // UploadString (Uri, string)
  1191. public void UploadString2_Data_Null ()
  1192. {
  1193. WebClient wc = new WebClient ();
  1194. try {
  1195. wc.UploadString (new Uri ("tp://scheme.notsupported"),
  1196. (string) null);
  1197. Assert.Fail ("#1");
  1198. } catch (ArgumentNullException ex) {
  1199. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  1200. Assert.IsNull (ex.InnerException, "#3");
  1201. Assert.IsNotNull (ex.Message, "#4");
  1202. Assert.IsNotNull (ex.ParamName, "#5");
  1203. Assert.AreEqual ("data", ex.ParamName, "#6");
  1204. }
  1205. }
  1206. [Test] // UploadString (string, string, string)
  1207. public void UploadString3_Address_Null ()
  1208. {
  1209. WebClient wc = new WebClient ();
  1210. try {
  1211. wc.UploadString ((string) null, (string) null,
  1212. (string) null);
  1213. Assert.Fail ("#1");
  1214. } catch (ArgumentNullException ex) {
  1215. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  1216. Assert.IsNull (ex.InnerException, "#3");
  1217. Assert.IsNotNull (ex.Message, "#4");
  1218. Assert.IsNotNull (ex.ParamName, "#5");
  1219. Assert.AreEqual ("address", ex.ParamName, "#6");
  1220. }
  1221. }
  1222. [Test] // UploadString (string, string, string)
  1223. public void UploadString3_Address_SchemeNotSupported ()
  1224. {
  1225. WebClient wc = new WebClient ();
  1226. try {
  1227. wc.UploadString ("tp://scheme.notsupported",
  1228. "POST", "abc");
  1229. Assert.Fail ("#1");
  1230. } catch (WebException ex) {
  1231. // An error occurred performing a WebClient request
  1232. Assert.AreEqual (typeof (WebException), ex.GetType (), "#2");
  1233. Assert.IsNotNull (ex.InnerException, "#3");
  1234. Assert.IsNotNull (ex.Message, "#4");
  1235. Assert.IsNull (ex.Response, "#5");
  1236. Assert.AreEqual (WebExceptionStatus.UnknownError, ex.Status, "#6");
  1237. // The URI prefix is not recognized
  1238. Exception inner = ex.InnerException;
  1239. Assert.AreEqual (typeof (NotSupportedException), inner.GetType (), "#7");
  1240. Assert.IsNull (inner.InnerException, "#8");
  1241. Assert.IsNotNull (inner.Message, "#9");
  1242. }
  1243. }
  1244. [Test] // UploadString (string, string, string)
  1245. public void UploadString3_Data_Null ()
  1246. {
  1247. WebClient wc = new WebClient ();
  1248. try {
  1249. wc.UploadString ("tp://scheme.notsupported",
  1250. "POST", (string) null);
  1251. Assert.Fail ("#1");
  1252. } catch (ArgumentNullException ex) {
  1253. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  1254. Assert.IsNull (ex.InnerException, "#3");
  1255. Assert.IsNotNull (ex.Message, "#4");
  1256. Assert.IsNotNull (ex.ParamName, "#5");
  1257. Assert.AreEqual ("data", ex.ParamName, "#6");
  1258. }
  1259. }
  1260. [Test] // UploadString (Uri, string, string)
  1261. public void UploadString4_Address_Null ()
  1262. {
  1263. WebClient wc = new WebClient ();
  1264. try {
  1265. wc.UploadString ((Uri) null, (string) null,
  1266. (string) null);
  1267. Assert.Fail ("#1");
  1268. } catch (ArgumentNullException ex) {
  1269. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  1270. Assert.IsNull (ex.InnerException, "#3");
  1271. Assert.IsNotNull (ex.Message, "#4");
  1272. Assert.IsNotNull (ex.ParamName, "#5");
  1273. Assert.AreEqual ("address", ex.ParamName, "#6");
  1274. }
  1275. }
  1276. [Test] // UploadString (Uri, string, string)
  1277. public void UploadString4_Address_SchemeNotSupported ()
  1278. {
  1279. WebClient wc = new WebClient ();
  1280. try {
  1281. wc.UploadString (new Uri ("tp://scheme.notsupported"),
  1282. "POST", "abc");
  1283. Assert.Fail ("#1");
  1284. } catch (WebException ex) {
  1285. // An error occurred performing a WebClient request
  1286. Assert.AreEqual (typeof (WebException), ex.GetType (), "#2");
  1287. Assert.IsNotNull (ex.InnerException, "#3");
  1288. Assert.IsNotNull (ex.Message, "#4");
  1289. Assert.IsNull (ex.Response, "#5");
  1290. Assert.AreEqual (WebExceptionStatus.UnknownError, ex.Status, "#6");
  1291. // The URI prefix is not recognized
  1292. Exception inner = ex.InnerException;
  1293. Assert.AreEqual (typeof (NotSupportedException), inner.GetType (), "#7");
  1294. Assert.IsNull (inner.InnerException, "#8");
  1295. Assert.IsNotNull (inner.Message, "#9");
  1296. }
  1297. }
  1298. [Test] // UploadString (Uri, string, string)
  1299. public void UploadString4_Data_Null ()
  1300. {
  1301. WebClient wc = new WebClient ();
  1302. try {
  1303. wc.UploadString (new Uri ("tp://scheme.notsupported"),
  1304. "POST", (string) null);
  1305. Assert.Fail ("#1");
  1306. } catch (ArgumentNullException ex) {
  1307. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  1308. Assert.IsNull (ex.InnerException, "#3");
  1309. Assert.IsNotNull (ex.Message, "#4");
  1310. Assert.IsNotNull (ex.ParamName, "#5");
  1311. Assert.AreEqual ("data", ex.ParamName, "#6");
  1312. }
  1313. }
  1314. [Test]
  1315. [Category ("AndroidNotWorking")] // Fails when ran as part of the entire BCL test suite. Works when only this fixture is ran
  1316. #if FEATURE_NO_BSD_SOCKETS
  1317. [ExpectedException (typeof (PlatformNotSupportedException))]
  1318. #endif
  1319. public void UploadValues1 ()
  1320. {
  1321. IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint ();
  1322. string url = "http://" + ep.ToString () + "/test/";
  1323. using (SocketResponder responder = new SocketResponder (ep, s => EchoRequestHandler (s))) {
  1324. WebClient wc = new WebClient ();
  1325. wc.Encoding = Encoding.ASCII;
  1326. NameValueCollection nvc = new NameValueCollection ();
  1327. nvc.Add ("Name", "\u0041\u2262\u0391\u002E");
  1328. nvc.Add ("Address", "\u002E\u2262\u0041\u0391");
  1329. byte [] buffer = wc.UploadValues (url, nvc);
  1330. string response = Encoding.UTF8.GetString (buffer);
  1331. Assert.AreEqual ("Name=A%e2%89%a2%ce%91.&Address=.%e2%89%a2A%ce%91\r\n", response);
  1332. }
  1333. }
  1334. [Test] // UploadValues (string, NameValueCollection)
  1335. public void UploadValues1_Address_Null ()
  1336. {
  1337. WebClient wc = new WebClient ();
  1338. try {
  1339. wc.UploadValues ((string) null, new NameValueCollection ());
  1340. Assert.Fail ("#1");
  1341. } catch (ArgumentNullException ex) {
  1342. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  1343. Assert.IsNull (ex.InnerException, "#3");
  1344. Assert.IsNotNull (ex.Message, "#4");
  1345. Assert.IsNotNull (ex.ParamName, "#5");
  1346. Assert.AreEqual ("address", ex.ParamName, "#6");
  1347. }
  1348. }
  1349. [Test] // UploadValues (string, NameValueCollection)
  1350. public void UploadValues1_Address_SchemeNotSupported ()
  1351. {
  1352. WebClient wc = new WebClient ();
  1353. try {
  1354. wc.UploadValues ("tp://scheme.notsupported",
  1355. new NameValueCollection ());
  1356. Assert.Fail ("#1");
  1357. } catch (WebException ex) {
  1358. // An error occurred performing a WebClient request
  1359. Assert.AreEqual (typeof (WebException), ex.GetType (), "#2");
  1360. Assert.IsNotNull (ex.InnerException, "#3");
  1361. Assert.IsNotNull (ex.Message, "#4");
  1362. Assert.IsNull (ex.Response, "#5");
  1363. Assert.AreEqual (WebExceptionStatus.UnknownError, ex.Status, "#6");
  1364. // The URI prefix is not recognized
  1365. Exception inner = ex.InnerException;
  1366. Assert.AreEqual (typeof (NotSupportedException), inner.GetType (), "#7");
  1367. Assert.IsNull (inner.InnerException, "#8");
  1368. Assert.IsNotNull (inner.Message, "#9");
  1369. }
  1370. }
  1371. [Test] // UploadValues (string, NameValueCollection)
  1372. public void UploadValues1_Data_Null ()
  1373. {
  1374. WebClient wc = new WebClient ();
  1375. try {
  1376. wc.UploadValues ("http://www.mono-project.com",
  1377. (NameValueCollection) null);
  1378. Assert.Fail ("#1");
  1379. } catch (ArgumentNullException ex) {
  1380. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  1381. Assert.IsNull (ex.InnerException, "#3");
  1382. Assert.IsNotNull (ex.Message, "#4");
  1383. Assert.IsNotNull (ex.ParamName, "#5");
  1384. Assert.AreEqual ("data", ex.ParamName, "#6");
  1385. }
  1386. }
  1387. [Test] // UploadValues (Uri, NameValueCollection)
  1388. public void UploadValues2_Address_Null ()
  1389. {
  1390. WebClient wc = new WebClient ();
  1391. try {
  1392. wc.UploadValues ((Uri) null, new NameValueCollection ());
  1393. Assert.Fail ("#1");
  1394. } catch (ArgumentNullException ex) {
  1395. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  1396. Assert.IsNull (ex.InnerException, "#3");
  1397. Assert.IsNotNull (ex.Message, "#4");
  1398. Assert.IsNotNull (ex.ParamName, "#5");
  1399. Assert.AreEqual ("address", ex.ParamName, "#6");
  1400. }
  1401. }
  1402. [Test] // UploadValues (Uri, NameValueCollection)
  1403. public void UploadValues2_Address_SchemeNotSupported ()
  1404. {
  1405. WebClient wc = new WebClient ();
  1406. try {
  1407. wc.UploadValues (new Uri ("tp://scheme.notsupported"),
  1408. new NameValueCollection ());
  1409. Assert.Fail ("#1");
  1410. } catch (WebException ex) {
  1411. // An error occurred performing a WebClient request
  1412. Assert.AreEqual (typeof (WebException), ex.GetType (), "#2");
  1413. Assert.IsNotNull (ex.InnerException, "#3");
  1414. Assert.IsNotNull (ex.Message, "#4");
  1415. Assert.IsNull (ex.Response, "#5");
  1416. Assert.AreEqual (WebExceptionStatus.UnknownError, ex.Status, "#6");
  1417. // The URI prefix is not recognized
  1418. Exception inner = ex.InnerException;
  1419. Assert.AreEqual (typeof (NotSupportedException), inner.GetType (), "#7");
  1420. Assert.IsNull (inner.InnerException, "#8");
  1421. Assert.IsNotNull (inner.Message, "#9");
  1422. }
  1423. }
  1424. [Test] // UploadValues (Uri, NameValueCollection)
  1425. public void UploadValues2_Data_Null ()
  1426. {
  1427. WebClient wc = new WebClient ();
  1428. try {
  1429. wc.UploadValues (new Uri ("http://www.mono-project.com"),
  1430. (NameValueCollection) null);
  1431. Assert.Fail ("#1");
  1432. } catch (ArgumentNullException ex) {
  1433. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  1434. Assert.IsNull (ex.InnerException, "#3");
  1435. Assert.IsNotNull (ex.Message, "#4");
  1436. Assert.IsNotNull (ex.ParamName, "#5");
  1437. Assert.AreEqual ("data", ex.ParamName, "#6");
  1438. }
  1439. }
  1440. [Test] // UploadValues (string, string, NameValueCollection)
  1441. public void UploadValues3_Address_Null ()
  1442. {
  1443. WebClient wc = new WebClient ();
  1444. try {
  1445. wc.UploadValues ((string) null, "POST",
  1446. new NameValueCollection ());
  1447. Assert.Fail ("#1");
  1448. } catch (ArgumentNullException ex) {
  1449. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  1450. Assert.IsNull (ex.InnerException, "#3");
  1451. Assert.IsNotNull (ex.Message, "#4");
  1452. Assert.IsNotNull (ex.ParamName, "#5");
  1453. Assert.AreEqual ("address", ex.ParamName, "#6");
  1454. }
  1455. }
  1456. [Test] // UploadValues (string, string, NameValueCollection)
  1457. public void UploadValues3_Address_SchemeNotSupported ()
  1458. {
  1459. WebClient wc = new WebClient ();
  1460. try {
  1461. wc.UploadValues ("tp://scheme.notsupported",
  1462. "POST", new NameValueCollection ());
  1463. Assert.Fail ("#1");
  1464. } catch (WebException ex) {
  1465. // An error occurred performing a WebClient request
  1466. Assert.AreEqual (typeof (WebException), ex.GetType (), "#2");
  1467. Assert.IsNotNull (ex.InnerException, "#3");
  1468. Assert.IsNotNull (ex.Message, "#4");
  1469. Assert.IsNull (ex.Response, "#5");
  1470. Assert.AreEqual (WebExceptionStatus.UnknownError, ex.Status, "#6");
  1471. // The URI prefix is not recognized
  1472. Exception inner = ex.InnerException;
  1473. Assert.AreEqual (typeof (NotSupportedException), inner.GetType (), "#7");
  1474. Assert.IsNull (inner.InnerException, "#8");
  1475. Assert.IsNotNull (inner.Message, "#9");
  1476. }
  1477. }
  1478. [Test] // UploadValues (string, string, NameValueCollection)
  1479. public void UploadValues3_Data_Null ()
  1480. {
  1481. WebClient wc = new WebClient ();
  1482. try {
  1483. wc.UploadValues ("http://www.mono-project.com",
  1484. "POST", (NameValueCollection) null);
  1485. Assert.Fail ("#1");
  1486. } catch (ArgumentNullException ex) {
  1487. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  1488. Assert.IsNull (ex.InnerException, "#3");
  1489. Assert.IsNotNull (ex.Message, "#4");
  1490. Assert.IsNotNull (ex.ParamName, "#5");
  1491. Assert.AreEqual ("data", ex.ParamName, "#6");
  1492. }
  1493. }
  1494. [Test] // UploadValues (Uri, string, NameValueCollection)
  1495. public void UploadValues4_Address_Null ()
  1496. {
  1497. WebClient wc = new WebClient ();
  1498. try {
  1499. wc.UploadValues ((Uri) null, "POST",
  1500. new NameValueCollection ());
  1501. Assert.Fail ("#1");
  1502. } catch (ArgumentNullException ex) {
  1503. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  1504. Assert.IsNull (ex.InnerException, "#3");
  1505. Assert.IsNotNull (ex.Message, "#4");
  1506. Assert.IsNotNull (ex.ParamName, "#5");
  1507. Assert.AreEqual ("address", ex.ParamName, "#6");
  1508. }
  1509. }
  1510. [Test] // UploadValues (Uri, string, NameValueCollection)
  1511. public void UploadValues4_Address_SchemeNotSupported ()
  1512. {
  1513. WebClient wc = new WebClient ();
  1514. try {
  1515. wc.UploadValues (new Uri ("tp://scheme.notsupported"),
  1516. "POST", new NameValueCollection ());
  1517. Assert.Fail ("#1");
  1518. } catch (WebException ex) {
  1519. // An error occurred performing a WebClient request
  1520. Assert.AreEqual (typeof (WebException), ex.GetType (), "#2");
  1521. Assert.IsNotNull (ex.InnerException, "#3");
  1522. Assert.IsNotNull (ex.Message, "#4");
  1523. Assert.IsNull (ex.Response, "#5");
  1524. Assert.AreEqual (WebExceptionStatus.UnknownError, ex.Status, "#6");
  1525. // The URI prefix is not recognized
  1526. Exception inner = ex.InnerException;
  1527. Assert.AreEqual (typeof (NotSupportedException), inner.GetType (), "#7");
  1528. Assert.IsNull (inner.InnerException, "#8");
  1529. Assert.IsNotNull (inner.Message, "#9");
  1530. }
  1531. }
  1532. [Test] // UploadValues (Uri, string, NameValueCollection)
  1533. public void UploadValues4_Data_Null ()
  1534. {
  1535. WebClient wc = new WebClient ();
  1536. try {
  1537. wc.UploadValues (new Uri ("http://www.mono-project.com"),
  1538. "POST", (NameValueCollection) null);
  1539. Assert.Fail ("#1");
  1540. } catch (ArgumentNullException ex) {
  1541. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  1542. Assert.IsNull (ex.InnerException, "#3");
  1543. Assert.IsNotNull (ex.Message, "#4");
  1544. Assert.IsNotNull (ex.ParamName, "#5");
  1545. Assert.AreEqual ("data", ex.ParamName, "#6");
  1546. }
  1547. }
  1548. [Test]
  1549. #if FEATURE_NO_BSD_SOCKETS
  1550. // We throw a PlatformNotSupportedException deeper, which is caught and re-thrown as WebException
  1551. [ExpectedException (typeof (WebException))]
  1552. #endif
  1553. public void GetWebRequestOverriding ()
  1554. {
  1555. GetWebRequestOverridingTestClass testObject = new GetWebRequestOverridingTestClass ();
  1556. testObject.DownloadData ("http://www.mono-project.com");
  1557. Assert.IsTrue (testObject.overridedCodeRan, "Overrided code wasn't called");
  1558. }
  1559. class GetWebRequestOverridingTestClass : WebClient
  1560. {
  1561. internal bool overridedCodeRan = false;
  1562. protected override WebRequest GetWebRequest(Uri address)
  1563. {
  1564. overridedCodeRan = true;
  1565. return base.GetWebRequest (address);
  1566. }
  1567. }
  1568. static byte [] EchoRequestHandler (Socket socket)
  1569. {
  1570. MemoryStream ms = new MemoryStream ();
  1571. byte [] buffer = new byte [4096];
  1572. int bytesReceived = socket.Receive (buffer);
  1573. while (bytesReceived > 0) {
  1574. ms.Write (buffer, 0, bytesReceived);
  1575. // We don't check for Content-Length or anything else here, so we give the client a little time to write
  1576. // after sending the headers
  1577. Thread.Sleep (200);
  1578. if (socket.Available > 0) {
  1579. bytesReceived = socket.Receive (buffer);
  1580. } else {
  1581. bytesReceived = 0;
  1582. }
  1583. }
  1584. ms.Flush ();
  1585. ms.Position = 0;
  1586. StringBuilder sb = new StringBuilder ();
  1587. string expect = null;
  1588. StreamReader sr = new StreamReader (ms, Encoding.UTF8);
  1589. string line = null;
  1590. byte state = 0;
  1591. while ((line = sr.ReadLine ()) != null) {
  1592. if (state > 0) {
  1593. state = 2;
  1594. sb.Append (line);
  1595. sb.Append ("\r\n");
  1596. } if (line.Length == 0) {
  1597. state = 1;
  1598. } else if (line.StartsWith ("Expect:")) {
  1599. expect = line.Substring (8);
  1600. }
  1601. }
  1602. StringWriter sw = new StringWriter ();
  1603. if (expect == "100-continue" && state != 2) {
  1604. sw.WriteLine ("HTTP/1.1 100 Continue");
  1605. sw.WriteLine ();
  1606. sw.Flush ();
  1607. socket.Send (Encoding.UTF8.GetBytes (sw.ToString ()));
  1608. // receive body
  1609. ms = new MemoryStream ();
  1610. buffer = new byte [4096];
  1611. bytesReceived = socket.Receive (buffer);
  1612. while (bytesReceived > 0) {
  1613. ms.Write (buffer, 0, bytesReceived);
  1614. Thread.Sleep (200);
  1615. if (socket.Available > 0) {
  1616. bytesReceived = socket.Receive (buffer);
  1617. } else {
  1618. bytesReceived = 0;
  1619. }
  1620. }
  1621. ms.Flush ();
  1622. ms.Position = 0;
  1623. sb = new StringBuilder ();
  1624. sr = new StreamReader (ms, Encoding.UTF8);
  1625. line = sr.ReadLine ();
  1626. while (line != null) {
  1627. sb.Append (line);
  1628. sb.Append ("\r\n");
  1629. line = sr.ReadLine ();
  1630. }
  1631. }
  1632. sw = new StringWriter ();
  1633. sw.WriteLine ("HTTP/1.1 200 OK");
  1634. sw.WriteLine ("Content-Type: text/xml");
  1635. sw.WriteLine ("Content-Length: " + sb.Length.ToString (CultureInfo.InvariantCulture));
  1636. sw.WriteLine ();
  1637. sw.Write (sb.ToString ());
  1638. sw.Flush ();
  1639. return Encoding.UTF8.GetBytes (sw.ToString ());
  1640. }
  1641. [Test]
  1642. #if FEATURE_NO_BSD_SOCKETS
  1643. [ExpectedException (typeof (PlatformNotSupportedException))]
  1644. #endif
  1645. public void DefaultProxy ()
  1646. {
  1647. WebClient wc = new WebClient ();
  1648. // this is never null on .net
  1649. Assert.IsNotNull (wc.Proxy);
  1650. // and return the same instance as WebRequest.DefaultWebProxy
  1651. Assert.AreSame (wc.Proxy, WebRequest.DefaultWebProxy);
  1652. }
  1653. [Test]
  1654. [Category ("AndroidNotWorking")] // Fails when ran as part of the entire BCL test suite. Works when only this fixture is ran
  1655. #if FEATURE_NO_BSD_SOCKETS
  1656. [ExpectedException (typeof (PlatformNotSupportedException))]
  1657. #endif
  1658. public void UploadStringAsyncCancelEvent ()
  1659. {
  1660. UploadAsyncCancelEventTest (9301, (webClient, uri, cancelEvent) =>
  1661. {
  1662. webClient.UploadStringCompleted += (sender, args) =>
  1663. {
  1664. if (args.Cancelled)
  1665. cancelEvent.Set ();
  1666. };
  1667. webClient.UploadStringAsync (uri, "PUT", "text");
  1668. });
  1669. }
  1670. [Test]
  1671. [Category ("AndroidNotWorking")] // Fails when ran as part of the entire BCL test suite. Works when only this fixture is ran
  1672. #if FEATURE_NO_BSD_SOCKETS
  1673. [ExpectedException (typeof (PlatformNotSupportedException))]
  1674. #endif
  1675. public void UploadDataAsyncCancelEvent ()
  1676. {
  1677. UploadAsyncCancelEventTest (9302, (webClient, uri, cancelEvent) =>
  1678. {
  1679. webClient.UploadDataCompleted += (sender, args) =>
  1680. {
  1681. if (args.Cancelled)
  1682. cancelEvent.Set ();
  1683. };
  1684. webClient.UploadDataAsync (uri, "PUT", new byte[] { });
  1685. });
  1686. }
  1687. [Test]
  1688. [Category ("AndroidNotWorking")] // Fails when ran as part of the entire BCL test suite. Works when only this fixture is ran
  1689. #if FEATURE_NO_BSD_SOCKETS
  1690. [ExpectedException (typeof (PlatformNotSupportedException))]
  1691. #endif
  1692. public void UploadValuesAsyncCancelEvent ()
  1693. {
  1694. UploadAsyncCancelEventTest (9303, (webClient, uri, cancelEvent) =>
  1695. {
  1696. webClient.UploadValuesCompleted += (sender, args) =>
  1697. {
  1698. if (args.Cancelled)
  1699. cancelEvent.Set ();
  1700. };
  1701. webClient.UploadValuesAsync (uri, "PUT", new NameValueCollection ());
  1702. });
  1703. }
  1704. [Test]
  1705. [Category ("AndroidNotWorking")] // Fails when ran as part of the entire BCL test suite. Works when only this fixture is ran
  1706. #if FEATURE_NO_BSD_SOCKETS
  1707. [ExpectedException (typeof (PlatformNotSupportedException))]
  1708. #endif
  1709. public void UploadFileAsyncCancelEvent ()
  1710. {
  1711. UploadAsyncCancelEventTest (9304,(webClient, uri, cancelEvent) =>
  1712. {
  1713. string tempFile = Path.Combine (_tempFolder, "upload.tmp");
  1714. File.Create (tempFile).Close ();
  1715. webClient.UploadFileCompleted += (sender, args) =>
  1716. {
  1717. if (args.Cancelled)
  1718. cancelEvent.Set ();
  1719. };
  1720. webClient.UploadFileAsync (uri, "PUT", tempFile);
  1721. });
  1722. }
  1723. [Test]
  1724. [Category ("AndroidNotWorking")] // Test suite hangs if the tests runs as part of the entire BCL suite. Works when only this fixture is ran
  1725. #if FEATURE_NO_BSD_SOCKETS
  1726. [ExpectedException (typeof (PlatformNotSupportedException))]
  1727. #endif
  1728. public void UploadFileAsyncContentType ()
  1729. {
  1730. var port = NetworkHelpers.FindFreePort ();
  1731. var serverUri = "http://localhost:" + port + "/";
  1732. var filename = Path.GetTempFileName ();
  1733. HttpListener listener = new HttpListener ();
  1734. listener.Prefixes.Add (serverUri);
  1735. listener.Start ();
  1736. using (var client = new WebClient ())
  1737. {
  1738. client.UploadFileTaskAsync (new Uri (serverUri), filename);
  1739. var request = listener.GetContext ().Request;
  1740. var expected = "multipart/form-data; boundary=---------------------";
  1741. Assert.AreEqual (expected.Length + 15, request.ContentType.Length);
  1742. Assert.AreEqual (expected, request.ContentType.Substring (0, expected.Length));
  1743. }
  1744. listener.Close ();
  1745. }
  1746. public void UploadAsyncCancelEventTest (int port, Action<WebClient, Uri, EventWaitHandle> uploadAction)
  1747. {
  1748. var ep = NetworkHelpers.LocalEphemeralEndPoint ();
  1749. string url = "http://" + ep.ToString() + "/test/";
  1750. using (var responder = new SocketResponder (ep, s => EchoRequestHandler (s)))
  1751. {
  1752. var webClient = new WebClient ();
  1753. var cancellationTokenSource = new CancellationTokenSource ();
  1754. cancellationTokenSource.Token.Register (webClient.CancelAsync);
  1755. var cancelEvent = new ManualResetEvent (false);
  1756. uploadAction.Invoke (webClient, new Uri (url), cancelEvent);
  1757. cancellationTokenSource.Cancel ();
  1758. Assert.IsTrue (cancelEvent.WaitOne (1000));
  1759. }
  1760. }
  1761. }
  1762. }