Preserve the spec version attribute when clearing CloudEventAttributes
Signed-off-by: Jon Skeet <jonskeet@google.com>
This commit is contained in:
parent
0441d93593
commit
c95871b782
|
@ -232,7 +232,12 @@ namespace CloudNative.CloudEvents
|
|||
|
||||
void ICollection<KeyValuePair<string, object>>.Clear()
|
||||
{
|
||||
// Clearing the collection doesn't remove the spec version attribute.
|
||||
// Preserve it, clear the dictionary, then put it back.
|
||||
string specAttributeName = SpecVersionAttributeName(this.SpecVersion);
|
||||
string specAttributeValue = (string) this[specAttributeName];
|
||||
dict.Clear();
|
||||
dict[specAttributeName] = specAttributeValue;
|
||||
}
|
||||
|
||||
bool ICollection<KeyValuePair<string, object>>.Contains(KeyValuePair<string, object> item)
|
||||
|
|
|
@ -46,5 +46,21 @@ namespace CloudNative.CloudEvents.UnitTests
|
|||
var pair = KeyValuePair.Create(attributeName, default(object));
|
||||
Assert.Throws<InvalidOperationException>(() => attributes.Add(pair));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Clear_PreservesSpecVersion()
|
||||
{
|
||||
IDictionary<string, object> attributes = new CloudEventAttributes(CloudEventsSpecVersion.Default, emptyExtensions);
|
||||
string specVersionAttributeName = CloudEventAttributes.SpecVersionAttributeName();
|
||||
string specVersionValue = (string) attributes[specVersionAttributeName];
|
||||
attributes[CloudEventAttributes.TypeAttributeName()] = "some event type";
|
||||
Assert.Equal(2, attributes.Count);
|
||||
attributes.Clear();
|
||||
|
||||
// We'd normally expect an empty dictionary now, but CloudEventAttributes always preserves the spec version.
|
||||
var entry = Assert.Single(attributes);
|
||||
Assert.Equal(CloudEventAttributes.SpecVersionAttributeName(), entry.Key);
|
||||
Assert.Equal(specVersionValue, entry.Value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue