parent
4c8b548b30
commit
07665c99e8
|
|
@ -1,6 +1,6 @@
|
|||
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||
# Visual Studio 15
|
||||
VisualStudioVersion = 15.0.26406.2
|
||||
VisualStudioVersion = 15.0.26228.9
|
||||
MinimumVisualStudioVersion = 10.0.40219.1
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.AspNetCore.AzureAppServicesIntegration", "src\Microsoft.AspNetCore.AzureAppServicesIntegration\Microsoft.AspNetCore.AzureAppServicesIntegration.csproj", "{5916BEB5-0969-469B-976C-A392E015DFAC}"
|
||||
EndProject
|
||||
|
|
@ -33,6 +33,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.Web.Xdt.Extension
|
|||
EndProject
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.AspNetCore.AzureAppServices.SiteExtension", "src\Microsoft.AspNetCore.AzureAppServices.SiteExtension\Microsoft.AspNetCore.AzureAppServices.SiteExtension.csproj", "{1CE2D76B-39E6-46C0-8F6F-C63E370955A9}"
|
||||
EndProject
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ApplicationInsightsJavaScriptSnippetTest", "test\ApplicationInsightsJavaScriptSnippetTest\ApplicationInsightsJavaScriptSnippetTest.csproj", "{0899A101-E451-40A4-81B0-7AA18202C25D}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
|
|
@ -75,6 +77,10 @@ Global
|
|||
{1CE2D76B-39E6-46C0-8F6F-C63E370955A9}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{1CE2D76B-39E6-46C0-8F6F-C63E370955A9}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{1CE2D76B-39E6-46C0-8F6F-C63E370955A9}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{0899A101-E451-40A4-81B0-7AA18202C25D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{0899A101-E451-40A4-81B0-7AA18202C25D}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{0899A101-E451-40A4-81B0-7AA18202C25D}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{0899A101-E451-40A4-81B0-7AA18202C25D}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
|
|
@ -89,5 +95,6 @@ Global
|
|||
{33E245F0-2566-4B5B-BA7C-8895B7A697AE} = {37237C93-6855-40D9-9E60-418B093EF49A}
|
||||
{9B22E525-FEC9-4C7C-9F9C-598C15BD0250} = {FF9B744E-6C59-40CC-9E41-9D2EBD292435}
|
||||
{1CE2D76B-39E6-46C0-8F6F-C63E370955A9} = {FF9B744E-6C59-40CC-9E41-9D2EBD292435}
|
||||
{0899A101-E451-40A4-81B0-7AA18202C25D} = {CD650B4B-81C2-4A44-AEF2-A251A877C1F0}
|
||||
EndGlobalSection
|
||||
EndGlobal
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
<Project>
|
||||
<PropertyGroup>
|
||||
<AspNetCoreVersion>2.0.0-*</AspNetCoreVersion>
|
||||
<AspNetIntegrationTestingVersion>0.4.0-*</AspNetIntegrationTestingVersion>
|
||||
<AppInsightsVersion>2.0.0</AppInsightsVersion>
|
||||
<CoreFxVersion>4.3.0</CoreFxVersion>
|
||||
<InternalAspNetCoreSdkVersion>2.0.0-*</InternalAspNetCoreSdkVersion>
|
||||
|
|
|
|||
|
|
@ -4,12 +4,15 @@
|
|||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netcoreapp2.0</TargetFramework>
|
||||
<RuntimeIdentifiers>win7-x86;win7-x64;linux-x64;osx.10.12-x64</RuntimeIdentifiers>
|
||||
<PackageTargetFallback Condition=" '$(TargetFramework)' == 'netcoreapp2.0' ">$(PackageTargetFallback);portable-net40+sl5+win8+wp8+wpa81;portable-net45+win8+wp8+wpa81</PackageTargetFallback>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.AspNetCore.Hosting" Version="$(AspNetCoreVersion)" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="$(AspNetCoreVersion)" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Server.Kestrel" Version="$(AspNetCoreVersion)" />
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.CommandLine" Version="$(AspNetCoreVersion)" />
|
||||
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="$(AspNetCoreVersion)" />
|
||||
</ItemGroup>
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,15 @@
|
|||
// 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 Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace ApplicationInsightsJavaScriptSnippetSample.Controllers
|
||||
{
|
||||
public class HomeController : Controller
|
||||
{
|
||||
public IActionResult ScriptCheck()
|
||||
{
|
||||
return View();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -16,9 +16,10 @@
|
|||
"ASPNETCORE_HOSTINGSTARTUPASSEMBLIES": "Microsoft.AspNetCore.ApplicationInsights.HostingStartup;Microsoft.AspNetCore.Server.IISIntegration"
|
||||
}
|
||||
},
|
||||
"AzureAppServicesHostingStartupSample": {
|
||||
"ApplicationInsightsHostingStartupSample": {
|
||||
"commandName": "project",
|
||||
"launchBrowser": true,
|
||||
"launchUrl": "http://localhost:5000/",
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development",
|
||||
"ASPNETCORE_HOSTINGSTARTUPASSEMBLIES": "Microsoft.AspNetCore.ApplicationInsights.HostingStartup"
|
||||
|
|
|
|||
|
|
@ -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.IO;
|
||||
using System.Linq;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
|
|
@ -12,6 +17,7 @@ namespace IISSample
|
|||
{
|
||||
public void ConfigureServices(IServiceCollection services)
|
||||
{
|
||||
services.AddMvc();
|
||||
}
|
||||
|
||||
public void Configure(IApplicationBuilder app, ILoggerFactory loggerfactory)
|
||||
|
|
@ -20,6 +26,7 @@ namespace IISSample
|
|||
|
||||
var logger = loggerfactory.CreateLogger("Requests");
|
||||
|
||||
app.UseMvcWithDefaultRoute();
|
||||
app.Run(async (context) =>
|
||||
{
|
||||
logger.LogDebug("Received request: " + context.Request.Method + " " + context.Request.Path);
|
||||
|
|
@ -67,9 +74,16 @@ namespace IISSample
|
|||
|
||||
public static void Main(string[] args)
|
||||
{
|
||||
var config = new ConfigurationBuilder()
|
||||
.AddCommandLine(args)
|
||||
.AddEnvironmentVariables(prefix: "ASPNETCORE_")
|
||||
.Build();
|
||||
|
||||
var host = new WebHostBuilder()
|
||||
.UseKestrel()
|
||||
.UseStartup<Startup>()
|
||||
.UseContentRoot(Directory.GetCurrentDirectory())
|
||||
.UseConfiguration(config)
|
||||
.Build();
|
||||
|
||||
host.Run();
|
||||
|
|
|
|||
|
|
@ -0,0 +1,3 @@
|
|||
@{
|
||||
ViewData["Title"] = "Home Page";
|
||||
}
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<title>@ViewData["Title"] - ApplicationInsightsJavaScriptSnippetSample</title>
|
||||
<script>document.write('Head tag script. Look for Application Insights JavaScriptSnippet below this script in the HTML file.')</script>
|
||||
</head>
|
||||
<body>
|
||||
<div class="container body-content">
|
||||
@RenderBody()
|
||||
<hr />
|
||||
<footer>
|
||||
<p>© 2017 - ApplicationInsightsJavaScriptSnippetSample</p>
|
||||
</footer>
|
||||
</div>
|
||||
@RenderSection("Scripts", required: false)
|
||||
</body>
|
||||
</html>
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
@using ApplicationInsightsJavaScriptSnippetSample
|
||||
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
@{
|
||||
Layout = "_Layout";
|
||||
}
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
{
|
||||
"Logging": {
|
||||
"IncludeScopes": false,
|
||||
"LogLevel": {
|
||||
"Default": "Debug",
|
||||
"System": "Information",
|
||||
"Microsoft": "Information"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
{
|
||||
"ApplicationInsights": {
|
||||
"InstrumentationKey": "11111111-2222-3333-4444-555555555555"
|
||||
},
|
||||
"Logging": {
|
||||
"IncludeScopes": false,
|
||||
"LogLevel": {
|
||||
"Default": "Warning"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -2,6 +2,8 @@
|
|||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.AspNetCore.Razor.TagHelpers;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
[assembly: HostingStartup(typeof(Microsoft.AspNetCore.ApplicationInsights.HostingStartup.ApplicationInsightsHostingStartup))]
|
||||
|
||||
|
|
@ -11,7 +13,7 @@ internal class Program { public static void Main() { } }
|
|||
namespace Microsoft.AspNetCore.ApplicationInsights.HostingStartup
|
||||
{
|
||||
/// <summary>
|
||||
/// A dynamic Application Insights lightup experiance
|
||||
/// A dynamic Application Insights lightup experience
|
||||
/// </summary>
|
||||
public class ApplicationInsightsHostingStartup : IHostingStartup
|
||||
{
|
||||
|
|
@ -22,6 +24,16 @@ namespace Microsoft.AspNetCore.ApplicationInsights.HostingStartup
|
|||
public void Configure(IWebHostBuilder builder)
|
||||
{
|
||||
builder.UseApplicationInsights();
|
||||
builder.ConfigureServices(InitializeServices);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds the Javascript <see cref="TagHelperComponent"/> to the <see cref="IServiceCollection"/>.
|
||||
/// </summary>
|
||||
/// <param name="services">The <see cref="IServiceCollection"/> associated with the application.</param>
|
||||
private void InitializeServices(IServiceCollection services)
|
||||
{
|
||||
services.AddSingleton<ITagHelperComponent, JavaScriptSnippetTagHelperComponent>();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,42 @@
|
|||
// 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 Microsoft.ApplicationInsights.AspNetCore;
|
||||
using Microsoft.AspNetCore.Razor.TagHelpers;
|
||||
|
||||
namespace Microsoft.AspNetCore.ApplicationInsights.HostingStartup
|
||||
{
|
||||
/// <summary>
|
||||
/// The <see cref="TagHelperComponent"/> that injects the <see cref="JavaScriptSnippet"/> at the end of the head tag.
|
||||
/// </summary>
|
||||
public class JavaScriptSnippetTagHelperComponent : TagHelperComponent
|
||||
{
|
||||
private readonly string _javaScriptSnippet;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes the <see cref="JavaScriptSnippetTagHelperComponent"/>.
|
||||
/// </summary>
|
||||
/// <param name="javaScriptSnippet">The <see cref="JavaScriptSnippet"/> to inject in the head tag.</param>
|
||||
public JavaScriptSnippetTagHelperComponent(JavaScriptSnippet javaScriptSnippet)
|
||||
{
|
||||
_javaScriptSnippet = javaScriptSnippet.FullScript;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override int Order => 100;
|
||||
|
||||
/// <summary>
|
||||
/// Appends the <see cref="JavaScriptSnippet"/> to the head tag.
|
||||
/// </summary>
|
||||
/// <param name="context">The <see cref="TagHelperContext"/> associated with the head tag.</param>
|
||||
/// <param name="output">The <see cref="TagHelperOutput"/> of the head tag.</param>
|
||||
public override void Process(TagHelperContext context, TagHelperOutput output)
|
||||
{
|
||||
if (string.Equals(context.TagName, "head", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
output.PostContent.AppendHtml(_javaScriptSnippet);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -14,6 +14,7 @@
|
|||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="$(AppInsightsVersion)" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Hosting.Abstractions" Version="$(AspNetCoreVersion)" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Razor.Runtime" Version="$(AspNetCoreVersion)" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
</script>
|
||||
</head>
|
||||
<body>
|
||||
<div class="container body-content">
|
||||
|
||||
<hr />
|
||||
<footer>
|
||||
<p>© 2017 - ApplicationInsightsJavaScriptSnippetSample</p>
|
||||
</footer>
|
||||
</div>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
|
|
@ -0,0 +1,132 @@
|
|||
// 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.IO;
|
||||
using System.Net.Http;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Server.IntegrationTesting;
|
||||
using Microsoft.AspNetCore.Server.IntegrationTesting.xunit;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Xunit;
|
||||
using Xunit.Abstractions;
|
||||
|
||||
namespace ApplicationInsightsJavaScriptSnippetTest
|
||||
{
|
||||
public class ApplicationInsightsJavaScriptSnippetTest : LoggedTest
|
||||
{
|
||||
public ApplicationInsightsJavaScriptSnippetTest(ITestOutputHelper output) : base(output)
|
||||
{
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(ApplicationType.Portable)]
|
||||
[InlineData(ApplicationType.Standalone)]
|
||||
public async Task ScriptInjected(ApplicationType applicationType)
|
||||
{
|
||||
await JavaScriptSnippetInjectionTestSuite(applicationType);
|
||||
}
|
||||
|
||||
private async Task JavaScriptSnippetInjectionTestSuite(ApplicationType applicationType)
|
||||
{
|
||||
var testName = $"ApplicationInsightsJavaScriptSnippetTest_{applicationType}";
|
||||
using (StartLog(out var loggerFactory, testName))
|
||||
{
|
||||
var logger = loggerFactory.CreateLogger(nameof(ApplicationInsightsJavaScriptSnippetTest));
|
||||
var deploymentParameters = new DeploymentParameters(GetApplicationPath(applicationType), ServerType.Kestrel, RuntimeFlavor.CoreClr, RuntimeArchitecture.x64)
|
||||
{
|
||||
PublishApplicationBeforeDeployment = true,
|
||||
PreservePublishedApplicationForDebugging = PreservePublishedApplicationForDebugging,
|
||||
TargetFramework = "netcoreapp2.0",
|
||||
Configuration = GetCurrentBuildConfiguration(),
|
||||
ApplicationType = applicationType,
|
||||
EnvironmentVariables =
|
||||
{
|
||||
new KeyValuePair<string, string>(
|
||||
"ASPNETCORE_HOSTINGSTARTUPASSEMBLIES",
|
||||
"Microsoft.AspNetCore.ApplicationInsights.HostingStartup"),
|
||||
},
|
||||
};
|
||||
|
||||
using (var deployer = ApplicationDeployerFactory.Create(deploymentParameters, loggerFactory))
|
||||
{
|
||||
var deploymentResult = await deployer.DeployAsync();
|
||||
var httpClientHandler = new HttpClientHandler();
|
||||
var httpClient = deploymentResult.CreateHttpClient(httpClientHandler);
|
||||
|
||||
// Request to base address and check if various parts of the body are rendered & measure the cold startup time.
|
||||
var response = await RetryHelper.RetryRequest(async () =>
|
||||
{
|
||||
return await httpClient.GetAsync("\\Home\\ScriptCheck");
|
||||
}, logger: logger, cancellationToken: deploymentResult.HostShutdownToken);
|
||||
|
||||
Assert.False(response == null, "Response object is null because the client could not " +
|
||||
"connect to the server after multiple retries");
|
||||
|
||||
var validator = new Validator(httpClient, httpClientHandler, logger, deploymentResult);
|
||||
|
||||
logger.LogInformation("Verifying layout page");
|
||||
await validator.VerifyLayoutPage(response);
|
||||
|
||||
logger.LogInformation("Verifying layout page before script");
|
||||
await validator.VerifyLayoutPageBeforeScript(response);
|
||||
|
||||
logger.LogInformation("Verifying layout page after script");
|
||||
await validator.VerifyLayoutPageAfterScript(response);
|
||||
|
||||
logger.LogInformation("Variation completed successfully.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static string GetApplicationPath(ApplicationType applicationType)
|
||||
{
|
||||
var current = new DirectoryInfo(AppContext.BaseDirectory);
|
||||
while (current != null)
|
||||
{
|
||||
if (File.Exists(Path.Combine(current.FullName, "AzureIntegration.sln")))
|
||||
{
|
||||
break;
|
||||
}
|
||||
current = current.Parent;
|
||||
}
|
||||
|
||||
if (current == null)
|
||||
{
|
||||
throw new InvalidOperationException("Could not find the solution directory");
|
||||
}
|
||||
|
||||
return Path.GetFullPath(Path.Combine(current.FullName, "sample", "ApplicationInsightsHostingStartupSample"));
|
||||
}
|
||||
|
||||
private static bool PreservePublishedApplicationForDebugging
|
||||
{
|
||||
get
|
||||
{
|
||||
var deletePublishedFolder = Environment.GetEnvironmentVariable("ASPNETCORE_DELETEPUBLISHEDFOLDER");
|
||||
|
||||
if (string.Equals("false", deletePublishedFolder, StringComparison.OrdinalIgnoreCase)
|
||||
|| string.Equals("0", deletePublishedFolder, StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
// preserve the published folder and do not delete it
|
||||
return true;
|
||||
}
|
||||
|
||||
// do not preserve the published folder and delete it
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private static string GetCurrentBuildConfiguration()
|
||||
{
|
||||
var configuration = "Debug";
|
||||
if (string.Equals(Environment.GetEnvironmentVariable("Configuration"), "Release", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
configuration = "Release";
|
||||
}
|
||||
|
||||
return configuration;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,39 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<Import Project="..\..\build\common.props" />
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netcoreapp2.0</TargetFramework>
|
||||
<TargetFrameworks Condition=" '$(OS)' != 'Windows_NT' ">netcoreapp2.0</TargetFrameworks>
|
||||
<PackageTargetFallback Condition=" '$(TargetFramework)' == 'netcoreapp2.0' ">$(PackageTargetFallback);portable-net40+sl5+win8+wp8+wpa81;portable-net45+win8+wp8+wpa81</PackageTargetFallback>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<None Remove="AfterScript.html" />
|
||||
<None Remove="BeforeScript.html" />
|
||||
<None Remove="Rendered.html" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<EmbeddedResource Include="AfterScript.html" />
|
||||
<EmbeddedResource Include="BeforeScript.html" />
|
||||
<EmbeddedResource Include="Rendered.html" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\src\Microsoft.AspNetCore.ApplicationInsights.HostingStartup\Microsoft.AspNetCore.ApplicationInsights.HostingStartup.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.AspNetCore.Server.IntegrationTesting" Version="$(AspNetIntegrationTestingVersion)" />
|
||||
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="$(AspNetCoreVersion)" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="$(TestSdkVersion)" />
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="$(XunitVersion)" />
|
||||
<PackageReference Include="xunit" Version="$(XunitVersion)" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Service Include="{82a7f48d-3b50-4b1e-b82e-3ada8210c358}" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<title>Home Page - ApplicationInsightsJavaScriptSnippetSample</title>
|
||||
<script>document.write('Head tag script. Look for Application Insights JavaScriptSnippet below this script in the HTML file.')</script>
|
||||
<script type="text/javascript">
|
||||
|
||||
|
|
@ -0,0 +1,26 @@
|
|||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<title>Home Page - ApplicationInsightsJavaScriptSnippetSample</title>
|
||||
<script>document.write('Head tag script. Look for Application Insights JavaScriptSnippet below this script in the HTML file.')</script>
|
||||
<script type="text/javascript">
|
||||
|
||||
({
|
||||
instrumentationKey: '11111111-2222-3333-4444-555555555555'
|
||||
});
|
||||
|
||||
</script>
|
||||
</head>
|
||||
<body>
|
||||
<div class="container body-content">
|
||||
|
||||
<hr />
|
||||
<footer>
|
||||
<p>© 2017 - ApplicationInsightsJavaScriptSnippetSample</p>
|
||||
</footer>
|
||||
</div>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
|
|
@ -0,0 +1,191 @@
|
|||
// 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.IO;
|
||||
using System.Net;
|
||||
using System.Net.Http;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Server.IntegrationTesting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Xunit;
|
||||
|
||||
namespace ApplicationInsightsJavaScriptSnippetTest
|
||||
{
|
||||
public class Validator
|
||||
{
|
||||
private HttpClient _httpClient;
|
||||
|
||||
private HttpClientHandler _httpClientHandler;
|
||||
|
||||
private readonly ILogger _logger;
|
||||
|
||||
private readonly DeploymentResult _deploymentResult;
|
||||
|
||||
private static readonly Assembly _resourcesAssembly = typeof(ApplicationInsightsJavaScriptSnippetTest).GetTypeInfo().Assembly;
|
||||
|
||||
public Validator(
|
||||
HttpClient httpClient,
|
||||
HttpClientHandler httpClientHandler,
|
||||
ILogger logger,
|
||||
DeploymentResult deploymentResult)
|
||||
{
|
||||
_httpClient = httpClient;
|
||||
_httpClientHandler = httpClientHandler;
|
||||
_logger = logger;
|
||||
_deploymentResult = deploymentResult;
|
||||
}
|
||||
|
||||
public async Task VerifyLayoutPage(HttpResponseMessage response)
|
||||
{
|
||||
var responseContent = await response.Content.ReadAsStringAsync();
|
||||
|
||||
if (response.StatusCode != HttpStatusCode.OK)
|
||||
{
|
||||
_logger.LogInformation("Layout page : {0}", responseContent);
|
||||
}
|
||||
|
||||
await ValidateLayoutPage(responseContent);
|
||||
}
|
||||
|
||||
public async Task VerifyLayoutPageBeforeScript(HttpResponseMessage response)
|
||||
{
|
||||
var responseContent = await response.Content.ReadAsStringAsync();
|
||||
|
||||
if (response.StatusCode != HttpStatusCode.OK)
|
||||
{
|
||||
_logger.LogInformation("Before app insights script : {0}", responseContent);
|
||||
}
|
||||
|
||||
await ValidateLayoutPageBeforeScript(responseContent);
|
||||
}
|
||||
|
||||
public async Task VerifyLayoutPageAfterScript(HttpResponseMessage response)
|
||||
{
|
||||
var responseContent = await response.Content.ReadAsStringAsync();
|
||||
|
||||
if (response.StatusCode != HttpStatusCode.OK)
|
||||
{
|
||||
_logger.LogInformation("After app insights script : {0}", responseContent);
|
||||
}
|
||||
|
||||
await ValidateLayoutPageAfterScript(responseContent);
|
||||
}
|
||||
|
||||
// Does not check the contents of the JavaScriptSnippet as it might change. Only checks the instrumentation key.
|
||||
private async Task ValidateLayoutPage(string responseContent)
|
||||
{
|
||||
var outputFile = "Rendered.html";
|
||||
var expectedContent = await ReadResourceAsync(_resourcesAssembly, outputFile, sourceFile: false);
|
||||
foreach (var substring in expectedContent)
|
||||
{
|
||||
#if GENERATE_BASELINES
|
||||
ResourceFile.UpdateFile(_resourcesAssembly, outputFile, expectedContent, responseContent);
|
||||
#else
|
||||
Assert.Contains(substring, responseContent);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
private async Task ValidateLayoutPageBeforeScript(string responseContent)
|
||||
{
|
||||
var outputFile = "BeforeScript.html";
|
||||
var expectedContent = await ReadResourceAsync(_resourcesAssembly, outputFile, sourceFile: false);
|
||||
|
||||
foreach (var substring in expectedContent)
|
||||
{
|
||||
#if GENERATE_BASELINES
|
||||
ResourceFile.UpdateFile(_resourcesAssembly, outputFile, expectedContent, responseContent);
|
||||
#else
|
||||
Assert.Contains(substring, responseContent);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
private async Task ValidateLayoutPageAfterScript(string responseContent)
|
||||
{
|
||||
var outputFile = "AfterScript.html";
|
||||
var expectedContent = await ReadResourceAsync(_resourcesAssembly, outputFile, sourceFile: false);
|
||||
|
||||
foreach (var substring in expectedContent)
|
||||
{
|
||||
#if GENERATE_BASELINES
|
||||
ResourceFile.UpdateFile(_resourcesAssembly, outputFile, expectedContent, responseContent);
|
||||
#else
|
||||
Assert.Contains(substring, responseContent);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
private static async Task<string> ReadResourceAsync(Assembly assembly, string resourceName, bool sourceFile)
|
||||
{
|
||||
using (var stream = GetResourceStream(assembly, resourceName, sourceFile))
|
||||
{
|
||||
if (stream == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
using (var streamReader = new StreamReader(stream))
|
||||
{
|
||||
return await streamReader.ReadToEndAsync();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static Stream GetResourceStream(Assembly assembly, string resourceName, bool sourceFile)
|
||||
{
|
||||
var fullName = $"{ assembly.GetName().Name }.{ resourceName.Replace('/', '.') }";
|
||||
if (!Exists(assembly, fullName))
|
||||
{
|
||||
#if GENERATE_BASELINES
|
||||
if (sourceFile)
|
||||
{
|
||||
// Even when generating baselines, a missing source file is a serious problem.
|
||||
Assert.True(false, $"Manifest resource: { fullName } not found.");
|
||||
}
|
||||
#else
|
||||
// When not generating baselines, a missing source or output file is always an error.
|
||||
Assert.True(false, $"Manifest resource '{ fullName }' not found.");
|
||||
#endif
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
var stream = assembly.GetManifestResourceStream(fullName);
|
||||
if (sourceFile)
|
||||
{
|
||||
// Normalize line endings to '\r\n' (CRLF). This removes core.autocrlf, core.eol, core.safecrlf, and
|
||||
// .gitattributes from the equation and treats "\r\n" and "\n" as equivalent. Does not handle
|
||||
// some line endings like "\r" but otherwise ensures checksums and line mappings are consistent.
|
||||
string text;
|
||||
using (var streamReader = new StreamReader(stream))
|
||||
{
|
||||
text = streamReader.ReadToEnd().Replace("\r", "").Replace("\n", "\r\n");
|
||||
}
|
||||
|
||||
var bytes = Encoding.UTF8.GetBytes(text);
|
||||
stream = new MemoryStream(bytes);
|
||||
}
|
||||
|
||||
return stream;
|
||||
}
|
||||
|
||||
private static bool Exists(Assembly assembly, string fullName)
|
||||
{
|
||||
var resourceNames = assembly.GetManifestResourceNames();
|
||||
foreach (var resourceName in resourceNames)
|
||||
{
|
||||
// Resource names are case-sensitive.
|
||||
if (string.Equals(fullName, resourceName, StringComparison.Ordinal))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -3,7 +3,6 @@
|
|||
|
||||
using System;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Moq;
|
||||
using Xunit;
|
||||
|
||||
|
|
|
|||
|
|
@ -20,4 +20,8 @@
|
|||
<PackageReference Include="xunit" Version="$(XunitVersion)" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Service Include="{82a7f48d-3b50-4b1e-b82e-3ada8210c358}" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
|
|
|||
Loading…
Reference in New Issue