Use DateTimeOffset for timestamp attributes

Also, format those timestamps using the Timestamps class introduced previously.

Fixes #61
Fixes #57
Fixes #48

Signed-off-by: Jon Skeet <jonskeet@google.com>
This commit is contained in:
Jon Skeet 2021-01-22 15:12:50 +00:00 committed by Jon Skeet
parent d5310c94d0
commit d234dfc4de
18 changed files with 173 additions and 151 deletions

View File

@ -73,13 +73,14 @@ namespace CloudNative.CloudEvents.Amqp
: CloudEventsSpecVersion.Default))
: CloudEventsSpecVersion.Default;
var cloudEvent = new CloudEvent(specVersion , extensions);
var cloudEvent = new CloudEvent(specVersion, extensions);
var attributes = cloudEvent.GetAttributes();
foreach (var prop in message.ApplicationProperties.Map)
{
if (prop.Key is string &&
((string)prop.Key).StartsWith(AmqpHeaderPrefix, StringComparison.InvariantCultureIgnoreCase))
if (prop.Key is string key &&
key.StartsWith(AmqpHeaderPrefix, StringComparison.InvariantCultureIgnoreCase))
{
string attrName = key.Substring(AmqpHeaderPrefix.Length).ToLowerInvariant();
if (cloudEvent.SpecVersion != CloudEventsSpecVersion.V1_0 && prop.Value is Map)
{
IDictionary<string, object> exp = new ExpandoObject();
@ -88,13 +89,22 @@ namespace CloudNative.CloudEvents.Amqp
exp[props.Key.ToString()] = props.Value;
}
attributes[((string)prop.Key).Substring(AmqpHeaderPrefix.Length).ToLowerInvariant()] =
exp;
attributes[attrName] = exp;
}
else if (prop.Value is DateTime dt)
{
if (dt.Kind != DateTimeKind.Utc)
{
// This should only happen for MinValue and MaxValue...
// just respecify as UTC. (We could add validation that it really
// *is* MinValue or MaxValue if we wanted to.)
dt = DateTime.SpecifyKind(dt, DateTimeKind.Utc);
}
attributes[attrName] = (DateTimeOffset) dt;
}
else
{
attributes[((string)prop.Key).Substring(AmqpHeaderPrefix.Length).ToLowerInvariant()] =
prop.Value;
attributes[attrName] = prop.Value;
}
}
}

View File

