diff options
Diffstat (limited to 'src/ProtocolBuffers.Test')
-rw-r--r-- | src/ProtocolBuffers.Test/ProtocolBuffers.Test.csproj | 1 | ||||
-rw-r--r-- | src/ProtocolBuffers.Test/TestMimeMessageFormats.cs | 225 | ||||
-rw-r--r-- | src/ProtocolBuffers.Test/TestWriterFormatJson.cs | 34 | ||||
-rw-r--r-- | src/ProtocolBuffers.Test/TestWriterFormatXml.cs | 63 |
4 files changed, 306 insertions, 17 deletions
diff --git a/src/ProtocolBuffers.Test/ProtocolBuffers.Test.csproj b/src/ProtocolBuffers.Test/ProtocolBuffers.Test.csproj index 95ab0b9c..549fe88c 100644 --- a/src/ProtocolBuffers.Test/ProtocolBuffers.Test.csproj +++ b/src/ProtocolBuffers.Test/ProtocolBuffers.Test.csproj @@ -106,6 +106,7 @@ <Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="ReflectionTester.cs" />
<Compile Include="ServiceTest.cs" />
+ <Compile Include="TestMimeMessageFormats.cs" />
<Compile Include="TestProtos\UnitTestCSharpOptionsProtoFile.cs" />
<Compile Include="TestProtos\UnitTestCustomOptionsProtoFile.cs" />
<Compile Include="TestProtos\UnitTestEmbedOptimizeForProtoFile.cs" />
diff --git a/src/ProtocolBuffers.Test/TestMimeMessageFormats.cs b/src/ProtocolBuffers.Test/TestMimeMessageFormats.cs new file mode 100644 index 00000000..6f4b7e0f --- /dev/null +++ b/src/ProtocolBuffers.Test/TestMimeMessageFormats.cs @@ -0,0 +1,225 @@ +using System;
+using System.Collections.Generic;
+using System.IO;
+using System.Text;
+using Google.ProtocolBuffers.Serialization;
+using Google.ProtocolBuffers.Serialization.Http;
+using Google.ProtocolBuffers.TestProtos;
+using NUnit.Framework;
+
+namespace Google.ProtocolBuffers
+{
+ [TestFixture]
+ public class TestMimeMessageFormats
+ {
+ // There is a whole host of various json mime types in use around the net, this is the set we accept...
+ readonly IEnumerable<string> JsonTypes = new string[] { "application/json", "application/x-json", "application/x-javascript", "text/javascript", "text/x-javascript", "text/x-json", "text/json" };
+ readonly IEnumerable<string> XmlTypes = new string[] { "text/xml", "application/xml" };
+ readonly IEnumerable<string> ProtobufTypes = new string[] { "application/binary", "application/x-protobuf", "application/vnd.google.protobuf" };
+
+ [Test]
+ public void TestReadJsonMimeTypes()
+ {
+ foreach (string type in JsonTypes)
+ {
+ Assert.IsTrue(
+ MessageFormatFactory.CreateInputStream(new MessageFormatOptions(), type, Stream.Null)
+ is JsonFormatReader);
+ }
+ Assert.IsTrue(
+ MessageFormatFactory.CreateInputStream(new MessageFormatOptions() { DefaultContentType = "application/json" }, null, Stream.Null)
+ is JsonFormatReader);
+ }
+ [Test]
+ public void TestWriteJsonMimeTypes()
+ {
+ foreach (string type in JsonTypes)
+ {
+ Assert.IsTrue(
+ MessageFormatFactory.CreateOutputStream(new MessageFormatOptions(), type, Stream.Null)
+ is JsonFormatWriter);
+ }
+ Assert.IsTrue(
+ MessageFormatFactory.CreateOutputStream(new MessageFormatOptions() { DefaultContentType = "application/json" }, null, Stream.Null)
+ is JsonFormatWriter);
+ }
+ [Test]
+ public void TestReadXmlMimeTypes()
+ {
+ foreach (string type in XmlTypes)
+ {
+ Assert.IsTrue(
+ MessageFormatFactory.CreateInputStream(new MessageFormatOptions(), type, Stream.Null)
+ is XmlFormatReader);
+ }
+ Assert.IsTrue(
+ MessageFormatFactory.CreateInputStream(new MessageFormatOptions() { DefaultContentType = "application/xml" }, null, Stream.Null)
+ is XmlFormatReader);
+ }
+ [Test]
+ public void TestWriteXmlMimeTypes()
+ {
+ foreach (string type in XmlTypes)
+ {
+ Assert.IsTrue(
+ MessageFormatFactory.CreateOutputStream(new MessageFormatOptions(), type, Stream.Null)
+ is XmlFormatWriter);
+ }
+ Assert.IsTrue(
+ MessageFormatFactory.CreateOutputStream(new MessageFormatOptions() { DefaultContentType = "application/xml" }, null, Stream.Null)
+ is XmlFormatWriter);
+ }
+ [Test]
+ public void TestReadProtoMimeTypes()
+ {
+ foreach (string type in ProtobufTypes)
+ {
+ Assert.IsTrue(
+ MessageFormatFactory.CreateInputStream(new MessageFormatOptions(), type, Stream.Null)
+ is CodedInputStream);
+ }
+ Assert.IsTrue(
+ MessageFormatFactory.CreateInputStream(new MessageFormatOptions() { DefaultContentType = "application/vnd.google.protobuf" }, null, Stream.Null)
+ is CodedInputStream);
+ }
+ [Test]
+ public void TestWriteProtoMimeTypes()
+ {
+ foreach (string type in ProtobufTypes)
+ {
+ Assert.IsTrue(
+ MessageFormatFactory.CreateOutputStream(new MessageFormatOptions(), type, Stream.Null)
+ is CodedOutputStream);
+ }
+ Assert.IsTrue(
+ MessageFormatFactory.CreateOutputStream(new MessageFormatOptions() { DefaultContentType = "application/vnd.google.protobuf" }, null, Stream.Null)
+ is CodedOutputStream);
+ }
+ [Test]
+ public void TestMergeFromJsonType()
+ {
+ TestXmlMessage msg = new TestXmlMessage.Builder().MergeFrom(
+ new MessageFormatOptions(), "application/json", new MemoryStream(Encoding.ASCII.GetBytes(
+ TestXmlMessage.CreateBuilder().SetText("a").SetNumber(1).Build().ToJson()
+ )))
+ .Build();
+ Assert.AreEqual("a", msg.Text);
+ Assert.AreEqual(1, msg.Number);
+ }
+ [Test]
+ public void TestMergeFromXmlType()
+ {
+ TestXmlMessage msg = new TestXmlMessage.Builder().MergeFrom(
+ new MessageFormatOptions(), "application/xml", new MemoryStream(Encoding.ASCII.GetBytes(
+ TestXmlMessage.CreateBuilder().SetText("a").SetNumber(1).Build().ToXml()
+ )))
+ .Build();
+ Assert.AreEqual("a", msg.Text);
+ Assert.AreEqual(1, msg.Number);
+ }
+ [Test]
+ public void TestMergeFromProtoType()
+ {
+ TestXmlMessage msg = new TestXmlMessage.Builder().MergeFrom(
+ new MessageFormatOptions(), "application/vnd.google.protobuf", new MemoryStream(
+ TestXmlMessage.CreateBuilder().SetText("a").SetNumber(1).Build().ToByteArray()
+ ))
+ .Build();
+ Assert.AreEqual("a", msg.Text);
+ Assert.AreEqual(1, msg.Number);
+ }
+ [Test]
+ public void TestWriteToJsonType()
+ {
+ MemoryStream ms = new MemoryStream();
+ TestXmlMessage.CreateBuilder().SetText("a").SetNumber(1).Build()
+ .WriteTo(new MessageFormatOptions(), "application/json", ms);
+
+ Assert.AreEqual(@"{""text"":""a"",""number"":1}", Encoding.UTF8.GetString(ms.ToArray()));
+ }
+ [Test]
+ public void TestWriteToXmlType()
+ {
+ MemoryStream ms = new MemoryStream();
+ TestXmlMessage.CreateBuilder().SetText("a").SetNumber(1).Build()
+ .WriteTo(new MessageFormatOptions(), "application/xml", ms);
+
+ Assert.AreEqual("<root><text>a</text><number>1</number></root>", Encoding.UTF8.GetString(ms.ToArray()));
+ }
+ [Test]
+ public void TestWriteToProtoType()
+ {
+ MemoryStream ms = new MemoryStream();
+ TestXmlMessage.CreateBuilder().SetText("a").SetNumber(1).Build()
+ .WriteTo(new MessageFormatOptions(), "application/vnd.google.protobuf", ms);
+
+ byte[] bytes = TestXmlMessage.CreateBuilder().SetText("a").SetNumber(1).Build().ToByteArray();
+ Assert.AreEqual(bytes, ms.ToArray());
+ }
+ [Test]
+ public void TestXmlReaderOptions()
+ {
+ MemoryStream ms = new MemoryStream();
+ XmlFormatWriter.CreateInstance(ms)
+ .SetOptions(XmlWriterOptions.OutputNestedArrays)
+ .WriteMessage("my-root-node", TestXmlMessage.CreateBuilder().SetText("a").AddNumbers(1).AddNumbers(2).Build());
+ ms.Position = 0;
+
+ MessageFormatOptions options = new MessageFormatOptions()
+ {
+ XmlReaderOptions = XmlReaderOptions.ReadNestedArrays,
+ XmlReaderRootElementName = "my-root-node"
+ };
+
+ TestXmlMessage msg = new TestXmlMessage.Builder().MergeFrom(
+ options, "application/xml", ms)
+ .Build();
+
+ Assert.AreEqual("a", msg.Text);
+ Assert.AreEqual(1, msg.NumbersList[0]);
+ Assert.AreEqual(2, msg.NumbersList[1]);
+
+ }
+ [Test]
+ public void TestXmlWriterOptions()
+ {
+ TestXmlMessage message = TestXmlMessage.CreateBuilder().SetText("a").AddNumbers(1).AddNumbers(2).Build();
+ MessageFormatOptions options = new MessageFormatOptions()
+ {
+ XmlWriterOptions = XmlWriterOptions.OutputNestedArrays,
+ XmlWriterRootElementName = "root-node"
+ };
+
+ MemoryStream ms = new MemoryStream();
+ message.WriteTo(options, "application/xml", ms);
+ ms.Position = 0;
+
+ TestXmlMessage.Builder builder = TestXmlMessage.CreateBuilder();
+ XmlFormatReader.CreateInstance(ms)
+ .SetOptions(XmlReaderOptions.ReadNestedArrays)
+ .Merge("root-node", builder);
+
+ Assert.AreEqual("a", builder.Text);
+ Assert.AreEqual(1, builder.NumbersList[0]);
+ Assert.AreEqual(2, builder.NumbersList[1]);
+ }
+ [Test]
+ public void TestJsonFormatted()
+ {
+ MemoryStream ms = new MemoryStream();
+ TestXmlMessage.CreateBuilder().SetText("a").SetNumber(1).Build()
+ .WriteTo(new MessageFormatOptions() { FormattedOutput = true }, "application/json", ms);
+
+ Assert.AreEqual("{\r\n \"text\": \"a\",\r\n \"number\": 1\r\n}", Encoding.UTF8.GetString(ms.ToArray()));
+ }
+ [Test]
+ public void TestXmlFormatted()
+ {
+ MemoryStream ms = new MemoryStream();
+ TestXmlMessage.CreateBuilder().SetText("a").SetNumber(1).Build()
+ .WriteTo(new MessageFormatOptions() { FormattedOutput = true }, "application/xml", ms);
+
+ Assert.AreEqual("<root>\r\n <text>a</text>\r\n <number>1</number>\r\n</root>", Encoding.UTF8.GetString(ms.ToArray()));
+ }
+ }
+}
\ No newline at end of file diff --git a/src/ProtocolBuffers.Test/TestWriterFormatJson.cs b/src/ProtocolBuffers.Test/TestWriterFormatJson.cs index 341f0b3c..1958df0d 100644 --- a/src/ProtocolBuffers.Test/TestWriterFormatJson.cs +++ b/src/ProtocolBuffers.Test/TestWriterFormatJson.cs @@ -45,7 +45,7 @@ namespace Google.ProtocolBuffers using (TextWriter output = new StringWriter())
using (AbstractWriter writer = JsonFormatWriter.CreateInstance(output))
{
- writer.StartMessage(); //manually begin the message, output is '{'
+ writer.WriteMessageStart(); //manually begin the message, output is '{'
writer.Flush();
Assert.AreEqual("{", output.ToString());
@@ -56,7 +56,7 @@ namespace Google.ProtocolBuffers writer.Flush();
Assert.AreEqual(@"{""valid"":true", output.ToString());
- writer.EndMessage(); //manually write the end message '}'
+ writer.WriteMessageEnd(); //manually write the end message '}'
Assert.AreEqual(@"{""valid"":true}", output.ToString());
}
}
@@ -65,13 +65,13 @@ namespace Google.ProtocolBuffers public void Example_ReadJsonUsingICodedInputStream()
{
TestXmlMessage.Builder builder = TestXmlMessage.CreateBuilder();
- AbstractReader reader = JsonFormatReader.CreateInstance(@"{""valid"":true}");
+ ICodedInputStream reader = JsonFormatReader.CreateInstance(@"{""valid"":true}");
- AbstractReader stream = reader.ReadStartMessage(); //manually read the begin the message '{'
+ reader.ReadMessageStart(); //manually read the begin the message '{'
- builder.MergeFrom(stream); //write the message normally
+ builder.MergeFrom(reader); //write the message normally
- stream.ReadEndMessage(); //manually read the end message '}'
+ reader.ReadMessageEnd(); //manually read the end message '}'
}
protected string Content;
@@ -401,6 +401,28 @@ namespace Google.ProtocolBuffers Assert.AreEqual(3, ordinal);
Assert.AreEqual(3, builder.TextlinesCount);
}
+ [Test]
+ public void TestReadWriteJsonWithoutRoot()
+ {
+ TestXmlMessage.Builder builder = TestXmlMessage.CreateBuilder();
+ TestXmlMessage message = builder.SetText("abc").SetNumber(123).Build();
+
+ string Json;
+ using (StringWriter sw = new StringWriter())
+ {
+ ICodedOutputStream output = JsonFormatWriter.CreateInstance(sw);
+
+ message.WriteTo(output);
+ output.Flush();
+ Json = sw.ToString();
+ }
+ Assert.AreEqual(@"""text"":""abc"",""number"":123", Json);
+
+ ICodedInputStream input = JsonFormatReader.CreateInstance(Json);
+ TestXmlMessage copy = TestXmlMessage.CreateBuilder().MergeFrom(input).Build();
+
+ Assert.AreEqual(message, copy);
+ }
[Test,ExpectedException(typeof(RecursionLimitExceededException))]
public void TestRecursiveLimit()
{
diff --git a/src/ProtocolBuffers.Test/TestWriterFormatXml.cs b/src/ProtocolBuffers.Test/TestWriterFormatXml.cs index d56cc525..f401481e 100644 --- a/src/ProtocolBuffers.Test/TestWriterFormatXml.cs +++ b/src/ProtocolBuffers.Test/TestWriterFormatXml.cs @@ -48,12 +48,12 @@ namespace Google.ProtocolBuffers using (TextWriter output = new StringWriter())
using (AbstractWriter writer = XmlFormatWriter.CreateInstance(output))
{
- writer.StartMessage(); //manually begin the message, output is '{'
+ writer.WriteMessageStart(); //manually begin the message, output is '{'
ICodedOutputStream stream = writer;
- message.WriteTo(stream); //write the message normally
+ message.WriteTo(stream); //write the message normally
- writer.EndMessage(); //manually write the end message '}'
+ writer.WriteMessageEnd(); //manually write the end message '}'
Assert.AreEqual(@"<root><valid>true</valid></root>", output.ToString());
}
}
@@ -62,13 +62,13 @@ namespace Google.ProtocolBuffers public void Example_ReadXmlUsingICodedInputStream()
{
TestXmlMessage.Builder builder = TestXmlMessage.CreateBuilder();
- AbstractReader reader = XmlFormatReader.CreateInstance(@"<root><valid>true</valid></root>");
+ ICodedInputStream reader = XmlFormatReader.CreateInstance(@"<root><valid>true</valid></root>");
- AbstractReader stream = reader.ReadStartMessage(); //manually read the begin the message '{'
+ reader.ReadMessageStart(); //manually read the begin the message '{'
- builder.MergeFrom(stream); //write the message normally
+ builder.MergeFrom(reader); //read the message normally
- stream.ReadEndMessage(); //manually read the end message '}'
+ reader.ReadMessageEnd(); //manually read the end message '}'
}
[Test]
@@ -387,13 +387,54 @@ namespace Google.ProtocolBuffers public void TestXmlReadEmptyRoot()
{
TestXmlMessage.Builder builder = TestXmlMessage.CreateBuilder();
- AbstractReader reader = XmlFormatReader.CreateInstance(@"<root/>");
+ ICodedInputStream reader = XmlFormatReader.CreateInstance(@"<root/>");
- AbstractReader stream = reader.ReadStartMessage(); //manually read the begin the message '{'
+ reader.ReadMessageStart(); //manually read the begin the message '{'
- builder.MergeFrom(stream); //write the message normally
+ builder.MergeFrom(reader); //write the message normally
- stream.ReadEndMessage(); //manually read the end message '}'
+ reader.ReadMessageEnd(); //manually read the end message '}'
+ }
+
+ [Test]
+ public void TestXmlReadEmptyChild()
+ {
+ TestXmlMessage.Builder builder = TestXmlMessage.CreateBuilder();
+ ICodedInputStream reader = XmlFormatReader.CreateInstance(@"<root><text /></root>");
+
+ reader.ReadMessageStart(); //manually read the begin the message '{'
+
+ builder.MergeFrom(reader); //write the message normally
+ Assert.IsTrue(builder.HasText);
+ Assert.AreEqual(String.Empty, builder.Text);
+ }
+
+ [Test]
+ public void TestXmlReadWriteWithoutRoot()
+ {
+ TestXmlMessage.Builder builder = TestXmlMessage.CreateBuilder();
+ TestXmlMessage message = builder.SetText("abc").SetNumber(123).Build();
+
+ string xml;
+ using (StringWriter sw = new StringWriter())
+ {
+ ICodedOutputStream output = XmlFormatWriter.CreateInstance(
+ XmlWriter.Create(sw, new XmlWriterSettings() { ConformanceLevel = ConformanceLevel.Fragment }));
+
+ message.WriteTo(output);
+ output.Flush();
+ xml = sw.ToString();
+ }
+ Assert.AreEqual("<text>abc</text><number>123</number>", xml);
+
+ TestXmlMessage copy;
+ using (XmlReader xr = XmlReader.Create(new StringReader(xml), new XmlReaderSettings() { ConformanceLevel = ConformanceLevel.Fragment }))
+ {
+ ICodedInputStream input = XmlFormatReader.CreateInstance(xr);
+ copy = TestXmlMessage.CreateBuilder().MergeFrom(input).Build();
+ }
+
+ Assert.AreEqual(message, copy);
}
[Test, ExpectedException(typeof(RecursionLimitExceededException))]
|