Skip to content

Commit c771a2a

Browse files
committed
Move RequestDelegateFactory tests to be shared with RDG
1 parent 2b3789a commit c771a2a

File tree

4 files changed

+41
-61
lines changed

4 files changed

+41
-61
lines changed

Diff for: src/Http/Http.Extensions/test/RequestDelegateFactoryTests.cs

-59
Original file line numberDiff line numberDiff line change
@@ -2043,7 +2043,6 @@ public static IEnumerable<object[]> JsonContextActions
20432043

20442044
[JsonSerializable(typeof(Todo))]
20452045
[JsonSerializable(typeof(TodoChild))]
2046-
[JsonSerializable(typeof(IAsyncEnumerable<JsonTodo>))]
20472046
private partial class TestJsonContext : JsonSerializerContext
20482047
{ }
20492048

@@ -2075,64 +2074,6 @@ public async Task RequestDelegateWritesAsJsonResponseBody_WithJsonSerializerCont
20752074
Assert.Equal("Write even more tests!", deserializedResponseBody!.Name);
20762075
}
20772076

2078-
[Theory]
2079-
[InlineData(true)]
2080-
[InlineData(false)]
2081-
public async Task RequestDelegateWritesAsJsonResponseBody_UnspeakableType(bool useJsonContext)
2082-
{
2083-
var httpContext = CreateHttpContext();
2084-
httpContext.RequestServices = new ServiceCollection()
2085-
.AddSingleton(LoggerFactory)
2086-
.ConfigureHttpJsonOptions(o => o.SerializerOptions.TypeInfoResolver = useJsonContext ? TestJsonContext.Default : o.SerializerOptions.TypeInfoResolver)
2087-
.BuildServiceProvider();
2088-
2089-
var responseBodyStream = new MemoryStream();
2090-
httpContext.Response.Body = responseBodyStream;
2091-
2092-
Delegate @delegate = IAsyncEnumerable<JsonTodo> () => GetTodosAsync();
2093-
2094-
var rdfOptions = new RequestDelegateFactoryOptions() { ServiceProvider = httpContext.RequestServices };
2095-
var factoryResult = RequestDelegateFactory.Create(@delegate, rdfOptions);
2096-
var requestDelegate = factoryResult.RequestDelegate;
2097-
2098-
await requestDelegate(httpContext);
2099-
2100-
var body = JsonSerializer.Deserialize<JsonTodo[]>(responseBodyStream.ToArray(), new JsonSerializerOptions
2101-
{
2102-
PropertyNameCaseInsensitive = true
2103-
});
2104-
2105-
Assert.NotNull(body);
2106-
Assert.Equal(3, body.Length);
2107-
2108-
var one = Assert.IsType<JsonTodo>(body[0]);
2109-
Assert.Equal(1, one.Id);
2110-
Assert.True(one.IsComplete);
2111-
Assert.Equal("One", one.Name);
2112-
2113-
var two = Assert.IsType<JsonTodo>(body[1]);
2114-
Assert.Equal(2, two.Id);
2115-
Assert.False(two.IsComplete);
2116-
Assert.Equal("Two", two.Name);
2117-
2118-
var three = Assert.IsType<JsonTodoChild>(body[2]);
2119-
Assert.Equal(3, three.Id);
2120-
Assert.True(three.IsComplete);
2121-
Assert.Equal("Three", three.Name);
2122-
Assert.Equal("ThreeChild", three.Child);
2123-
}
2124-
2125-
private static async IAsyncEnumerable<JsonTodo> GetTodosAsync()
2126-
{
2127-
yield return new JsonTodo() { Id = 1, IsComplete = true, Name = "One" };
2128-
2129-
// ensure this is async
2130-
await Task.Yield();
2131-
2132-
yield return new JsonTodo() { Id = 2, IsComplete = false, Name = "Two" };
2133-
yield return new JsonTodoChild() { Id = 3, IsComplete = true, Name = "Three", Child = "ThreeChild" };
2134-
}
2135-
21362077
[Fact]
21372078
public void CreateDelegateThrows_WhenGetJsonTypeInfoFail()
21382079
{

Diff for: src/Http/Http.Extensions/test/RequestDelegateGenerator/RequestDelegateCreationTestBase.cs

-2
Original file line numberDiff line numberDiff line change
@@ -192,8 +192,6 @@ public ServiceProvider CreateServiceProvider(Action<IServiceCollection> configur
192192
{
193193
var serviceCollection = new ServiceCollection();
194194
serviceCollection.AddSingleton(LoggerFactory);
195-
var jsonOptions = new JsonOptions();
196-
serviceCollection.AddSingleton(Options.Create(jsonOptions));
197195
if (configureServices is not null)
198196
{
199197
configureServices(serviceCollection);

Diff for: src/Http/Http.Extensions/test/RequestDelegateGenerator/RequestDelegateCreationTests.Responses.cs

+37
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33
using Microsoft.AspNetCore.Routing.Internal;
4+
using Microsoft.Extensions.DependencyInjection;
45
using Microsoft.Extensions.ObjectPool;
56
using Microsoft.Extensions.Primitives;
67
using System.Text;
@@ -248,4 +249,40 @@ public async Task MapAction_HandlesCompletedTaskReturn()
248249
await endpoints[1].RequestDelegate(httpContext);
249250
await VerifyResponseBodyAsync(httpContext, string.Empty);
250251
}
252+
253+
[Theory]
254+
[InlineData(true)]
255+
[InlineData(false)]
256+
public async Task RequestDelegateWritesAsJsonResponseBody_UnspeakableType(bool useJsonContext)
257+
{
258+
var source = """
259+
app.MapGet("/todos", () => GetTodosAsync());
260+
261+
static async IAsyncEnumerable<JsonTodo> GetTodosAsync()
262+
{
263+
yield return new JsonTodo() { Id = 1, IsComplete = true, Name = "One" };
264+
265+
// ensure this is async
266+
await Task.Yield();
267+
268+
yield return new JsonTodoChild() { Id = 2, IsComplete = false, Name = "Two", Child = "TwoChild" };
269+
}
270+
""";
271+
var (_, compilation) = await RunGeneratorAsync(source);
272+
var serviceProvider = CreateServiceProvider(serviceCollection =>
273+
{
274+
if (useJsonContext)
275+
{
276+
serviceCollection.ConfigureHttpJsonOptions(o => o.SerializerOptions.TypeInfoResolver = SharedTestJsonContext.Default);
277+
}
278+
});
279+
var endpoint = GetEndpointFromCompilation(compilation, serviceProvider: serviceProvider);
280+
281+
var httpContext = CreateHttpContext(serviceProvider);
282+
283+
await endpoint.RequestDelegate(httpContext);
284+
285+
var expectedBody = """[{"id":1,"name":"One","isComplete":true},{"$type":"JsonTodoChild","child":"TwoChild","id":2,"name":"Two","isComplete":false}]""";
286+
await VerifyResponseBodyAsync(httpContext, expectedBody);
287+
}
251288
}

Diff for: src/Http/Http.Extensions/test/RequestDelegateGenerator/SharedTypes.cs

+4
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ public class JsonTodoChild : JsonTodo
6464
public string? Child { get; set; }
6565
}
6666

67+
[JsonSerializable(typeof(IAsyncEnumerable<JsonTodo>))]
68+
public partial class SharedTestJsonContext : JsonSerializerContext
69+
{ }
70+
6771
public class CustomFromBodyAttribute : Attribute, IFromBodyMetadata
6872
{
6973
public bool AllowEmpty { get; set; }

0 commit comments

Comments
 (0)