Пример #1
0
        public async Task CopyToHttpResponseAsync_StructuredMode()
        {
            var cloudEvent = new CloudEvent
            {
                Data            = "plain text",
                DataContentType = "text/plain"
            }.PopulateRequiredAttributes();
            var formatter = new JsonEventFormatter();
            var response  = CreateResponse();
            await cloudEvent.CopyToHttpResponseAsync(response, ContentMode.Structured, formatter);

            var content = GetContent(response);

            Assert.Equal(MimeUtilities.MediaType + "+json; charset=utf-8", response.ContentType);

            var parsed = new JsonEventFormatter().DecodeStructuredModeMessage(content, new ContentType(response.ContentType), extensionAttributes: null);

            AssertCloudEventsEqual(cloudEvent, parsed);
            Assert.Equal(cloudEvent.Data, parsed.Data);

            // We populate headers even though we don't strictly need to; let's validate that.
            Assert.Equal("1.0", response.Headers["ce-specversion"]);
            Assert.Equal(cloudEvent.Type, response.Headers["ce-type"]);
            Assert.Equal(cloudEvent.Id, response.Headers["ce-id"]);
            Assert.Equal(CloudEventAttributeType.UriReference.Format(cloudEvent.Source !), response.Headers["ce-source"]);
            // We don't populate the data content type header
            Assert.False(response.Headers.ContainsKey("ce-datacontenttype"));
        }
Пример #2
0
 public async Task CopyToHttpResponseAsync_BadContentMode()
 {
     var cloudEvent = new CloudEvent().PopulateRequiredAttributes();
     var formatter  = new JsonEventFormatter();
     var response   = CreateResponse();
     await Assert.ThrowsAsync <ArgumentOutOfRangeException>(() => cloudEvent.CopyToHttpResponseAsync(response, (ContentMode)100, formatter));
 }
Пример #3
0
 public async Task CopyToHttpResponseAsync_BinaryDataButNoDataContentType()
 {
     var cloudEvent = new CloudEvent
     {
         Data = new byte[10],
     }.PopulateRequiredAttributes();
     var formatter = new JsonEventFormatter();
     var response  = CreateResponse();
     // The formatter doesn't infer the data content type for binary data.
     await Assert.ThrowsAsync <ArgumentException>(() => cloudEvent.CopyToHttpResponseAsync(response, ContentMode.Binary, formatter));
 }
        public async Task Write(object eventData, HttpContext context)
        {
            var replyEvent = new CloudEvent
            {
                Type            = _eventType,
                Source          = new Uri($"urn:{_eventSource}"),
                Time            = DateTimeOffset.Now,
                DataContentType = "application/json",
                Id   = Guid.NewGuid().ToString(),
                Data = eventData
            };

            _logger.LogInformation("Replying with CloudEvent\n" + replyEvent.GetLog());

            await replyEvent.CopyToHttpResponseAsync(context.Response, ContentMode.Binary, formatter);
        }
Пример #5
0
        public async Task CopyToHttpResponseAsync_NonBinaryDataButNoDataContentType_ContentTypeIsInferred()
        {
            var cloudEvent = new CloudEvent
            {
                Data = "plain text",
            }.PopulateRequiredAttributes();
            var formatter = new JsonEventFormatter();
            var response  = CreateResponse();
            await cloudEvent.CopyToHttpResponseAsync(response, ContentMode.Binary, formatter);

            var content = GetContent(response);

            // The formatter infers that it should encode the string as a JSON value (so it includes the double quotes)
            Assert.Equal("application/json", response.ContentType);
            Assert.Equal("\"plain text\"", Encoding.UTF8.GetString(content.Span));
        }
Пример #6
0
        public async Task CopyToHttpResponseAsync_BinaryMode()
        {
            var cloudEvent = new CloudEvent
            {
                Data            = "plain text",
                DataContentType = "text/plain"
            }.PopulateRequiredAttributes();
            var formatter = new JsonEventFormatter();
            var response  = CreateResponse();
            await cloudEvent.CopyToHttpResponseAsync(response, ContentMode.Binary, formatter);

            var content = GetContent(response);

            Assert.Equal("text/plain", response.ContentType);
            Assert.Equal("plain text", Encoding.UTF8.GetString(content.Span));
            Assert.Equal("1.0", response.Headers["ce-specversion"]);
            Assert.Equal(cloudEvent.Type, response.Headers["ce-type"]);
            Assert.Equal(cloudEvent.Id, response.Headers["ce-id"]);
            Assert.Equal(CloudEventAttributeType.UriReference.Format(cloudEvent.Source !), response.Headers["ce-source"]);
            // There's no data content type header; the content type itself is used for that.
            Assert.False(response.Headers.ContainsKey("ce-datacontenttype"));
        }