From 6cc47cd5f17f08ede84a173a5095cf04dc7ff887 Mon Sep 17 00:00:00 2001 From: "N. Taylor Mullen" Date: Mon, 16 Jun 2014 16:51:39 -0700 Subject: [PATCH] Add Autofac functional test. Added very simple Autofac functional test that validates that we can add custom classes to the DI system as well as just use the ones provided to us. #669 --- Mvc.sln | 15 ++++++- .../DependencyResolverTests.cs | 39 ++++++++++++++++++ ...Microsoft.AspNet.Mvc.FunctionalTests.kproj | 1 + .../project.json | 1 + .../AutofacWebSite/AutofacWebSite.kproj | 40 +++++++++++++++++++ .../Controllers/BasicController.cs | 12 ++++++ .../Controllers/DIController.cs | 19 +++++++++ .../AutofacWebSite/HelloWorldBuilder.cs | 10 +++++ test/WebSites/AutofacWebSite/Startup.cs | 35 ++++++++++++++++ .../AutofacWebSite/Views/Basic/Index.cshtml | 1 + .../AutofacWebSite/Views/DI/Index.cshtml | 3 ++ test/WebSites/AutofacWebSite/project.json | 12 ++++++ 12 files changed, 187 insertions(+), 1 deletion(-) create mode 100644 test/Microsoft.AspNet.Mvc.FunctionalTests/DependencyResolverTests.cs create mode 100644 test/WebSites/AutofacWebSite/AutofacWebSite.kproj create mode 100644 test/WebSites/AutofacWebSite/Controllers/BasicController.cs create mode 100644 test/WebSites/AutofacWebSite/Controllers/DIController.cs create mode 100644 test/WebSites/AutofacWebSite/HelloWorldBuilder.cs create mode 100644 test/WebSites/AutofacWebSite/Startup.cs create mode 100644 test/WebSites/AutofacWebSite/Views/Basic/Index.cshtml create mode 100644 test/WebSites/AutofacWebSite/Views/DI/Index.cshtml create mode 100644 test/WebSites/AutofacWebSite/project.json diff --git a/Mvc.sln b/Mvc.sln index f5b4c9e0f9..3620af64f9 100644 --- a/Mvc.sln +++ b/Mvc.sln @@ -1,7 +1,7 @@  Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio 14 -VisualStudioVersion = 14.0.21806.0 +VisualStudioVersion = 14.0.21722.1 MinimumVisualStudioVersion = 10.0.40219.1 Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "samples", "samples", "{DAAE4C74-D06F-4874-A166-33305D2643CE}" EndProject @@ -41,6 +41,8 @@ Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "ActivatorWebSite", "test\We EndProject Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "InlineConstraintsWebSite", "test\WebSites\InlineConstraintsWebSite\InlineConstraintsWebSite.kproj", "{EA34877F-1AC1-42B7-B4E6-15A093F40CAE}" EndProject +Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "AutofacWebSite", "test\WebSites\AutofacWebSite\AutofacWebSite.kproj", "{07C0E921-FCBB-458C-AC11-3D01CE68B16B}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -201,6 +203,16 @@ Global {EA34877F-1AC1-42B7-B4E6-15A093F40CAE}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU {EA34877F-1AC1-42B7-B4E6-15A093F40CAE}.Release|Mixed Platforms.Build.0 = Release|Any CPU {EA34877F-1AC1-42B7-B4E6-15A093F40CAE}.Release|x86.ActiveCfg = Release|Any CPU + {07C0E921-FCBB-458C-AC11-3D01CE68B16B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {07C0E921-FCBB-458C-AC11-3D01CE68B16B}.Debug|Any CPU.Build.0 = Debug|Any CPU + {07C0E921-FCBB-458C-AC11-3D01CE68B16B}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU + {07C0E921-FCBB-458C-AC11-3D01CE68B16B}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU + {07C0E921-FCBB-458C-AC11-3D01CE68B16B}.Debug|x86.ActiveCfg = Debug|Any CPU + {07C0E921-FCBB-458C-AC11-3D01CE68B16B}.Release|Any CPU.ActiveCfg = Release|Any CPU + {07C0E921-FCBB-458C-AC11-3D01CE68B16B}.Release|Any CPU.Build.0 = Release|Any CPU + {07C0E921-FCBB-458C-AC11-3D01CE68B16B}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU + {07C0E921-FCBB-458C-AC11-3D01CE68B16B}.Release|Mixed Platforms.Build.0 = Release|Any CPU + {07C0E921-FCBB-458C-AC11-3D01CE68B16B}.Release|x86.ActiveCfg = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -222,5 +234,6 @@ Global {34DF1487-12C6-476C-BE0A-F31DF1939AE5} = {16703B76-C9F7-4C75-AE6C-53D92E308E3C} {DB79BCBA-9538-4A53-87D9-77728E2BAA39} = {16703B76-C9F7-4C75-AE6C-53D92E308E3C} {EA34877F-1AC1-42B7-B4E6-15A093F40CAE} = {16703B76-C9F7-4C75-AE6C-53D92E308E3C} + {07C0E921-FCBB-458C-AC11-3D01CE68B16B} = {16703B76-C9F7-4C75-AE6C-53D92E308E3C} EndGlobalSection EndGlobal diff --git a/test/Microsoft.AspNet.Mvc.FunctionalTests/DependencyResolverTests.cs b/test/Microsoft.AspNet.Mvc.FunctionalTests/DependencyResolverTests.cs new file mode 100644 index 0000000000..20f43c18a6 --- /dev/null +++ b/test/Microsoft.AspNet.Mvc.FunctionalTests/DependencyResolverTests.cs @@ -0,0 +1,39 @@ +using System; +using System.IO; +using System.Threading.Tasks; +using AutofacWebSite; +using Microsoft.AspNet.Builder; +using Microsoft.AspNet.Http; +using Microsoft.AspNet.TestHost; +using Xunit; + +namespace Microsoft.AspNet.Mvc.FunctionalTests +{ + public class DependencyResolverTests + { + [Theory] + [InlineData("http://localhost/di", "