@ -59,13 +59,20 @@ namespace CloudNative.CloudEvents.Amqp
if (!attribute.Key.Equals(CloudEventAttributes.DataAttributeName(cloudEvent.SpecVersion)) &&
!attribute.Key.Equals(CloudEventAttributes.DataContentTypeAttributeName(cloudEvent.SpecVersion)))
{
string key = "cloudEvents:" + attribute.Key;
if (attribute.Value is Uri)
{
this.ApplicationProperties.Map.Add("cloudEvents:" + attribute.Key, attribute.Value.ToString());
this.ApplicationProperties.Map.Add(key, attribute.Value.ToString());
}
else if (attribute.Value is DateTime || attribute.Value is DateTime || attribute.Value is string)
else if (attribute.Value is DateTimeOffset dto)
{
this.ApplicationProperties.Map.Add("cloudEvents:" + attribute.Key, attribute.Value);
// AMQPNetLite doesn't support DateTimeOffset values, so convert to UTC.
// That means we can't roundtrip events with non-UTC timestamps, but that's not awful.
this.ApplicationProperties.Map.Add(key, dto.UtcDateTime);
}
else if (attribute.Value is string)
{
this.ApplicationProperties.Map.Add(key, attribute.Value);
}
else
{

View File

@ -89,14 +89,14 @@ namespace CloudNative.CloudEvents
{
continue;
}
if (keyValuePair.Key == CloudEventAttributes.SourceAttributeName() ||
keyValuePair.Key == CloudEventAttributes.DataSchemaAttributeName())
if (keyValuePair.Key == CloudEventAttributes.SourceAttributeName(specVersion) ||
keyValuePair.Key == CloudEventAttributes.DataSchemaAttributeName(specVersion))
{
attributes[keyValuePair.Key] = new Uri((string)keyValuePair.Value);
}
else if (keyValuePair.Key == CloudEventAttributes.TimeAttributeName())
else if (keyValuePair.Key == CloudEventAttributes.TimeAttributeName(specVersion))
{
attributes[keyValuePair.Key] = DateTime.Parse((string)keyValuePair.Value);
attributes[keyValuePair.Key] = Timestamps.Parse((string)keyValuePair.Value);
}
else
{
@ -121,24 +121,24 @@ namespace CloudNative.CloudEvents
continue;
}
if (keyValuePair.Value is ContentType && !string.IsNullOrEmpty(((ContentType)keyValuePair.Value).MediaType))
if (keyValuePair.Value is ContentType valueContentType && !string.IsNullOrEmpty(valueContentType.MediaType))
{
recordAttributes[keyValuePair.Key] = ((ContentType)keyValuePair.Value).ToString();
recordAttributes[keyValuePair.Key] = valueContentType.ToString();
}
else if (keyValuePair.Value is Uri)
else if (keyValuePair.Value is Uri uri)
{
recordAttributes[keyValuePair.Key] = ((Uri)keyValuePair.Value).ToString();
recordAttributes[keyValuePair.Key] = uri .ToString();
}
else if (keyValuePair.Value is DateTime)
else if (keyValuePair.Value is DateTimeOffset timestamp)
{
recordAttributes[keyValuePair.Key] = ((DateTime)keyValuePair.Value).ToString("o");
recordAttributes[keyValuePair.Key] = Timestamps.Format(timestamp);
}
else if (cloudEvent.SpecVersion == CloudEventsSpecVersion.V1_0 &&
keyValuePair.Key.Equals(CloudEventAttributes.DataAttributeName(cloudEvent.SpecVersion)))
{
if (keyValuePair.Value is Stream)
if (keyValuePair.Value is Stream stream)
{
using (var sr = new BinaryReader((Stream)keyValuePair.Value))
using (var sr = new BinaryReader(stream))
{
record.Add("data", sr.ReadBytes((int)sr.BaseStream.Length));
}

View File

@ -26,7 +26,7 @@ namespace CloudNative.CloudEvents
/// <param name="id">'id' of the CloudEvent</param>
/// <param name="time">'time' of the CloudEvent</param>
/// <param name="extensions">Extensions to be added to this CloudEvents</param>
public CloudEvent(string type, Uri source, string id = null, DateTime? time = null,
public CloudEvent(string type, Uri source, string id = null, DateTimeOffset? time = null,
params ICloudEventExtension[] extensions) : this(CloudEventsSpecVersion.Default, type, source, id, time, extensions)
{
}
@ -40,13 +40,13 @@ namespace CloudNative.CloudEvents
/// <param name="id">'id' of the CloudEvent</param>
/// <param name="time">'time' of the CloudEvent</param>
/// <param name="extensions">Extensions to be added to this CloudEvents</param>
public CloudEvent(CloudEventsSpecVersion specVersion, string type, Uri source, string id = null, DateTime? time = null,
public CloudEvent(CloudEventsSpecVersion specVersion, string type, Uri source, string id = null, DateTimeOffset? time = null,
params ICloudEventExtension[] extensions) : this(specVersion, extensions)
{
Type = type;
Source = source;
Id = id ?? Guid.NewGuid().ToString();
Time = time ?? DateTime.UtcNow;
Time = time ?? DateTimeOffset.UtcNow;
}
/// <summary>
@ -59,7 +59,7 @@ namespace CloudNative.CloudEvents
/// <param name="id">'id' of the CloudEvent</param>
/// <param name="time">'time' of the CloudEvent</param>
/// <param name="extensions">Extensions to be added to this CloudEvents</param>
public CloudEvent(CloudEventsSpecVersion specVersion, string type, Uri source, string subject, string id = null, DateTime? time = null,
public CloudEvent(CloudEventsSpecVersion specVersion, string type, Uri source, string subject, string id = null, DateTimeOffset? time = null,
params ICloudEventExtension[] extensions) : this(specVersion, type, source, id, time, extensions)
{
Subject = subject;
@ -179,9 +179,9 @@ namespace CloudNative.CloudEvents
/// CloudEvents 'time' attribute. Timestamp of when the event happened.
/// </summary>
/// <see cref="https://github.com/cloudevents/spec/blob/master/spec.md#time"/>
public DateTime? Time
public DateTimeOffset? Time
{
get => (DateTime?)attributes[CloudEventAttributes.TimeAttributeName(attributes.SpecVersion)];
get => (DateTimeOffset?)attributes[CloudEventAttributes.TimeAttributeName(attributes.SpecVersion)];
set => attributes[CloudEventAttributes.TimeAttributeName(attributes.SpecVersion)] = value;
}

View File

@ -7,8 +7,6 @@ namespace CloudNative.CloudEvents
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Globalization;
using System.Net.Mime;
/// <summary>
@ -319,17 +317,16 @@ namespace CloudNative.CloudEvents
}
else if (key.Equals(TimeAttributeName(this.SpecVersion), StringComparison.InvariantCultureIgnoreCase))
{
if (value is DateTime)
if (value is DateTimeOffset)
{
return true;
}
if (value is string)
{
if (DateTime.TryParse((string)value, CultureInfo.InvariantCulture,
DateTimeStyles.AssumeUniversal, out var dateTimeVal))
if (Timestamps.TryParse((string)value, out var result))
{
value = dateTimeVal;
value = result;
return true;
}
}

View File

@ -12,7 +12,6 @@ namespace CloudNative.CloudEvents
using System.Net.Mime;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
/// <summary>
/// This class is for use with `HttpClient` and constructs content and headers for
@ -108,7 +107,7 @@ namespace CloudNative.CloudEvents
{
string text => WebUtility.UrlEncode(text),
ContentType contentType => contentType.ToString(),
DateTime dt => XmlConvert.ToString(dt, XmlDateTimeSerializationMode.Utc),
DateTimeOffset dto => Timestamps.Format(dto),
Uri uri => uri.ToString(),
int integer => integer.ToString(),
_ => WebUtility.UrlEncode(Encoding.UTF8.GetString(

View File

@ -494,7 +494,7 @@ namespace CloudNative.CloudEvents
switch (attributeValue)
{
case string text: return text;
case DateTime dateTime: return dateTime.ToString("u");
case DateTimeOffset dateTimeOffset: return Timestamps.Format(dateTimeOffset);
case Uri uri: return uri.ToString();
case int integer: return integer.ToString(CultureInfo.InvariantCulture);
default:

View File

@ -19,7 +19,6 @@ namespace CloudNative.CloudEvents
/// </summary>
public class JsonEventFormatter : ICloudEventFormatter
{
public const string MediaTypeSuffix = "+json";
public CloudEvent DecodeStructuredEvent(Stream data, params ICloudEventExtension[] extensions)
@ -29,14 +28,20 @@ namespace CloudNative.CloudEvents
public async Task<CloudEvent> DecodeStructuredEventAsync(Stream data, IEnumerable<ICloudEventExtension> extensions)
{
var jsonReader = new JsonTextReader(new StreamReader(data, Encoding.UTF8, true, 8192, true));
var jsonReader = new JsonTextReader(new StreamReader(data, Encoding.UTF8, true, 8192, true))
{
DateParseHandling = DateParseHandling.DateTimeOffset
};
var jObject = await JObject.LoadAsync(jsonReader);
return DecodeJObject(jObject, extensions);
}
public CloudEvent DecodeStructuredEvent(Stream data, IEnumerable<ICloudEventExtension> extensions = null)
{
var jsonReader = new JsonTextReader(new StreamReader(data, Encoding.UTF8, true, 8192, true));
var jsonReader = new JsonTextReader(new StreamReader(data, Encoding.UTF8, true, 8192, true))
{
DateParseHandling = DateParseHandling.DateTimeOffset
};
var jObject = JObject.Load(jsonReader);
return DecodeJObject(jObject, extensions);
}
@ -46,12 +51,8 @@ namespace CloudNative.CloudEvents
return DecodeStructuredEvent(data, (IEnumerable<ICloudEventExtension>)extensions);
}
public CloudEvent DecodeStructuredEvent(byte[] data, IEnumerable<ICloudEventExtension> extensions = null)
{
var jsonText = Encoding.UTF8.GetString(data);
var jObject = JObject.Parse(jsonText);
return DecodeJObject(jObject, extensions);
}
public CloudEvent DecodeStructuredEvent(byte[] data, IEnumerable<ICloudEventExtension> extensions = null) =>
DecodeStructuredEvent(new MemoryStream(data), extensions);
public CloudEvent DecodeJObject(JObject jObject, IEnumerable<ICloudEventExtension> extensions = null)
{
@ -101,7 +102,8 @@ namespace CloudNative.CloudEvents
attributes[keyValuePair.Key] = keyValuePair.Value.ToObject<string>();
break;
case JTokenType.Date:
attributes[keyValuePair.Key] = keyValuePair.Value.ToObject<DateTime>();
// TODO: Check this is appropriate. (Should we use Timestamps instead?)
attributes[keyValuePair.Key] = keyValuePair.Value.ToObject<DateTimeOffset>();
break;
case JTokenType.Uri:
attributes[keyValuePair.Key] = keyValuePair.Value.ToObject<Uri>();
@ -137,24 +139,24 @@ namespace CloudNative.CloudEvents
continue;
}
if (keyValuePair.Value is ContentType && !string.IsNullOrEmpty(((ContentType)keyValuePair.Value).MediaType))
if (keyValuePair.Value is ContentType contentTypeValue && !string.IsNullOrEmpty(contentTypeValue.MediaType))
{
jObject[keyValuePair.Key] = JToken.FromObject(((ContentType)keyValuePair.Value).ToString());
jObject[keyValuePair.Key] = JToken.FromObject(contentTypeValue.ToString());
}
else if (cloudEvent.SpecVersion == CloudEventsSpecVersion.V1_0 &&
keyValuePair.Key.Equals(CloudEventAttributes.DataAttributeName(cloudEvent.SpecVersion)))
{
if (keyValuePair.Value is Stream)
if (keyValuePair.Value is Stream stream)
{
using (var sr = new BinaryReader((Stream)keyValuePair.Value))
using (var sr = new BinaryReader(stream))
{
jObject["data_base64"] = Convert.ToBase64String(sr.ReadBytes((int)sr.BaseStream.Length));
}
}
else if (keyValuePair.Value is IEnumerable<byte>)
else if (keyValuePair.Value is IEnumerable<byte> bytes)
{
jObject["data_base64"] =
Convert.ToBase64String(((IEnumerable<byte>)keyValuePair.Value).ToArray());
// TODO: Avoid creating a copy if it's already a byte array.
jObject["data_base64"] = Convert.ToBase64String(bytes.ToArray());
}
else
{
@ -180,7 +182,7 @@ namespace CloudNative.CloudEvents
if (name.Equals(CloudEventAttributes.TimeAttributeName(specVersion)))
{
return JsonConvert.DeserializeObject(Encoding.UTF8.GetString(data), typeof(DateTime));
return Timestamps.Parse(Encoding.UTF8.GetString(data));
}
if (name.Equals(CloudEventAttributes.SourceAttributeName(specVersion)) ||
@ -200,6 +202,7 @@ namespace CloudNative.CloudEvents
{
foreach (var extension in extensions)
{
// TODO: Use appropriate parsing, e.g. for timestamps
Type type = extension.GetAttributeType(name);
if (type != null)
{
@ -231,11 +234,17 @@ namespace CloudNative.CloudEvents
Type type = extension.GetAttributeType(name);
if (type != null)
{
// TODO: Use appropriate formatting, e.g. for timestamps
return Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(Convert.ChangeType(value, type)));
}
}
}
if (value is DateTimeOffset dto)
{
return Encoding.UTF8.GetBytes(Timestamps.Format(dto));
}
return Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(value));
}
}

View File

@ -9,6 +9,7 @@ namespace CloudNative.CloudEvents.UnitTests
using CloudNative.CloudEvents.Amqp;
using global::Amqp;
using Xunit;
using static TestHelpers;
public class AmqpTest
{
@ -26,7 +27,7 @@ namespace CloudNative.CloudEvents.UnitTests
subject: "123")
{
Id = "A234-1234-1234",
Time = new DateTime(2018, 4, 5, 17, 31, 0, DateTimeKind.Utc),
Time = new DateTimeOffset(2018, 4, 5, 17, 31, 0, TimeSpan.Zero),
DataContentType = new ContentType(MediaTypeNames.Text.Xml),
Data = "<much wow=\"xml\"/>"
};
@ -47,8 +48,7 @@ namespace CloudNative.CloudEvents.UnitTests
Assert.Equal(new Uri("https://github.com/cloudevents/spec/pull"), receivedCloudEvent.Source);
Assert.Equal("123", receivedCloudEvent.Subject);
Assert.Equal("A234-1234-1234", receivedCloudEvent.Id);
Assert.Equal(DateTime.Parse("2018-04-05T17:31:00Z").ToUniversalTime(),
receivedCloudEvent.Time.Value.ToUniversalTime());
AssertTimestampsEqual("2018-04-05T17:31:00Z", receivedCloudEvent.Time.Value);
Assert.Equal(new ContentType(MediaTypeNames.Text.Xml), receivedCloudEvent.DataContentType);
Assert.Equal("<much wow=\"xml\"/>", receivedCloudEvent.Data);
@ -68,7 +68,7 @@ namespace CloudNative.CloudEvents.UnitTests
new Uri("https://github.com/cloudevents/spec/pull/123"))
{
Id = "A234-1234-1234",
Time = new DateTime(2018, 4, 5, 17, 31, 0, DateTimeKind.Utc),
Time = new DateTimeOffset(2018, 4, 5, 17, 31, 0, TimeSpan.Zero),
DataContentType = new ContentType(MediaTypeNames.Text.Xml),
Data = "<much wow=\"xml\"/>"
};
@ -88,14 +88,34 @@ namespace CloudNative.CloudEvents.UnitTests
Assert.Equal("com.github.pull.create", receivedCloudEvent.Type);
Assert.Equal(new Uri("https://github.com/cloudevents/spec/pull/123"), receivedCloudEvent.Source);
Assert.Equal("A234-1234-1234", receivedCloudEvent.Id);
Assert.Equal(DateTime.Parse("2018-04-05T17:31:00Z").ToUniversalTime(),
receivedCloudEvent.Time.Value.ToUniversalTime());
AssertTimestampsEqual("2018-04-05T17:31:00Z", receivedCloudEvent.Time.Value);
Assert.Equal(new ContentType(MediaTypeNames.Text.Xml), receivedCloudEvent.DataContentType);
Assert.Equal("<much wow=\"xml\"/>", receivedCloudEvent.Data);
var attr = receivedCloudEvent.GetAttributes();
Assert.Equal("value", (string)attr["comexampleextension1"]);
Assert.Equal("value", (string)attr["comexampleextension1"]);
}
[Fact]
public void AmqpNormalizesTimestampsToUtc()
{
var cloudEvent = new CloudEvent("com.github.pull.create",
new Uri("https://github.com/cloudevents/spec/pull/123"))
{
Id = "A234-1234-1234",
// 2018-04-05T18:31:00+01:00 => 2018-04-05T17:31:00Z
Time = new DateTimeOffset(2018, 4, 5, 18, 31, 0, TimeSpan.FromHours(1)),
DataContentType = new ContentType(MediaTypeNames.Text.Xml),
Data = "<much wow=\"xml\"/>"
};
var message = new AmqpCloudEventMessage(cloudEvent, ContentMode.Binary, new JsonEventFormatter());
var encodedAmqpMessage = message.Encode();
var message1 = Message.Decode(encodedAmqpMessage);
var receivedCloudEvent = message1.ToCloudEvent();
AssertTimestampsEqual("2018-04-05T17:31:00Z", receivedCloudEvent.Time.Value);
}
}
}

View File

@ -8,6 +8,7 @@ namespace CloudNative.CloudEvents.UnitTests
using System.Net.Mime;
using System.Text;
using Xunit;
using static TestHelpers;
public class AvroTest
{
@ -36,7 +37,7 @@ namespace CloudNative.CloudEvents.UnitTests
Assert.Equal(cloudEvent2.Type, cloudEvent.Type);
Assert.Equal(cloudEvent2.Source, cloudEvent.Source);
Assert.Equal(cloudEvent2.Id, cloudEvent.Id);
Assert.Equal(cloudEvent2.Time.Value.ToUniversalTime(), cloudEvent.Time.Value.ToUniversalTime());
AssertTimestampsEqual(cloudEvent2.Time.Value, cloudEvent.Time.Value);
Assert.Equal(cloudEvent2.DataContentType, cloudEvent.DataContentType);
Assert.Equal(cloudEvent2.Data, cloudEvent.Data);
}
@ -55,8 +56,7 @@ namespace CloudNative.CloudEvents.UnitTests
Assert.Equal("com.github.pull.create", cloudEvent.Type);
Assert.Equal(new Uri("https://github.com/cloudevents/spec/pull/123"), cloudEvent.Source);
Assert.Equal("A234-1234-1234", cloudEvent.Id);
Assert.Equal(DateTime.Parse("2018-04-05T17:31:00Z").ToUniversalTime(),
cloudEvent.Time.Value.ToUniversalTime());
AssertTimestampsEqual("2018-04-05T17:31:00Z", cloudEvent.Time.Value);
Assert.Equal(new ContentType(MediaTypeNames.Text.Xml), cloudEvent.DataContentType);
Assert.Equal("<much wow=\"xml\"/>", cloudEvent.Data);
@ -78,8 +78,7 @@ namespace CloudNative.CloudEvents.UnitTests
Assert.Equal("com.github.pull.create", cloudEvent.Type);
Assert.Equal(new Uri("https://github.com/cloudevents/spec/pull/123"), cloudEvent.Source);
Assert.Equal("A234-1234-1234", cloudEvent.Id);
Assert.Equal(DateTime.Parse("2018-04-05T17:31:00Z").ToUniversalTime(),
cloudEvent.Time.Value.ToUniversalTime());
AssertTimestampsEqual("2018-04-05T17:31:00Z", cloudEvent.Time.Value);
Assert.Equal(new ContentType(MediaTypeNames.Text.Xml), cloudEvent.DataContentType);
Assert.Equal("<much wow=\"xml\"/>", cloudEvent.Data);

View File

@ -39,6 +39,6 @@ namespace CloudNative.CloudEvents.UnitTests
static CloudEvent CreateEmptyCloudEvent() =>
new CloudEvent(CloudEventsSpecVersion.V1_0, "type",
new Uri("https://source"), "subject", "id", DateTime.UtcNow);
new Uri("https://source"), "subject", "id", DateTimeOffset.UtcNow);
}
}

View File

@ -14,7 +14,7 @@ namespace CloudNative.CloudEvents.UnitTests
public void SetAttributePropertiesToNull()
{
var cloudEvent = new CloudEvent(CloudEventsSpecVersion.V1_0, "type",
new Uri("https://source"), "subject", "id", DateTime.UtcNow)
new Uri("https://source"), "subject", "id", DateTimeOffset.UtcNow)
{
Data = "some data",
DataContentType = new ContentType("text/plain"),

View File

@ -7,9 +7,12 @@ namespace CloudNative.CloudEvents.UnitTests
using System;
using System.Net.Mime;
using Xunit;
using static TestHelpers;
public class ConstructorTest
{
private static readonly DateTimeOffset sampleTimestamp = new DateTimeOffset(2018, 4, 5, 17, 31, 0, TimeSpan.Zero);
[Fact]
public void CreateBaseEvent1()
{
@ -17,7 +20,7 @@ namespace CloudNative.CloudEvents.UnitTests
new Uri("https://github.com/cloudevents/spec/pull/123"))
{
Id = "A234-1234-1234",
Time = new DateTime(2018, 4, 5, 17, 31, 0, DateTimeKind.Utc),
Time = sampleTimestamp,
DataContentType = new ContentType(MediaTypeNames.Text.Xml),
Data = "<much wow=\"xml\"/>"
};
@ -30,8 +33,7 @@ namespace CloudNative.CloudEvents.UnitTests
Assert.Equal("com.github.pull.create", cloudEvent.Type);
Assert.Equal(new Uri("https://github.com/cloudevents/spec/pull/123"), cloudEvent.Source);
Assert.Equal("A234-1234-1234", cloudEvent.Id);
Assert.Equal(DateTime.Parse("2018-04-05T17:31:00Z").ToUniversalTime(),
cloudEvent.Time.Value.ToUniversalTime());
AssertTimestampsEqual("2018-04-05T17:31:00Z", cloudEvent.Time.Value);
Assert.Equal(new ContentType(MediaTypeNames.Text.Xml), cloudEvent.DataContentType);
Assert.Equal("<much wow=\"xml\"/>", cloudEvent.Data);
@ -47,7 +49,7 @@ namespace CloudNative.CloudEvents.UnitTests
"com.github.pull.create",
new Uri("https://github.com/cloudevents/spec/pull/123"),
"A234-1234-1234",
new DateTime(2018, 4, 5, 17, 31, 0, DateTimeKind.Utc))
sampleTimestamp)
{
DataContentType = new ContentType(MediaTypeNames.Text.Xml),
Data = "<much wow=\"xml\"/>"
@ -61,8 +63,7 @@ namespace CloudNative.CloudEvents.UnitTests
Assert.Equal("com.github.pull.create", cloudEvent.Type);
Assert.Equal(new Uri("https://github.com/cloudevents/spec/pull/123"), cloudEvent.Source);
Assert.Equal("A234-1234-1234", cloudEvent.Id);
Assert.Equal(DateTime.Parse("2018-04-05T17:31:00Z").ToUniversalTime(),
cloudEvent.Time.Value.ToUniversalTime());
AssertTimestampsEqual("2018-04-05T17:31:00Z", cloudEvent.Time.Value);
Assert.Equal(new ContentType(MediaTypeNames.Text.Xml), cloudEvent.DataContentType);
Assert.Equal("<much wow=\"xml\"/>", cloudEvent.Data);
@ -79,7 +80,7 @@ namespace CloudNative.CloudEvents.UnitTests
new Uri("https://github.com/cloudevents/spec/pull/123"))
{
Id = "A234-1234-1234",
Time = new DateTime(2018, 4, 5, 17, 31, 0, DateTimeKind.Utc),
Time = sampleTimestamp,
DataContentType = new ContentType(MediaTypeNames.Text.Xml),
Data = "<much wow=\"xml\"/>"
};
@ -92,8 +93,7 @@ namespace CloudNative.CloudEvents.UnitTests
Assert.Equal("com.github.pull.create", cloudEvent.Type);
Assert.Equal(new Uri("https://github.com/cloudevents/spec/pull/123"), cloudEvent.Source);
Assert.Equal("A234-1234-1234", cloudEvent.Id);
Assert.Equal(DateTime.Parse("2018-04-05T17:31:00Z").ToUniversalTime(),
cloudEvent.Time.Value.ToUniversalTime());
AssertTimestampsEqual("2018-04-05T17:31:00Z", cloudEvent.Time.Value);
Assert.Equal(new ContentType(MediaTypeNames.Text.Xml), cloudEvent.DataContentType);
Assert.Equal("<much wow=\"xml\"/>", cloudEvent.Data);
@ -109,7 +109,7 @@ namespace CloudNative.CloudEvents.UnitTests
new Uri("https://github.com/cloudevents/spec/pull/123"))
{
Id = "A234-1234-1234",
Time = new DateTime(2018, 4, 5, 17, 31, 0, DateTimeKind.Utc),
Time = sampleTimestamp,
DataContentType = new ContentType(MediaTypeNames.Text.Xml),
Data = "<much wow=\"xml\"/>"
};
@ -124,8 +124,7 @@ namespace CloudNative.CloudEvents.UnitTests
Assert.Equal("com.github.pull.create", cloudEvent.Type);
Assert.Equal(new Uri("https://github.com/cloudevents/spec/pull/123"), cloudEvent.Source);
Assert.Equal("A234-1234-1234", cloudEvent.Id);
Assert.Equal(DateTime.Parse("2018-04-05T17:31:00Z").ToUniversalTime(),
cloudEvent.Time.Value.ToUniversalTime());
AssertTimestampsEqual("2018-04-05T17:31:00Z", cloudEvent.Time.Value);
Assert.Equal(new ContentType(MediaTypeNames.Text.Xml), cloudEvent.DataContentType);
Assert.Equal("<much wow=\"xml\"/>", cloudEvent.Data);
@ -142,7 +141,7 @@ namespace CloudNative.CloudEvents.UnitTests
new Uri("https://github.com/cloudevents/spec/pull/123"))
{
Id = "A234-1234-1234",
Time = new DateTime(2018, 4, 5, 17, 31, 0, DateTimeKind.Utc),
Time = sampleTimestamp,
DataContentType = new ContentType(MediaTypeNames.Text.Xml),
Data = "<much wow=\"xml\"/>"
};
@ -156,8 +155,7 @@ namespace CloudNative.CloudEvents.UnitTests
Assert.Equal("com.github.pull.create", cloudEvent.Type);
Assert.Equal(new Uri("https://github.com/cloudevents/spec/pull/123"), cloudEvent.Source);
Assert.Equal("A234-1234-1234", cloudEvent.Id);
Assert.Equal(DateTime.Parse("2018-04-05T17:31:00Z").ToUniversalTime(),
cloudEvent.Time.Value.ToUniversalTime());
AssertTimestampsEqual("2018-04-05T17:31:00Z", cloudEvent.Time.Value);
Assert.Equal(new ContentType(MediaTypeNames.Text.Xml), cloudEvent.DataContentType);
Assert.Equal("<much wow=\"xml\"/>", cloudEvent.Data);
@ -172,7 +170,7 @@ namespace CloudNative.CloudEvents.UnitTests
"com.github.pull.create",
new Uri("https://github.com/cloudevents/spec/pull/123"),
"A234-1234-1234",
new DateTime(2018, 4, 5, 17, 31, 0, DateTimeKind.Utc),
sampleTimestamp,
new ComExampleExtension1Extension()
{
ComExampleExtension1 = "value"
@ -186,8 +184,7 @@ namespace CloudNative.CloudEvents.UnitTests
Assert.Equal("com.github.pull.create", cloudEvent.Type);
Assert.Equal(new Uri("https://github.com/cloudevents/spec/pull/123"), cloudEvent.Source);
Assert.Equal("A234-1234-1234", cloudEvent.Id);
Assert.Equal(DateTime.Parse("2018-04-05T17:31:00Z").ToUniversalTime(),
cloudEvent.Time.Value.ToUniversalTime());
AssertTimestampsEqual("2018-04-05T17:31:00Z", cloudEvent.Time.Value);
Assert.Equal(new ContentType(MediaTypeNames.Text.Xml), cloudEvent.DataContentType);
Assert.Equal("<much wow=\"xml\"/>", cloudEvent.Data);

View File

@ -9,6 +9,7 @@ namespace CloudNative.CloudEvents.UnitTests
using System.Text;
using CloudNative.CloudEvents.Extensions;
using Xunit;
using static TestHelpers;
public class ExtensionsTest
{
@ -72,9 +73,8 @@ namespace CloudNative.CloudEvents.UnitTests
Assert.Equal("com.github.pull.create", cloudEvent.Type);
Assert.Equal(new Uri("https://github.com/cloudevents/spec/pull/123"), cloudEvent.Source);
Assert.Equal("A234-1234-1234", cloudEvent.Id);
Assert.Equal(DateTime.Parse("2018-04-05T17:31:00Z").ToUniversalTime(),
cloudEvent.Time.Value.ToUniversalTime());
AssertTimestampsEqual("2018-04-05T17:31:00Z", cloudEvent.Time.Value);
Assert.Equal("00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01", cloudEvent.Extension<DistributedTracingExtension>().TraceParent);
Assert.Equal("rojo=00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01,congo=lZWRzIHRoNhcm5hbCBwbGVhc3VyZS4=", cloudEvent.Extension<DistributedTracingExtension>().TraceState);
}
@ -91,8 +91,7 @@ namespace CloudNative.CloudEvents.UnitTests
Assert.Equal("com.github.pull.create", cloudEvent.Type);
Assert.Equal(new Uri("https://github.com/cloudevents/spec/pull/123"), cloudEvent.Source);
Assert.Equal("A234-1234-1234", cloudEvent.Id);
Assert.Equal(DateTime.Parse("2018-04-05T17:31:00Z").ToUniversalTime(),
cloudEvent.Time.Value.ToUniversalTime());
AssertTimestampsEqual("2018-04-05T17:31:00Z", cloudEvent.Time.Value);
Assert.Equal("00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01", cloudEvent.Extension<DistributedTracingExtension>().TraceParent);
Assert.Equal("rojo=00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01,congo=lZWRzIHRoNhcm5hbCBwbGVhc3VyZS4=", cloudEvent.Extension<DistributedTracingExtension>().TraceState);
@ -107,8 +106,7 @@ namespace CloudNative.CloudEvents.UnitTests
Assert.Equal("com.github.pull.create", cloudEvent.Type);
Assert.Equal(new Uri("https://github.com/cloudevents/spec/pull/123"), cloudEvent.Source);
Assert.Equal("A234-1234-1234", cloudEvent.Id);
Assert.Equal(DateTime.Parse("2018-04-05T17:31:00Z").ToUniversalTime(),
cloudEvent.Time.Value.ToUniversalTime());
AssertTimestampsEqual("2018-04-05T17:31:00Z", cloudEvent.Time.Value);
Assert.Equal("Integer", cloudEvent.Extension<SequenceExtension>().SequenceType);
Assert.Equal("25", cloudEvent.Extension<SequenceExtension>().Sequence);
@ -126,8 +124,7 @@ namespace CloudNative.CloudEvents.UnitTests
Assert.Equal("com.github.pull.create", cloudEvent.Type);
Assert.Equal(new Uri("https://github.com/cloudevents/spec/pull/123"), cloudEvent.Source);
Assert.Equal("A234-1234-1234", cloudEvent.Id);
Assert.Equal(DateTime.Parse("2018-04-05T17:31:00Z").ToUniversalTime(),
cloudEvent.Time.Value.ToUniversalTime());
AssertTimestampsEqual("2018-04-05T17:31:00Z", cloudEvent.Time.Value);
Assert.Equal("Integer", cloudEvent.Extension<SequenceExtension>().SequenceType);
Assert.Equal("25", cloudEvent.Extension<SequenceExtension>().Sequence);
@ -142,8 +139,7 @@ namespace CloudNative.CloudEvents.UnitTests
Assert.Equal("com.github.pull.create", cloudEvent.Type);
Assert.Equal(new Uri("https://github.com/cloudevents/spec/pull/123"), cloudEvent.Source);
Assert.Equal("A234-1234-1234", cloudEvent.Id);
Assert.Equal(DateTime.Parse("2018-04-05T17:31:00Z").ToUniversalTime(),
cloudEvent.Time.Value.ToUniversalTime());
AssertTimestampsEqual("2018-04-05T17:31:00Z", cloudEvent.Time.Value);
Assert.Equal(25, cloudEvent.Extension<IntegerSequenceExtension>().Sequence);
}
@ -160,8 +156,7 @@ namespace CloudNative.CloudEvents.UnitTests
Assert.Equal("com.github.pull.create", cloudEvent.Type);
Assert.Equal(new Uri("https://github.com/cloudevents/spec/pull/123"), cloudEvent.Source);
Assert.Equal("A234-1234-1234", cloudEvent.Id);
Assert.Equal(DateTime.Parse("2018-04-05T17:31:00Z").ToUniversalTime(),
cloudEvent.Time.Value.ToUniversalTime());
AssertTimestampsEqual("2018-04-05T17:31:00Z", cloudEvent.Time.Value);
Assert.Equal(25, cloudEvent.Extension<IntegerSequenceExtension>().Sequence);
}
@ -175,8 +170,7 @@ namespace CloudNative.CloudEvents.UnitTests
Assert.Equal("com.github.pull.create", cloudEvent.Type);
Assert.Equal(new Uri("https://github.com/cloudevents/spec/pull/123"), cloudEvent.Source);
Assert.Equal("A234-1234-1234", cloudEvent.Id);
Assert.Equal(DateTime.Parse("2018-04-05T17:31:00Z").ToUniversalTime(),
cloudEvent.Time.Value.ToUniversalTime());
AssertTimestampsEqual("2018-04-05T17:31:00Z", cloudEvent.Time.Value);
Assert.Equal(1, cloudEvent.Extension<SamplingExtension>().SampledRate.Value);
}
@ -193,8 +187,7 @@ namespace CloudNative.CloudEvents.UnitTests
Assert.Equal("com.github.pull.create", cloudEvent.Type);
Assert.Equal(new Uri("https://github.com/cloudevents/spec/pull/123"), cloudEvent.Source);
Assert.Equal("A234-1234-1234", cloudEvent.Id);
Assert.Equal(DateTime.Parse("2018-04-05T17:31:00Z").ToUniversalTime(),
cloudEvent.Time.Value.ToUniversalTime());
AssertTimestampsEqual("2018-04-05T17:31:00Z", cloudEvent.Time.Value);
Assert.Equal(1, cloudEvent.Extension<SamplingExtension>().SampledRate.Value);
}

