pull/22743/head
James Newton-King 5 years ago
parent b8f2034c35
commit bb557bb6ff
No known key found for this signature in database
GPG Key ID: A66B2F456BF5526
  1. 15
      src/csharp/Grpc.Core.Api/Metadata.cs
  2. 106
      src/csharp/Grpc.Core.Tests/MetadataTest.cs

@ -76,7 +76,8 @@ namespace Grpc.Core
}
/// <summary>
/// Gets the last metadata entry with the specified key. If there are no matching entries then <c>null</c> is returned.
/// Gets the last metadata entry with the specified key.
/// If there are no matching entries then <c>null</c> is returned.
/// </summary>
public Entry Get(string key)
{
@ -92,7 +93,9 @@ namespace Grpc.Core
}
/// <summary>
/// Gets the string value of the last metadata entry with the specified key. If there are no matching entries then <c>null</c> 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 <c>null</c> is returned.
/// </summary>
public string GetValue(string key)
{
@ -100,7 +103,9 @@ namespace Grpc.Core
}
/// <summary>
/// Gets the bytes value of the last metadata entry with the specified key. If there are no matching entries then <c>null</c> 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 <c>null</c> is returned.
/// </summary>
public byte[] GetValueBytes(string key)
{
@ -324,6 +329,7 @@ namespace Grpc.Core
/// <summary>
/// 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.
/// </summary>
public byte[] ValueBytes
{
@ -343,13 +349,14 @@ namespace Grpc.Core
/// <summary>
/// Gets the string value of this metadata entry.
/// If the metadata entry is binary then an exception is thrown.
/// </summary>
public string Value
{
get
{
GrpcPreconditions.CheckState(!IsBinary, "Cannot access string value of a binary metadata entry");
return value ?? EncodingASCII.GetString(valueBytes);
return value;
}
}

@ -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<InvalidOperationException>(() => 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<InvalidOperationException>(() => 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

Loading…
Cancel
Save