Skip to content

Commit

Permalink
#286 Add in a framework reference for net8 (#289)
Browse files Browse the repository at this point in the history
Signed-off-by: James Thompson <thompson.tomo@outlook.com>
  • Loading branch information
thompson-tomo authored Apr 8, 2024
1 parent 684698c commit 0c44aad
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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>

Expand Down
Original file line number Diff line number Diff line change
@@ -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.

Expand Down Expand Up @@ -94,11 +94,12 @@ public static async Task<CloudEvent> ToCloudEventAsync(
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);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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.

Expand Down Expand Up @@ -57,8 +57,8 @@ public static async Task CopyToHttpResponseAsync(this CloudEvent cloudEvent, Htt

// 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;
Expand All @@ -67,7 +67,7 @@ public static async Task CopyToHttpResponseAsync(this CloudEvent cloudEvent, Htt
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);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -130,12 +129,13 @@ public async Task ToCloudEventBatchAsync_Invalid()
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)
{
Expand All @@ -145,7 +145,7 @@ private static void CopyHeaders(IDictionary<string, string>? source, HttpRequest
}
foreach (var header in source)
{
target.Headers.Add(header.Key, header.Value);
target.Headers.Append(header.Key, header.Value);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -92,6 +91,7 @@ public async Task CopyToHttpResponseAsync_StructuredMode()
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);
Expand All @@ -114,11 +114,18 @@ public async Task CopyToHttpResponseAsync_Batch()

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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
// 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.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;
Expand Down Expand Up @@ -156,7 +155,7 @@ private static async Task<GameResult> DeserializeGameResult2(HttpRequest request

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();
Expand Down

0 comments on commit 0c44aad

Please sign in to comment.