WebClientTest.cs 62 KB

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