Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@
<PackageVersion Include="MQTTnet" Version="4.3.7.1207" />
<PackageVersion Include="Newtonsoft.Json" Version="13.0.4" />
<PackageVersion Include="Nullable" Version="1.3.1" />
<PackageVersion Include="System.Net.Http" Version="4.3.4" />
<PackageVersion Include="System.Memory" Version="4.6.3" />
<PackageVersion Include="System.Text.Encodings.Web" Version="10.0.7" />
<PackageVersion Include="System.Text.Json" Version="10.0.7" />
<PackageVersion Include="xunit" Version="2.9.3" />
<PackageVersion Include="xunit.runner.console" Version="2.9.3" />
<PackageVersion Include="xunit.runner.visualstudio" Version="3.1.5" />
</ItemGroup>
</Project>
</Project>
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 @@ -198,8 +198,7 @@ private async Task<JsonDocument> ReadDocumentAsync(Stream data, ContentType? con
using var reader = new StreamReader(data, encoding);
var json = async
? await reader.ReadToEndAsync().ConfigureAwait(false)
: reader.ReadToEnd();

: reader.ReadToEnd();
return JsonDocument.Parse(json, DocumentOptions);
#endif
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public async Task CopyToHttpResponseAsync_BinaryMode()

var content = GetContent(response);
Assert.Equal("text/plain", response.ContentType);
Assert.Equal("plain text", Encoding.UTF8.GetString(content.Span));
Assert.Equal("plain text", Encoding.UTF8.GetString(content.Span.ToArray()));
Assert.Equal("1.0", response.Headers["ce-specversion"]);
Assert.Equal(cloudEvent.Type, response.Headers["ce-type"]);
Assert.Equal(cloudEvent.Id, response.Headers["ce-id"]);
Expand Down Expand Up @@ -66,7 +66,7 @@ public async Task CopyToHttpResponseAsync_NonBinaryDataButNoDataContentType_Cont
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));
Assert.Equal("\"plain text\"", Encoding.UTF8.GetString(content.Span.ToArray()));
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,20 @@

