Ejemplo n.º 1
0
        public void VerifyInitializerRespectsChangesToRoleNameContainerRoleName()
        {
            try
            {
                Environment.SetEnvironmentVariable("WEBSITE_HOSTNAME", "a.b.c.azurewebsites.net");
                var ac = new HttpContextAccessor {
                    HttpContext = null
                };

                var initializer = new AzureAppServiceRoleNameFromHostNameHeaderInitializer(webAppSuffix: ".azurewebsites.net");

                var requestTelemetry1 = new RequestTelemetry();
                initializer.Initialize(requestTelemetry1);
                Assert.Equal("a.b.c", requestTelemetry1.Context.Cloud.RoleName);

                RoleNameContainer.Instance.RoleName = "test";

                var requestTelemetry2 = new RequestTelemetry();
                initializer.Initialize(requestTelemetry2);
                Assert.Equal("test", requestTelemetry2.Context.Cloud.RoleName);
            }
            finally
            {
                Environment.SetEnvironmentVariable("WEBSITE_HOSTNAME", null);
            }
        }
        public void InitializeDoesNotThrowIfRequestIsUnavailable()
        {
            var contextAccessor = HttpContextAccessorHelper.CreateHttpContextAccessorWithoutRequest(new HttpContextStub(), new RequestTelemetry());

            var initializer = new AzureAppServiceRoleNameFromHostNameHeaderInitializer(contextAccessor);

            initializer.Initialize(new EventTelemetry());
        }
        public void InitializeDoesNotThrowIfHostNameHeaderIsNull()
        {
            var requestTelemetry = new RequestTelemetry();
            var contextAccessor  = HttpContextAccessorHelper.CreateHttpContextAccessor(requestTelemetry);

            var initializer = new AzureAppServiceRoleNameFromHostNameHeaderInitializer(contextAccessor);

            initializer.Initialize(requestTelemetry);
        }
        public void InitializeDoesNotThrowIfRequestServicesAreUnavailable()
        {
            var ac = new HttpContextAccessor {
                HttpContext = new DefaultHttpContext()
            };

            var initializer = new AzureAppServiceRoleNameFromHostNameHeaderInitializer(ac);

            initializer.Initialize(new RequestTelemetry());
        }
        public void InitializeDoesNotThrowIfHeaderCollectionIsUnavailable()
        {
            var httpContext = new HttpContextStub();

            httpContext.OnRequestGetter = () => new HttpRequestStub(httpContext);

            var contextAccessor = HttpContextAccessorHelper.CreateHttpContextAccessorWithoutRequest(httpContext, new RequestTelemetry());

            var initializer = new AzureAppServiceRoleNameFromHostNameHeaderInitializer(contextAccessor);

            initializer.Initialize(new EventTelemetry());
        }
        public void InitializeSetsRoleNameFromHostNameWithAzureWebsites()
        {
            var requestTelemetry = new RequestTelemetry();
            var contextAccessor  = HttpContextAccessorHelper.CreateHttpContextAccessor(requestTelemetry);

            contextAccessor.HttpContext.Request.Headers.Add("WAS-DEFAULT-HOSTNAME", new string[] { "appserviceslottest-ppe.azurewebsites.net" });

            var initializer = new AzureAppServiceRoleNameFromHostNameHeaderInitializer(contextAccessor);

            initializer.Initialize(requestTelemetry);

            Assert.Equal("appserviceslottest-ppe", requestTelemetry.Context.Cloud.RoleName);
        }
        public void InitializewithNoEnvToHostNameHeader()
        {
            Environment.SetEnvironmentVariable("WEBSITE_HOSTNAME", null);
            var ac = new HttpContextAccessor {
                HttpContext = null
            };

            var initializer = new AzureAppServiceRoleNameFromHostNameHeaderInitializer(ac);

            var requestTelemetry = new RequestTelemetry();

            initializer.Initialize(requestTelemetry);
            Assert.Null(requestTelemetry.Context.Cloud.RoleName);
        }
        public void InitializeSavesRoleNameIntoRequestFromHostNameHeader()
        {
            var requestTelemetry = new RequestTelemetry();
            var contextAccessor  = HttpContextAccessorHelper.CreateHttpContextAccessor(requestTelemetry);

            contextAccessor.HttpContext.Request.Headers.Add("WAS-DEFAULT-HOSTNAME", new string[] { "MyAppServiceProd" });

            var initializer = new AzureAppServiceRoleNameFromHostNameHeaderInitializer(contextAccessor);

            var evt = new EventTelemetry();

            initializer.Initialize(evt);

            Assert.Equal("MyAppServiceProd", evt.Context.Cloud.RoleName);
            Assert.Equal("MyAppServiceProd", requestTelemetry.Context.Cloud.RoleName);
        }
        public void InitializeDoesNotOverrideRoleName()
        {
            var requestTelemetry = new RequestTelemetry();

            requestTelemetry.Context.Cloud.RoleName = "ExistingRoleName";

            var contextAccessor = HttpContextAccessorHelper.CreateHttpContextAccessor(requestTelemetry);

            contextAccessor.HttpContext.Request.Headers.Add("WAS-DEFAULT-HOSTNAME", new string[] { "MyAppServiceProd" });

            var initializer = new AzureAppServiceRoleNameFromHostNameHeaderInitializer(contextAccessor);

            initializer.Initialize(requestTelemetry);

            Assert.Equal("ExistingRoleName", requestTelemetry.Context.Cloud.RoleName);
        }
