1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
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()));
}
}
}
|