XmlMtomDictionaryWriterTest.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. //
  2. // XmlMtomDictionaryWriterTest.cs
  3. //
  4. // Author:
  5. // Atsushi Enomoto <[email protected]>
  6. //
  7. // Copyright (C) 2009 Novell, Inc. http://www.novell.com
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. #if !MOBILE
  29. using System;
  30. using System.IO;
  31. using System.Text;
  32. using System.Xml;
  33. using NUnit.Framework;
  34. namespace MonoTests.System.Xml
  35. {
  36. [TestFixture]
  37. public class XmlMtomDictionaryWriterTest
  38. {
  39. // Nothing gave me either multi-parted MTOM message or
  40. // xop-included xml so far ...
  41. [Test]
  42. public void UseCase1 ()
  43. {
  44. MemoryStream ms = new MemoryStream ();
  45. var w = XmlDictionaryWriter.CreateMtomWriter (ms, Encoding.UTF8, 10000, "sTaRt", "myboundary", "urn:foo", false, false);
  46. w.WriteStartElement ("root");
  47. w.WriteRaw ("RAW");
  48. w.WriteStartElement ("foo");
  49. w.WriteChars (new char [] {'b', 'c', 'd'}, 0, 3);
  50. w.WriteBase64 (new byte [] {50, 60, 70}, 0, 3);
  51. w.WriteArray ("", "arr", "", new bool [] {true,false,true},0,3);
  52. w.WriteValue (new MyStreamProvider ());
  53. w.WriteString ("999\r\n\r\n666");
  54. w.WriteEndElement ();
  55. //w.WriteProcessingInstruction ("pi", "data");
  56. w.WriteEndElement (); // it outputs end of mime data.
  57. w.WriteStartElement ("root"); // it is in the next part).
  58. w.WriteEndElement (); // it does not close current part.
  59. w.WriteEndDocument (); // no effect.
  60. w.WriteStartElement ("root");
  61. w.WriteEndElement ();
  62. w.WriteStartElement ("root");
  63. w.WriteEndElement ();
  64. w.WriteEndDocument ();
  65. w.Flush ();
  66. ms.Position = 0;
  67. // there are some insiginificant output differences
  68. Assert.AreEqual (usecase1, new StreamReader (ms).ReadToEnd ().Replace ("<root />", "<root/>"));
  69. }
  70. string usecase1 = @"
  71. --myboundary
  72. Content-ID: <urn:foo>
  73. Content-Transfer-Encoding: 8bit
  74. Content-Type: application/xop+xml;charset=utf-8;type=""sTaRt""
  75. <root>RAW<foo>bcdMjxG<arr>true</arr><arr>false</arr><arr>true</arr>AQIDBAU=999&#xD;XXX
  76. &#xD;XXX
  77. 666</foo></root>
  78. --myboundary--
  79. <root/><root/><root/>".Replace ("\n", "\r\n").Replace ("XXX\r\n", "\n");
  80. }
  81. class MyStreamProvider : IStreamProvider
  82. {
  83. public Stream GetStream ()
  84. {
  85. return new MemoryStream (new byte [] {1, 2, 3, 4, 5});
  86. }
  87. public void ReleaseStream (Stream s)
  88. {
  89. }
  90. }
  91. }
  92. #endif