Moved away from using internal apis in tests

Signed-off-by: James Thompson <thompson.tomo@outlook.com>
This commit is contained in:
James Thompson 2024-03-29 11:37:38 +11:00
parent af662ecdef
commit 2eef08f8b7
4 changed files with 24 additions and 15 deletions

View File

@ -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);
}
}
}

View File

@ -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,17 @@ 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;

View File

@ -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" />

View File

@ -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();