diff options
author | Jon Skeet <skeet@pobox.com> | 2016-05-06 06:29:03 +0100 |
---|---|---|
committer | Jon Skeet <skeet@pobox.com> | 2016-05-06 06:29:03 +0100 |
commit | 5b5e369b185325c49ee450965ceceb6ac5586b16 (patch) | |
tree | 5dac86476f4d2cafa84333d3c1114a9632c71395 /csharp/src/Google.Protobuf.Test | |
parent | 17b6fc31eb8a9b65501cd7c53235a88efdc94394 (diff) | |
parent | 61307b86f561721ac47ecd74d9fd1dffa0acad53 (diff) | |
download | protobuf-5b5e369b185325c49ee450965ceceb6ac5586b16.tar.gz protobuf-5b5e369b185325c49ee450965ceceb6ac5586b16.tar.bz2 protobuf-5b5e369b185325c49ee450965ceceb6ac5586b16.zip |
Merge pull request #1471 from jskeet/any-host
Allow custom type URL prefixes in Any.Pack
Diffstat (limited to 'csharp/src/Google.Protobuf.Test')
-rw-r--r-- | csharp/src/Google.Protobuf.Test/JsonFormatterTest.cs | 9 | ||||
-rw-r--r-- | csharp/src/Google.Protobuf.Test/JsonParserTest.cs | 11 | ||||
-rw-r--r-- | csharp/src/Google.Protobuf.Test/WellKnownTypes/AnyTest.cs | 27 |
3 files changed, 47 insertions, 0 deletions
diff --git a/csharp/src/Google.Protobuf.Test/JsonFormatterTest.cs b/csharp/src/Google.Protobuf.Test/JsonFormatterTest.cs index 0b6cd7e6..827a7595 100644 --- a/csharp/src/Google.Protobuf.Test/JsonFormatterTest.cs +++ b/csharp/src/Google.Protobuf.Test/JsonFormatterTest.cs @@ -481,6 +481,15 @@ namespace Google.Protobuf } [Test] + public void AnyMessageType_CustomPrefix() + { + var formatter = new JsonFormatter(new JsonFormatter.Settings(false, TypeRegistry.FromMessages(TestAllTypes.Descriptor))); + var message = new TestAllTypes { SingleInt32 = 10 }; + var any = Any.Pack(message, "foo.bar/baz"); + AssertJson("{ '@type': 'foo.bar/baz/protobuf_unittest.TestAllTypes', 'singleInt32': 10 }", formatter.Format(any)); + } + + [Test] public void AnyNested() { var registry = TypeRegistry.FromMessages(TestWellKnownTypes.Descriptor, TestAllTypes.Descriptor); diff --git a/csharp/src/Google.Protobuf.Test/JsonParserTest.cs b/csharp/src/Google.Protobuf.Test/JsonParserTest.cs index 684f52b4..c3ad851b 100644 --- a/csharp/src/Google.Protobuf.Test/JsonParserTest.cs +++ b/csharp/src/Google.Protobuf.Test/JsonParserTest.cs @@ -811,6 +811,17 @@ namespace Google.Protobuf } [Test] + public void Any_CustomPrefix() + { + var registry = TypeRegistry.FromMessages(TestAllTypes.Descriptor); + var message = new TestAllTypes { SingleInt32 = 10 }; + var original = Any.Pack(message, "custom.prefix/middle-part"); + var parser = new JsonParser(new JsonParser.Settings(10, registry)); + string json = "{ \"@type\": \"custom.prefix/middle-part/protobuf_unittest.TestAllTypes\", \"singleInt32\": 10 }"; + Assert.AreEqual(original, parser.Parse<Any>(json)); + } + + [Test] public void Any_UnknownType() { string json = "{ \"@type\": \"type.googleapis.com/bogus\" }"; diff --git a/csharp/src/Google.Protobuf.Test/WellKnownTypes/AnyTest.cs b/csharp/src/Google.Protobuf.Test/WellKnownTypes/AnyTest.cs index f3593e5f..f21be7d9 100644 --- a/csharp/src/Google.Protobuf.Test/WellKnownTypes/AnyTest.cs +++ b/csharp/src/Google.Protobuf.Test/WellKnownTypes/AnyTest.cs @@ -47,6 +47,24 @@ namespace Google.Protobuf.WellKnownTypes } [Test] + public void Pack_WithCustomPrefix() + { + var message = SampleMessages.CreateFullTestAllTypes(); + var any = Any.Pack(message, "foo.bar/baz"); + Assert.AreEqual("foo.bar/baz/protobuf_unittest.TestAllTypes", any.TypeUrl); + Assert.AreEqual(message.CalculateSize(), any.Value.Length); + } + + [Test] + public void Pack_WithCustomPrefixTrailingSlash() + { + var message = SampleMessages.CreateFullTestAllTypes(); + var any = Any.Pack(message, "foo.bar/baz/"); + Assert.AreEqual("foo.bar/baz/protobuf_unittest.TestAllTypes", any.TypeUrl); + Assert.AreEqual(message.CalculateSize(), any.Value.Length); + } + + [Test] public void Unpack_WrongType() { var message = SampleMessages.CreateFullTestAllTypes(); @@ -64,6 +82,15 @@ namespace Google.Protobuf.WellKnownTypes } [Test] + public void Unpack_CustomPrefix_Success() + { + var message = SampleMessages.CreateFullTestAllTypes(); + var any = Any.Pack(message, "foo.bar/baz"); + var unpacked = any.Unpack<TestAllTypes>(); + Assert.AreEqual(message, unpacked); + } + + [Test] public void ToString_WithValues() { var message = SampleMessages.CreateFullTestAllTypes(); |