Builder Output: Hello from builder.

")] + [InlineData("http://localhost/basic", "

Hello From Basic View

")] + public async Task AutofacDIContainerCanUseMvc(string url, string expectedResponseBody) + { + // Arrange + var provider = TestHelper.CreateServices("AutofacWebSite"); + Action app = new Startup().Configure; + TestServer server = null; + HttpResponse response = null; + + // Act & Assert + await Assert.DoesNotThrowAsync(async () => + { + // This essentially calls into the Startup.Configuration method + server = TestServer.Create(provider, app); + + // Make a request to start resolving DI pieces + response = await server.Handler.GetAsync(url); + }); + + var actualResponseBody = new StreamReader(response.Body).ReadToEnd(); + Assert.Equal(expectedResponseBody, actualResponseBody); + } + } +} \ No newline at end of file diff --git a/test/Microsoft.AspNet.Mvc.FunctionalTests/Microsoft.AspNet.Mvc.FunctionalTests.kproj b/test/Microsoft.AspNet.Mvc.FunctionalTests/Microsoft.AspNet.Mvc.FunctionalTests.kproj index 6285f25067..cfa99bcd09 100644 --- a/test/Microsoft.AspNet.Mvc.FunctionalTests/Microsoft.AspNet.Mvc.FunctionalTests.kproj +++ b/test/Microsoft.AspNet.Mvc.FunctionalTests/Microsoft.AspNet.Mvc.FunctionalTests.kproj @@ -31,6 +31,7 @@ + diff --git a/test/Microsoft.AspNet.Mvc.FunctionalTests/project.json b/test/Microsoft.AspNet.Mvc.FunctionalTests/project.json index 1df30bf1d6..73f3a9a6a0 100644 --- a/test/Microsoft.AspNet.Mvc.FunctionalTests/project.json +++ b/test/Microsoft.AspNet.Mvc.FunctionalTests/project.json @@ -6,6 +6,7 @@ "BasicWebSite": "", "ActivatorWebSite": "", "InlineConstraintsWebSite": "", + "AutofacWebSite": "", "Microsoft.AspNet.TestHost": "1.0.0-*", "Microsoft.Framework.Runtime.Interfaces": "1.0.0-*", "Xunit.KRunner": "1.0.0-*", diff --git a/test/WebSites/AutofacWebSite/AutofacWebSite.kproj b/test/WebSites/AutofacWebSite/AutofacWebSite.kproj new file mode 100644 index 0000000000..95ea09b874 --- /dev/null +++ b/test/WebSites/AutofacWebSite/AutofacWebSite.kproj @@ -0,0 +1,40 @@ + + + + 12.0 + $(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion) + + + + 07c0e921-fcbb-458c-ac11-3d01ce68b16b + Web + + + ConsoleDebugger + + + WebDebugger + + + + + + + 2.0 + + + 38823 + + + + + + + + + + + + + + \ No newline at end of file diff --git a/test/WebSites/AutofacWebSite/Controllers/BasicController.cs b/test/WebSites/AutofacWebSite/Controllers/BasicController.cs new file mode 100644 index 0000000000..968295645f --- /dev/null +++ b/test/WebSites/AutofacWebSite/Controllers/BasicController.cs @@ -0,0 +1,12 @@ +using Microsoft.AspNet.Mvc; + +namespace AutofacWebSite.Controllers +{ + public class BasicController : Controller + { + public IActionResult Index() + { + return View(); + } + } +} diff --git a/test/WebSites/AutofacWebSite/Controllers/DIController.cs b/test/WebSites/AutofacWebSite/Controllers/DIController.cs new file mode 100644 index 0000000000..bc2be22ed0 --- /dev/null +++ b/test/WebSites/AutofacWebSite/Controllers/DIController.cs @@ -0,0 +1,19 @@ +using Microsoft.AspNet.Mvc; + +namespace AutofacWebSite.Controllers +{ + public class DIController : Controller + { + public DIController(HelloWorldBuilder builder) + { + Builder = builder; + } + + public HelloWorldBuilder Builder { get; private set; } + + public IActionResult Index() + { + return View(model: Builder.Build()); + } + } +} diff --git a/test/WebSites/AutofacWebSite/HelloWorldBuilder.cs b/test/WebSites/AutofacWebSite/HelloWorldBuilder.cs new file mode 100644 index 0000000000..c7665d3ddd --- /dev/null +++ b/test/WebSites/AutofacWebSite/HelloWorldBuilder.cs @@ -0,0 +1,10 @@ +namespace AutofacWebSite +{ + public class HelloWorldBuilder + { + public string Build() + { + return "Hello from builder."; + } + } +} \ No newline at end of file diff --git a/test/WebSites/AutofacWebSite/Startup.cs b/test/WebSites/AutofacWebSite/Startup.cs new file mode 100644 index 0000000000..c39aff22ce --- /dev/null +++ b/test/WebSites/AutofacWebSite/Startup.cs @@ -0,0 +1,35 @@ +using System; +using Autofac; +using Microsoft.AspNet.Builder; +using Microsoft.AspNet.Routing; +using Microsoft.Framework.DependencyInjection; +using Microsoft.Framework.DependencyInjection.Autofac; + +namespace AutofacWebSite +{ + public class Startup + { + public void Configure(IBuilder app) + { + app.UseServices(services => { + services.AddMvc(); + services.AddTransient(); + + var builder = new ContainerBuilder(); + AutofacRegistration.Populate(builder, + services, + fallbackServiceProvider: app.ApplicationServices); + + var container = builder.Build(); + + return container.Resolve(); + }); + + app.UseMvc(routes => + { + // This default route is for running the project directly. + routes.MapRoute("default", "{controller=DI}/{action=Index}"); + }); + } + } +} diff --git a/test/WebSites/AutofacWebSite/Views/Basic/Index.cshtml b/test/WebSites/AutofacWebSite/Views/Basic/Index.cshtml new file mode 100644 index 0000000000..2457ac3a83 --- /dev/null +++ b/test/WebSites/AutofacWebSite/Views/Basic/Index.cshtml @@ -0,0 +1 @@ +

Hello From Basic View

\ No newline at end of file diff --git a/test/WebSites/AutofacWebSite/Views/DI/Index.cshtml b/test/WebSites/AutofacWebSite/Views/DI/Index.cshtml new file mode 100644 index 0000000000..d4c4236f10 --- /dev/null +++ b/test/WebSites/AutofacWebSite/Views/DI/Index.cshtml @@ -0,0 +1,3 @@ +@model string + +

Builder Output: @Model

\ No newline at end of file diff --git a/test/WebSites/AutofacWebSite/project.json b/test/WebSites/AutofacWebSite/project.json new file mode 100644 index 0000000000..e104b3b06c --- /dev/null +++ b/test/WebSites/AutofacWebSite/project.json @@ -0,0 +1,12 @@ +{ + "dependencies": { + "Microsoft.AspNet.Server.IIS": "1.0.0-*", + "Microsoft.AspNet.Mvc": "", + "Microsoft.Framework.DependencyInjection.Autofac": "1.0.0-*", + "Autofac": "3.3.0" + }, + "configurations" : { + "net45" : { }, + "k10" : { } + } +}