当前位置: 首页>>代码示例>>C#>>正文


C# AutoMoqer.MockServiceLocator方法代码示例

本文整理汇总了C#中AutoMoqer.MockServiceLocator方法的典型用法代码示例。如果您正苦于以下问题:C# AutoMoqer.MockServiceLocator方法的具体用法?C# AutoMoqer.MockServiceLocator怎么用?C# AutoMoqer.MockServiceLocator使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在AutoMoqer的用法示例。


在下文中一共展示了AutoMoqer.MockServiceLocator方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: WhenClientIdIsInvalid_ThenThrowsException

        public void WhenClientIdIsInvalid_ThenThrowsException()
        {
            var mocker = new AutoMoqer();
            mocker.MockServiceLocator();
            mocker.GetMock<IOAuthRequest>().Setup(x => x.ContentType).Returns(ContentType.FormEncoded);
            mocker.GetMock<IOAuthRequest>().Setup(x => x.ClientId).Returns("");
            mocker.GetMock<IOAuthRequest>().Setup(x => x.ClientSecret).Returns("clientsecret");
            mocker.GetMock<IOAuthRequest>().Setup(x => x.Username).Returns("username");
            mocker.GetMock<IOAuthRequest>().Setup(x => x.Password).Returns("password");
            mocker.GetMock<IOAuthRequest>().Setup(x => x.GrantType).Returns(GrantType.Password);
            mocker.GetMock<IOAuthRequest>().Setup(x => x.ClientId).Returns("clientid");
            mocker.GetMock<IConsumerRepository>().Setup(x => x.GetByClientId("clientid")).Returns<ConsumerImpl>(null);

            var authorizer = mocker.Resolve<PasswordTokenRequestAuthorizer>();

            try
            {
                authorizer.Authorize(mocker.GetMock<IOAuthRequest>().Object);
                Assert.Fail("Exception not thrown");
            }
            catch (OAuthException ex)
            {
                Assert.AreEqual(ErrorCode.InvalidClient, ex.ErrorCode);
                Assert.IsTrue(!string.IsNullOrWhiteSpace(ex.ErrorDescription));
            }
        }
开发者ID:joeyburgett,项目名称:OAuth2Provider,代码行数:26,代码来源:PasswordTokenRequestAuthorizerTest.cs

示例2: ReturnsAuthorizedToken

        public void ReturnsAuthorizedToken()
        {
            var mocker = new AutoMoqer();
            mocker.MockServiceLocator();
            mocker.GetMock<IOAuthRequest>().Setup(x => x.ContentType).Returns(ContentType.FormEncoded);
            mocker.GetMock<IOAuthRequest>().Setup(x => x.ClientId).Returns("clientid");
            mocker.GetMock<IOAuthRequest>().Setup(x => x.ClientSecret).Returns("clientsecret");
            mocker.GetMock<IOAuthRequest>().Setup(x => x.Username).Returns("username");
            mocker.GetMock<IOAuthRequest>().Setup(x => x.GrantType).Returns(GrantType.Password);
            mocker.GetMock<IConsumerRepository>().Setup(x => x.GetByClientId("clientid")).Returns(new ConsumerImpl { ConsumerId = 1, ClientId = "clientid", Secret = "clientsecret" });
            mocker.GetMock<IResourceOwnerRepository>().Setup(x => x.GetByUsername(1, "username")).Returns(new ResourceOwnerImpl { ResourceOwnerId = 2, Username = "username", Password = "password".ToHash() });
            mocker.GetMock<IPasswordHasher>().Setup(x => x.CheckPassword("password", "password".ToHash())).Returns(true);
            mocker.GetMock<IOAuthRequest>().Setup(x => x.Password).Returns("password");
            mocker.GetMock<IConfiguration>().Setup(x => x.AccessTokenExpirationLength).Returns(3600);
            mocker.GetMock<IOAuthServiceLocator>().Setup(x => x.Issuer).Returns(new OAuthIssuer());

            var authorizer = mocker.Resolve<PasswordTokenRequestAuthorizer>();

            var token = authorizer.Authorize(mocker.GetMock<IOAuthRequest>().Object);

            mocker.GetMock<IResourceOwnerRepository>().Verify(x => x.ApproveConsumer(2, 1), Times.Once());
            Assert.IsNotNull(token);
            Assert.IsNotNull(token.AccessToken);
            Assert.AreEqual(3600, token.ExpiresIn);
            Assert.IsNotNull(token.RefreshToken);
        }
