示例#1
0
        private async Task <string> GetOAuthParameterFromHandlerAsync(OAuthMessageHandler handler, HttpRequestMessage request, string requestedParameter)
        {
            var testHandler = new TestHttpMessageHandler();

            handler.InnerHandler = testHandler;

            using (var httpClient = new HttpClient(handler, disposeHandler: false))
            {
                await httpClient.SendAsync(request);
            }

            var request2            = testHandler.SentMessages.Single();
            var authHeaderParameter = request.Headers.Authorization.Parameter;

            // extract the parameter
            string[] elems = authHeaderParameter.Split(',');
            return(elems.SingleOrDefault(x => x.StartsWith($"{requestedParameter}="))?.Split('=')[1]);
        }
示例#2
0
        private async Task <string> GetOAuthParameterFromHandlerAsync(OAuthMessageHandler handler, HttpRequestMessage request, string requestedParameter)
        {
            var testHandler = new TestHttpMessageHandler();

            handler.InnerHandler = testHandler;

            using (var httpClient = new HttpClient(handler, disposeHandler: false))
            {
                await httpClient.SendAsync(request);
            }

            var request2            = testHandler.SentMessages.Single();
            var authHeaderParameter = request.Headers.Authorization.Parameter;

            // extract the parameter
            string[] elems     = authHeaderParameter.Split(',');
            string   parameter = elems.SingleOrDefault(x => x.StartsWith($"{requestedParameter}="))?.Split('=')[1];

            // we might not have the parameter here.
            if (parameter == null)
            {
                return(null);
            }

            // we are now ensuring that the parameters are contained within double quotes.
            Assert.Equal('"', parameter[0]);
            Assert.Equal('"', parameter[parameter.Length - 1]);

            // if we only have 2 characters, they are both double quotes, so return empty string
            if (parameter.Length == 2)
            {
                return(string.Empty);
            }

            // for the purposes of the test, extract the double quotes
            return(parameter.Substring(1, parameter.Length - 2));
        }