This repository was archived by the owner on Dec 14, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Issue #668: Sample should be covered by a basic functional test. #1056
Closed
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,3 @@ | ||
| { | ||
| "sources": ["src", "test\\WebSites"] | ||
| "sources": ["src", "test\\WebSites", "samples"] | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| using System; | ||
| using Microsoft.AspNet.Builder; | ||
| using Microsoft.AspNet.Mvc; | ||
| using Microsoft.AspNet.Routing; | ||
| using Microsoft.Framework.ConfigurationModel; | ||
| using Microsoft.Framework.DependencyInjection; | ||
|
|
@@ -21,12 +22,11 @@ public void Configure(IBuilder app) | |
| app.UseFileServer(); | ||
| #if ASPNET50 | ||
| var configuration = new Configuration() | ||
| .AddJsonFile(@"App_Data\config.json") | ||
| .AddEnvironmentVariables(); | ||
|
|
||
| .AddJsonFile(@"App_Data\config.json") | ||
| .AddEnvironmentVariables(); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Try to avoid doing autoformats and the like when making small changes like this, it just makes it harder to review. |
||
| string diSystem; | ||
|
|
||
| if (configuration.TryGet("DependencyInjection", out diSystem) && | ||
| if (configuration.TryGet("DependencyInjection", out diSystem) && | ||
| diSystem.Equals("AutoFac", StringComparison.OrdinalIgnoreCase)) | ||
| { | ||
| app.UseMiddleware<MonitoringMiddlware>(); | ||
|
|
@@ -36,8 +36,12 @@ public void Configure(IBuilder app) | |
| services.AddMvc(); | ||
| services.AddSingleton<PassThroughAttribute>(); | ||
| services.AddSingleton<UserNameService>(); | ||
| services.AddTransient<ITestService, TestService>(); | ||
| services.AddTransient<ITestService, TestService>(); | ||
| services.Add(OptionsServices.GetDefaultServices()); | ||
| // Setup services with a test AssemblyProvider so that only the | ||
| // sample's assemblies are loaded. This prevents loading controllers from other assemblies | ||
| // when the sample is used in the Functional Tests. | ||
| services.AddTransient<IControllerAssemblyProvider, TestAssemblyProvider<Startup>>(); | ||
|
|
||
| // Create the autofac container | ||
| ContainerBuilder builder = new ContainerBuilder(); | ||
|
|
@@ -63,6 +67,10 @@ public void Configure(IBuilder app) | |
| services.AddSingleton<PassThroughAttribute>(); | ||
| services.AddSingleton<UserNameService>(); | ||
| services.AddTransient<ITestService, TestService>(); | ||
| // Setup services with a test AssemblyProvider so that only the | ||
| // sample's assemblies are loaded. This prevents loading controllers from other assemblies | ||
| // when the sample is used in the Functional Tests. | ||
| services.AddTransient<IControllerAssemblyProvider, TestAssemblyProvider<Startup>>(); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Mention here that this sample is used in functional test, and it prevents including extra controllers. |
||
| }); | ||
| } | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| // Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. | ||
| // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
|
||
| using System.Collections.Generic; | ||
| using System.Reflection; | ||
| using Microsoft.AspNet.Mvc; | ||
|
|
||
| namespace MvcSample.Web | ||
| { | ||
| /// <summary> | ||
| /// Limits MVC to use a single Assembly for controller discovery. | ||
| /// This is used by the functional test to limit the Controller discovery to | ||
| /// MvcSample.Web Assembly alone. | ||
| /// The sample should work in the absense of this file. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// This is a generic type because it needs to instantiated by a service provider to replace | ||
| /// a built-in MVC service. | ||
| /// </remarks> | ||
| public class TestAssemblyProvider<T> : IControllerAssemblyProvider | ||
| { | ||
| public TestAssemblyProvider() | ||
| { | ||
| CandidateAssemblies = new Assembly[] { typeof(T).GetTypeInfo().Assembly }; | ||
| } | ||
|
|
||
| public IEnumerable<Assembly> CandidateAssemblies { get; private set; } | ||
| } | ||
| } |
114 changes: 114 additions & 0 deletions
114
test/Microsoft.AspNet.Mvc.FunctionalTests/MvcSampleTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,114 @@ | ||
| // Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. | ||
| // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
|
||
| using System; | ||
| using System.IO; | ||
| using System.Net; | ||
| using System.Net.Http; | ||
| using System.Net.Http.Headers; | ||
| using System.Text; | ||
| using System.Threading.Tasks; | ||
| using Microsoft.AspNet.Builder; | ||
| using Microsoft.AspNet.TestHost; | ||
| using Xunit; | ||
|
|
||
| namespace Microsoft.AspNet.Mvc.FunctionalTests | ||
| { | ||
| public class MvcSampleTests | ||
| { | ||
| // Path relative to Mvc\\test\Microsoft.AspNet.Mvc.FunctionalTests | ||
| private readonly IServiceProvider _services = | ||
| TestHelper.CreateServices("MvcSample.Web", Path.Combine("..", "..", "samples")); | ||
| private readonly Action<IBuilder> _app = new MvcSample.Web.Startup().Configure; | ||
|
|
||
| [Fact] | ||
| public async Task Home_Index_ReturnsSuccess() | ||
| { | ||
| // Arrange | ||
| var server = TestServer.Create(_services, _app); | ||
| var client = server.CreateClient(); | ||
|
|
||
| // Act | ||
| var response = await client.GetAsync("http://localhost/Home/Index"); | ||
|
|
||
| // Assert | ||
| Assert.NotNull(response); | ||
| Assert.Equal(HttpStatusCode.OK, response.StatusCode); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task Home_NotFoundAction_Returns404() | ||
| { | ||
| // Arrange | ||
| var server = TestServer.Create(_services, _app); | ||
| var client = server.CreateClient(); | ||
|
|
||
| // Act | ||
| var response = await client.GetAsync("http://localhost/Home/NotFound"); | ||
|
|
||
| // Assert | ||
| Assert.NotNull(response); | ||
| Assert.Equal(HttpStatusCode.NotFound, response.StatusCode); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task Home_CreateUser_ReturnsXmlBasedOnAcceptHeader() | ||
| { | ||
| // Arrange | ||
| var server = TestServer.Create(_services, _app); | ||
| var client = server.CreateClient(); | ||
| var request = new HttpRequestMessage(HttpMethod.Get, "http://localhost/Home/ReturnUser"); | ||
| request.Headers.Accept.Add(MediaTypeWithQualityHeaderValue.Parse("application/xml;charset=utf-8")); | ||
|
|
||
| // Act | ||
| var response = await client.SendAsync(request); | ||
|
|
||
| // Assert | ||
| Assert.NotNull(response); | ||
| Assert.Equal(HttpStatusCode.OK, response.StatusCode); | ||
| Assert.Equal("<User xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=" + | ||
| "\"http://schemas.datacontract.org/2004/07/MvcSample.Web.Models\"><About>I like playing Football" + | ||
| "</About><Address>My address</Address><Age>13</Age><Alive>true</Alive><Dependent><About i:nil=\"true\" />" + | ||
| "<Address>Dependents address</Address><Age>0</Age><Alive>false</Alive><Dependent i:nil=\"true\" />" + | ||
| "<GPA>0</GPA><Log i:nil=\"true\" /><Name>Dependents name</Name><Password i:nil=\"true\" />" + | ||
| "<Profession i:nil=\"true\" /></Dependent><GPA>13.37</GPA><Log i:nil=\"true\" />" + | ||
| "<Name>My name</Name><Password>Secure string</Password><Profession>Software Engineer</Profession></User>", | ||
| new StreamReader(await response.Content.ReadAsStreamAsync(), Encoding.UTF8).ReadToEnd()); | ||
| } | ||
|
|
||
| [Theory] | ||
| [InlineData("http://localhost/Filters/ChallengeUser", HttpStatusCode.Unauthorized)] | ||
| [InlineData("http://localhost/Filters/AllGranted", HttpStatusCode.Unauthorized)] | ||
| [InlineData("http://localhost/Filters/NotGrantedClaim", HttpStatusCode.Unauthorized)] | ||
| public async Task FiltersController_Tests(string url, HttpStatusCode statusCode) | ||
| { | ||
| // Arrange | ||
| var server = TestServer.Create(_services, _app); | ||
| var client = server.CreateClient(); | ||
|
|
||
| // Act | ||
| var response = await client.GetAsync(url); | ||
|
|
||
| // Assert | ||
| Assert.NotNull(response); | ||
| Assert.Equal(statusCode, response.StatusCode); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task FiltersController_Crash_ThrowsException() | ||
| { | ||
| // Arrange | ||
| var server = TestServer.Create(_services, _app); | ||
| var client = server.CreateClient(); | ||
|
|
||
| // Act | ||
| var response = await client.GetAsync("http://localhost/Filters/Crash?message=HelloWorld"); | ||
|
|
||
| // Assert | ||
| Assert.NotNull(response); | ||
| Assert.Equal(HttpStatusCode.OK, response.StatusCode); | ||
| Assert.Equal("Boom HelloWorld", | ||
| new StreamReader(await response.Content.ReadAsStreamAsync(), Encoding.UTF8).ReadToEnd()); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this needed? Worked before, what changed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With this PR, Functional tests started accessing samples.