private static async Task <T> ConvertAndDeserialize <T>(string resourceName) where T : class
        {
            var context    = GcfEventResources.CreateHttpContext(resourceName);
            var cloudEvent = await GcfConverters.ConvertGcfEventToCloudEvent(context.Request);

            return(CloudEventConverters.ConvertCloudEventData <T>(cloudEvent));
        }
        public async Task ConvertGcfEvent(string resourceName, string expectedType, string expectedSource)
        {
            var context    = GcfEventResources.CreateHttpContext(resourceName);
            var cloudEvent = await GcfConverters.ConvertGcfEventToCloudEvent(context.Request);

            Assert.Equal(expectedType, cloudEvent.Type);
            Assert.Equal(new Uri(expectedSource), cloudEvent.Source);
        }
        public async Task ConvertGcfEvent(string resourceName, string expectedType, string expectedSource, string expectedSubject)
        {
            var context    = GcfEventResources.CreateHttpContext(resourceName);
            var cloudEvent = await GcfConverters.ConvertGcfEventToCloudEvent(context.Request, s_jsonFormatter);

            Assert.Equal(expectedType, cloudEvent.Type);
            Assert.Equal(expectedSource, cloudEvent.Source.ToString());
            Assert.Equal(expectedSubject, cloudEvent.Subject);
        }
示例#4
0
        private static async Task <T> ConvertAndDeserialize <T>(string resourceName) where T : class
        {
            var context   = GcfEventResources.CreateHttpContext(resourceName);
            var formatter = CloudEventFormatterAttribute.CreateFormatter(typeof(T))
                            ?? throw new InvalidOperationException("No formatter available");
            var cloudEvent = await GcfConverters.ConvertGcfEventToCloudEvent(context.Request, formatter);

            return((T)cloudEvent.Data);
        }
示例#5
0
        public async Task ConvertGcfEvent_ExtensionAttributes(string resourceName, string extensionAttributeName, string expectedValue)
        {
            var context    = GcfEventResources.CreateHttpContext(resourceName);
            var cloudEvent = await GcfConverters.ConvertGcfEventToCloudEvent(context.Request, s_jsonFormatter);

            var attributeValue = (string)cloudEvent[extensionAttributeName];

            Assert.Equal(expectedValue, attributeValue);
        }
示例#6
0
        public async Task PubSubTimeStampPrecision(string resourceName, string expectedPublishTime)
        {
            var context    = GcfEventResources.CreateHttpContext(resourceName);
            var cloudEvent = await GcfConverters.ConvertGcfEventToCloudEvent(context.Request, s_jsonFormatter);

            var data = (JsonElement)cloudEvent.Data;
            var actualPublishTime = data.GetProperty("message").GetProperty("publishTime").GetString();

            Assert.Equal(expectedPublishTime, actualPublishTime);
        }
        private static async Task <CloudEvent> ConvertJson(string json, string?contentType = null)
        {
            var request = new DefaultHttpContext
            {
                Request =
                {
                    Body        = new MemoryStream(Encoding.UTF8.GetBytes(json.Replace('\'', '"'))),
                    ContentType = contentType ?? "application/json"
                }
            }.Request;

            return(await GcfConverters.ConvertGcfEventToCloudEvent(request, s_jsonFormatter));
        }
        public async Task CheckAllProperties()
        {
            var context    = GcfEventResources.CreateHttpContext("storage.json");
            var cloudEvent = await GcfConverters.ConvertGcfEventToCloudEvent(context.Request, s_jsonFormatter);

            Assert.Equal("application/json", cloudEvent.DataContentType);
            Assert.Equal("1147091835525187", cloudEvent.Id);
            Assert.Equal("google.cloud.storage.object.v1.finalized", cloudEvent.Type);
            Assert.Equal(new DateTimeOffset(2020, 4, 23, 7, 38, 57, 772, TimeSpan.Zero), cloudEvent.Time);
            Assert.Equal("//storage.googleapis.com/projects/_/buckets/some-bucket", cloudEvent.Source.ToString());
            Assert.Equal("objects/folder/Test.cs", cloudEvent.Subject);
            Assert.Equal(CloudEventsSpecVersion.V1_0, cloudEvent.SpecVersion);
            Assert.Null(cloudEvent.DataSchema);
            Assert.IsType <JsonElement>(cloudEvent.Data);
        }
        public async Task CheckAllProperties()
        {
            var context    = GcfEventResources.CreateHttpContext("storage.json");
            var cloudEvent = await GcfConverters.ConvertGcfEventToCloudEvent(context.Request);

            Assert.Equal("application/json", cloudEvent.DataContentType.MediaType);
            Assert.Equal("1147091835525187", cloudEvent.Id);
            Assert.Equal("com.google.cloud.storage.object.finalize.v0", cloudEvent.Type);
            Assert.Equal(new DateTime(2020, 4, 23, 7, 38, 57, 772), cloudEvent.Time);
            Assert.Equal(new Uri("//storage.googleapis.com/projects/_/buckets/some-bucket/objects/Test.cs"), cloudEvent.Source);
            Assert.Equal(CloudEventsSpecVersion.V1_0, cloudEvent.SpecVersion);
            Assert.Null(cloudEvent.Subject);
            Assert.Null(cloudEvent.DataSchema);
            Assert.IsType <string>(cloudEvent.Data);
        }
 public async Task InvalidRequest_FirebaseAnalytics(string resourceName)
 {
     var context = GcfEventResources.CreateHttpContext(resourceName);
     await Assert.ThrowsAsync <GcfConverters.ConversionException>(() => GcfConverters.ConvertGcfEventToCloudEvent(context.Request, s_jsonFormatter));
 }
示例#11
0
 /// <summary>
 /// Converts an HTTP request into a CloudEvent, either using regular CloudEvent parsing,
 /// or GCF event conversion if necessary.
 /// </summary>
 /// <param name="request">The request to convert.</param>
 /// <param name="formatter">The formatter to use for conversion.</param>
 /// <returns>A valid CloudEvent</returns>
 internal static Task <CloudEvent> ConvertRequestAsync(HttpRequest request, CloudEventFormatter formatter) =>
 request.IsCloudEvent()
     ? request.ToCloudEventAsync(formatter)
     : GcfConverters.ConvertGcfEventToCloudEvent(request, formatter);
 /// <summary>
 /// Converts an HTTP request into a CloudEvent, either using regular CloudEvent parsing,
 /// or GCF event conversion if necessary.
 /// </summary>
 /// <param name="request">The request to convert.</param>
 /// <exception cref="ConversionException">The Cloud Event couldn't be converted.</exception>
 /// <returns>A valid CloudEvent</returns>
 internal static ValueTask <CloudEvent> ConvertRequest(HttpRequest request) =>
 IsNativeCloudEventRequest(request)
     ? ConvertNativeCloudEventRequest(request)
     : GcfConverters.ConvertGcfEventToCloudEvent(request);