Skip to content

Commit 1e6eab3

Browse files
committed
#10: tests for IOwinContext registration
1 parent 00d9b11 commit 1e6eab3

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

test/Autofac.Integration.Owin.Test/AutofacAppBuilderExtensionsFixture.cs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using System.Linq;
44
using System.Threading;
5+
using System.Threading.Tasks;
56
using Autofac.Core.Lifetime;
67
using Autofac.Core.Registration;
78
using Autofac.Features.ResolveAnything;
@@ -328,6 +329,51 @@ public void UseMiddlewareFromContainerRequiresInjectorRegistrationFirst()
328329
Assert.Throws<InvalidOperationException>(() => app.Object.UseMiddlewareFromContainer<TestMiddleware>());
329330
}
330331

332+
[Fact]
333+
public async void UseAutofacLifetimeScopeInjectorWithContainerRegistersOwinContextInTheScope()
334+
{
335+
using (var server = TestServer.Create(app =>
336+
{
337+
app.UseAutofacLifetimeScopeInjector(new ContainerBuilder().Build());
338+
app.Run(context =>
339+
{
340+
//we can't directly compare contexts because they are recreated at each step in UseHandlerMidleware
341+
Assert.Same(
342+
context.Environment,
343+
context
344+
.GetAutofacLifetimeScope()
345+
.Resolve<IOwinContext>()
346+
.Environment
347+
);
348+
return Task.FromResult(0);
349+
});
350+
}))
351+
{
352+
await server.HttpClient.GetAsync("/");
353+
}
354+
}
355+
356+
[Fact]
357+
public async void UseAutofacLifetimeScopeInjectorWithExternalScopeDoesNotRegisterOwinContextInTheScope()
358+
{
359+
using (var server = TestServer.Create(app =>
360+
{
361+
app.UseAutofacLifetimeScopeInjector(context => new ContainerBuilder().Build());
362+
app.Run(context =>
363+
{
364+
Assert.Null(
365+
context
366+
.GetAutofacLifetimeScope()
367+
.ResolveOptional<IOwinContext>()
368+
);
369+
return Task.FromResult(0);
370+
});
371+
}))
372+
{
373+
await server.HttpClient.GetAsync("/");
374+
}
375+
}
376+
331377
class TestableLifetimeScope : LifetimeScope
332378
{
333379
public bool ScopeIsDisposed { get; set; }

0 commit comments

Comments
 (0)