< Summary

Class:Azure.Core.Pipeline.HttpEnvironmentProxyCredentials
Assembly:Azure.Core
File(s):C:\Git\azure-sdk-for-net\sdk\core\Azure.Core\src\Pipeline\Internal\HttpEnvironmentProxyCredentials.cs
Covered lines:34
Uncovered lines:0
Coverable lines:34
Total lines:91
Line coverage:100% (34 of 34)
Covered branches:19
Total branches:20
Branch coverage:95% (19 of 20)

Metrics

MethodCyclomatic complexity Line coverage Branch coverage
.ctor(...)-100%100%
GetCredential(...)-100%83.33%
TryCreate(...)-100%100%
GetCredentialsFromString(...)-100%100%

File(s)

C:\Git\azure-sdk-for-net\sdk\core\Azure.Core\src\Pipeline\Internal\HttpEnvironmentProxyCredentials.cs

#LineLine coverage
 1// Copyright (c) Microsoft Corporation. All rights reserved.
 2// Licensed under the MIT License.
 3
 4// Copied from https://raw.githubusercontent.com/dotnet/corefx/master/src/System.Net.Http/src/System/Net/Http/SocketsHtt
 5
 6#nullable disable
 7
 8using System;
 9using System.Net;
 10
 11namespace Azure.Core.Pipeline
 12{
 13    internal sealed class HttpEnvironmentProxyCredentials : ICredentials
 14    {
 15        // Wrapper class for cases when http and https have different authentication.
 16        private readonly NetworkCredential _httpCred;
 17        private readonly NetworkCredential _httpsCred;
 18        private readonly Uri _httpProxy;
 19        private readonly Uri _httpsProxy;
 20
 2621        public HttpEnvironmentProxyCredentials(Uri httpProxy, NetworkCredential httpCred,
 2622                                               Uri httpsProxy, NetworkCredential httpsCred)
 23        {
 2624            _httpCred = httpCred;
 2625            _httpsCred = httpsCred;
 2626            _httpProxy = httpProxy;
 2627            _httpsProxy = httpsProxy;
 2628        }
 29
 30        public NetworkCredential GetCredential(Uri uri, string authType)
 31        {
 1832            if (uri == null)
 33            {
 234                return null;
 35            }
 1636            return uri.Equals(_httpProxy) ? _httpCred :
 1637                   uri.Equals(_httpsProxy) ? _httpsCred : null;
 38        }
 39
 40        public static HttpEnvironmentProxyCredentials TryCreate(Uri httpProxy, Uri httpsProxy)
 41        {
 5842            NetworkCredential httpCred = null;
 5843            NetworkCredential httpsCred = null;
 44
 5845            if (httpProxy != null)
 46            {
 5847                httpCred = GetCredentialsFromString(httpProxy.UserInfo);
 48            }
 5849            if (httpsProxy != null)
 50            {
 4651                httpsCred = GetCredentialsFromString(httpsProxy.UserInfo);
 52            }
 5853            if (httpCred == null && httpsCred == null)
 54            {
 3255                return null;
 56            }
 2657            return new HttpEnvironmentProxyCredentials(httpProxy, httpCred, httpsProxy, httpsCred);
 58        }
 59
 60        /// <summary>
 61        /// Converts string containing user:password to NetworkCredential object.
 62        /// </summary>
 63        private static NetworkCredential GetCredentialsFromString(string value)
 64        {
 10465            if (string.IsNullOrWhiteSpace(value))
 66            {
 6067                return null;
 68            }
 69
 4470            value = Uri.UnescapeDataString(value);
 71
 4472            string password = "";
 4473            string domain = null;
 4474            int idx = value.IndexOf(':');
 4475            if (idx != -1)
 76            {
 3477                password = value.Substring(idx + 1);
 3478                value = value.Substring(0, idx);
 79            }
 80
 4481            idx = value.IndexOf('\\');
 4482            if (idx != -1)
 83            {
 884                domain = value.Substring(0, idx);
 885                value = value.Substring(idx + 1);
 86            }
 87
 4488            return new NetworkCredential(value, password, domain);
 89        }
 90    }
 91}