Skip to content

Commit

Permalink
Add a test that verifies the default provider gets restored
Browse files Browse the repository at this point in the history
  • Loading branch information
danielmarbach committed Sep 6, 2024
1 parent 493c4b9 commit bfb54bf
Showing 1 changed file with 37 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,45 @@ public async Task ServiceProviders_Equal()
var middleware = new WorkerRequestServicesMiddleware(next, httpCoordinator.Object);
await middleware.Invoke(httpContext);
}

private class MyService

[Fact]
public async Task DefaultServiceProvider_Restored()
{
var services = new ServiceCollection();
services.AddScoped<MyService>();

var httpContextProvider = services.BuildServiceProvider().CreateScope().ServiceProvider;

// FunctionContext services will be scoped.
var functionContextProvider = services.BuildServiceProvider().CreateScope().ServiceProvider;

RequestDelegate next = (ctxt) =>
{
Assert.Same(functionContextProvider, ctxt.RequestServices);

return Task.CompletedTask;
};

var functionContext = new TestFunctionContext(new TestFunctionDefinition(), new TestFunctionInvocation(), CancellationToken.None,
serviceProvider: functionContextProvider);

var httpContext = new DefaultHttpContext();
httpContext.RequestServices = httpContextProvider;
httpContext.Request.Headers.Add(Constants.CorrelationHeader, functionContext.InvocationId);

var httpCoordinator = new Mock<IHttpCoordinator>();
httpCoordinator
.Setup(p => p.SetHttpContextAsync(functionContext.InvocationId, httpContext))
.ReturnsAsync(functionContext);

var middleware = new WorkerRequestServicesMiddleware(next, httpCoordinator.Object);
await middleware.Invoke(httpContext);

Assert.Same(httpContextProvider, httpContext.RequestServices);
Assert.NotSame(functionContextProvider, httpContext.RequestServices);
}

private class MyService;
}
}

0 comments on commit bfb54bf

Please sign in to comment.