From dbb69ef952089e7d652033febac74177327427f5 Mon Sep 17 00:00:00 2001 From: "N. Taylor Mullen" Date: Fri, 8 Jan 2016 16:32:32 -0800 Subject: [PATCH] Add `TagHelperSample.Web` functional tests. - Validated that each of `TagHelperSample.Web`s sites work properly. - Found that the `ConditionalCommentTagHelper` page wasn't working as intended due to never being updated after `TagHelperContent` encoding changes. - Added content verification for the `TagHelper/ConditionalComment` endpoint only since it's static HTML. #3530 --- .../TagHelpers/ConditionalCommentTagHelper.cs | 16 +- samples/TagHelperSample.Web/readme.md | 12 +- .../TagHelperSample.Web/wwwroot/web.config | 9 + .../TagHelperSampleTest.cs | 187 +++++++++++++++--- .../TagHelperSample.Web.Home.Index.html | 76 +++++++ ...mple.Web.TagHelper.ConditionalComment.html | 19 ++ 6 files changed, 285 insertions(+), 34 deletions(-) create mode 100644 samples/TagHelperSample.Web/wwwroot/web.config create mode 100644 test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/TagHelperSample.Web.Home.Index.html create mode 100644 test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/TagHelperSample.Web.TagHelper.ConditionalComment.html diff --git a/samples/TagHelperSample.Web/TagHelpers/ConditionalCommentTagHelper.cs b/samples/TagHelperSample.Web/TagHelpers/ConditionalCommentTagHelper.cs index aa25eecfae..a1f0a640fc 100644 --- a/samples/TagHelperSample.Web/TagHelpers/ConditionalCommentTagHelper.cs +++ b/samples/TagHelperSample.Web/TagHelpers/ConditionalCommentTagHelper.cs @@ -23,15 +23,15 @@ namespace TagHelperSample.Web modeModifier = "--"; } - output.PreContent.Append(""); + output.PreContent.AppendHtml(""); - output.PostContent.Append(""); + output.PostContent.AppendHtml(""); } } } diff --git a/samples/TagHelperSample.Web/readme.md b/samples/TagHelperSample.Web/readme.md index 63af81f326..69d598f4c3 100644 --- a/samples/TagHelperSample.Web/readme.md +++ b/samples/TagHelperSample.Web/readme.md @@ -1,4 +1,14 @@ TagHelperSample.Web === This sample web project illustrates TagHelper use. Please build from root -(`.\build.cmd` on Windows; `./build.sh` elsewhere) before using this site. \ No newline at end of file +(`.\build.cmd` on Windows; `./build.sh` elsewhere) before using this site. + +### What this sample contains +1. Creating, editing and viewing custom users with validation. + 1. `/` lists current users. + 2. `/Home/Create` create a new user. + 3. `/Home/Edit/{id:int}` edit a user. +2. View Components and nested caching in views. + 1. `/Movies/` Current movie ratings and critic quotes. Click on the buttons on the page to update values in the side bar. +3. Custom TagHelper to enable browser detection based content. + 1. `/TagHelper/ConditionalComment` Shows different content if your browser is IE7. diff --git a/samples/TagHelperSample.Web/wwwroot/web.config b/samples/TagHelperSample.Web/wwwroot/web.config new file mode 100644 index 0000000000..9a0d90abf8 --- /dev/null +++ b/samples/TagHelperSample.Web/wwwroot/web.config @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/test/Microsoft.AspNet.Mvc.FunctionalTests/TagHelperSampleTest.cs b/test/Microsoft.AspNet.Mvc.FunctionalTests/TagHelperSampleTest.cs index c1770ab59d..942b80c18d 100644 --- a/test/Microsoft.AspNet.Mvc.FunctionalTests/TagHelperSampleTest.cs +++ b/test/Microsoft.AspNet.Mvc.FunctionalTests/TagHelperSampleTest.cs @@ -1,8 +1,13 @@ // Copyright (c) .NET Foundation. 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.Collections.Generic; +using System.Linq; using System.Net; using System.Net.Http; +using System.Net.Http.Headers; +using System.Reflection; using System.Threading.Tasks; using Microsoft.AspNet.Testing; using Xunit; @@ -18,44 +23,176 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests public HttpClient Client { get; } - public static TheoryData PathData + [Fact] + public async Task HomeController_Index_ReturnsExpectedContent() + { + // Arrange + var expectedMediaType = MediaTypeHeaderValue.Parse("text/html; charset=utf-8"); + var outputFile = "compiler/resources/TagHelperSample.Web.Home.Index.html"; + var resourceAssembly = typeof(TagHelperSampleTest).GetTypeInfo().Assembly; + var expectedContent = await ResourceFile.ReadResourceAsync(resourceAssembly, outputFile, sourceFile: false); + + // Act + var response = await Client.GetAsync("http://localhost/"); + var responseContent = await response.Content.ReadAsStringAsync(); + + // Assert + Assert.Equal(HttpStatusCode.OK, response.StatusCode); + Assert.Equal(expectedMediaType, response.Content.Headers.ContentType); + +#if GENERATE_BASELINES + ResourceFile.UpdateFile(resourceAssembly, outputFile, expectedContent, responseContent); +#else + Assert.Equal(expectedContent, responseContent, ignoreLineEndingDifferences: true); +#endif + } + + + [Fact] + public async Task HomeController_Create_Get_ReturnsSuccess() + { + // Act + var response = await Client.GetAsync("http://localhost/Home/Create"); + + // Assert + Assert.Equal(HttpStatusCode.OK, response.StatusCode); + } + + [Fact] + public async Task HomeController_Create_Post_ReturnsSuccess() + { + // Arrange + var createBillyContent = CreateUserFormContent("Billy", "2000-11-30", 0, "hello"); + + // Act + var response = await Client.PostAsync("http://localhost/Home/Create", createBillyContent); + + // Assert + AssertRedirectsToHome(response); + } + + [Fact] + public async Task HomeController_Edit_Get_ReturnsSuccess() + { + // Arrange + var createBillyContent = CreateUserFormContent("Billy", "2000-11-30", 0, "hello"); + var createBilly = await Client.PostAsync("http://localhost/Home/Create", createBillyContent); + + // Guard + AssertRedirectsToHome(createBilly); + + // Act + var response = await Client.GetAsync("http://localhost/Home/Edit/0"); + + // Assert + Assert.Equal(HttpStatusCode.OK, response.StatusCode); + } + + [Fact] + public async Task HomeController_Edit_Post_ReturnsSuccess() + { + // Arrange + var createBillyContent = CreateUserFormContent("Billy", "2000-11-30", 0, "hello"); + var changeBillyContent = CreateUserFormContent("Bobby", "1999-11-30", 1, "howdy"); + var createBilly = await Client.PostAsync("http://localhost/Home/Create", createBillyContent); + + // Guard + AssertRedirectsToHome(createBilly); + + // Act + var changeBilly = await Client.PostAsync("http://localhost/Home/Edit/0", changeBillyContent); + + // Assert + AssertRedirectsToHome(changeBilly); + } + + public static TheoryData MoviesControllerPageData { get { - var data = new TheoryData + return new TheoryData>> { - "/Home/Create", - "/Home/Create?Name=Billy&Blurb=hello&DateOfBirth=2000-11-30&YearsEmployeed=0", - "/Home/Create?Name=Joe&Blurb=goodbye&DateOfBirth=1980-10-20&YearsEmployeed=1", - "/Home/Edit/0", - "/Home/Edit/0?Name=Bobby&Blurb=howdy&DateOfBirth=1999-11-30&YearsEmployeed=1", - "/Home/Edit/0?Name=Bobby&Blurb=howdy&DateOfBirth=1999-11-30&YearsEmployeed=2", - "/Home/Edit/1", - "/Home/Edit/1?Name=Jack&Blurb=goodbye&DateOfBirth=1979-10-20&YearsEmployeed=4", + async (client) => await client.GetAsync("http://localhost/Movies"), + async (client) => await client.PostAsync( + "http://localhost/Movies/UpdateMovieRatings", + new FormUrlEncodedContent(Enumerable.Empty>())), + async (client) => await client.PostAsync( + "http://localhost/Movies/UpdateCriticsQuotes", + new FormUrlEncodedContent(Enumerable.Empty>())), + async (client) => + { + await client.PostAsync( + "http://localhost/Movies/UpdateCriticsQuotes", + new FormUrlEncodedContent(Enumerable.Empty>())); + await client.PostAsync( + "http://localhost/Movies/UpdateCriticsQuotes", + new FormUrlEncodedContent(Enumerable.Empty>())); + + return await client.GetAsync("http://localhost/Movies/Index"); + } }; - - // Three paths hit aspnet/External#50 with Mono on Mac. - if (!TestPlatformHelper.IsMac || !TestPlatformHelper.IsMono) - { - data.Add(string.Empty); - data.Add("/"); - data.Add("/Home/Index"); - } - - return data; } } [Theory] - [MemberData(nameof(PathData))] - public async Task Home_Pages_ReturnSuccess(string path) + [MemberData(nameof(MoviesControllerPageData))] + public async Task MoviesController_Pages_ReturnSuccess(Func> requestPage) { - // Arrange & Act - var response = await Client.GetAsync("http://localhost" + path); + // Act + var response = await requestPage(Client); // Assert - Assert.NotNull(response); Assert.Equal(HttpStatusCode.OK, response.StatusCode); } + + [Fact] + public async Task TagHelperController_ConditionalComment_ReturnsExpectedContent() + { + // Arrange + var expectedMediaType = MediaTypeHeaderValue.Parse("text/html; charset=utf-8"); + var outputFile = "compiler/resources/TagHelperSample.Web.TagHelper.ConditionalComment.html"; + var resourceAssembly = typeof(TagHelperSampleTest).GetTypeInfo().Assembly; + var expectedContent = await ResourceFile.ReadResourceAsync(resourceAssembly, outputFile, sourceFile: false); + + // Act + var response = await Client.GetAsync("http://localhost/TagHelper/ConditionalComment"); + var responseContent = await response.Content.ReadAsStringAsync(); + + // Assert + Assert.Equal(HttpStatusCode.OK, response.StatusCode); + Assert.Equal(expectedMediaType, response.Content.Headers.ContentType); + +#if GENERATE_BASELINES + ResourceFile.UpdateFile(resourceAssembly, outputFile, expectedContent, responseContent); +#else + Assert.Equal(expectedContent, responseContent, ignoreLineEndingDifferences: true); +#endif + } + + private static void AssertRedirectsToHome(HttpResponseMessage response) + { + Assert.Equal(HttpStatusCode.Redirect, response.StatusCode); + var redirectLocations = response.Headers.GetValues("Location"); + var redirectsTo = Assert.Single(redirectLocations); + Assert.Equal("/", redirectsTo, StringComparer.Ordinal); + } + + private static HttpContent CreateUserFormContent( + string name, + string dateOfBirth, + int yearsEmployeed, + string blurb) + { + var form = new List>() + { + new KeyValuePair("Name", name), + new KeyValuePair("DateOfBirth", dateOfBirth), + new KeyValuePair("YearsEmployeed", yearsEmployeed.ToString()), + new KeyValuePair("Blurb", blurb), + }; + var content = new FormUrlEncodedContent(form); + + return content; + } } } \ No newline at end of file diff --git a/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/TagHelperSample.Web.Home.Index.html b/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/TagHelperSample.Web.Home.Index.html new file mode 100644 index 0000000000..7e2e8ddcd0 --- /dev/null +++ b/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/TagHelperSample.Web.Home.Index.html @@ -0,0 +1,76 @@ + + + + + + + + + + + + + + + + + +

Index

+
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+

+ Edit +

+
+ + +
+
+ + +
+
+ + +
+
+ + +
+

+ Edit +

+
+

+ Create New +

+ + + \ No newline at end of file diff --git a/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/TagHelperSample.Web.TagHelper.ConditionalComment.html b/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/TagHelperSample.Web.TagHelper.ConditionalComment.html new file mode 100644 index 0000000000..2f335e19bc --- /dev/null +++ b/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/TagHelperSample.Web.TagHelper.ConditionalComment.html @@ -0,0 +1,19 @@ + + + + + + + + + + + +

Content visible to all browsers newer than Internet Explorer 7.

+ + + + + \ No newline at end of file