|
| 1 | +// Copyright (c) .NET Foundation. All rights reserved. |
| 2 | +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Collections.Generic; |
| 6 | +using System.Linq; |
| 7 | +using System.Threading.Tasks; |
| 8 | +using Microsoft.AspNet.Builder; |
| 9 | +using Microsoft.Extensions.DependencyInjection; |
| 10 | +using Xunit; |
| 11 | + |
| 12 | +namespace Microsoft.AspNet.Owin |
| 13 | +{ |
| 14 | + using AppFunc = Func<IDictionary<string, object>, Task>; |
| 15 | + using CreateMiddleware = Func< |
| 16 | + Func<IDictionary<string, object>, Task>, |
| 17 | + Func<IDictionary<string, object>, Task> |
| 18 | + >; |
| 19 | + using AddMiddleware = Action<Func< |
| 20 | + Func<IDictionary<string, object>, Task>, |
| 21 | + Func<IDictionary<string, object>, Task> |
| 22 | + >>; |
| 23 | + |
| 24 | + public class OwinExtensionTests |
| 25 | + { |
| 26 | + static AppFunc notFound = env => new Task(() => { env["owin.ResponseStatusCode"] = 404; }); |
| 27 | + |
| 28 | + [Fact] |
| 29 | + public void OwinConfigureServiceProviderAddsServices() |
| 30 | + { |
| 31 | + var list = new List<CreateMiddleware>(); |
| 32 | + AddMiddleware build = list.Add; |
| 33 | + IServiceProvider serviceProvider = null; |
| 34 | + FakeService fakeService = null; |
| 35 | + |
| 36 | + var builder = build.UseBuilder(applicationBuilder => |
| 37 | + { |
| 38 | + serviceProvider = applicationBuilder.ApplicationServices; |
| 39 | + applicationBuilder.Run(async context => |
| 40 | + { |
| 41 | + fakeService = context.ApplicationServices.GetService<FakeService>(); |
| 42 | + }); |
| 43 | + }, new ServiceCollection().AddSingleton(new FakeService()).BuildServiceProvider()); |
| 44 | + |
| 45 | + list.Reverse(); |
| 46 | + list.Aggregate(notFound, (next, middleware) => middleware(next)).Invoke(new Dictionary<string, object>()); |
| 47 | + |
| 48 | + Assert.NotNull(fakeService); |
| 49 | + Assert.NotNull(serviceProvider.GetService<FakeService>()); |
| 50 | + } |
| 51 | + |
| 52 | + [Fact] |
| 53 | + public void OwinDefaultNoServices() |
| 54 | + { |
| 55 | + var list = new List<CreateMiddleware>(); |
| 56 | + AddMiddleware build = list.Add; |
| 57 | + IServiceProvider serviceProvider = null; |
| 58 | + FakeService fakeService = null; |
| 59 | + bool builderExecuted = false; |
| 60 | + bool applicationExecuted = false; |
| 61 | + |
| 62 | + var builder = build.UseBuilder(applicationBuilder => |
| 63 | + { |
| 64 | + builderExecuted = true; |
| 65 | + serviceProvider = applicationBuilder.ApplicationServices; |
| 66 | + applicationBuilder.Run(async context => |
| 67 | + { |
| 68 | + applicationExecuted = true; |
| 69 | + fakeService = context.ApplicationServices.GetService<FakeService>(); |
| 70 | + }); |
| 71 | + }); |
| 72 | + |
| 73 | + list.Reverse(); |
| 74 | + list.Aggregate(notFound, (next, middleware) => middleware(next)).Invoke(new Dictionary<string, object>()); |
| 75 | + |
| 76 | + Assert.True(builderExecuted); |
| 77 | + Assert.Null(fakeService); |
| 78 | + Assert.True(applicationExecuted); |
| 79 | + Assert.Null(serviceProvider); |
| 80 | + } |
| 81 | + |
| 82 | + private class FakeService |
| 83 | + { |
| 84 | + |
| 85 | + } |
| 86 | + } |
| 87 | +} |
0 commit comments