Skip to content

Commit 0c44aad

Browse files
#286 Add in a framework reference for net8 (#289)
Signed-off-by: James Thompson <[email protected]>
1 parent 684698c commit 0c44aad

File tree

7 files changed

+42
-24
lines changed

7 files changed

+42
-24
lines changed

src/CloudNative.CloudEvents.AspNetCore/CloudNative.CloudEvents.AspNetCore.csproj

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,16 @@
88
<PackageTags>cncf;cloudnative;cloudevents;events;aspnetcore;aspnet</PackageTags>
99
</PropertyGroup>
1010

11-
<ItemGroup>
11+
<ItemGroup Condition="'$(TargetFramework)' != 'netstandard2.0' and '$(TargetFramework)' != 'netstandard2.1'">
12+
<FrameworkReference Include="Microsoft.AspNetCore.App"/>
13+
</ItemGroup>
14+
15+
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0' or '$(TargetFramework)' == 'netstandard2.1'">
1216
<PackageReference Include="Microsoft.AspNetCore.Http" Version="2.1.34" />
13-
<PackageReference Include="System.Text.Encodings.Web" Version="6.0.0" Condition="'$(TargetFramework)'=='netstandard2.0' or '$(TargetFramework)'=='netstandard2.1'" />
17+
<PackageReference Include="System.Text.Encodings.Web" Version="6.0.0" />
18+
</ItemGroup>
19+
20+
<ItemGroup>
1421
<ProjectReference Include="..\CloudNative.CloudEvents\CloudNative.CloudEvents.csproj" />
1522
</ItemGroup>
1623

src/CloudNative.CloudEvents.AspNetCore/HttpRequestExtensions.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) Cloud Native Foundation.
1+
// Copyright (c) Cloud Native Foundation.
22
// Licensed under the Apache 2.0 license.
33
// See LICENSE file in the project root for full license information.
44

@@ -94,11 +94,12 @@ public static async Task<CloudEvent> ToCloudEventAsync(
9494
foreach (var header in headers)
9595
{
9696
string? attributeName = HttpUtilities.GetAttributeNameFromHeaderName(header.Key);
97-
if (attributeName is null || attributeName == CloudEventsSpecVersion.SpecVersionAttribute.Name)
97+
string? headerValue = header.Value.First();
98+
if (attributeName is null || attributeName == CloudEventsSpecVersion.SpecVersionAttribute.Name || headerValue is null)
9899
{
99100
continue;
100101
}
101-
string attributeValue = HttpUtilities.DecodeHeaderValue(header.Value.First());
102+
string attributeValue = HttpUtilities.DecodeHeaderValue(headerValue);
102103

103104
cloudEvent.SetAttributeFromString(attributeName, attributeValue);
104105
}

src/CloudNative.CloudEvents.AspNetCore/HttpResponseExtensions.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) Cloud Native Foundation.
1+
// Copyright (c) Cloud Native Foundation.
22
// Licensed under the Apache 2.0 license.
33
// See LICENSE file in the project root for full license information.
44

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

5858
// Map headers in either mode.
5959
// Including the headers in structured mode is optional in the spec (as they're already within the body) but
60-
// can be useful.
61-
destination.Headers.Add(HttpUtilities.SpecVersionHttpHeader, HttpUtilities.EncodeHeaderValue(cloudEvent.SpecVersion.VersionId));
60+
// can be useful.
61+
destination.Headers.Append(HttpUtilities.SpecVersionHttpHeader, HttpUtilities.EncodeHeaderValue(cloudEvent.SpecVersion.VersionId));
6262
foreach (var attributeAndValue in cloudEvent.GetPopulatedAttributes())
6363
{
6464
var attribute = attributeAndValue.Key;
@@ -67,7 +67,7 @@ public static async Task CopyToHttpResponseAsync(this CloudEvent cloudEvent, Htt
6767
if (attribute != cloudEvent.SpecVersion.DataContentTypeAttribute)
6868
{
6969
string headerValue = HttpUtilities.EncodeHeaderValue(attribute.Format(value));
70-
destination.Headers.Add(HttpUtilities.HttpHeaderPrefix + attribute.Name, headerValue);
70+
destination.Headers.Append(HttpUtilities.HttpHeaderPrefix + attribute.Name, headerValue);
7171
}
7272
}
7373

test/CloudNative.CloudEvents.UnitTests/AspNetCore/HttpRequestExtensionsTest.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
// Copyright 2021 Cloud Native Foundation.
1+
// Copyright 2021 Cloud Native Foundation.
22
// Licensed under the Apache 2.0 license.
33
// See LICENSE file in the project root for full license information.
44

55
using CloudNative.CloudEvents.Core;
66
using CloudNative.CloudEvents.NewtonsoftJson;
77
using Microsoft.AspNetCore.Http;
8-
using Microsoft.AspNetCore.Http.Internal;
98
using System;
109
using System.Collections.Generic;
1110
using System.Net.Mime;
@@ -130,12 +129,13 @@ public async Task ToCloudEventBatchAsync_Invalid()
130129
await Assert.ThrowsAsync<ArgumentException>(() => CreateRequest(contentBytes, contentType).ToCloudEventBatchAsync(formatter, EmptyExtensionSequence));
131130
}
132131