View File

@ -13,9 +13,12 @@ namespace CloudNative.CloudEvents.UnitTests
using System.Net.Mime;
using System.Threading.Tasks;
using Xunit;
using static TestHelpers;
public class HttpTest : IDisposable
{
private static readonly DateTimeOffset sampleTimestamp = new DateTimeOffset(2018, 4, 5, 17, 31, 0, TimeSpan.Zero);
const string listenerAddress = "http://localhost:52671/";
const string testContextHeader = "testcontext";
@ -94,7 +97,7 @@ namespace CloudNative.CloudEvents.UnitTests
new Uri("https://github.com/cloudevents/spec/pull/123"))
{
Id = "A234-1234-1234",
Time = new DateTime(2018, 4, 5, 17, 31, 0, DateTimeKind.Utc),
Time = sampleTimestamp,
DataContentType = new ContentType(MediaTypeNames.Text.Xml),
Data = "<much wow=\"xml\"/>"
};
@ -134,8 +137,7 @@ namespace CloudNative.CloudEvents.UnitTests
Assert.Equal("com.github.pull.create", receivedCloudEvent.Type);
Assert.Equal(new Uri("https://github.com/cloudevents/spec/pull/123"), receivedCloudEvent.Source);
Assert.Equal("A234-1234-1234", receivedCloudEvent.Id);
Assert.Equal(DateTime.Parse("2018-04-05T17:31:00Z").ToUniversalTime(),
receivedCloudEvent.Time.Value.ToUniversalTime());
AssertTimestampsEqual(sampleTimestamp, receivedCloudEvent.Time.Value);
Assert.Equal(new ContentType(MediaTypeNames.Text.Xml), receivedCloudEvent.DataContentType);
using (var sr = new StreamReader((Stream)receivedCloudEvent.Data))
{
@ -154,7 +156,7 @@ namespace CloudNative.CloudEvents.UnitTests
new Uri("https://github.com/cloudevents/spec/pull/123"))
{
Id = "A234-1234-1234",
Time = new DateTime(2018, 4, 5, 17, 31, 0, DateTimeKind.Utc),
Time = sampleTimestamp,
DataContentType = new ContentType(MediaTypeNames.Text.Xml),
Data = "<much wow=\"xml\"/>"
};
@ -179,8 +181,7 @@ namespace CloudNative.CloudEvents.UnitTests
Assert.Equal("com.github.pull.create", receivedCloudEvent.Type);
Assert.Equal(new Uri("https://github.com/cloudevents/spec/pull/123"), receivedCloudEvent.Source);
Assert.Equal("A234-1234-1234", receivedCloudEvent.Id);
Assert.Equal(DateTime.Parse("2018-04-05T17:31:00Z").ToUniversalTime(),
receivedCloudEvent.Time.Value.ToUniversalTime());
AssertTimestampsEqual(sampleTimestamp, cloudEvent.Time.Value);
Assert.Equal(new ContentType(MediaTypeNames.Text.Xml), receivedCloudEvent.DataContentType);
// The non-ASCII attribute value should have been URL-encoded using UTF-8 for the header.
@ -231,7 +232,7 @@ namespace CloudNative.CloudEvents.UnitTests
new Uri("https://github.com/cloudevents/spec/pull/123"))
{
Id = "A234-1234-1234",
Time = new DateTime(2018, 4, 5, 17, 31, 0, DateTimeKind.Utc),
Time = sampleTimestamp,
DataContentType = new ContentType(MediaTypeNames.Text.Xml),
Data = "<much wow=\"xml\"/>"
};
@ -267,8 +268,7 @@ namespace CloudNative.CloudEvents.UnitTests
Assert.Equal("com.github.pull.create", receivedCloudEvent.Type);
Assert.Equal(new Uri("https://github.com/cloudevents/spec/pull/123"), receivedCloudEvent.Source);
Assert.Equal("A234-1234-1234", receivedCloudEvent.Id);
Assert.Equal(DateTime.Parse("2018-04-05T17:31:00Z").ToUniversalTime(),
receivedCloudEvent.Time.Value.ToUniversalTime());
AssertTimestampsEqual(sampleTimestamp, receivedCloudEvent.Time.Value);
Assert.Equal(new ContentType(MediaTypeNames.Text.Xml), receivedCloudEvent.DataContentType);
Assert.Equal("<much wow=\"xml\"/>", receivedCloudEvent.Data);
@ -284,7 +284,7 @@ namespace CloudNative.CloudEvents.UnitTests
new Uri("https://github.com/cloudevents/spec/pull/123"))
{
Id = "A234-1234-1234",
Time = new DateTime(2018, 4, 5, 17, 31, 0, DateTimeKind.Utc),
Time = sampleTimestamp,
DataContentType = new ContentType(MediaTypeNames.Text.Xml),
Data = "<much wow=\"xml\"/>"
};
@ -321,8 +321,7 @@ namespace CloudNative.CloudEvents.UnitTests
Assert.Equal("com.github.pull.create", receivedCloudEvent.Type);
Assert.Equal(new Uri("https://github.com/cloudevents/spec/pull/123"), receivedCloudEvent.Source);
Assert.Equal("A234-1234-1234", receivedCloudEvent.Id);
Assert.Equal(DateTime.Parse("2018-04-05T17:31:00Z").ToUniversalTime(),
receivedCloudEvent.Time.Value.ToUniversalTime());
AssertTimestampsEqual(sampleTimestamp, cloudEvent.Time.Value);
Assert.Equal(new ContentType(MediaTypeNames.Text.Xml), receivedCloudEvent.DataContentType);
Assert.Equal("<much wow=\"xml\"/>", receivedCloudEvent.Data);
@ -359,7 +358,7 @@ namespace CloudNative.CloudEvents.UnitTests
new Uri("https://github.com/cloudevents/spec/pull/123"))
{
Id = "A234-1234-1234",
Time = new DateTime(2018, 4, 5, 17, 31, 0, DateTimeKind.Utc),
Time = sampleTimestamp,
DataContentType = new ContentType(MediaTypeNames.Text.Xml),
Data = "<much wow=\"xml\"/>"
};
@ -384,8 +383,7 @@ namespace CloudNative.CloudEvents.UnitTests
Assert.Equal("com.github.pull.create", receivedCloudEvent.Type);
Assert.Equal(new Uri("https://github.com/cloudevents/spec/pull/123"), receivedCloudEvent.Source);
Assert.Equal("A234-1234-1234", receivedCloudEvent.Id);
Assert.Equal(DateTime.Parse("2018-04-05T17:31:00Z").ToUniversalTime(),
receivedCloudEvent.Time.Value.ToUniversalTime());
AssertTimestampsEqual(sampleTimestamp, cloudEvent.Time.Value);
Assert.Equal(new ContentType(MediaTypeNames.Text.Xml), receivedCloudEvent.DataContentType);
using (var sr = new StreamReader((Stream) receivedCloudEvent.Data))
{
@ -427,7 +425,7 @@ namespace CloudNative.CloudEvents.UnitTests
new Uri("https://github.com/cloudevents/spec/pull/123"))
{
Id = "A234-1234-1234",
Time = new DateTime(2018, 4, 5, 17, 31, 0, DateTimeKind.Utc),
Time = sampleTimestamp,
DataContentType = new ContentType(MediaTypeNames.Text.Xml),
Data = "<much wow=\"xml\"/>"
};
@ -455,8 +453,7 @@ namespace CloudNative.CloudEvents.UnitTests
Assert.Equal("com.github.pull.create", receivedCloudEvent.Type);
Assert.Equal(new Uri("https://github.com/cloudevents/spec/pull/123"), receivedCloudEvent.Source);
Assert.Equal("A234-1234-1234", receivedCloudEvent.Id);
Assert.Equal(DateTime.Parse("2018-04-05T17:31:00Z").ToUniversalTime(),
receivedCloudEvent.Time.Value.ToUniversalTime());
AssertTimestampsEqual(sampleTimestamp, cloudEvent.Time.Value);
Assert.Equal(new ContentType(MediaTypeNames.Text.Xml), receivedCloudEvent.DataContentType);
Assert.Equal("<much wow=\"xml\"/>", receivedCloudEvent.Data);

