WebClientTest.cs 62 KB

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