133-
private static HttpRequest CreateRequest(ReadOnlyMemory<byte> content, ContentType contentType) =>
134-
new DefaultHttpRequest(new DefaultHttpContext())
135-
{
136-
ContentType = contentType.ToString(),
137-
Body = BinaryDataUtilities.AsStream(content)
138-
};
132+
private static HttpRequest CreateRequest(ReadOnlyMemory<byte> content, ContentType contentType)
133+
{
134+
var request = new DefaultHttpContext().Request;
135+
request.ContentType = contentType.ToString();
136+
request.Body = BinaryDataUtilities.AsStream(content);
137+
return request;
138+
}
139139

140140
private static void CopyHeaders(IDictionary<string, string>? source, HttpRequest target)
141141
{
@@ -145,7 +145,7 @@ private static void CopyHeaders(IDictionary<string, string>? source, HttpRequest
145145
}
146146
foreach (var header in source)
147147
{
148-
target.Headers.Add(header.Key, header.Value);
148+
target.Headers.Append(header.Key, header.Value);
149149
}
150150
}
151151
}

test/CloudNative.CloudEvents.UnitTests/AspNetCore/HttpResponseExtensionsTest.cs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
// Copyright 2021 Cloud Native Foundation.
1+
// Copyright 2021 Cloud Native Foundation.
22
// Licensed under the Apache 2.0 license.
33
// See LICENSE file in the project root for full license information.
44

55
using CloudNative.CloudEvents.Core;
66
using CloudNative.CloudEvents.NewtonsoftJson;
77
using Microsoft.AspNetCore.Http;
8-
using Microsoft.AspNetCore.Http.Internal;
98
using System;
109
using System.IO;
1110
using System.Net.Mime;
@@ -92,6 +91,7 @@ public async Task CopyToHttpResponseAsync_StructuredMode()
9291
await cloudEvent.CopyToHttpResponseAsync(response, ContentMode.Structured, formatter);
9392
var content = GetContent(response);
9493
Assert.Equal(MimeUtilities.MediaType + "+json; charset=utf-8", response.ContentType);
94+
Assert.NotNull(response.ContentType);
9595

9696
var parsed = new JsonEventFormatter().DecodeStructuredModeMessage(content, new ContentType(response.ContentType), extensionAttributes: null);
9797
AssertCloudEventsEqual(cloudEvent, parsed);
@@ -114,11 +114,18 @@ public async Task CopyToHttpResponseAsync_Batch()
114114

115115
var content = GetContent(response);
116116
Assert.Equal(MimeUtilities.BatchMediaType + "+json; charset=utf-8", response.ContentType);
117+
Assert.NotNull(response.ContentType);
117118
var parsedBatch = new JsonEventFormatter().DecodeBatchModeMessage(content, new ContentType(response.ContentType), extensionAttributes: null);
118119
AssertBatchesEqual(batch, parsedBatch);
119120
}
120121

121-
private static HttpResponse CreateResponse() => new DefaultHttpResponse(new DefaultHttpContext()) { Body = new MemoryStream() };
122+
private static HttpResponse CreateResponse()
123+
{
124+
var response = new DefaultHttpContext().Response;
125+
response.Body = new MemoryStream();
126+
return response;
127+
}
128+
122129
private static ReadOnlyMemory<byte> GetContent(HttpResponse response)
123130
{
124131
response.Body.Position = 0;

test/CloudNative.CloudEvents.UnitTests/CloudNative.CloudEvents.UnitTests.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
<Nullable>enable</Nullable>
66
</PropertyGroup>
77

8+
<ItemGroup>
9+
<FrameworkReference Include="Microsoft.AspNetCore.App" />
10+
</ItemGroup>
11+
812
<ItemGroup>
913
<PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
1014
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.5.0" />

test/CloudNative.CloudEvents.UnitTests/DocumentationSamples.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
// Copyright 2021 Cloud Native Foundation.
1+
// Copyright 2021 Cloud Native Foundation.
22
// Licensed under the Apache 2.0 license.
33
// See LICENSE file in the project root for full license information.
44

55
using CloudNative.CloudEvents.AspNetCore;
66
using CloudNative.CloudEvents.Http;
77
using CloudNative.CloudEvents.NewtonsoftJson;
88
using Microsoft.AspNetCore.Http;
9-
using Microsoft.AspNetCore.Http.Internal;
109
using Newtonsoft.Json;
1110
using Newtonsoft.Json.Linq;
1211
using System;
@@ -156,7 +155,7 @@ private static async Task<GameResult> DeserializeGameResult2(HttpRequest request
156155

157156
private static async Task<HttpRequest> ConvertHttpRequestMessage(HttpRequestMessage message)
158157
{
159-
var request = new DefaultHttpRequest(new DefaultHttpContext());
158+
var request = new DefaultHttpContext().Request;
160159
foreach (var header in message.Headers)
161160
{
162161
request.Headers[header.Key] = header.Value.Single();

0 commit comments

Comments
 (0)