csharptest | 60fd773 | 2011-09-09 12:18:16 -0500 | [diff] [blame] | 1 | using System;
|
| 2 | using System.Collections.Generic;
|
| 3 | using System.IO;
|
| 4 | using System.Text;
|
| 5 | using Google.ProtocolBuffers.Serialization;
|
| 6 | using Google.ProtocolBuffers.Serialization.Http;
|
| 7 | using Google.ProtocolBuffers.TestProtos;
|
| 8 | using NUnit.Framework;
|
| 9 |
|
| 10 | namespace Google.ProtocolBuffers
|
| 11 | {
|
| 12 | [TestFixture]
|
| 13 | public class TestMimeMessageFormats
|
| 14 | {
|
| 15 | // There is a whole host of various json mime types in use around the net, this is the set we accept...
|
| 16 | readonly IEnumerable<string> JsonTypes = new string[] { "application/json", "application/x-json", "application/x-javascript", "text/javascript", "text/x-javascript", "text/x-json", "text/json" };
|
| 17 | readonly IEnumerable<string> XmlTypes = new string[] { "text/xml", "application/xml" };
|
| 18 | readonly IEnumerable<string> ProtobufTypes = new string[] { "application/binary", "application/x-protobuf", "application/vnd.google.protobuf" };
|
| 19 |
|
| 20 | [Test]
|
| 21 | public void TestReadJsonMimeTypes()
|
| 22 | {
|
| 23 | foreach (string type in JsonTypes)
|
| 24 | {
|
| 25 | Assert.IsTrue(
|
| 26 | MessageFormatFactory.CreateInputStream(new MessageFormatOptions(), type, Stream.Null)
|
| 27 | is JsonFormatReader);
|
| 28 | }
|
| 29 | Assert.IsTrue(
|
| 30 | MessageFormatFactory.CreateInputStream(new MessageFormatOptions() { DefaultContentType = "application/json" }, null, Stream.Null)
|
| 31 | is JsonFormatReader);
|
| 32 | }
|
| 33 | [Test]
|
| 34 | public void TestWriteJsonMimeTypes()
|
| 35 | {
|
| 36 | foreach (string type in JsonTypes)
|
| 37 | {
|
| 38 | Assert.IsTrue(
|
| 39 | MessageFormatFactory.CreateOutputStream(new MessageFormatOptions(), type, Stream.Null)
|
| 40 | is JsonFormatWriter);
|
| 41 | }
|
| 42 | Assert.IsTrue(
|
| 43 | MessageFormatFactory.CreateOutputStream(new MessageFormatOptions() { DefaultContentType = "application/json" }, null, Stream.Null)
|
| 44 | is JsonFormatWriter);
|
| 45 | }
|
| 46 | [Test]
|
| 47 | public void TestReadXmlMimeTypes()
|
| 48 | {
|
| 49 | foreach (string type in XmlTypes)
|
| 50 | {
|
| 51 | Assert.IsTrue(
|
| 52 | MessageFormatFactory.CreateInputStream(new MessageFormatOptions(), type, Stream.Null)
|
| 53 | is XmlFormatReader);
|
| 54 | }
|
| 55 | Assert.IsTrue(
|
| 56 | MessageFormatFactory.CreateInputStream(new MessageFormatOptions() { DefaultContentType = "application/xml" }, null, Stream.Null)
|
| 57 | is XmlFormatReader);
|
| 58 | }
|
| 59 | [Test]
|
| 60 | public void TestWriteXmlMimeTypes()
|
| 61 | {
|
| 62 | foreach (string type in XmlTypes)
|
| 63 | {
|
| 64 | Assert.IsTrue(
|
| 65 | MessageFormatFactory.CreateOutputStream(new MessageFormatOptions(), type, Stream.Null)
|
| 66 | is XmlFormatWriter);
|
| 67 | }
|
| 68 | Assert.IsTrue(
|
| 69 | MessageFormatFactory.CreateOutputStream(new MessageFormatOptions() { DefaultContentType = "application/xml" }, null, Stream.Null)
|
| 70 | is XmlFormatWriter);
|
| 71 | }
|
| 72 | [Test]
|
| 73 | public void TestReadProtoMimeTypes()
|
| 74 | {
|
| 75 | foreach (string type in ProtobufTypes)
|
| 76 | {
|
| 77 | Assert.IsTrue(
|
| 78 | MessageFormatFactory.CreateInputStream(new MessageFormatOptions(), type, Stream.Null)
|
| 79 | is CodedInputStream);
|
| 80 | }
|
| 81 | Assert.IsTrue(
|
| 82 | MessageFormatFactory.CreateInputStream(new MessageFormatOptions() { DefaultContentType = "application/vnd.google.protobuf" }, null, Stream.Null)
|
| 83 | is CodedInputStream);
|
| 84 | }
|
| 85 | [Test]
|
| 86 | public void TestWriteProtoMimeTypes()
|
| 87 | {
|
| 88 | foreach (string type in ProtobufTypes)
|
| 89 | {
|
| 90 | Assert.IsTrue(
|
| 91 | MessageFormatFactory.CreateOutputStream(new MessageFormatOptions(), type, Stream.Null)
|
| 92 | is CodedOutputStream);
|
| 93 | }
|
| 94 | Assert.IsTrue(
|
| 95 | MessageFormatFactory.CreateOutputStream(new MessageFormatOptions() { DefaultContentType = "application/vnd.google.protobuf" }, null, Stream.Null)
|
| 96 | is CodedOutputStream);
|
| 97 | }
|
| 98 | [Test]
|
| 99 | public void TestMergeFromJsonType()
|
| 100 | {
|
| 101 | TestXmlMessage msg = new TestXmlMessage.Builder().MergeFrom(
|
| 102 | new MessageFormatOptions(), "application/json", new MemoryStream(Encoding.ASCII.GetBytes(
|
| 103 | TestXmlMessage.CreateBuilder().SetText("a").SetNumber(1).Build().ToJson()
|
| 104 | )))
|
| 105 | .Build();
|
| 106 | Assert.AreEqual("a", msg.Text);
|
| 107 | Assert.AreEqual(1, msg.Number);
|
| 108 | }
|
| 109 | [Test]
|
| 110 | public void TestMergeFromXmlType()
|
| 111 | {
|
| 112 | TestXmlMessage msg = new TestXmlMessage.Builder().MergeFrom(
|
| 113 | new MessageFormatOptions(), "application/xml", new MemoryStream(Encoding.ASCII.GetBytes(
|
| 114 | TestXmlMessage.CreateBuilder().SetText("a").SetNumber(1).Build().ToXml()
|
| 115 | )))
|
| 116 | .Build();
|
| 117 | Assert.AreEqual("a", msg.Text);
|
| 118 | Assert.AreEqual(1, msg.Number);
|
| 119 | }
|
| 120 | [Test]
|
| 121 | public void TestMergeFromProtoType()
|
| 122 | {
|
| 123 | TestXmlMessage msg = new TestXmlMessage.Builder().MergeFrom(
|
| 124 | new MessageFormatOptions(), "application/vnd.google.protobuf", new MemoryStream(
|
| 125 | TestXmlMessage.CreateBuilder().SetText("a").SetNumber(1).Build().ToByteArray()
|
| 126 | ))
|
| 127 | .Build();
|
| 128 | Assert.AreEqual("a", msg.Text);
|
| 129 | Assert.AreEqual(1, msg.Number);
|
| 130 | }
|
| 131 | [Test]
|
| 132 | public void TestWriteToJsonType()
|
| 133 | {
|
| 134 | MemoryStream ms = new MemoryStream();
|
| 135 | TestXmlMessage.CreateBuilder().SetText("a").SetNumber(1).Build()
|
| 136 | .WriteTo(new MessageFormatOptions(), "application/json", ms);
|
| 137 |
|
| 138 | Assert.AreEqual(@"{""text"":""a"",""number"":1}", Encoding.UTF8.GetString(ms.ToArray()));
|
| 139 | }
|
| 140 | [Test]
|
| 141 | public void TestWriteToXmlType()
|
| 142 | {
|
| 143 | MemoryStream ms = new MemoryStream();
|
| 144 | TestXmlMessage.CreateBuilder().SetText("a").SetNumber(1).Build()
|
| 145 | .WriteTo(new MessageFormatOptions(), "application/xml", ms);
|
| 146 |
|
| 147 | Assert.AreEqual("<root><text>a</text><number>1</number></root>", Encoding.UTF8.GetString(ms.ToArray()));
|
| 148 | }
|
| 149 | [Test]
|
| 150 | public void TestWriteToProtoType()
|
| 151 | {
|
| 152 | MemoryStream ms = new MemoryStream();
|
| 153 | TestXmlMessage.CreateBuilder().SetText("a").SetNumber(1).Build()
|
| 154 | .WriteTo(new MessageFormatOptions(), "application/vnd.google.protobuf", ms);
|
| 155 |
|
| 156 | byte[] bytes = TestXmlMessage.CreateBuilder().SetText("a").SetNumber(1).Build().ToByteArray();
|
| 157 | Assert.AreEqual(bytes, ms.ToArray());
|
| 158 | }
|
| 159 | [Test]
|
| 160 | public void TestXmlReaderOptions()
|
| 161 | {
|
| 162 | MemoryStream ms = new MemoryStream();
|
| 163 | XmlFormatWriter.CreateInstance(ms)
|
| 164 | .SetOptions(XmlWriterOptions.OutputNestedArrays)
|
| 165 | .WriteMessage("my-root-node", TestXmlMessage.CreateBuilder().SetText("a").AddNumbers(1).AddNumbers(2).Build());
|
| 166 | ms.Position = 0;
|
| 167 |
|
| 168 | MessageFormatOptions options = new MessageFormatOptions()
|
| 169 | {
|
| 170 | XmlReaderOptions = XmlReaderOptions.ReadNestedArrays,
|
| 171 | XmlReaderRootElementName = "my-root-node"
|
| 172 | };
|
| 173 |
|
| 174 | TestXmlMessage msg = new TestXmlMessage.Builder().MergeFrom(
|
| 175 | options, "application/xml", ms)
|
| 176 | .Build();
|
| 177 |
|
| 178 | Assert.AreEqual("a", msg.Text);
|
| 179 | Assert.AreEqual(1, msg.NumbersList[0]);
|
| 180 | Assert.AreEqual(2, msg.NumbersList[1]);
|
| 181 |
|
| 182 | }
|
| 183 | [Test]
|
| 184 | public void TestXmlWriterOptions()
|
| 185 | {
|
| 186 | TestXmlMessage message = TestXmlMessage.CreateBuilder().SetText("a").AddNumbers(1).AddNumbers(2).Build();
|
| 187 | MessageFormatOptions options = new MessageFormatOptions()
|
| 188 | {
|
| 189 | XmlWriterOptions = XmlWriterOptions.OutputNestedArrays,
|
| 190 | XmlWriterRootElementName = "root-node"
|
| 191 | };
|
| 192 |
|
| 193 | MemoryStream ms = new MemoryStream();
|
| 194 | message.WriteTo(options, "application/xml", ms);
|
| 195 | ms.Position = 0;
|
| 196 |
|
| 197 | TestXmlMessage.Builder builder = TestXmlMessage.CreateBuilder();
|
| 198 | XmlFormatReader.CreateInstance(ms)
|
| 199 | .SetOptions(XmlReaderOptions.ReadNestedArrays)
|
| 200 | .Merge("root-node", builder);
|
| 201 |
|
| 202 | Assert.AreEqual("a", builder.Text);
|
| 203 | Assert.AreEqual(1, builder.NumbersList[0]);
|
| 204 | Assert.AreEqual(2, builder.NumbersList[1]);
|
| 205 | }
|
| 206 | [Test]
|
| 207 | public void TestJsonFormatted()
|
| 208 | {
|
| 209 | MemoryStream ms = new MemoryStream();
|
| 210 | TestXmlMessage.CreateBuilder().SetText("a").SetNumber(1).Build()
|
| 211 | .WriteTo(new MessageFormatOptions() { FormattedOutput = true }, "application/json", ms);
|
| 212 |
|
| 213 | Assert.AreEqual("{\r\n \"text\": \"a\",\r\n \"number\": 1\r\n}", Encoding.UTF8.GetString(ms.ToArray()));
|
| 214 | }
|
| 215 | [Test]
|
| 216 | public void TestXmlFormatted()
|
| 217 | {
|
| 218 | MemoryStream ms = new MemoryStream();
|
| 219 | TestXmlMessage.CreateBuilder().SetText("a").SetNumber(1).Build()
|
| 220 | .WriteTo(new MessageFormatOptions() { FormattedOutput = true }, "application/xml", ms);
|
| 221 |
|
| 222 | Assert.AreEqual("<root>\r\n <text>a</text>\r\n <number>1</number>\r\n</root>", Encoding.UTF8.GetString(ms.ToArray()));
|
| 223 | }
|
csharptest | 2cf6e1b | 2011-09-16 10:55:10 -0500 | [diff] [blame^] | 224 |
|
| 225 | [Test]
|
| 226 | public void TestReadCustomMimeTypes()
|
| 227 | {
|
| 228 | var options = new MessageFormatOptions();
|
| 229 | //Remove existing mime-type mappings
|
| 230 | options.MimeInputTypes.Clear();
|
| 231 | //Add our own
|
| 232 | options.MimeInputTypes.Add("-custom-XML-mime-type-", XmlFormatReader.CreateInstance);
|
| 233 | Assert.AreEqual(1, options.MimeInputTypes.Count);
|
| 234 |
|
| 235 | Stream xmlStream = new MemoryStream(Encoding.ASCII.GetBytes(
|
| 236 | TestXmlMessage.CreateBuilder().SetText("a").SetNumber(1).Build().ToXml()
|
| 237 | ));
|
| 238 |
|
| 239 | TestXmlMessage msg = new TestXmlMessage.Builder().MergeFrom(
|
| 240 | options, "-custom-XML-mime-type-", xmlStream)
|
| 241 | .Build();
|
| 242 | Assert.AreEqual("a", msg.Text);
|
| 243 | Assert.AreEqual(1, msg.Number);
|
| 244 | }
|
| 245 |
|
| 246 | [Test]
|
| 247 | public void TestWriteToCustomType()
|
| 248 | {
|
| 249 | var options = new MessageFormatOptions();
|
| 250 | //Remove existing mime-type mappings
|
| 251 | options.MimeOutputTypes.Clear();
|
| 252 | //Add our own
|
| 253 | options.MimeOutputTypes.Add("-custom-XML-mime-type-", XmlFormatWriter.CreateInstance);
|
| 254 |
|
| 255 | Assert.AreEqual(1, options.MimeOutputTypes.Count);
|
| 256 |
|
| 257 | MemoryStream ms = new MemoryStream();
|
| 258 | TestXmlMessage.CreateBuilder().SetText("a").SetNumber(1).Build()
|
| 259 | .WriteTo(options, "-custom-XML-mime-type-", ms);
|
| 260 |
|
| 261 | Assert.AreEqual("<root><text>a</text><number>1</number></root>", Encoding.UTF8.GetString(ms.ToArray()));
|
| 262 | }
|
csharptest | 60fd773 | 2011-09-09 12:18:16 -0500 | [diff] [blame] | 263 | }
|
| 264 | } |