<PropertyGroup>
<TargetFrameworks>net8.0;net10.0</TargetFrameworks>
<TargetFrameworks Condition="'$(OS)' == 'Windows_NT'">$(TargetFrameworks);net48</TargetFrameworks>
<LangVersion>latest</LangVersion>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' != 'net48'">
<FrameworkReference Include="Microsoft.AspNetCore.App" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net48'">
<PackageReference Include="Microsoft.AspNetCore.Http" />
<PackageReference Include="System.Net.Http" />
<PackageReference Include="System.Text.Encodings.Web" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ public void ContentTypeConversions(string text)
// This just makes them deterministic.
void AssertEqualParts(string expected, string actual)
{
expected = string.Join(";", expected.Split(";").OrderBy(x => x));
actual = string.Join(";", actual.Split(";").OrderBy(x => x));
expected = string.Join(";", expected.Split(';').OrderBy(x => x));
actual = string.Join(";", actual.Split(';').OrderBy(x => x));
Assert.Equal(expected, actual);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,7 @@ internal static void CopyHeaders(IDictionary<string, string>? source, HttpHeader
internal static HttpRequestMessage CreateRequestMessage(ReadOnlyMemory<byte> content, ContentType contentType) =>
new HttpRequestMessage
{
Method = HttpMethod.Post,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is POST better on older frameworks?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, you can't create an HttpRequestMessage with content and a GET method.

Content = new ByteArrayContent(content.ToArray())
{
Headers = { ContentType = MimeUtilities.ToMediaTypeHeaderValue(contentType) }
Expand Down
Original file line number Diff line number Diff line change
@@ -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.

Expand Down Expand Up @@ -26,7 +26,7 @@ public async Task IsCloudEvent_True(string description, HttpContent content, IDi
// Really only present for display purposes.
Assert.NotNull(description);

var request = new HttpRequestMessage(HttpMethod.Get, ListenerAddress) { Content = content };
var request = new HttpRequestMessage(HttpMethod.Post, ListenerAddress) { Content = content };
HttpClientExtensionsTest.CopyHeaders(headers, request.Headers);
var result = await SendRequestAsync(request, context => Task.FromResult(context.Request.IsCloudEvent()));
Assert.True(result);
Expand All @@ -40,7 +40,7 @@ public async Task IsCloudEvent_False(string description, HttpContent content, ID
// Really only present for display purposes.
Assert.NotNull(description);

var request = new HttpRequestMessage(HttpMethod.Get, ListenerAddress) { Content = content };
var request = new HttpRequestMessage(HttpMethod.Post, ListenerAddress) { Content = content };
HttpClientExtensionsTest.CopyHeaders(headers, request.Headers);
var result = await SendRequestAsync(request, context => Task.FromResult(context.Request.IsCloudEvent()));
Assert.False(result);
Expand All @@ -53,7 +53,7 @@ public async Task IsCloudEventBatch_True(string description, HttpContent content
// Really only present for display purposes.
Assert.NotNull(description);

var request = new HttpRequestMessage(HttpMethod.Get, ListenerAddress) { Content = content };
var request = new HttpRequestMessage(HttpMethod.Post, ListenerAddress) { Content = content };
HttpClientExtensionsTest.CopyHeaders(headers, request.Headers);
var result = await SendRequestAsync(request, context => Task.FromResult(context.Request.IsCloudEventBatch()));
Assert.True(result);
Expand All @@ -67,7 +67,7 @@ public async Task IsCloudEventBatch_False(string description, HttpContent conten
// Really only present for display purposes.
Assert.NotNull(description);

var request = new HttpRequestMessage(HttpMethod.Get, ListenerAddress) { Content = content };
var request = new HttpRequestMessage(HttpMethod.Post, ListenerAddress) { Content = content };
HttpClientExtensionsTest.CopyHeaders(headers, request.Headers);
var result = await SendRequestAsync(request, context => Task.FromResult(context.Request.IsCloudEventBatch()));
Assert.False(result);
Expand All @@ -80,6 +80,7 @@ public async Task ToCloudEvent_BinaryMode(bool async)
{
var request = new HttpRequestMessage
{
Method = HttpMethod.Post,
RequestUri = new Uri(ListenerAddress),
Headers =
{
Expand Down Expand Up @@ -121,6 +122,7 @@ public async Task ToCloudEvent_StructuredMode(bool async)
var bytes = new JsonEventFormatter().EncodeStructuredModeMessage(originalCloudEvent, out var contentType);
var request = new HttpRequestMessage
{
Method = HttpMethod.Post,
RequestUri = new Uri(ListenerAddress),
Content = new ByteArrayContent(bytes.ToArray())
{
Expand Down
4 changes: 2 additions & 2 deletions test/CloudNative.CloudEvents.UnitTests/Http/HttpTestBase.cs
Original file line number Diff line number Diff line change
@@ -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.

Expand Down Expand Up @@ -99,7 +99,7 @@ private async Task ProcessRequestsAsync()
var responseContent = Encoding.UTF8.GetBytes($"Error processing request: {e}");
response.ContentLength64 = responseContent.Length;
response.StatusCode = 500;
response.OutputStream.Write(responseContent);
response.OutputStream.Write(responseContent, 0, responseContent.Length);
}
context.Response.Close();
}
Expand Down
7 changes: 7 additions & 0 deletions test/CloudNative.CloudEvents.UnitTests/Kafka/KafkaTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,15 @@ public void KafkaBinaryMessageTest()

[Theory]
[InlineData(MediaTypeNames.Application.Octet, new byte[0])]
#if NET8_0_OR_GREATER
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about:

[InlineData("application/json", null)]
[InlineData("application/xml", new byte[0])]

Or was that not the issue on lower framework versions?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've now got both, so we can see the equivalence.

[InlineData(MediaTypeNames.Application.Json, null)]
[InlineData(MediaTypeNames.Application.Xml, new byte[0])]
#else
// Equivalent to the above, but those constants don't exist
// in .NET Framework
[InlineData("application/json", null)]
[InlineData("application/xml", new byte[0])]
#endif
[InlineData(MediaTypeNames.Text.Plain, "")]
[InlineData(null, null)]
public void KafkaBinaryMessageTombstoneTest(string? contentType, object? expectedDecodedResult)
Expand Down