コード例 #1
0
        public async Task AuthenticateAsync_TokenRequestedAndPrepareRequest_RequestAuthHeaderIsSetCorrect()
        {
            const string expectedToken = "TokenData";

            var jwtClientMock = A.Fake <IJwtClient>();

            A.CallTo(() => jwtClientMock.RequestTokenInfoAsync(new Uri("http://authtest.com/auth"), A <JwtTokenRequest> .Ignored))
            .Returns(Task.FromResult(new JwtTokenInfo(expectedToken)));

            var jwtHttpClientAuthenticator = new JwtHttpClientAuthenticator(jwtClientMock)
            {
                Credentials     = new NetworkCredential("user", "pass"),
                TokenRequestUri = new Uri("http://authtest.com/auth")
            };

            await jwtHttpClientAuthenticator.AuthenticateAsync(new Uri("http://test.com"));

            var request = new HttpRequestMessage();

            jwtHttpClientAuthenticator.PrepareHttpRequest(request);

            var authHeader = request.Headers.Authorization;

            Assert.NotNull(authHeader);
            Assert.Equal("Bearer", authHeader.Scheme);
            Assert.Equal(expectedToken, authHeader.Parameter);
        }
コード例 #2
0
        public void PrepareHttpRequest_NoToken_AuthHeaderOnRequestIsNotSet()
        {
            var jwtClientMock = A.Fake <IJwtClient>();

            var jwtHttpClientAuthenticator = new JwtHttpClientAuthenticator(jwtClientMock);

            var request = new HttpRequestMessage();

            jwtHttpClientAuthenticator.PrepareHttpRequest(request);

            Assert.Null(request.Headers.Authorization);
        }