View File

@ -8,6 +8,7 @@ namespace CloudNative.CloudEvents.UnitTests
using System.Net.Mime;
using System.Text;
using Xunit;
using static TestHelpers;
public class JsonTest
{
@ -50,7 +51,7 @@ namespace CloudNative.CloudEvents.UnitTests
Assert.Equal(cloudEvent2.Type, cloudEvent.Type);
Assert.Equal(cloudEvent2.Source, cloudEvent.Source);
Assert.Equal(cloudEvent2.Id, cloudEvent.Id);
Assert.Equal(cloudEvent2.Time.Value.ToUniversalTime(), cloudEvent.Time.Value.ToUniversalTime());
AssertTimestampsEqual(cloudEvent2.Time, cloudEvent.Time);
Assert.Equal(cloudEvent2.DataContentType, cloudEvent.DataContentType);
Assert.Equal(cloudEvent2.Data, cloudEvent.Data);
}
@ -67,7 +68,7 @@ namespace CloudNative.CloudEvents.UnitTests
Assert.Equal(cloudEvent2.Type, cloudEvent.Type);
Assert.Equal(cloudEvent2.Source, cloudEvent.Source);
Assert.Equal(cloudEvent2.Id, cloudEvent.Id);
Assert.Equal(cloudEvent2.Time.Value.ToUniversalTime(), cloudEvent.Time.Value.ToUniversalTime());
AssertTimestampsEqual(cloudEvent2.Time, cloudEvent.Time);
Assert.Equal(cloudEvent2.DataContentType, cloudEvent.DataContentType);
Assert.Equal(cloudEvent2.Data, cloudEvent.Data);
}
@ -85,7 +86,7 @@ namespace CloudNative.CloudEvents.UnitTests
Assert.Equal(cloudEvent2.Type, cloudEvent.Type);
Assert.Equal(cloudEvent2.Source, cloudEvent.Source);
Assert.Equal(cloudEvent2.Id, cloudEvent.Id);
Assert.Equal(cloudEvent2.Time.Value.ToUniversalTime(), cloudEvent.Time.Value.ToUniversalTime());
AssertTimestampsEqual(cloudEvent2.Time, cloudEvent.Time);
Assert.Equal(cloudEvent2.DataContentType, cloudEvent.DataContentType);
Assert.Equal(cloudEvent2.Data, cloudEvent.Data);
}
@ -103,7 +104,7 @@ namespace CloudNative.CloudEvents.UnitTests
Assert.Equal(cloudEvent2.Type, cloudEvent.Type);
Assert.Equal(cloudEvent2.Source, cloudEvent.Source);
Assert.Equal(cloudEvent2.Id, cloudEvent.Id);
Assert.Equal(cloudEvent2.Time.Value.ToUniversalTime(), cloudEvent.Time.Value.ToUniversalTime());
AssertTimestampsEqual(cloudEvent2.Time, cloudEvent.Time);
Assert.Equal(cloudEvent2.DataContentType, cloudEvent.DataContentType);
Assert.Equal(cloudEvent2.Data, cloudEvent.Data);
}
@ -117,8 +118,7 @@ namespace CloudNative.CloudEvents.UnitTests
Assert.Equal("com.github.pull.create", cloudEvent.Type);
Assert.Equal(new Uri("https://github.com/cloudevents/spec/pull/123"), cloudEvent.Source);
Assert.Equal("A234-1234-1234", cloudEvent.Id);
Assert.Equal(DateTime.Parse("2018-04-05T17:31:00Z").ToUniversalTime(),
cloudEvent.Time.Value.ToUniversalTime());
AssertTimestampsEqual("2018-04-05T17:31:00Z", cloudEvent.Time.Value);
Assert.Equal(new ContentType(MediaTypeNames.Text.Xml), cloudEvent.DataContentType);
Assert.Equal("<much wow=\"xml\"/>", cloudEvent.Data);
@ -136,8 +136,7 @@ namespace CloudNative.CloudEvents.UnitTests
Assert.Equal("com.github.pull.create", cloudEvent.Type);
Assert.Equal(new Uri("https://github.com/cloudevents/spec/pull/123"), cloudEvent.Source);
Assert.Equal("A234-1234-1234", cloudEvent.Id);
Assert.Equal(DateTime.Parse("2018-04-05T17:31:00Z").ToUniversalTime(),
cloudEvent.Time.Value.ToUniversalTime());
AssertTimestampsEqual("2018-04-05T17:31:00Z", cloudEvent.Time.Value);
Assert.Equal(new ContentType(MediaTypeNames.Text.Xml), cloudEvent.DataContentType);
Assert.Equal("<much wow=\"xml\"/>", cloudEvent.Data);
@ -155,8 +154,7 @@ namespace CloudNative.CloudEvents.UnitTests
Assert.Equal("com.github.pull.create", cloudEvent.Type);
Assert.Equal(new Uri("https://github.com/cloudevents/spec/pull/123"), cloudEvent.Source);
Assert.Equal("A234-1234-1234", cloudEvent.Id);
Assert.Equal(DateTime.Parse("2018-04-05T17:31:00Z").ToUniversalTime(),
cloudEvent.Time.Value.ToUniversalTime());
AssertTimestampsEqual("2018-04-05T17:31:00Z", cloudEvent.Time.Value);
Assert.Equal(new ContentType(MediaTypeNames.Text.Xml), cloudEvent.DataContentType);
Assert.Equal("<much wow=\"xml\"/>", cloudEvent.Data);
@ -173,8 +171,7 @@ namespace CloudNative.CloudEvents.UnitTests
Assert.Equal("com.github.pull.create", cloudEvent.Type);
Assert.Equal(new Uri("https://github.com/cloudevents/spec/pull/123"), cloudEvent.Source);
Assert.Equal("A234-1234-1234", cloudEvent.Id);
Assert.Equal(DateTime.Parse("2018-04-05T17:31:00Z").ToUniversalTime(),
cloudEvent.Time.Value.ToUniversalTime());
AssertTimestampsEqual("2018-04-05T17:31:00Z", cloudEvent.Time.Value);
Assert.Equal(new ContentType(MediaTypeNames.Text.Xml), cloudEvent.DataContentType);
Assert.Equal("<much wow=\"xml\"/>", cloudEvent.Data);