开发者ID:joeyburgett,项目名称:OAuth2Provider,代码行数:26,代码来源:PasswordTokenRequestAuthorizerTest.cs

示例3: WhenAccessTokenIsExpired_ThenReturnFalse

        public void WhenAccessTokenIsExpired_ThenReturnFalse()
        {
            var mocker = new AutoMoqer();
            mocker.MockServiceLocator();

            var issuer = new OAuthIssuer();
            mocker.GetMock<IOAuthServiceLocator>().Setup(x => x.Issuer).Returns(issuer);
            mocker.GetMock<IConfiguration>().Setup(x => x.AccessTokenExpirationLength).Returns(3600);

            var validator = mocker.Resolve<ResourceRequestAuthorizer>();

            var token =
                issuer.GenerateAccessToken(new TokenData
                                               {
                                                   ConsumerId = 1,
                                                   ResourceOwnerId = 5,
                                                   Timestamp = DateTimeOffset.UtcNow.AddMinutes(-65).Ticks
                                               });

            mocker.GetMock<IOAuthRequest>().Setup(x => x.AccessToken).Returns(token);

            var result = validator.Authorize(mocker.GetMock<IOAuthRequest>().Object);

            Assert.IsFalse(result);
        }
开发者ID:naasir,项目名称:OAuth2Provider,代码行数:25,代码来源:ResourceRequestAuthorizerTest.cs

示例4: ReturnsAuthorizedToken

        public void ReturnsAuthorizedToken()
        {
            var mocker = new AutoMoqer();
            mocker.MockServiceLocator();
            mocker.GetMock<IOAuthRequest>().Setup(x => x.ClientId).Returns("clientid");
            mocker.GetMock<IOAuthRequest>().Setup(x => x.ClientSecret).Returns("clientsecret");
            mocker.GetMock<IConsumerRepository>().Setup(x => x.GetByClientId("clientid")).Returns(new ConsumerImpl { ConsumerId = 1, ClientId = "clientid", Secret = "clientsecret" });
            mocker.GetMock<IConfiguration>().Setup(x => x.AccessTokenExpirationLength).Returns(3600);
            mocker.GetMock<IOAuthServiceLocator>().Setup(x => x.Issuer).Returns(new OAuthIssuer());

            var authorizer = mocker.Resolve<ClientCredentialsTokenRequestAuthorizer>();

            var token = authorizer.Authorize(mocker.GetMock<IOAuthRequest>().Object);

            Assert.IsNotNull(token);
            Assert.IsNotNull(token.AccessToken);
            Assert.AreEqual(3600, token.ExpiresIn);
            Assert.IsNotNull(token.RefreshToken);
        }
开发者ID:naasir,项目名称:OAuth2Provider,代码行数:19,代码来源:ClientCredentialsTokenRequestAuthorizerTest.cs

示例5: GetAuthorizationToken

        public void GetAuthorizationToken()
        {
            var mocker = new AutoMoqer();
            mocker.MockServiceLocator();
            var properties = new Dictionary<string, IList<string>>
                                 {
                                     {OAuthTokens.ResponseType, new[]{ResponseType.Code}},
                                     {OAuthTokens.ClientId, new[]{"1"}},
                                     {OAuthTokens.RedirectUri, new[]{"http://mydomain.com"}}
                                 };
            mocker.GetMock<IRequest>().Setup(x => x.Values).Returns(properties);
            mocker.GetMock<IConfiguration>().Setup(x => x.AuthorizationTokenExpirationLength).Returns(500);

            mocker.GetMock<IOAuthServiceLocator>().Setup(x => x.Issuer).Returns(new OAuthIssuer());
            var request = new AuthorizationRequest(mocker.GetMock<IRequest>().Object,mocker.GetMock<IOAuthServiceLocator>().Object);

            var token = request.GetAuthorizationToken(1, 5, null);
            Assert.AreEqual(500, token.ExpiresIn);
            Assert.IsTrue(token.AuthorizationToken.HasValue());
        }
