metadata: add Delete method to MD (#4549)

This commit is contained in:
Konrad Reiche 2021-06-16 16:56:04 -07:00 committed by GitHub
parent 4c651eda23
commit 7e35356501
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 36 additions and 0 deletions

View File

@ -123,6 +123,13 @@ func (md MD) Append(k string, vals ...string) {
md[k] = append(md[k], vals...)
}
// Delete removes the values for a given key k which is converted to lowercase
// before removing it from md.
func (md MD) Delete(k string) {
k = strings.ToLower(k)
delete(md, k)
}
// Join joins any number of mds into a single MD.
//
// The order of values for each key is determined by the order in which the mds

View File

@ -169,6 +169,35 @@ func (s) TestAppend(t *testing.T) {
}
}
func (s) TestDelete(t *testing.T) {
for _, test := range []struct {
md MD
deleteKey string
want MD
}{
{
md: Pairs("My-Optional-Header", "42"),
deleteKey: "My-Optional-Header",
want: Pairs(),
},
{
md: Pairs("My-Optional-Header", "42"),
deleteKey: "Other-Key",
want: Pairs("my-optional-header", "42"),
},
{
md: Pairs("My-Optional-Header", "42"),
deleteKey: "my-OptIoNal-HeAder",
want: Pairs(),
},
} {
test.md.Delete(test.deleteKey)
if !reflect.DeepEqual(test.md, test.want) {
t.Errorf("value of metadata is %v, want %v", test.md, test.want)
}
}
}
func (s) TestAppendToOutgoingContext(t *testing.T) {
// Pre-existing metadata
tCtx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)