Signed-off-by: James Thompson <thompson.tomo@outlook.com>
This commit is contained in:
parent
684698c09d
commit
0c44aade1d
|
@ -8,9 +8,16 @@
|
|||
<PackageTags>cncf;cloudnative;cloudevents;events;aspnetcore;aspnet</PackageTags>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ItemGroup Condition="'$(TargetFramework)' != 'netstandard2.0' and '$(TargetFramework)' != 'netstandard2.1'">
|
||||
<FrameworkReference Include="Microsoft.AspNetCore.App"/>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0' or '$(TargetFramework)' == 'netstandard2.1'">
|
||||
<PackageReference Include="Microsoft.AspNetCore.Http" Version="2.1.34" />
|
||||
<PackageReference Include="System.Text.Encodings.Web" Version="6.0.0" Condition="'$(TargetFramework)'=='netstandard2.0' or '$(TargetFramework)'=='netstandard2.1'" />
|
||||
<PackageReference Include="System.Text.Encodings.Web" Version="6.0.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\CloudNative.CloudEvents\CloudNative.CloudEvents.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
// Copyright (c) Cloud Native Foundation.
|
||||
// Copyright (c) Cloud Native Foundation.
|
||||
// Licensed under the Apache 2.0 license.
|
||||
// See LICENSE file in the project root for full license information.
|
||||
|
||||
|
@ -94,11 +94,12 @@ namespace CloudNative.CloudEvents.AspNetCore
|
|||
foreach (var header in headers)
|
||||
{
|
||||
string? attributeName = HttpUtilities.GetAttributeNameFromHeaderName(header.Key);
|
||||
if (attributeName is null || attributeName == CloudEventsSpecVersion.SpecVersionAttribute.Name)
|
||||
string? headerValue = header.Value.First();
|
||||
if (attributeName is null || attributeName == CloudEventsSpecVersion.SpecVersionAttribute.Name || headerValue is null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
string attributeValue = HttpUtilities.DecodeHeaderValue(header.Value.First());
|
||||
string attributeValue = HttpUtilities.DecodeHeaderValue(headerValue);
|
||||
|
||||
cloudEvent.SetAttributeFromString(attributeName, attributeValue);
|
||||
}
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
// Copyright (c) Cloud Native Foundation.
|
||||
// Copyright (c) Cloud Native Foundation.
|
||||
// Licensed under the Apache 2.0 license.
|
||||
// See LICENSE file in the project root for full license information.
|
||||
|
||||
|
@ -57,8 +57,8 @@ namespace CloudNative.CloudEvents.AspNetCore
|
|||
|
||||
// Map headers in either mode.
|
||||
// Including the headers in structured mode is optional in the spec (as they're already within the body) but
|
||||
// can be useful.
|
||||
destination.Headers.Add(HttpUtilities.SpecVersionHttpHeader, HttpUtilities.EncodeHeaderValue(cloudEvent.SpecVersion.VersionId));
|
||||
// can be useful.
|
||||
destination.Headers.Append(HttpUtilities.SpecVersionHttpHeader, HttpUtilities.EncodeHeaderValue(cloudEvent.SpecVersion.VersionId));
|
||||
foreach (var attributeAndValue in cloudEvent.GetPopulatedAttributes())
|
||||
{
|
||||
var attribute = attributeAndValue.Key;
|
||||
|
@ -67,7 +67,7 @@ namespace CloudNative.CloudEvents.AspNetCore
|
|||
if (attribute != cloudEvent.SpecVersion.DataContentTypeAttribute)
|
||||
{
|
||||
string headerValue = HttpUtilities.EncodeHeaderValue(attribute.Format(value));
|
||||
destination.Headers.Add(HttpUtilities.HttpHeaderPrefix + attribute.Name, headerValue);
|
||||
destination.Headers.Append(HttpUtilities.HttpHeaderPrefix + attribute.Name, headerValue);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,11 +1,10 @@
|
|||
// Copyright 2021 Cloud Native Foundation.
|
||||
// Copyright 2021 Cloud Native Foundation.
|
||||
// Licensed under the Apache 2.0 license.
|
||||
// See LICENSE file in the project root for full license information.
|
||||
|
||||
using CloudNative.CloudEvents.Core;
|
||||
using CloudNative.CloudEvents.NewtonsoftJson;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Http.Internal;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Net.Mime;
|
||||
|
@ -130,12 +129,13 @@ namespace CloudNative.CloudEvents.AspNetCore.UnitTests
|
|||
await Assert.ThrowsAsync<ArgumentException>(() => CreateRequest(contentBytes, contentType).ToCloudEventBatchAsync(formatter, EmptyExtensionSequence));
|
||||
}
|
||||
|
||||
private static HttpRequest CreateRequest(ReadOnlyMemory<byte> content, ContentType contentType) =>
|
||||
new DefaultHttpRequest(new DefaultHttpContext())
|
||||
{
|
||||
ContentType = contentType.ToString(),
|
||||
Body = BinaryDataUtilities.AsStream(content)
|
||||
};
|
||||
private static HttpRequest CreateRequest(ReadOnlyMemory<byte> content, ContentType contentType)
|
||||
{
|
||||
var request = new DefaultHttpContext().Request;
|
||||
request.ContentType = contentType.ToString();
|
||||
request.Body = BinaryDataUtilities.AsStream(content);
|
||||
return request;
|
||||
}
|
||||
|
||||
private static void CopyHeaders(IDictionary<string, string>? source, HttpRequest target)
|
||||
{
|
||||
|
@ -145,7 +145,7 @@ namespace CloudNative.CloudEvents.AspNetCore.UnitTests
|
|||
}
|
||||
foreach (var header in source)
|
||||
{
|
||||
target.Headers.Add(header.Key, header.Value);
|
||||
target.Headers.Append(header.Key, header.Value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,11 +1,10 @@
|
|||
// Copyright 2021 Cloud Native Foundation.
|
||||
// Copyright 2021 Cloud Native Foundation.
|
||||
// Licensed under the Apache 2.0 license.
|
||||
// See LICENSE file in the project root for full license information.
|
||||
|
||||
using CloudNative.CloudEvents.Core;
|
||||
using CloudNative.CloudEvents.NewtonsoftJson;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Http.Internal;
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Net.Mime;
|
||||
|
@ -92,6 +91,7 @@ namespace CloudNative.CloudEvents.AspNetCore.UnitTests
|
|||
await cloudEvent.CopyToHttpResponseAsync(response, ContentMode.Structured, formatter);
|
||||
var content = GetContent(response);
|
||||
Assert.Equal(MimeUtilities.MediaType + "+json; charset=utf-8", response.ContentType);
|
||||
Assert.NotNull(response.ContentType);
|
||||
|
||||
var parsed = new JsonEventFormatter().DecodeStructuredModeMessage(content, new ContentType(response.ContentType), extensionAttributes: null);
|
||||
AssertCloudEventsEqual(cloudEvent, parsed);
|
||||
|
@ -114,11 +114,18 @@ namespace CloudNative.CloudEvents.AspNetCore.UnitTests
|
|||
|
||||
var content = GetContent(response);
|
||||
Assert.Equal(MimeUtilities.BatchMediaType + "+json; charset=utf-8", response.ContentType);
|
||||
Assert.NotNull(response.ContentType);
|
||||
var parsedBatch = new JsonEventFormatter().DecodeBatchModeMessage(content, new ContentType(response.ContentType), extensionAttributes: null);
|
||||
AssertBatchesEqual(batch, parsedBatch);
|
||||
}
|
||||
|
||||
private static HttpResponse CreateResponse() => new DefaultHttpResponse(new DefaultHttpContext()) { Body = new MemoryStream() };
|
||||
private static HttpResponse CreateResponse()
|
||||
{
|
||||
var response = new DefaultHttpContext().Response;
|
||||
response.Body = new MemoryStream();
|
||||
return response;
|
||||
}
|
||||
|
||||
private static ReadOnlyMemory<byte> GetContent(HttpResponse response)
|
||||
{
|
||||
response.Body.Position = 0;
|
||||
|
|
|
@ -5,6 +5,10 @@
|
|||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<FrameworkReference Include="Microsoft.AspNetCore.App" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.5.0" />
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
// Copyright 2021 Cloud Native Foundation.
|
||||
// Copyright 2021 Cloud Native Foundation.
|
||||
// Licensed under the Apache 2.0 license.
|
||||
// See LICENSE file in the project root for full license information.
|
||||
|
||||
|
@ -6,7 +6,6 @@ using CloudNative.CloudEvents.AspNetCore;
|
|||
using CloudNative.CloudEvents.Http;
|
||||
using CloudNative.CloudEvents.NewtonsoftJson;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Http.Internal;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using System;
|
||||
|
@ -156,7 +155,7 @@ namespace CloudNative.CloudEvents.UnitTests
|
|||
|
||||
private static async Task<HttpRequest> ConvertHttpRequestMessage(HttpRequestMessage message)
|
||||
{
|
||||
var request = new DefaultHttpRequest(new DefaultHttpContext());
|
||||
var request = new DefaultHttpContext().Request;
|
||||
foreach (var header in message.Headers)
|
||||
{
|
||||
request.Headers[header.Key] = header.Value.Single();
|
||||
|
|
Loading…
Reference in New Issue