diff options
author | Jan Tattermusch <jtattermusch@users.noreply.github.com> | 2015-04-17 14:15:52 -0700 |
---|---|---|
committer | Jan Tattermusch <jtattermusch@users.noreply.github.com> | 2015-04-17 14:15:52 -0700 |
commit | 01c8c35ca3d3424a9473dd22ea2456933b3e8d36 (patch) | |
tree | de582cb9ef3e449f24015dec09f80e998806b26a /csharp/src/ProtocolBuffers.Test/TestReaderForUrlEncoded.cs | |
parent | 813d6d652d8091e9365bfbd01efb5f2ee8bdfbce (diff) | |
parent | 1eab56aee8dddcb656db67d9fb87a8cc6a2ac112 (diff) | |
download | protobuf-01c8c35ca3d3424a9473dd22ea2456933b3e8d36.tar.gz protobuf-01c8c35ca3d3424a9473dd22ea2456933b3e8d36.tar.bz2 protobuf-01c8c35ca3d3424a9473dd22ea2456933b3e8d36.zip |
Merge pull request #2 from jtattermusch/csharp_protobuf_imported
Pulling protobuf-csharp-port into csharp/ directory
Diffstat (limited to 'csharp/src/ProtocolBuffers.Test/TestReaderForUrlEncoded.cs')
-rw-r--r-- | csharp/src/ProtocolBuffers.Test/TestReaderForUrlEncoded.cs | 84 |
1 files changed, 84 insertions, 0 deletions
diff --git a/csharp/src/ProtocolBuffers.Test/TestReaderForUrlEncoded.cs b/csharp/src/ProtocolBuffers.Test/TestReaderForUrlEncoded.cs new file mode 100644 index 00000000..1c43e24d --- /dev/null +++ b/csharp/src/ProtocolBuffers.Test/TestReaderForUrlEncoded.cs @@ -0,0 +1,84 @@ +using System;
+using System.IO;
+using System.Text;
+using Microsoft.VisualStudio.TestTools.UnitTesting;
+using Google.ProtocolBuffers.TestProtos;
+using Google.ProtocolBuffers.Serialization.Http;
+
+namespace Google.ProtocolBuffers
+{
+ [TestClass]
+ public class TestReaderForUrlEncoded
+ {
+ [TestMethod]
+ public void Example_FromQueryString()
+ {
+ Uri sampleUri = new Uri("http://sample.com/Path/File.ext?text=two+three%20four&valid=true&numbers=1&numbers=2", UriKind.Absolute);
+
+ ICodedInputStream input = FormUrlEncodedReader.CreateInstance(sampleUri.Query);
+
+ TestXmlMessage.Builder builder = TestXmlMessage.CreateBuilder();
+ builder.MergeFrom(input);
+
+ TestXmlMessage message = builder.Build();
+ Assert.AreEqual(true, message.Valid);
+ Assert.AreEqual("two three four", message.Text);
+ Assert.AreEqual(2, message.NumbersCount);
+ Assert.AreEqual(1, message.NumbersList[0]);
+ Assert.AreEqual(2, message.NumbersList[1]);
+ }
+
+ [TestMethod]
+ public void Example_FromFormData()
+ {
+ Stream rawPost = new MemoryStream(Encoding.UTF8.GetBytes("text=two+three%20four&valid=true&numbers=1&numbers=2"), false);
+
+ ICodedInputStream input = FormUrlEncodedReader.CreateInstance(rawPost);
+
+ TestXmlMessage.Builder builder = TestXmlMessage.CreateBuilder();
+ builder.MergeFrom(input);
+
+ TestXmlMessage message = builder.Build();
+ Assert.AreEqual(true, message.Valid);
+ Assert.AreEqual("two three four", message.Text);
+ Assert.AreEqual(2, message.NumbersCount);
+ Assert.AreEqual(1, message.NumbersList[0]);
+ Assert.AreEqual(2, message.NumbersList[1]);
+ }
+
+ [TestMethod]
+ public void TestEmptyValues()
+ {
+ ICodedInputStream input = FormUrlEncodedReader.CreateInstance("valid=true&text=&numbers=1");
+ TestXmlMessage.Builder builder = TestXmlMessage.CreateBuilder();
+ builder.MergeFrom(input);
+
+ Assert.IsTrue(builder.Valid);
+ Assert.IsTrue(builder.HasText);
+ Assert.AreEqual("", builder.Text);
+ Assert.AreEqual(1, builder.NumbersCount);
+ Assert.AreEqual(1, builder.NumbersList[0]);
+ }
+
+ [TestMethod]
+ public void TestNoValue()
+ {
+ ICodedInputStream input = FormUrlEncodedReader.CreateInstance("valid=true&text&numbers=1");
+ TestXmlMessage.Builder builder = TestXmlMessage.CreateBuilder();
+ builder.MergeFrom(input);
+
+ Assert.IsTrue(builder.Valid);
+ Assert.IsTrue(builder.HasText);
+ Assert.AreEqual("", builder.Text);
+ Assert.AreEqual(1, builder.NumbersCount);
+ Assert.AreEqual(1, builder.NumbersList[0]);
+ }
+
+ [TestMethod, ExpectedException(typeof(NotSupportedException))]
+ public void FormUrlEncodedReaderDoesNotSupportChildren()
+ {
+ ICodedInputStream input = FormUrlEncodedReader.CreateInstance("child=uh0");
+ TestXmlMessage.CreateBuilder().MergeFrom(input);
+ }
+ }
+}
|