View File

@ -15,6 +15,7 @@ namespace CloudNative.CloudEvents.UnitTests
using System.Collections.Generic;
using System.Text;
using CloudNative.CloudEvents.Extensions;
using static TestHelpers;
public class KafkaTest
{
@ -33,7 +34,7 @@ namespace CloudNative.CloudEvents.UnitTests
subject: "123")
{
Id = "A234-1234-1234",
Time = new DateTime(2018, 4, 5, 17, 31, 0, DateTimeKind.Utc),
Time = new DateTimeOffset(2018, 4, 5, 17, 31, 0, TimeSpan.Zero),
DataContentType = new ContentType(MediaTypeNames.Text.Xml),
Data = "<much wow=\"xml\"/>"
};
@ -58,8 +59,7 @@ namespace CloudNative.CloudEvents.UnitTests
Assert.Equal(new Uri("https://github.com/cloudevents/spec/pull"), receivedCloudEvent.Source);
Assert.Equal("123", receivedCloudEvent.Subject);
Assert.Equal("A234-1234-1234", receivedCloudEvent.Id);
Assert.Equal(DateTime.Parse("2018-04-05T17:31:00Z").ToUniversalTime(),
receivedCloudEvent.Time.Value.ToUniversalTime());
AssertTimestampsEqual("2018-04-05T17:31:00Z", receivedCloudEvent.Time.Value);
Assert.Equal(new ContentType(MediaTypeNames.Text.Xml), receivedCloudEvent.DataContentType);
Assert.Equal("<much wow=\"xml\"/>", receivedCloudEvent.Data);
@ -80,7 +80,7 @@ namespace CloudNative.CloudEvents.UnitTests
extensions: new PartitioningExtension())
{
Id = "A234-1234-1234",
Time = new DateTime(2018, 4, 5, 17, 31, 0, DateTimeKind.Utc),
Time = new DateTimeOffset(2018, 4, 5, 17, 31, 0, TimeSpan.Zero),
DataContentType = new ContentType(MediaTypeNames.Text.Xml),
Data = Encoding.UTF8.GetBytes("<much wow=\"xml\"/>")
};
@ -95,7 +95,11 @@ namespace CloudNative.CloudEvents.UnitTests
// using serialization to create fully independent copy thus simulating message transport
// real transport will work in a similar way
var serialized = JsonConvert.SerializeObject(message, new HeaderConverter());
var messageCopy = JsonConvert.DeserializeObject<Message<string, byte[]>>(serialized, new HeadersConverter(), new HeaderConverter());
var settings = new JsonSerializerSettings
{
Converters = { new HeadersConverter(), new HeaderConverter() }
};
var messageCopy = JsonConvert.DeserializeObject<Message<string, byte[]>>(serialized, settings);
Assert.True(messageCopy.IsCloudEvent());
var receivedCloudEvent = messageCopy.ToCloudEvent(jsonEventFormatter, new PartitioningExtension());
@ -104,8 +108,7 @@ namespace CloudNative.CloudEvents.UnitTests
Assert.Equal("com.github.pull.create", receivedCloudEvent.Type);
Assert.Equal(new Uri("https://github.com/cloudevents/spec/pull/123"), receivedCloudEvent.Source);
Assert.Equal("A234-1234-1234", receivedCloudEvent.Id);
Assert.Equal(DateTime.Parse("2018-04-05T17:31:00Z").ToUniversalTime(),
receivedCloudEvent.Time.Value.ToUniversalTime());
AssertTimestampsEqual("2018-04-05T17:31:00Z", receivedCloudEvent.Time.Value);
Assert.Equal(new ContentType(MediaTypeNames.Text.Xml), receivedCloudEvent.DataContentType);
Assert.Equal(Encoding.UTF8.GetBytes("<much wow=\"xml\"/>"), receivedCloudEvent.Data);
Assert.Equal("hello much wow", receivedCloudEvent.Extension<PartitioningExtension>().PartitioningKeyValue);

