|
| 1 | +// Copyright 2021 Cloud Native Foundation. |
| 2 | +// Licensed under the Apache 2.0 license. |
| 3 | +// See LICENSE file in the project root for full license information. |
| 4 | + |
| 5 | +using CloudNative.CloudEvents.Core; |
| 6 | +using CloudNative.CloudEvents.NewtonsoftJson; |
| 7 | +using Microsoft.AspNetCore.Http; |
| 8 | +using Microsoft.AspNetCore.Http.Internal; |
| 9 | +using System; |
| 10 | +using System.IO; |
| 11 | +using System.Net.Mime; |
| 12 | +using System.Text; |
| 13 | +using System.Threading.Tasks; |
| 14 | +using Xunit; |
| 15 | +using static CloudNative.CloudEvents.UnitTests.TestHelpers; |
| 16 | + |
| 17 | +namespace CloudNative.CloudEvents.AspNetCore.UnitTests |
| 18 | +{ |
| 19 | + public class HttpResponseExtensionsTest |
| 20 | + { |
| 21 | + [Fact] |
| 22 | + public async Task CopyToHttpResponseAsync_BinaryMode() |
| 23 | + { |
| 24 | + var cloudEvent = new CloudEvent |
| 25 | + { |
| 26 | + Data = "plain text", |
| 27 | + DataContentType = "text/plain" |
| 28 | + }.PopulateRequiredAttributes(); |
| 29 | + var formatter = new JsonEventFormatter(); |
| 30 | + var response = CreateResponse(); |
| 31 | + await cloudEvent.CopyToHttpResponseAsync(response, ContentMode.Binary, formatter); |
| 32 | + |
| 33 | + var content = GetContent(response); |
| 34 | + Assert.Equal("text/plain", response.ContentType); |
| 35 | + Assert.Equal("plain text", Encoding.UTF8.GetString(content.Span)); |
| 36 | + Assert.Equal("1.0", response.Headers["ce-specversion"]); |
| 37 | + Assert.Equal(cloudEvent.Type, response.Headers["ce-type"]); |
| 38 | + Assert.Equal(cloudEvent.Id, response.Headers["ce-id"]); |
| 39 | + Assert.Equal(CloudEventAttributeType.UriReference.Format(cloudEvent.Source), response.Headers["ce-source"]); |
| 40 | + // There's no data content type header; the content type itself is used for that. |
| 41 | + Assert.False(response.Headers.ContainsKey("ce-datacontenttype")); |
| 42 | + } |
| 43 | + |
| 44 | + [Fact] |
| 45 | + public async Task CopyToHttpResponseAsync_ContentButNoContentType() |
| 46 | + { |
| 47 | + var cloudEvent = new CloudEvent |
| 48 | + { |
| 49 | + Data = "plain text", |
| 50 | + }.PopulateRequiredAttributes(); |
| 51 | + var formatter = new JsonEventFormatter(); |
| 52 | + var response = CreateResponse(); |
| 53 | + await Assert.ThrowsAsync<ArgumentException>(() => cloudEvent.CopyToHttpResponseAsync(response, ContentMode.Binary, formatter)); |
| 54 | + } |
| 55 | + |
| 56 | + [Fact] |
| 57 | + public async Task CopyToHttpResponseAsync_BadContentMode() |
| 58 | + { |
| 59 | + var cloudEvent = new CloudEvent().PopulateRequiredAttributes(); |
| 60 | + var formatter = new JsonEventFormatter(); |
| 61 | + var response = CreateResponse(); |
| 62 | + await Assert.ThrowsAsync<ArgumentOutOfRangeException>(() => cloudEvent.CopyToHttpResponseAsync(response, (ContentMode)100, formatter)); |
| 63 | + } |
| 64 | + |
| 65 | + [Fact] |
| 66 | + public async Task CopyToHttpResponseAsync_StructuredMode() |
| 67 | + { |
| 68 | + var cloudEvent = new CloudEvent |
| 69 | + { |
| 70 | + Data = "plain text", |
| 71 | + DataContentType = "text/plain" |
| 72 | + }.PopulateRequiredAttributes(); |
| 73 | + var formatter = new JsonEventFormatter(); |
| 74 | + var response = CreateResponse(); |
| 75 | + await cloudEvent.CopyToHttpResponseAsync(response, ContentMode.Structured, formatter); |
| 76 | + var content = GetContent(response); |
| 77 | + Assert.Equal(MimeUtilities.MediaType + "+json; charset=utf-8", response.ContentType); |
| 78 | + |
| 79 | + var parsed = new JsonEventFormatter().DecodeStructuredModeMessage(content, new ContentType(response.ContentType), extensionAttributes: null); |
| 80 | + AssertCloudEventsEqual(cloudEvent, parsed); |
| 81 | + Assert.Equal(cloudEvent.Data, parsed.Data); |
| 82 | + |
| 83 | + // We populate headers even though we don't strictly need to; let's validate that. |
| 84 | + Assert.Equal("1.0", response.Headers["ce-specversion"]); |
| 85 | + Assert.Equal(cloudEvent.Type, response.Headers["ce-type"]); |
| 86 | + Assert.Equal(cloudEvent.Id, response.Headers["ce-id"]); |
| 87 | + Assert.Equal(CloudEventAttributeType.UriReference.Format(cloudEvent.Source), response.Headers["ce-source"]); |
| 88 | + // We don't populate the data content type header |
| 89 | + Assert.False(response.Headers.ContainsKey("ce-datacontenttype")); |
| 90 | + } |
| 91 | + |
| 92 | + [Fact] |
| 93 | + public async Task CopyToHttpResponseAsync_Batch() |
| 94 | + { |
| 95 | + var batch = CreateSampleBatch(); |
| 96 | + var response = CreateResponse(); |
| 97 | + await batch.CopyToHttpResponseAsync(response, new JsonEventFormatter()); |
| 98 | + |
| 99 | + var content = GetContent(response); |
| 100 | + Assert.Equal(MimeUtilities.BatchMediaType + "+json; charset=utf-8", response.ContentType); |
| 101 | + var parsedBatch = new JsonEventFormatter().DecodeBatchModeMessage(content, new ContentType(response.ContentType), extensionAttributes: null); |
| 102 | + AssertBatchesEqual(batch, parsedBatch); |
| 103 | + } |
| 104 | + |
| 105 | + private static HttpResponse CreateResponse() => new DefaultHttpResponse(new DefaultHttpContext()) { Body = new MemoryStream() }; |
| 106 | + private static ReadOnlyMemory<byte> GetContent(HttpResponse response) |
| 107 | + { |
| 108 | + response.Body.Position = 0; |
| 109 | + return BinaryDataUtilities.ToReadOnlyMemory(response.Body); |
| 110 | + } |
| 111 | + } |
| 112 | +} |
0 commit comments