开发者ID:naasir,项目名称:OAuth2Provider,代码行数:20,代码来源:AuthorizationRequestTest.cs

示例6: WhenClientSecretIsInvalid_ThenThrowsException

        public void WhenClientSecretIsInvalid_ThenThrowsException()
        {
            var mocker = new AutoMoqer();
            mocker.MockServiceLocator();
            mocker.GetMock<IOAuthRequest>().Setup(x => x.ClientId).Returns("clientid");
            mocker.GetMock<IOAuthRequest>().Setup(x => x.ClientSecret).Returns("clientsecret");
            mocker.GetMock<IConsumerRepository>().Setup(x => x.GetByClientId("clientid")).Returns(new ConsumerImpl { ClientId = "clientid", Secret = "secret" });

            var authorizer = mocker.Resolve<ClientCredentialsTokenRequestAuthorizer>();

            try
            {
                authorizer.Authorize(mocker.GetMock<IOAuthRequest>().Object);
                Assert.Fail("Exception not thrown");
            }
            catch (OAuthException ex)
            {
                Assert.AreEqual(ErrorCode.InvalidClient, ex.ErrorCode);
                Assert.IsTrue(!string.IsNullOrWhiteSpace(ex.ErrorDescription));
            }
        }
开发者ID:naasir,项目名称:OAuth2Provider,代码行数:21,代码来源:ClientCredentialsTokenRequestAuthorizerTest.cs

示例7: EnsureApplicationIsApproved

        public void EnsureApplicationIsApproved()
        {
            var mocker = new AutoMoqer();
            mocker.MockServiceLocator();
            mocker.GetMock<IOAuthRequest>().Setup(x => x.ContentType).Returns(ContentType.FormEncoded);
            mocker.GetMock<IOAuthRequest>().Setup(x => x.ClientId).Returns("clientid");
            mocker.GetMock<IOAuthRequest>().Setup(x => x.ClientSecret).Returns("clientsecret");
            mocker.GetMock<IOAuthRequest>().Setup(x => x.Username).Returns("username");
            mocker.GetMock<IOAuthRequest>().Setup(x => x.GrantType).Returns(GrantType.Password);
            mocker.GetMock<IConsumerRepository>().Setup(x => x.GetByClientId("clientid")).Returns(new ConsumerImpl { ConsumerId = 1, ClientId = "clientid", Secret = "clientsecret" });
            mocker.GetMock<IResourceOwnerRepository>().Setup(x => x.GetByUsername(1, "username")).Returns(new ResourceOwnerImpl { ResourceOwnerId = 2, Username = "username", Password = "password".ToHash() });
            mocker.GetMock<IPasswordHasher>().Setup(x => x.CheckPassword("password", "password".ToHash())).Returns(true);
            mocker.GetMock<IOAuthRequest>().Setup(x => x.Password).Returns("password");
            mocker.SetInstance<IOAuthIssuer>(new OAuthIssuer());

            var authorizer = mocker.Resolve<PasswordTokenRequestAuthorizer>();

            var token = authorizer.Authorize(mocker.GetMock<IOAuthRequest>().Object);

            mocker.GetMock<IResourceOwnerRepository>().Verify(x => x.ApproveConsumer(2, 1), Times.Once());
        }
开发者ID:joeyburgett,项目名称:OAuth2Provider,代码行数:21,代码来源:PasswordTokenRequestAuthorizerTest.cs


注:本文中的AutoMoqer.MockServiceLocator方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。