< Summary

Class:Microsoft.Azure.KeyVault.HttpBearerChallengeCache
Assembly:Microsoft.Azure.KeyVault
File(s):C:\Git\azure-sdk-for-net\sdk\keyvault\Microsoft.Azure.KeyVault\src\Customized\Authentication\HttpBearerChallengeCache.cs
Covered lines:19
Uncovered lines:14
Coverable lines:33
Total lines:140
Line coverage:57.5% (19 of 33)
Covered branches:4
Total branches:10
Branch coverage:40% (4 of 10)

Metrics

MethodCyclomatic complexity Line coverage Branch coverage
.cctor()-100%100%
GetInstance()-100%100%
.ctor()-100%100%
GetChallengeForURL(...)-85.71%50%
RemoveChallengeForURL(...)-0%0%
SetChallengeForURL(...)-70%50%
Clear()-0%100%

File(s)

C:\Git\azure-sdk-for-net\sdk\keyvault\Microsoft.Azure.KeyVault\src\Customized\Authentication\HttpBearerChallengeCache.cs

#LineLine coverage
 1// Copyright (c) Microsoft Corporation. All rights reserved.
 2// Licensed under the MIT License. See License.txt in the project root for
 3// license information.
 4
 5using System;
 6using System.Collections.Generic;
 7
 8namespace Microsoft.Azure.KeyVault
 9{
 10    /// <summary>
 11    /// Singleton class for handling caching of the http bearer challenge
 12    /// </summary>
 13    public sealed class HttpBearerChallengeCache
 14    {
 215        private static HttpBearerChallengeCache _instance = new HttpBearerChallengeCache();
 16
 17        /// <summary>
 18        /// Gets the singleton instance of <see cref="HttpBearerChallengeCache"/>
 19        /// </summary>
 20        /// <returns>Instance of this class</returns>
 21        public static HttpBearerChallengeCache GetInstance()
 22        {
 2023            return _instance;
 24        }
 25
 26        private Dictionary<string, HttpBearerChallenge> _cache = null;
 27        private object _cacheLock = null;
 28
 229        private HttpBearerChallengeCache()
 30        {
 231            _cache = new Dictionary<string, HttpBearerChallenge>();
 232            _cacheLock = new object();
 233        }
 34
 35#if WINDOWS_PHONE
 36
 37        public HttpBearerChallenge this[Uri url]
 38        {
 39            get
 40            {
 41                if ( url == null )
 42                    throw new ArgumentNullException( "url" );
 43
 44                HttpBearerChallenge value = null;
 45
 46                lock ( _cacheLock )
 47                {
 48                    _cache.TryGetValue( url.FullAuthority(), out value );
 49                }
 50
 51                return value;
 52            }
 53            set
 54            {
 55                if ( url == null )
 56                    throw new ArgumentNullException( "url" );
 57
 58                if ( value != null && string.Compare( url.FullAuthority(), value.SourceAuthority, StringComparison.Ordin
 59                    throw new ArgumentException( "Source URL and Challenge URL do not match" );
 60
 61                lock ( _cacheLock )
 62                {
 63                    if ( value == null )
 64                        _cache.Remove( url.FullAuthority() );
 65                    else
 66                        _cache[url.FullAuthority()] = value;
 67                }
 68            }
 69        }
 70
 71#else
 72        /// <summary>
 73        /// Gets the challenge for the cached URL.
 74        /// </summary>
 75        /// <param name="url"> the URL that the challenge is cached for.</param>
 76        /// <returns>the cached challenge or null otherwise.</returns>
 77        public HttpBearerChallenge GetChallengeForURL(Uri url)
 78        {
 1479            if (url == null)
 080                throw new ArgumentNullException("url");
 81
 1482            HttpBearerChallenge value = null;
 83
 1484            lock (_cacheLock)
 85            {
 1486                _cache.TryGetValue(url.FullAuthority(), out value);
 1487            }
 88
 1489            return value;
 90        }
 91
 92        /// <summary>
 93        /// Removes the cached challenge for the specified URL
 94        /// </summary>
 95        /// <param name="url"> the URL to remove its cached challenge </param>
 96        public void RemoveChallengeForURL(Uri url)
 97        {
 098            if (url == null)
 099                throw new ArgumentNullException("url");
 100
 0101            lock (_cacheLock)
 102            {
 0103                _cache.Remove(url.FullAuthority());
 0104            }
 0105        }
 106
 107        /// <summary>
 108        /// Caches the challenge for the specified URL
 109        /// </summary>
 110        /// <param name="url"> URL corresponding to challenge as cache key </param>
 111        /// <param name="value"> the challenge </param>
 112        public void SetChallengeForURL(Uri url, HttpBearerChallenge value)
 113        {
 6114            if (url == null)
 0115                throw new ArgumentNullException("url");
 116
 6117            if (value == null)
 0118                throw new ArgumentNullException("value");
 119
 6120            if (string.Compare(url.FullAuthority(), value.SourceAuthority, StringComparison.OrdinalIgnoreCase) != 0)
 0121                throw new ArgumentException("Source URL and Challenge URL do not match");
 122
 6123            lock (_cacheLock)
 124            {
 6125                _cache[url.FullAuthority()] = value;
 6126            }
 6127        }
 128#endif
 129        /// <summary>
 130        /// Clears the cache
 131        /// </summary>
 132        public void Clear()
 133        {
 0134            lock (_cacheLock)
 135            {
 0136                _cache.Clear();
 0137            }
 0138        }
 139    }
 140}