View File

@ -17,6 +17,7 @@ namespace CloudNative.CloudEvents.UnitTests
using MQTTnet;
using MQTTnet.Client;
using MQTTnet.Server;
using static TestHelpers;
public class MqttTest : IDisposable
{
@ -46,7 +47,7 @@ namespace CloudNative.CloudEvents.UnitTests
new Uri("https://github.com/cloudevents/spec/pull/123"))
{
Id = "A234-1234-1234",
Time = new DateTime(2018, 4, 5, 17, 31, 0, DateTimeKind.Utc),
Time = new DateTimeOffset(2018, 4, 5, 17, 31, 0, TimeSpan.Zero),
DataContentType = new ContentType(MediaTypeNames.Text.Xml),
Data = "<much wow=\"xml\"/>"
};
@ -77,19 +78,12 @@ namespace CloudNative.CloudEvents.UnitTests
Assert.Equal("com.github.pull.create", receivedCloudEvent.Type);
Assert.Equal(new Uri("https://github.com/cloudevents/spec/pull/123"), receivedCloudEvent.Source);
Assert.Equal("A234-1234-1234", receivedCloudEvent.Id);
Assert.Equal(DateTime.Parse("2018-04-05T17:31:00Z").ToUniversalTime(),
receivedCloudEvent.Time.Value.ToUniversalTime());
AssertTimestampsEqual("2018-04-05T17:31:00Z", receivedCloudEvent.Time.Value);
Assert.Equal(new ContentType(MediaTypeNames.Text.Xml), receivedCloudEvent.DataContentType);
Assert.Equal("<much wow=\"xml\"/>", receivedCloudEvent.Data);
var attr = receivedCloudEvent.GetAttributes();
Assert.Equal("value", (string)attr["comexampleextension1"]);
}
}
}