public async Task StaticProviderIsUsedFirst_Async()
        {
            // Arrange
            _staticMetadataProvider.GetMetadata("some_env.com").Returns(_expectedResult);

            // Act
            InstanceDiscoveryMetadataEntry actualResult1 = await _discoveryManager.GetMetadataEntryTryAvoidNetworkAsync(
                Authority,
                new[] { "env1", "env2" },
                _testRequestContext)
                                                           .ConfigureAwait(false);

            _staticMetadataProvider.Received(1).GetMetadata("some_env.com");

            InstanceDiscoveryMetadataEntry actualResult2 = await _discoveryManager.GetMetadataEntryAsync(
                "https://some_env.com/tid",
                _testRequestContext)
                                                           .ConfigureAwait(false);

            _staticMetadataProvider.Received(2).GetMetadata("some_env.com");
            _staticMetadataProvider.AddMetadata(null, null);

            // Assert
            Assert.AreSame(_expectedResult, actualResult1, "The static provider should be queried first");
            Assert.AreSame(_expectedResult, actualResult2, "The static provider should be queried first");
        }
        public async Task <InstanceDiscoveryMetadataEntry> GetMetadataEntryTryAvoidNetworkAsync(
            string authority,
            IEnumerable <string> existingEnvironmentsInCache,
            RequestContext requestContext)
        {
            AuthorityType type         = Authority.GetAuthorityType(authority);
            Uri           authorityUri = new Uri(authority);
            string        environment  = authorityUri.Host;

            switch (type)
            {
            case AuthorityType.Aad:
                InstanceDiscoveryMetadataEntry entry = _staticMetadataProvider.GetMetadata(environment);

                if (entry != null)
                {
                    return(entry);
                }

                entry = _knownMetadataProvider.GetMetadata(environment, existingEnvironmentsInCache);

                if (entry != null)
                {
                    return(entry);
                }

                return(await GetMetadataEntryAsync(authority, requestContext).ConfigureAwait(false));

            case AuthorityType.Adfs:
            case AuthorityType.B2C:

                return(await GetMetadataEntryAsync(authority, requestContext).ConfigureAwait(false));

            default:
                throw new InvalidOperationException("Unexpected authority type " + type);
            }
        }