diff --git a/src/csharp/Grpc.Core.Api/Metadata.cs b/src/csharp/Grpc.Core.Api/Metadata.cs index 376ccc531b7..9c564766568 100644 --- a/src/csharp/Grpc.Core.Api/Metadata.cs +++ b/src/csharp/Grpc.Core.Api/Metadata.cs @@ -76,7 +76,8 @@ namespace Grpc.Core } /// - /// Gets the last metadata entry with the specified key. If there are no matching entries then null is returned. + /// Gets the last metadata entry with the specified key. + /// If there are no matching entries then null is returned. /// public Entry Get(string key) { @@ -92,7 +93,9 @@ namespace Grpc.Core } /// - /// Gets the string value of the last metadata entry with the specified key. If there are no matching entries then null is returned. + /// Gets the string value of the last metadata entry with the specified key. + /// If the metadata entry is binary then an exception is thrown. + /// If there are no matching entries then null is returned. /// public string GetValue(string key) { @@ -100,7 +103,9 @@ namespace Grpc.Core } /// - /// Gets the bytes value of the last metadata entry with the specified key. If there are no matching entries then null is returned. + /// Gets the bytes value of the last metadata entry with the specified key. + /// If the metadata entry is not binary the string value will be returned as ASCII encoded bytes. + /// If there are no matching entries then null is returned. /// public byte[] GetValueBytes(string key) { @@ -324,6 +329,7 @@ namespace Grpc.Core /// /// Gets the binary value of this metadata entry. + /// If the metadata entry is not binary the string value will be returned as ASCII encoded bytes. /// public byte[] ValueBytes { @@ -343,13 +349,14 @@ namespace Grpc.Core /// /// Gets the string value of this metadata entry. + /// If the metadata entry is binary then an exception is thrown. /// public string Value { get { GrpcPreconditions.CheckState(!IsBinary, "Cannot access string value of a binary metadata entry"); - return value ?? EncodingASCII.GetString(valueBytes); + return value; } } diff --git a/src/csharp/Grpc.Core.Tests/MetadataTest.cs b/src/csharp/Grpc.Core.Tests/MetadataTest.cs index 0b5f1c76e0c..991f8b6f91b 100644 --- a/src/csharp/Grpc.Core.Tests/MetadataTest.cs +++ b/src/csharp/Grpc.Core.Tests/MetadataTest.cs @@ -18,7 +18,9 @@ using System; using System.Diagnostics; +using System.Linq; using System.Runtime.InteropServices; +using System.Text; using System.Threading; using System.Threading.Tasks; using Grpc.Core; @@ -242,6 +244,110 @@ namespace Grpc.Core.Tests Assert.Throws(() => metadata.Remove(metadata[0])); } + [Test] + public void GetAll() + { + var metadata = new Metadata + { + { "abc", "abc-value1" }, + { "abc", "abc-value2" }, + { "xyz", "xyz-value1" }, + }; + + var abcEntries = metadata.GetAll("abc").ToList(); + Assert.AreEqual(2, abcEntries.Count); + Assert.AreEqual("abc-value1", abcEntries[0].Value); + Assert.AreEqual("abc-value2", abcEntries[1].Value); + + var xyzEntries = metadata.GetAll("xyz").ToList(); + Assert.AreEqual(1, xyzEntries.Count); + Assert.AreEqual("xyz-value1", xyzEntries[0].Value); + } + + [Test] + public void Get() + { + var metadata = new Metadata + { + { "abc", "abc-value1" }, + { "abc", "abc-value2" }, + { "xyz", "xyz-value1" }, + }; + + var abcEntry = metadata.Get("abc"); + Assert.AreEqual("abc-value2", abcEntry.Value); + + var xyzEntry = metadata.Get("xyz"); + Assert.AreEqual("xyz-value1", xyzEntry.Value); + + var notFound = metadata.Get("not-found"); + Assert.AreEqual(null, notFound); + } + + [Test] + public void GetValue() + { + var metadata = new Metadata + { + { "abc", "abc-value1" }, + { "abc", "abc-value2" }, + { "xyz", "xyz-value1" }, + { "xyz-bin", Encoding.ASCII.GetBytes("xyz-value1") }, + }; + + var abcEntry = metadata.GetValue("abc"); + Assert.AreEqual("abc-value2", abcEntry); + + var xyzEntry = metadata.GetValue("xyz"); + Assert.AreEqual("xyz-value1", xyzEntry); + + var notFound = metadata.GetValue("not-found"); + Assert.AreEqual(null, notFound); + } + + [Test] + public void GetValue_BytesValue() + { + var metadata = new Metadata + { + { "xyz-bin", Encoding.ASCII.GetBytes("xyz-value1") }, + }; + + Assert.Throws(() => metadata.GetValue("xyz-bin")); + } + + [Test] + public void GetValueBytes() + { + var metadata = new Metadata + { + { "abc-bin", Encoding.ASCII.GetBytes("abc-value1") }, + { "abc-bin", Encoding.ASCII.GetBytes("abc-value2") }, + { "xyz-bin", Encoding.ASCII.GetBytes("xyz-value1") }, + }; + + var abcEntry = metadata.GetValueBytes("abc-bin"); + Assert.AreEqual(Encoding.ASCII.GetBytes("abc-value2"), abcEntry); + + var xyzEntry = metadata.GetValueBytes("xyz-bin"); + Assert.AreEqual(Encoding.ASCII.GetBytes("xyz-value1"), xyzEntry); + + var notFound = metadata.GetValueBytes("not-found"); + Assert.AreEqual(null, notFound); + } + + [Test] + public void GetValueBytes_StringValue() + { + var metadata = new Metadata + { + { "xyz", "xyz-value1" }, + }; + + var xyzEntry = metadata.GetValueBytes("xyz"); + Assert.AreEqual(Encoding.ASCII.GetBytes("xyz-value1"), xyzEntry); + } + private Metadata CreateMetadata() { return new Metadata