Exemplo n.º 1
0
        public async Task CachesExistingTokenWhenPossible()
        {
            // Arrange
            var tokenProvider = new Mock <IAccessTokenProvider>();

            tokenProvider.Setup(tp => tp.RequestAccessToken())
            .Returns(new ValueTask <AccessTokenResult>(new AccessTokenResult(AccessTokenResultStatus.Success,
                                                                             new AccessToken
            {
                Expires       = DateTime.Now.AddHours(1),
                GrantedScopes = new string[] { "All" },
                Value         = "asdf"
            },
                                                                             "https://www.example.com")));

            var handler = new AuthorizationMessageHandler(tokenProvider.Object, Mock.Of <NavigationManager>());

            handler.ConfigureHandler(new[] { "https://localhost:5001" });

            var response = new HttpResponseMessage(HttpStatusCode.OK);

            handler.InnerHandler = new TestMessageHandler(response);

            // Act
            _ = await new HttpClient(handler).GetAsync("https://localhost:5001/weather");
            response.RequestMessage = null;

            _ = await new HttpClient(handler).GetAsync("https://localhost:5001/weather");

            // Assert
            Assert.Single(tokenProvider.Invocations);
            Assert.Equal("asdf", response.RequestMessage.Headers.Authorization.Parameter);
        }
Exemplo n.º 2
0
        public async Task UsesCustomScopesAndReturnUrlWhenProvided()
        {
            // Arrange
            var tokenProvider = new Mock <IAccessTokenProvider>();

            tokenProvider.Setup(tp => tp.RequestAccessToken(It.IsAny <AccessTokenRequestOptions>()))
            .Returns(new ValueTask <AccessTokenResult>(new AccessTokenResult(AccessTokenResultStatus.Success,
                                                                             new AccessToken
            {
                Expires       = DateTime.Now.AddMinutes(3),
                GrantedScopes = new string[] { "All" },
                Value         = "asdf"
            },
                                                                             "https://www.example.com/return")));

            var handler = new AuthorizationMessageHandler(tokenProvider.Object, Mock.Of <NavigationManager>());

            handler.ConfigureHandler(
                new[] { "https://localhost:5001" },
                scopes: new[] { "example.read", "example.write" },
                returnUrl: "https://www.example.com/return");

            var response = new HttpResponseMessage(HttpStatusCode.OK);

            handler.InnerHandler = new TestMessageHandler(response);

            // Act
            _ = await new HttpClient(handler).GetAsync("https://localhost:5001/weather");

            // Assert
            Assert.Equal(1, tokenProvider.Invocations.Count);
        }
Exemplo n.º 3
0
        public async Task RequestNewTokenWhenCurrentTokenIsAboutToExpire()
        {
            // Arrange
            var tokenProvider = new Mock <IAccessTokenProvider>();

            tokenProvider.Setup(tp => tp.RequestAccessToken())
            .Returns(new ValueTask <AccessTokenResult>(new AccessTokenResult(AccessTokenResultStatus.Success,
                                                                             new AccessToken
            {
                Expires       = DateTime.Now.AddMinutes(3),
                GrantedScopes = new string[] { "All" },
                Value         = "asdf"
            },
                                                                             "https://www.example.com")));

            var handler = new AuthorizationMessageHandler(tokenProvider.Object, Mock.Of <NavigationManager>());

            handler.ConfigureHandler(new[] { "https://localhost:5001" });

            var response = new HttpResponseMessage(HttpStatusCode.OK);

            handler.InnerHandler = new TestMessageHandler(response);

            // Act
            _ = await new HttpClient(handler).GetAsync("https://localhost:5001/weather");
            response.RequestMessage = null;

            _ = await new HttpClient(handler).GetAsync("https://localhost:5001/weather");

            // Assert
            Assert.Equal(2, tokenProvider.Invocations.Count);
        }
Exemplo n.º 4
0
        public async Task DoesNotAttachTokenToRequest_IfNotPresentInListOfAllowedUrls()
        {
            // Arrange
            var tokenProvider = new Mock <IAccessTokenProvider>();

            var handler = new AuthorizationMessageHandler(tokenProvider.Object, Mock.Of <NavigationManager>());

            handler.ConfigureHandler(new[] { "https://localhost:5001" });

            var response = new HttpResponseMessage(HttpStatusCode.OK);

            handler.InnerHandler = new TestMessageHandler(response);

            // Act
            _ = await new HttpClient(handler).GetAsync("https://www.example.com");

            // Assert
            tokenProvider.VerifyNoOtherCalls();
        }
Exemplo n.º 5
0
        public async Task ThrowsWhenItCanNotProvisionANewToken()
        {
            // Arrange
            var tokenProvider = new Mock <IAccessTokenProvider>();

            tokenProvider.Setup(tp => tp.RequestAccessToken())
            .Returns(new ValueTask <AccessTokenResult>(new AccessTokenResult(AccessTokenResultStatus.RequiresRedirect,
                                                                             null,
                                                                             "https://www.example.com")));

            var handler = new AuthorizationMessageHandler(tokenProvider.Object, Mock.Of <NavigationManager>());

            handler.ConfigureHandler(new[] { "https://localhost:5001" });

            var response = new HttpResponseMessage(HttpStatusCode.OK);

            handler.InnerHandler = new TestMessageHandler(response);

            // Act & assert
            var exception = await Assert.ThrowsAsync <AccessTokenNotAvailableException>(() => new HttpClient(handler).GetAsync("https://localhost:5001/weather"));
        }