public async Task FormTagHelper_GeneratesExpectedContent(bool?optionsAntiForgery)
        {
            // Arrange
            var newServices = new ServiceCollection();

            newServices.InitializeTagHelper <FormTagHelper>((helper, _) => helper.AntiForgery = optionsAntiForgery);
            var server = TestHelper.CreateServer(_app, SiteName,
                                                 services =>
            {
                services.Add(newServices);
                _configureServices(services);
            });
            var client            = server.CreateClient();
            var expectedMediaType = MediaTypeHeaderValue.Parse("text/html; charset=utf-8");

            var outputFile = string.Format(
                "compiler/resources/HtmlGenerationWebSite.HtmlGeneration_Home.Form.Options.AntiForgery.{0}.html",
                optionsAntiForgery?.ToString() ?? "null");
            var expectedContent =
                await ResourceFile.ReadResourceAsync(_resourcesAssembly, outputFile, sourceFile : false);

            // Act
            // The host is not important as everything runs in memory and tests are isolated from each other.
            var response = await client.GetAsync("http://localhost/HtmlGeneration_Home/Form");

            var responseContent = await response.Content.ReadAsStringAsync();

            // Assert
            Assert.Equal(HttpStatusCode.OK, response.StatusCode);
            Assert.Equal(expectedMediaType, response.Content.Headers.ContentType);

            responseContent = responseContent.Trim();
            var forgeryTokens = AntiForgeryTestHelper.RetrieveAntiForgeryTokens(responseContent).ToArray();

#if GENERATE_BASELINES
            // Reverse usual substitutions and insert format items into the new file content.
            for (var index = 0; index < forgeryTokens.Length; index++)
            {
                responseContent = responseContent.Replace(forgeryTokens[index], $"{{{ index }}}");
            }

            ResourceFile.UpdateFile(_resourcesAssembly, outputFile, expectedContent, responseContent);
#else
            expectedContent = string.Format(expectedContent, forgeryTokens);
            Assert.Equal(expectedContent.Trim(), responseContent);
#endif
        }
Esempio n. 2
0
        public async Task FormTagHelper_GeneratesExpectedContent(bool?optionsAntiForgery)
        {
            // Arrange
            var newServices = new ServiceCollection();

            newServices.InitializeTagHelper <FormTagHelper>((helper, _) => helper.AntiForgery = optionsAntiForgery);
            var server = TestHelper.CreateServer(_app, SiteName,
                                                 services =>
            {
                services.Add(newServices);
                _configureServices(services);
            });
            var client            = server.CreateClient();
            var expectedMediaType = MediaTypeHeaderValue.Parse("text/html; charset=utf-8");

            // The K runtime compiles every file under compiler/resources as a resource at runtime with the same name
            // as the file name, in order to update a baseline you just need to change the file in that folder.
            var resourceName = string.Format(
                "compiler/resources/MvcTagHelpersWebSite.MvcTagHelper_Home.Form.Options.AntiForgery.{0}.html",
                optionsAntiForgery?.ToString() ?? "null"
                );
            var expectedContent = await _resourcesAssembly.ReadResourceAsStringAsync(resourceName);

            // Act
            // The host is not important as everything runs in memory and tests are isolated from each other.
            var response = await client.GetAsync("http://localhost/MvcTagHelper_Home/Form");

            var responseContent = await response.Content.ReadAsStringAsync();

            var forgeryTokens = AntiForgeryTestHelper.RetrieveAntiForgeryTokens(responseContent);

            expectedContent = string.Format(expectedContent, forgeryTokens.ToArray());

            // Assert
            Assert.Equal(HttpStatusCode.OK, response.StatusCode);
            Assert.Equal(expectedMediaType, response.Content.Headers.ContentType);
            Assert.Equal(expectedContent.Trim(), responseContent.Trim());
        }