Ejemplo n.º 10
0
        public void InitializeFallsbackToEnvIfRequestIsUnavailable()
        {
            try
            {
                Environment.SetEnvironmentVariable("WEBSITE_HOSTNAME", "RoleNameEnv");
                var initializer = new AzureAppServiceRoleNameFromHostNameHeaderInitializer();
                var req         = new RequestTelemetry();
                initializer.Initialize(req);

                Assert.Equal("RoleNameEnv", req.Context.Cloud.RoleName);
            }
            finally
            {
                Environment.SetEnvironmentVariable("WEBSITE_HOSTNAME", null);
            }
        }
Ejemplo n.º 11
0
        public void VerifyInitializerWorksAsExpected()
        {
            try
            {
                Environment.SetEnvironmentVariable("WEBSITE_HOSTNAME", "a.b.c.azurewebsites.net");

                var initializer = new AzureAppServiceRoleNameFromHostNameHeaderInitializer(webAppSuffix: ".azurewebsites.net");

                var requestTelemetry1 = new RequestTelemetry();
                initializer.Initialize(requestTelemetry1);
                Assert.Equal("a.b.c", requestTelemetry1.Context.Cloud.RoleName);
            }
            finally
            {
                Environment.SetEnvironmentVariable("WEBSITE_HOSTNAME", null);
            }
        }
        public void InitializeSetsRoleNameFromRequestTelemetryIfPresent()
        {
            var requestTelemetry = new RequestTelemetry();

            requestTelemetry.Context.Cloud.RoleName = "RoleNameOnRequest";
            var contextAccessor = HttpContextAccessorHelper.CreateHttpContextAccessor(requestTelemetry);

            contextAccessor.HttpContext.Request.Headers.Add("WAS-DEFAULT-HOSTNAME", new string[] { "RoleNameOnHeader" });

            var initializer = new AzureAppServiceRoleNameFromHostNameHeaderInitializer(contextAccessor);

            var evt = new EventTelemetry();

            initializer.Initialize(evt);

            Assert.Equal("RoleNameOnRequest", evt.Context.Cloud.RoleName);
        }
        public void InitializeFallsbackToEnvIfHostNameHeaderIsNull()
        {
            try
            {
                Environment.SetEnvironmentVariable("WEBSITE_HOSTNAME", "RoleNameEnv");
                var contextAccessor = HttpContextAccessorHelper.CreateHttpContextAccessor();

                var initializer = new AzureAppServiceRoleNameFromHostNameHeaderInitializer(contextAccessor);
                var req         = new RequestTelemetry();
                initializer.Initialize(req);

                Assert.Equal("RoleNameEnv", req.Context.Cloud.RoleName);
            }
            finally
            {
                Environment.SetEnvironmentVariable("WEBSITE_HOSTNAME", null);
            }
        }
        public void InitializeSetsRoleNameFromEnvWithAzureWebsitesCustom()
        {
            try
            {
                Environment.SetEnvironmentVariable("WEBSITE_HOSTNAME", "appserviceslottest-ppe.azurewebsites.us");
                var requestTelemetry = new RequestTelemetry();
                var contextAccessor  = HttpContextAccessorHelper.CreateHttpContextAccessor(requestTelemetry);
                var initializer      = new AzureAppServiceRoleNameFromHostNameHeaderInitializer(contextAccessor, ".azurewebsites.us");

                initializer.Initialize(requestTelemetry);

                Assert.Equal("appserviceslottest-ppe", requestTelemetry.Context.Cloud.RoleName);
            }
            finally
            {
                Environment.SetEnvironmentVariable("WEBSITE_HOSTNAME", null);
            }
        }
        public void InitializeFallsbackToEnvIfHttpContextIsUnavailableWithAzureWebsitesHostnameending()
        {
            try
            {
                Environment.SetEnvironmentVariable("WEBSITE_HOSTNAME", "RoleNameEnv.azurewebsites.net");
                var ac = new HttpContextAccessor {
                    HttpContext = null
                };

                var initializer = new AzureAppServiceRoleNameFromHostNameHeaderInitializer(ac);
                var req         = new RequestTelemetry();
                initializer.Initialize(req);

                Assert.Equal("RoleNameEnv", req.Context.Cloud.RoleName);
            }
            finally
            {
                Environment.SetEnvironmentVariable("WEBSITE_HOSTNAME", null);
            }
        }
        public void InitializeFallsbackToEnvIfHostNameIsEmptyInHeader()
        {
            try
            {
                Environment.SetEnvironmentVariable("WEBSITE_HOSTNAME", "RoleNameEnv");
                var requestTelemetry = new RequestTelemetry();
                var contextAccessor  = HttpContextAccessorHelper.CreateHttpContextAccessor(requestTelemetry);

                contextAccessor.HttpContext.Request.Headers.Add("WAS-DEFAULT-HOSTNAME", new string[] { string.Empty });

                var initializer = new AzureAppServiceRoleNameFromHostNameHeaderInitializer(contextAccessor);

                initializer.Initialize(requestTelemetry);

                Assert.Equal("RoleNameEnv", requestTelemetry.Context.Cloud.RoleName);
            }
            finally
            {
                Environment.SetEnvironmentVariable("WEBSITE_HOSTNAME", null);
            }
        }
        public void InitializeFallsbackToEnvIfHeaderCollectionIsUnavailable()
        {
            try
            {
                Environment.SetEnvironmentVariable("WEBSITE_HOSTNAME", "RoleNameEnv");
                var httpContext = new HttpContextStub();
                httpContext.OnRequestGetter = () => new HttpRequestStub(httpContext);

                var contextAccessor = HttpContextAccessorHelper.CreateHttpContextAccessorWithoutRequest(httpContext, new RequestTelemetry());

                var initializer = new AzureAppServiceRoleNameFromHostNameHeaderInitializer(contextAccessor);
                var req         = new RequestTelemetry();
                initializer.Initialize(req);

                Assert.Equal("RoleNameEnv", req.Context.Cloud.RoleName);
            }
            finally
            {
                Environment.SetEnvironmentVariable("WEBSITE_HOSTNAME", null);
            }
        }
 public void InitializeThrowIfHttpContextAccessorIsNull()
 {
     Assert.ThrowsAny <ArgumentNullException>(() => { var initializer = new AzureAppServiceRoleNameFromHostNameHeaderInitializer(null); });
 }
Ejemplo n.º 19
0
        public void InitializeDoesNotThrowIfRequestIsUnavailable()
        {
            var initializer = new AzureAppServiceRoleNameFromHostNameHeaderInitializer();

            initializer.Initialize(new EventTelemetry());
        }