FileWebRequestTest.cs 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899
  1. //
  2. // FileWebRequestTest.cs - NUnit Test Cases for System.Net.FileWebRequest
  3. //
  4. // Authors:
  5. // Lawrence Pit ([email protected])
  6. // Martin Willemoes Hansen ([email protected])
  7. // Gert Driesen ([email protected])
  8. //
  9. // (C) 2003 Martin Willemoes Hansen
  10. //
  11. using System;
  12. using System.Collections;
  13. using System.IO;
  14. using System.Net;
  15. using System.Runtime.Serialization;
  16. using System.Runtime.Serialization.Formatters;
  17. using System.Runtime.Serialization.Formatters.Binary;
  18. using System.Security;
  19. using System.Security.Permissions;
  20. using NUnit.Framework;
  21. namespace MonoTests.System.Net
  22. {
  23. [TestFixture]
  24. public class FileWebRequestTest
  25. {
  26. private string _tempDirectory;
  27. private string _tempFile;
  28. private Uri _tempFileUri;
  29. [SetUp]
  30. public void SetUp ()
  31. {
  32. _tempDirectory = Path.Combine (Path.GetTempPath (), "MonoTests.System.Net.FileWebRequestTest");
  33. _tempFile = Path.Combine (_tempDirectory, "FileWebRequestTest.tmp");
  34. if (!Directory.Exists (_tempDirectory)) {
  35. Directory.CreateDirectory (_tempDirectory);
  36. } else {
  37. // ensure no files are left over from previous runs
  38. string [] files = Directory.GetFiles (_tempDirectory, "*");
  39. foreach (string file in files)
  40. File.Delete (file);
  41. }
  42. _tempFileUri = GetTempFileUri ();
  43. }
  44. [TearDown]
  45. public void TearDown ()
  46. {
  47. if (Directory.Exists (_tempDirectory))
  48. Directory.Delete (_tempDirectory, true);
  49. }
  50. [Test]
  51. public void Async ()
  52. {
  53. WebResponse res = null;
  54. try {
  55. FileWebRequest req = (FileWebRequest) WebRequest.Create (_tempFileUri);
  56. req.Method = "PUT";
  57. req.ContentLength = 1;
  58. req.ContentType = "image/png";
  59. req.Timeout = 2 * 1000;
  60. IAsyncResult async = req.BeginGetRequestStream (null, null);
  61. try {
  62. req.BeginGetRequestStream (null, null);
  63. Assert.Fail ("#1 should've failed");
  64. } catch (InvalidOperationException) {
  65. // Cannot re-call BeginGetRequestStream/BeginGetResponse while
  66. // a previous call is still in progress
  67. }
  68. try {
  69. req.GetRequestStream ();
  70. Assert.Fail ("#3 should've failed");
  71. } catch (InvalidOperationException) {
  72. // Cannot re-call BeginGetRequestStream/BeginGetResponse while
  73. // a previous call is still in progress
  74. }
  75. using (Stream wstream = req.EndGetRequestStream (async)) {
  76. Assert.IsFalse (wstream.CanRead, "#1r");
  77. Assert.IsTrue (wstream.CanWrite, "#1w");
  78. Assert.IsTrue (wstream.CanSeek, "#1s");
  79. wstream.WriteByte (72);
  80. wstream.WriteByte (101);
  81. wstream.WriteByte (108);
  82. wstream.WriteByte (108);
  83. wstream.WriteByte (111);
  84. wstream.Close ();
  85. }
  86. Assert.AreEqual (1, req.ContentLength, "#1cl");
  87. Assert.AreEqual ("image/png", req.ContentType, "#1ct");
  88. // stream written
  89. req = (FileWebRequest) WebRequest.Create (_tempFileUri);
  90. res = req.GetResponse ();
  91. try {
  92. req.BeginGetRequestStream (null, null);
  93. Assert.Fail ("#20: should've failed");
  94. } catch (InvalidOperationException) {
  95. // Cannot send a content-body with this verb-type
  96. }
  97. try {
  98. req.Method = "PUT";
  99. req.BeginGetRequestStream (null, null);
  100. Assert.Fail ("#21: should've failed");
  101. } catch (InvalidOperationException) {
  102. // This operation cannot be perfomed after the request has been submitted.
  103. }
  104. req.GetResponse ();
  105. IAsyncResult async2 = req.BeginGetResponse (null, null);
  106. // this succeeds !!
  107. WebResponse res2 = req.EndGetResponse (async2);
  108. Assert.AreSame (res, res2, "#23");
  109. Assert.AreEqual (5, res.ContentLength, "#2 len");
  110. Assert.AreEqual ("application/octet-stream", res.ContentType, "#2 type");
  111. Assert.AreEqual ("file", res.ResponseUri.Scheme, "#2 scheme");
  112. Stream rstream = res.GetResponseStream ();
  113. Assert.IsTrue (rstream.CanRead, "#3r");
  114. Assert.IsFalse (rstream.CanWrite, "#3w");
  115. Assert.IsTrue (rstream.CanSeek, "#3s");
  116. Assert.AreEqual (72, rstream.ReadByte (), "#4a");
  117. Assert.AreEqual (101, rstream.ReadByte (), "#4b");
  118. Assert.AreEqual (108, rstream.ReadByte (), "#4c");
  119. Assert.AreEqual (108, rstream.ReadByte (), "#4d");
  120. Assert.AreEqual (111, rstream.ReadByte (), "#4e");
  121. rstream.Close ();
  122. try {
  123. long len = res.ContentLength;
  124. Assert.AreEqual ((long) 5, len, "#5");
  125. } catch (ObjectDisposedException) {
  126. Assert.Fail ("#disposed contentlength");
  127. }
  128. try {
  129. WebHeaderCollection w = res.Headers;
  130. } catch (ObjectDisposedException) {
  131. Assert.Fail ("#disposed headers");
  132. }
  133. try {
  134. res.Close ();
  135. } catch (ObjectDisposedException) {
  136. Assert.Fail ("#disposed close");
  137. }
  138. } finally {
  139. if (res != null)
  140. res.Close ();
  141. }
  142. }
  143. [Test]
  144. [Category ("NotWorking")] // bug #323388
  145. public void Async_GetResponse_Failure ()
  146. {
  147. FileWebRequest req = (FileWebRequest) WebRequest.Create (_tempFileUri);
  148. req.Method = "PUT";
  149. req.ContentLength = 1;
  150. req.ContentType = "image/png";
  151. req.Timeout = 500;
  152. IAsyncResult async = req.BeginGetRequestStream (null, null);
  153. try {
  154. req.GetResponse ();
  155. Assert.Fail ("#1");
  156. } catch (WebException) {
  157. // The operation has timed out
  158. }
  159. try {
  160. req.BeginGetResponse (null, null);
  161. Assert.Fail ("#2");
  162. } catch (InvalidOperationException) {
  163. // Cannot re-call BeginGetRequestStream/BeginGetResponse while
  164. // a previous call is still in progress
  165. }
  166. using (Stream wstream = req.EndGetRequestStream (async)) {
  167. wstream.WriteByte (72);
  168. }
  169. // the temp file should not be in use
  170. Directory.Delete (_tempDirectory, true);
  171. }
  172. [Test]
  173. public void Sync ()
  174. {
  175. WebResponse res = null;
  176. try {
  177. FileWebRequest req = (FileWebRequest) WebRequest.Create (_tempFileUri);
  178. req.ContentLength = 1;
  179. req.ContentType = "image/png";
  180. try {
  181. Stream stream = req.GetRequestStream ();
  182. Assert.Fail ("should throw exception");
  183. } catch (ProtocolViolationException) {
  184. }
  185. req.Method = "PUT";
  186. Stream wstream = req.GetRequestStream ();
  187. Assert.IsFalse (wstream.CanRead, "#1r");
  188. Assert.IsTrue (wstream.CanWrite, "#1w");
  189. Assert.IsTrue (wstream.CanSeek, "#1s");
  190. wstream.WriteByte (72);
  191. wstream.WriteByte (101);
  192. wstream.WriteByte (108);
  193. wstream.WriteByte (108);
  194. wstream.WriteByte (111);
  195. wstream.Close ();
  196. Assert.AreEqual (1, req.ContentLength, "#1cl");
  197. Assert.AreEqual ("image/png", req.ContentType, "#1ct");
  198. // stream written
  199. req = (FileWebRequest) WebRequest.Create (_tempFileUri);
  200. res = req.GetResponse ();
  201. Assert.AreEqual ((long) 5, res.ContentLength, "#2 len");
  202. Assert.AreEqual ("application/octet-stream", res.ContentType, "#2 type");
  203. Assert.AreEqual ("file", res.ResponseUri.Scheme, "#2 scheme");
  204. Stream rstream = res.GetResponseStream ();
  205. Assert.IsTrue (rstream.CanRead, "#3r");
  206. Assert.IsFalse (rstream.CanWrite, "#3w");
  207. Assert.IsTrue (rstream.CanSeek, "#3s");
  208. Assert.AreEqual (72, rstream.ReadByte (), "#4a");
  209. Assert.AreEqual (101, rstream.ReadByte (), "#4b");
  210. Assert.AreEqual (108, rstream.ReadByte (), "#4c");
  211. Assert.AreEqual (108, rstream.ReadByte (), "#4d");
  212. Assert.AreEqual (111, rstream.ReadByte (), "#4e");
  213. rstream.Close ();
  214. try {
  215. long len = res.ContentLength;
  216. Assert.AreEqual ((long) 5, len, "#5");
  217. } catch (ObjectDisposedException) {
  218. Assert.Fail ("#disposed contentlength");
  219. }
  220. try {
  221. WebHeaderCollection w = res.Headers;
  222. } catch (ObjectDisposedException) {
  223. Assert.Fail ("#disposed headers");
  224. }
  225. try {
  226. res.Close ();
  227. } catch (ObjectDisposedException) {
  228. Assert.Fail ("#disposed close");
  229. }
  230. } finally {
  231. if (res != null)
  232. res.Close ();
  233. }
  234. }
  235. [Test]
  236. [Category ("NotWorking")] // bug #323388
  237. public void Sync_GetResponse_Failure ()
  238. {
  239. FileWebRequest req = (FileWebRequest) WebRequest.Create (_tempFileUri);
  240. req.Method = "PUT";
  241. req.ContentLength = 1;
  242. req.ContentType = "image/png";
  243. req.Timeout = 500;
  244. using (Stream rs = req.GetRequestStream ()) {
  245. try {
  246. req.GetResponse ();
  247. Assert.Fail ("#1");
  248. } catch (WebException) {
  249. // The operation has timed out
  250. }
  251. try {
  252. req.BeginGetResponse (null, null);
  253. Assert.Fail ("#2");
  254. } catch (InvalidOperationException) {
  255. // Cannot re-call BeginGetRequestStream/BeginGetResponse while
  256. // a previous call is still in progress
  257. }
  258. }
  259. // the temp file should not be in use
  260. Directory.Delete (_tempDirectory, true);
  261. }
  262. [Test]
  263. public void ConnectionGroupName ()
  264. {
  265. FileWebRequest req = (FileWebRequest) WebRequest.Create (_tempFileUri);
  266. Assert.IsNull (req.ConnectionGroupName, "#A");
  267. req.ConnectionGroupName = "whatever";
  268. Assert.IsNotNull (req.ConnectionGroupName, "#B1");
  269. Assert.AreEqual ("whatever", req.ConnectionGroupName, "#B2");
  270. req.ConnectionGroupName = string.Empty;
  271. Assert.IsNotNull (req.ConnectionGroupName, "#C1");
  272. Assert.AreEqual (string.Empty, req.ConnectionGroupName, "#C2");
  273. req.ConnectionGroupName = null;
  274. Assert.IsNull (req.ConnectionGroupName, "#D");
  275. }
  276. [Test]
  277. public void ContentLength ()
  278. {
  279. FileWebRequest req = (FileWebRequest) WebRequest.Create (_tempFileUri);
  280. Assert.AreEqual (0, req.Headers.Count, "#A1");
  281. Assert.AreEqual (0, req.ContentLength, "#A2");
  282. req.ContentLength = 5;
  283. Assert.AreEqual (5, req.ContentLength, "#A3");
  284. Assert.AreEqual (0, req.Headers.Count, "#A4");
  285. req.Method = "PUT";
  286. using (Stream s = req.GetRequestStream ()) {
  287. s.WriteByte (5);
  288. Assert.AreEqual (5, req.ContentLength, "#B1");
  289. s.WriteByte (4);
  290. Assert.AreEqual (5, req.ContentLength, "#B2");
  291. s.Flush ();
  292. Assert.AreEqual (5, req.ContentLength, "#B3");
  293. }
  294. Assert.AreEqual (5, req.ContentLength, "#B4");
  295. }
  296. [Test]
  297. public void ContentLength_Negative ()
  298. {
  299. FileWebRequest req = (FileWebRequest) WebRequest.Create (_tempFileUri);
  300. try {
  301. req.ContentLength = -1;
  302. Assert.Fail ("#1");
  303. } catch (ArgumentException ex) {
  304. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  305. Assert.IsNotNull (ex.Message, "#3");
  306. Assert.IsFalse (ex.Message == "value", "#4");
  307. Assert.IsNotNull (ex.ParamName, "#5");
  308. Assert.AreEqual ("value", ex.ParamName, "#6");
  309. Assert.IsNull (ex.InnerException, "#7");
  310. }
  311. }
  312. [Test]
  313. public void ContentType ()
  314. {
  315. FileWebRequest req = (FileWebRequest) WebRequest.Create (_tempFileUri);
  316. Assert.AreEqual (0, req.Headers.Count, "#A1");
  317. Assert.IsNull (req.ContentType, "#A2");
  318. req.ContentType = "application/x-gzip";
  319. Assert.AreEqual (1, req.Headers.Count, "#B1");
  320. Assert.AreEqual ("Content-Type", req.Headers.GetKey (0), "#B2");
  321. Assert.AreEqual ("application/x-gzip", req.Headers.Get (0), "#B3");
  322. Assert.AreEqual ("application/x-gzip", req.ContentType, "#B4");
  323. req.Headers.Set ("Content-Type", "image/png");
  324. Assert.AreEqual ("image/png", req.ContentType, "#C1");
  325. req.ContentType = null;
  326. Assert.AreEqual (1, req.Headers.Count, "#D1");
  327. Assert.AreEqual ("Content-Type", req.Headers.GetKey (0), "#D2");
  328. Assert.AreEqual (string.Empty, req.Headers.Get (0), "#D3");
  329. Assert.AreEqual (string.Empty, req.ContentType, "#D4");
  330. req.Headers.Remove ("Content-Type");
  331. Assert.AreEqual (0, req.Headers.Count, "#E1");
  332. Assert.IsNull (req.ContentType, "#E2");
  333. }
  334. [Test]
  335. public void Credentials ()
  336. {
  337. FileWebRequest req = (FileWebRequest) WebRequest.Create (_tempFileUri);
  338. Assert.IsNull (req.Credentials, "#1");
  339. req.Credentials = new NetworkCredential ();
  340. Assert.IsNotNull (req.Credentials, "#2");
  341. req.Credentials = null;
  342. Assert.IsNull (req.Credentials, "#3");
  343. }
  344. [Test]
  345. [Category ("NotWorking")]
  346. public void GetRequestStream ()
  347. {
  348. FileWebRequest req = (FileWebRequest) WebRequest.Create (
  349. _tempFileUri);
  350. req.Timeout = 1000;
  351. req.Method = "POST";
  352. FileStream fsA = null;
  353. FileStream fsB = null;
  354. try {
  355. fsA = req.GetRequestStream () as FileStream;
  356. Assert.IsNotNull (fsA, "#A1");
  357. try {
  358. req.GetRequestStream ();
  359. Assert.Fail ("#A2");
  360. } catch (WebException) {
  361. // The operation has timed out
  362. }
  363. fsA.Close ();
  364. try {
  365. req.GetRequestStream ();
  366. Assert.Fail ("#A3");
  367. } catch (InvalidOperationException) {
  368. // Cannot re-call BeginGetRequestStream/BeginGetResponse
  369. // while a previous call is still in progress.
  370. }
  371. } finally {
  372. if (fsA != null)
  373. fsA.Close ();
  374. if (fsB != null)
  375. fsB.Close ();
  376. }
  377. req = (FileWebRequest) WebRequest.Create (_tempFileUri);
  378. req.Timeout = 1000;
  379. req.Method = "POST";
  380. try {
  381. fsA = req.GetRequestStream () as FileStream;
  382. Assert.IsNotNull (fsA, "#B1");
  383. fsA.Close ();
  384. try {
  385. req.GetRequestStream ();
  386. Assert.Fail ("#B2");
  387. } catch (WebException) {
  388. // The operation has timed out
  389. }
  390. fsA.Close ();
  391. try {
  392. req.GetRequestStream ();
  393. Assert.Fail ("#B3");
  394. } catch (InvalidOperationException) {
  395. // Cannot re-call BeginGetRequestStream/BeginGetResponse
  396. // while a previous call is still in progress.
  397. }
  398. } finally {
  399. if (fsA != null)
  400. fsA.Close ();
  401. if (fsB != null)
  402. fsB.Close ();
  403. }
  404. }
  405. [Test]
  406. public void GetRequestStream_File_Exists ()
  407. {
  408. Stream s = File.Create (_tempFile);
  409. s.Close ();
  410. FileWebRequest req = (FileWebRequest) WebRequest.Create (
  411. _tempFileUri);
  412. req.Method = "POST";
  413. s = req.GetRequestStream ();
  414. s.Close ();
  415. }
  416. [Test]
  417. public void GetRequestStream_Method_Valid ()
  418. {
  419. string [] methods = new string [] { "PUT", "POST", "CHECKOUT",
  420. "DELETE", "OPTIONS", "TRACE", "GET ", "DUNNO" };
  421. foreach (string method in methods) {
  422. FileWebRequest req = (FileWebRequest) WebRequest.Create (
  423. _tempFileUri);
  424. req.Method = method;
  425. using (Stream s = req.GetRequestStream ()) {
  426. Assert.IsNotNull (s, "#1:" + method);
  427. Assert.IsFalse (s.CanRead, "#2:" + method);
  428. Assert.IsTrue (s.CanSeek, "#3:" + method);
  429. Assert.IsFalse (s.CanTimeout, "#4:" + method);
  430. Assert.IsTrue (s.CanWrite, "#5:" + method);
  431. Assert.AreEqual (0, s.Length, "#6:" + method);
  432. Assert.AreEqual (0, s.Position, "#7:" + method);
  433. try {
  434. int i = s.ReadTimeout;
  435. Assert.Fail ("#8:" + method + "=>" + i);
  436. } catch (InvalidOperationException) {
  437. }
  438. try {
  439. int i = s.WriteTimeout;
  440. Assert.Fail ("#9:" + method + "=>" + i);
  441. } catch (InvalidOperationException) {
  442. }
  443. }
  444. }
  445. }
  446. [Test]
  447. public void GetRequestStream_Method_Invalid ()
  448. {
  449. string [] methods = new string [] { "GET", "get", "HEAD", "head",
  450. "CONNECT", "connect"};
  451. foreach (string method in methods) {
  452. FileWebRequest req = (FileWebRequest) WebRequest.Create (
  453. _tempFileUri);
  454. req.Method = method;
  455. try {
  456. req.GetRequestStream ();
  457. Assert.Fail ("#1:" + method);
  458. } catch (ProtocolViolationException ex) {
  459. Assert.AreEqual (typeof (ProtocolViolationException), ex.GetType (), "#2:" + method);
  460. Assert.IsNotNull (ex.Message, "#3:" + method);
  461. Assert.IsNull (ex.InnerException, "#4:" + method);
  462. }
  463. }
  464. }
  465. [Test]
  466. public void GetResponse_File_Exists ()
  467. {
  468. Stream s = File.Create (_tempFile);
  469. s.Close ();
  470. FileWebRequest req = (FileWebRequest) WebRequest.Create (_tempFileUri);
  471. FileWebResponse respA = null;
  472. FileWebResponse respB = null;
  473. try {
  474. respA = req.GetResponse () as FileWebResponse;
  475. Assert.IsNotNull (respA, "#1");
  476. respB = req.GetResponse () as FileWebResponse;
  477. Assert.IsNotNull (respB, "#2");
  478. Assert.AreSame (respA, respB, "#3");
  479. } finally {
  480. if (respA != null)
  481. respA.Close ();
  482. if (respB != null)
  483. respB.Close ();
  484. }
  485. }
  486. [Test]
  487. public void GetResponse_File_DoesNotExist ()
  488. {
  489. FileWebRequest req = (FileWebRequest) WebRequest.Create (_tempFileUri);
  490. try {
  491. req.GetResponse ();
  492. Assert.Fail ("#1");
  493. } catch (WebException ex) {
  494. Assert.AreEqual (typeof (WebException), ex.GetType (), "#1");
  495. Assert.IsNotNull (ex.Message, "#2");
  496. Assert.IsTrue (ex.Message.IndexOf ("FileWebRequestTest.tmp") != -1, "#3");
  497. Assert.IsNull (ex.Response, "#4");
  498. Assert.IsNotNull (ex.InnerException, "#5");
  499. }
  500. }
  501. [Test]
  502. public void Method ()
  503. {
  504. FileWebRequest req = (FileWebRequest) WebRequest.Create (_tempFileUri);
  505. Assert.IsNotNull (req.Method, "#A1");
  506. Assert.AreEqual ("GET", req.Method, "#A2");
  507. req.Method = "whatever";
  508. Assert.IsNotNull (req.Method, "#B1");
  509. Assert.AreEqual ("whatever", req.Method, "#B2");
  510. req.Method = "get ";
  511. Assert.IsNotNull (req.Method, "#C1");
  512. Assert.AreEqual ("get ", req.Method, "#C2");
  513. }
  514. [Test]
  515. public void Method_Empty ()
  516. {
  517. FileWebRequest req = (FileWebRequest) WebRequest.Create (_tempFileUri);
  518. try {
  519. req.Method = string.Empty;
  520. Assert.Fail ("#1");
  521. } catch (ArgumentException ex) {
  522. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  523. Assert.IsNotNull (ex.Message, "#3");
  524. Assert.AreEqual ("value", ex.ParamName, "#4");
  525. Assert.IsNull (ex.InnerException, "#5");
  526. }
  527. }
  528. [Test]
  529. public void Method_Null ()
  530. {
  531. FileWebRequest req = (FileWebRequest) WebRequest.Create (_tempFileUri);
  532. try {
  533. req.Method = null;
  534. Assert.Fail ("#1");
  535. } catch (ArgumentException ex) {
  536. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  537. Assert.IsNotNull (ex.Message, "#3");
  538. Assert.AreEqual ("value", ex.ParamName, "#4");
  539. Assert.IsNull (ex.InnerException, "#5");
  540. }
  541. }
  542. [Test]
  543. public void PreAuthenticate ()
  544. {
  545. FileWebRequest req = (FileWebRequest) WebRequest.Create (_tempFileUri);
  546. Assert.IsFalse (req.PreAuthenticate, "#1");
  547. req.PreAuthenticate = true;
  548. Assert.IsTrue (req.PreAuthenticate, "#2");
  549. }
  550. [Test]
  551. public void Proxy ()
  552. {
  553. FileWebRequest req = (FileWebRequest) WebRequest.Create (_tempFileUri);
  554. Assert.IsNull (req.Proxy, "#1");
  555. req.Proxy = new WebProxy ();
  556. Assert.IsNotNull (req.Proxy, "#2");
  557. req.Proxy = null;
  558. Assert.IsNull (req.Proxy, "#3");
  559. }
  560. [Test]
  561. public void RequestUri ()
  562. {
  563. FileWebRequest req = (FileWebRequest) WebRequest.Create (_tempFileUri);
  564. Assert.AreSame (_tempFileUri, req.RequestUri);
  565. }
  566. [Test]
  567. public void Timeout ()
  568. {
  569. FileWebRequest req = (FileWebRequest) WebRequest.Create (_tempFileUri);
  570. Assert.AreEqual (100000, req.Timeout, "#1");
  571. req.Timeout = int.MaxValue;
  572. Assert.AreEqual (int.MaxValue, req.Timeout, "#2");
  573. req.Timeout = 0;
  574. Assert.AreEqual (0, req.Timeout, "#3");
  575. }
  576. [Test]
  577. public void Timeout_Negative ()
  578. {
  579. FileWebRequest req = (FileWebRequest) WebRequest.Create (_tempFileUri);
  580. req.Timeout = -1;
  581. Assert.AreEqual (-1, req.Timeout, "#1");
  582. try {
  583. req.Timeout = -2;
  584. Assert.Fail ("#2");
  585. } catch (ArgumentOutOfRangeException ex) {
  586. Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#3");
  587. Assert.IsNotNull (ex.Message, "#4");
  588. Assert.IsNotNull (ex.ParamName, "#5");
  589. Assert.AreEqual ("value", ex.ParamName, "#6");
  590. Assert.IsNull (ex.InnerException, "#7");
  591. }
  592. }
  593. [Test]
  594. public void GetObjectData ()
  595. {
  596. FileWebRequest fwr = (FileWebRequest) WebRequest.Create ("file:///test.txt");
  597. fwr.ConnectionGroupName = "CGN";
  598. fwr.ContentLength = 10;
  599. fwr.ContentType = "image/png";
  600. fwr.Credentials = new NetworkCredential ("Miguel", "de Icaza", "Novell");
  601. fwr.Headers.Add ("Disposition", "attach");
  602. fwr.Method = "PUT";
  603. fwr.PreAuthenticate = true;
  604. fwr.Proxy = new WebProxy ("proxy.ximian.com");
  605. fwr.Timeout = 20;
  606. SerializationInfo si = new SerializationInfo (typeof (FileWebRequest),
  607. new FormatterConverter ());
  608. ((ISerializable) fwr).GetObjectData (si, new StreamingContext ());
  609. Assert.AreEqual (9, si.MemberCount, "#A1");
  610. int i = 0;
  611. foreach (SerializationEntry entry in si) {
  612. Assert.IsNotNull (entry.Name, "#B1:" + i);
  613. Assert.IsNotNull (entry.ObjectType, "#B2:" + i);
  614. Assert.IsNotNull (entry.Value, "#B3:" + i);
  615. switch (i) {
  616. case 0:
  617. Assert.AreEqual ("headers", entry.Name, "#B4:" + i);
  618. Assert.AreEqual (typeof (WebHeaderCollection), entry.ObjectType, "#B5:" + i);
  619. break;
  620. case 1:
  621. Assert.AreEqual ("proxy", entry.Name, "#B4:" + i);
  622. Assert.AreEqual (typeof (IWebProxy), entry.ObjectType, "#B5:" + i);
  623. break;
  624. case 2:
  625. Assert.AreEqual ("uri", entry.Name, "#B4:" + i);
  626. Assert.AreEqual (typeof (Uri), entry.ObjectType, "#B5:" + i);
  627. break;
  628. case 3:
  629. Assert.AreEqual ("connectionGroupName", entry.Name, "#B4:" + i);
  630. Assert.AreEqual (typeof (string), entry.ObjectType, "#B5:" + i);
  631. Assert.AreEqual ("CGN", entry.Value, "#B6:" + i);
  632. break;
  633. case 4:
  634. Assert.AreEqual ("method", entry.Name, "#B4:" + i);
  635. Assert.AreEqual (typeof (string), entry.ObjectType, "#B5:" + i);
  636. Assert.AreEqual ("PUT", entry.Value, "#B6:" + i);
  637. break;
  638. case 5:
  639. Assert.AreEqual ("contentLength", entry.Name, "#B4:" + i);
  640. Assert.AreEqual (typeof (long), entry.ObjectType, "#B5:" + i);
  641. Assert.AreEqual (10, entry.Value, "#B6:" + i);
  642. break;
  643. case 6:
  644. Assert.AreEqual ("timeout", entry.Name, "#B4:" + i);
  645. Assert.AreEqual (typeof (int), entry.ObjectType, "#B5:" + i);
  646. Assert.AreEqual (20, entry.Value, "#B6:" + i);
  647. break;
  648. case 7:
  649. Assert.AreEqual ("fileAccess", entry.Name, "#B4:" + i);
  650. Assert.AreEqual (typeof (FileAccess), entry.ObjectType, "#B5:" + i);
  651. Assert.AreEqual (FileAccess.Read, entry.Value, "#B6:" + i);
  652. break;
  653. case 8:
  654. Assert.AreEqual ("preauthenticate", entry.Name, "#B4:" + i);
  655. Assert.AreEqual (typeof (bool), entry.ObjectType, "#B5:" + i);
  656. Assert.AreEqual (false, entry.Value, "#B6:" + i);
  657. break;
  658. }
  659. i++;
  660. }
  661. }
  662. [Test]
  663. [Category ("NotWorking")] // Difference at index 272: 20 instead of 19
  664. public void Serialize ()
  665. {
  666. FileWebRequest fwr = (FileWebRequest) WebRequest.Create ("file://test.txt/");
  667. fwr.ConnectionGroupName = "CGN";
  668. fwr.ContentLength = 10;
  669. fwr.ContentType = "image/png";
  670. fwr.Credentials = new NetworkCredential ("Miguel", "de Icaza", "Novell");
  671. fwr.Headers.Add ("Disposition", "attach");
  672. fwr.Method = "PUT";
  673. fwr.PreAuthenticate = true;
  674. fwr.Proxy = new WebProxy ("proxy.ximian.com");
  675. fwr.Timeout = 20;
  676. BinaryFormatter bf = new BinaryFormatter ();
  677. bf.AssemblyFormat = FormatterAssemblyStyle.Full;
  678. MemoryStream ms = new MemoryStream ();
  679. bf.Serialize (ms, fwr);
  680. ms.Position = 0;
  681. byte [] buffer = new byte [ms.Length];
  682. ms.Read (buffer, 0, buffer.Length);
  683. Assert.AreEqual (_serialized, buffer);
  684. }
  685. [Test]
  686. public void Deserialize ()
  687. {
  688. MemoryStream ms = new MemoryStream ();
  689. ms.Write (_serialized, 0, _serialized.Length);
  690. ms.Position = 0;
  691. BinaryFormatter bf = new BinaryFormatter ();
  692. FileWebRequest req = (FileWebRequest) bf.Deserialize (ms);
  693. Assert.AreEqual ("CGN", req.ConnectionGroupName, "#A1");
  694. Assert.AreEqual (10, req.ContentLength, "#A2");
  695. Assert.AreEqual ("image/png", req.ContentType, "#A3");
  696. Assert.IsNull (req.Credentials, "#A4");
  697. Assert.AreEqual ("PUT", req.Method, "#A5");
  698. Assert.IsFalse (req.PreAuthenticate, "#A6");
  699. Assert.AreEqual ("file://test.txt/", req.RequestUri.AbsoluteUri, "#A7");
  700. Assert.AreEqual (20, req.Timeout, "#A8");
  701. WebHeaderCollection headers = req.Headers;
  702. Assert.IsNotNull (headers, "#C1");
  703. Assert.AreEqual (2, headers.Count, "#C2");
  704. Assert.AreEqual ("Content-Type", req.Headers.GetKey (0), "#C3");
  705. Assert.AreEqual ("image/png", req.Headers.Get (0), "#C4");
  706. Assert.AreEqual ("Disposition", req.Headers.GetKey (1), "#C5");
  707. Assert.AreEqual ("attach", req.Headers.Get (1), "#C6");
  708. WebProxy proxy = req.Proxy as WebProxy;
  709. Assert.IsNotNull (proxy, "#D1");
  710. Assert.AreEqual ("http://proxy.ximian.com/", proxy.Address.AbsoluteUri, "#D2");
  711. Assert.IsNotNull (proxy.BypassArrayList, "#D3");
  712. Assert.AreEqual (0, proxy.BypassArrayList.Count, "#D4");
  713. Assert.IsNotNull (proxy.BypassList, "#D5");
  714. Assert.AreEqual (0, proxy.BypassList.Length, "#D6");
  715. Assert.IsFalse (proxy.BypassProxyOnLocal, "#D7");
  716. Assert.IsNull (proxy.Credentials, "#D8");
  717. }
  718. private Uri GetTempFileUri ()
  719. {
  720. string tempFile = _tempFile;
  721. if (RunningOnUnix) {
  722. // remove leading slash for absolute paths
  723. tempFile = tempFile.TrimStart ('/');
  724. } else {
  725. tempFile = tempFile.Replace ('\\', '/');
  726. }
  727. return new Uri ("file:///" + tempFile);
  728. }
  729. private bool RunningOnUnix {
  730. get {
  731. // check for Unix platforms - see FAQ for more details
  732. // http://www.mono-project.com/FAQ:_Technical#How_to_detect_the_execution_platform_.3F
  733. int platform = (int) Environment.OSVersion.Platform;
  734. return ((platform == 4) || (platform == 128) || (platform == 6));
  735. }
  736. }
  737. private static readonly byte [] _serialized = new byte [] {
  738. 0x00, 0x01, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x01, 0x00,
  739. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0c, 0x02, 0x00, 0x00, 0x00,
  740. 0x49, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x2c, 0x20, 0x56, 0x65,
  741. 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x3d, 0x32, 0x2e, 0x30, 0x2e, 0x30,
  742. 0x2e, 0x30, 0x2c, 0x20, 0x43, 0x75, 0x6c, 0x74, 0x75, 0x72, 0x65,
  743. 0x3d, 0x6e, 0x65, 0x75, 0x74, 0x72, 0x61, 0x6c, 0x2c, 0x20, 0x50,
  744. 0x75, 0x62, 0x6c, 0x69, 0x63, 0x4b, 0x65, 0x79, 0x54, 0x6f, 0x6b,
  745. 0x65, 0x6e, 0x3d, 0x62, 0x37, 0x37, 0x61, 0x35, 0x63, 0x35, 0x36,
  746. 0x31, 0x39, 0x33, 0x34, 0x65, 0x30, 0x38, 0x39, 0x05, 0x01, 0x00,
  747. 0x00, 0x00, 0x19, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x2e, 0x4e,
  748. 0x65, 0x74, 0x2e, 0x46, 0x69, 0x6c, 0x65, 0x57, 0x65, 0x62, 0x52,
  749. 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x09, 0x00, 0x00, 0x00, 0x07,
  750. 0x68, 0x65, 0x61, 0x64, 0x65, 0x72, 0x73, 0x05, 0x70, 0x72, 0x6f,
  751. 0x78, 0x79, 0x03, 0x75, 0x72, 0x69, 0x13, 0x63, 0x6f, 0x6e, 0x6e,
  752. 0x65, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x47, 0x72, 0x6f, 0x75, 0x70,
  753. 0x4e, 0x61, 0x6d, 0x65, 0x06, 0x6d, 0x65, 0x74, 0x68, 0x6f, 0x64,
  754. 0x0d, 0x63, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x4c, 0x65, 0x6e,
  755. 0x67, 0x74, 0x68, 0x07, 0x74, 0x69, 0x6d, 0x65, 0x6f, 0x75, 0x74,
  756. 0x0a, 0x66, 0x69, 0x6c, 0x65, 0x41, 0x63, 0x63, 0x65, 0x73, 0x73,
  757. 0x0f, 0x70, 0x72, 0x65, 0x61, 0x75, 0x74, 0x68, 0x65, 0x6e, 0x74,
  758. 0x69, 0x63, 0x61, 0x74, 0x65, 0x04, 0x04, 0x04, 0x01, 0x01, 0x00,
  759. 0x00, 0x03, 0x00, 0x1e, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x2e,
  760. 0x4e, 0x65, 0x74, 0x2e, 0x57, 0x65, 0x62, 0x48, 0x65, 0x61, 0x64,
  761. 0x65, 0x72, 0x43, 0x6f, 0x6c, 0x6c, 0x65, 0x63, 0x74, 0x69, 0x6f,
  762. 0x6e, 0x02, 0x00, 0x00, 0x00, 0x13, 0x53, 0x79, 0x73, 0x74, 0x65,
  763. 0x6d, 0x2e, 0x4e, 0x65, 0x74, 0x2e, 0x57, 0x65, 0x62, 0x50, 0x72,
  764. 0x6f, 0x78, 0x79, 0x02, 0x00, 0x00, 0x00, 0x0a, 0x53, 0x79, 0x73,
  765. 0x74, 0x65, 0x6d, 0x2e, 0x55, 0x72, 0x69, 0x02, 0x00, 0x00, 0x00,
  766. 0x09, 0x08, 0x14, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x2e, 0x49,
  767. 0x4f, 0x2e, 0x46, 0x69, 0x6c, 0x65, 0x41, 0x63, 0x63, 0x65, 0x73,
  768. 0x73, 0x01, 0x02, 0x00, 0x00, 0x00, 0x09, 0x03, 0x00, 0x00, 0x00,
  769. 0x09, 0x04, 0x00, 0x00, 0x00, 0x09, 0x05, 0x00, 0x00, 0x00, 0x06,
  770. 0x06, 0x00, 0x00, 0x00, 0x03, 0x43, 0x47, 0x4e, 0x06, 0x07, 0x00,
  771. 0x00, 0x00, 0x03, 0x50, 0x55, 0x54, 0x0a, 0x00, 0x00, 0x00, 0x00,
  772. 0x00, 0x00, 0x00, 0x14, 0x00, 0x00, 0x00, 0x04, 0xf8, 0xff, 0xff,
  773. 0xff, 0x14, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x2e, 0x49, 0x4f,
  774. 0x2e, 0x46, 0x69, 0x6c, 0x65, 0x41, 0x63, 0x63, 0x65, 0x73, 0x73,
  775. 0x01, 0x00, 0x00, 0x00, 0x07, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x5f,
  776. 0x5f, 0x00, 0x08, 0x01, 0x00, 0x00, 0x00, 0x00, 0x05, 0x03, 0x00,
  777. 0x00, 0x00, 0x1e, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x2e, 0x4e,
  778. 0x65, 0x74, 0x2e, 0x57, 0x65, 0x62, 0x48, 0x65, 0x61, 0x64, 0x65,
  779. 0x72, 0x43, 0x6f, 0x6c, 0x6c, 0x65, 0x63, 0x74, 0x69, 0x6f, 0x6e,
  780. 0x05, 0x00, 0x00, 0x00, 0x05, 0x43, 0x6f, 0x75, 0x6e, 0x74, 0x01,
  781. 0x30, 0x01, 0x32, 0x01, 0x31, 0x01, 0x33, 0x00, 0x01, 0x01, 0x01,
  782. 0x01, 0x08, 0x02, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x06,
  783. 0x09, 0x00, 0x00, 0x00, 0x0c, 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x6e,
  784. 0x74, 0x2d, 0x54, 0x79, 0x70, 0x65, 0x06, 0x0a, 0x00, 0x00, 0x00,
  785. 0x09, 0x69, 0x6d, 0x61, 0x67, 0x65, 0x2f, 0x70, 0x6e, 0x67, 0x06,
  786. 0x0b, 0x00, 0x00, 0x00, 0x0b, 0x44, 0x69, 0x73, 0x70, 0x6f, 0x73,
  787. 0x69, 0x74, 0x69, 0x6f, 0x6e, 0x06, 0x0c, 0x00, 0x00, 0x00, 0x06,
  788. 0x61, 0x74, 0x74, 0x61, 0x63, 0x68, 0x05, 0x04, 0x00, 0x00, 0x00,
  789. 0x13, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x2e, 0x4e, 0x65, 0x74,
  790. 0x2e, 0x57, 0x65, 0x62, 0x50, 0x72, 0x6f, 0x78, 0x79, 0x04, 0x00,
  791. 0x00, 0x00, 0x0e, 0x5f, 0x42, 0x79, 0x70, 0x61, 0x73, 0x73, 0x4f,
  792. 0x6e, 0x4c, 0x6f, 0x63, 0x61, 0x6c, 0x0d, 0x5f, 0x50, 0x72, 0x6f,
  793. 0x78, 0x79, 0x41, 0x64, 0x64, 0x72, 0x65, 0x73, 0x73, 0x0b, 0x5f,
  794. 0x42, 0x79, 0x70, 0x61, 0x73, 0x73, 0x4c, 0x69, 0x73, 0x74, 0x16,
  795. 0x5f, 0x55, 0x73, 0x65, 0x44, 0x65, 0x66, 0x61, 0x75, 0x6c, 0x74,
  796. 0x43, 0x72, 0x65, 0x64, 0x65, 0x6e, 0x74, 0x69, 0x61, 0x6c, 0x73,
  797. 0x00, 0x04, 0x02, 0x00, 0x01, 0x0a, 0x53, 0x79, 0x73, 0x74, 0x65,
  798. 0x6d, 0x2e, 0x55, 0x72, 0x69, 0x02, 0x00, 0x00, 0x00, 0x01, 0x02,
  799. 0x00, 0x00, 0x00, 0x00, 0x09, 0x0d, 0x00, 0x00, 0x00, 0x0a, 0x00,
  800. 0x05, 0x05, 0x00, 0x00, 0x00, 0x0a, 0x53, 0x79, 0x73, 0x74, 0x65,
  801. 0x6d, 0x2e, 0x55, 0x72, 0x69, 0x01, 0x00, 0x00, 0x00, 0x0b, 0x41,
  802. 0x62, 0x73, 0x6f, 0x6c, 0x75, 0x74, 0x65, 0x55, 0x72, 0x69, 0x01,
  803. 0x02, 0x00, 0x00, 0x00, 0x06, 0x0e, 0x00, 0x00, 0x00, 0x10, 0x66,
  804. 0x69, 0x6c, 0x65, 0x3a, 0x2f, 0x2f, 0x74, 0x65, 0x73, 0x74, 0x2e,
  805. 0x74, 0x78, 0x74, 0x2f, 0x01, 0x0d, 0x00, 0x00, 0x00, 0x05, 0x00,
  806. 0x00, 0x00, 0x06, 0x0f, 0x00, 0x00, 0x00, 0x18, 0x68, 0x74, 0x74,
  807. 0x70, 0x3a, 0x2f, 0x2f, 0x70, 0x72, 0x6f, 0x78, 0x79, 0x2e, 0x78,
  808. 0x69, 0x6d, 0x69, 0x61, 0x6e, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x0b
  809. };
  810. }
  811. }