Пример #1
0
        public async Task ValidateAsync_InvalidToken()
        {
            var authenticationSettings = new AuthenticationSettings
            {
                Microsoft = new MicrosoftAuthenticationSettings
                {
                    ClientId = ClientId,
                },
            };
            var options = Options.Create(authenticationSettings);

            var configuration = new OpenIdConnectConfiguration();

            configuration.JsonWebKeySet = new JsonWebKeySet();
            configuration.JsonWebKeySet.Keys.Add(jsonWebKey);

            using (var http = new HttpClientTestingFactory())
            {
                var handler    = new MicrosoftAssertionGrantHandler(options, http.HttpClient);
                var resultTask = handler.ValidateAsync("SomeBadAssertion");

                http.Expect(ConfigurationEndpoint).Respond(OpenIdConnectConfiguration.Write(configuration));

                var result = await resultTask;
                Assert.NotNull(result);
                Assert.False(result.IsSuccessful);

                http.EnsureNoOutstandingRequests();
            }
        }
        public static async Task ValidateAsync_WrongAudience()
        {
            var authenticationSettings = new AuthenticationSettings
            {
                Google = new GoogleAuthenticationSettings
                {
                    ClientId = ClientId,
                },
            };
            var options = Options.Create(authenticationSettings);

            using (var http = new HttpClientTestingFactory())
            {
                var handler    = new GoogleAssertionGrantHandler(options, http.HttpClient);
                var resultTask = handler.ValidateAsync(Assertion);

                http.Expect(ValidationEndpoint).Respond(JsonConvert.SerializeObject(new JsonWebToken
                {
                    Aud = "SomeOtherClientId",
                    Sub = ExternalUserId,
                }));

                var result = await resultTask;
                Assert.NotNull(result);
                Assert.False(result.IsSuccessful);

                http.EnsureNoOutstandingRequests();
            }
        }
        public static async Task ValidateAsync_HttpError_UserEndpoint()
        {
            var authenticationSettings = new AuthenticationSettings
            {
                Facebook = new FacebookAuthenticationSettings
                {
                    AppId = AppId,
                },
            };
            var options = Options.Create(authenticationSettings);

            using (var http = new HttpClientTestingFactory(HttpClientTestingFactorySettings))
            {
                var handler    = new FacebookAssertionGrantHandler(options, http.HttpClient);
                var resultTask = handler.ValidateAsync(Assertion);

                http.Expect(AppEndpoint).Respond(JsonConvert.SerializeObject(new FacebookApp {
                    Id = AppId
                }));
                http.Expect(UserEndpoint).Respond(HttpStatusCode.BadRequest);

                var result = await resultTask;
                Assert.NotNull(result);
                Assert.False(result.IsSuccessful);

                http.EnsureNoOutstandingRequests();
            }
        }
Пример #4
0
        public void Setup()
        {
            _options                = new RepositoryAgentOptions();
            _httpClientFactory      = new Mock <IHttpClientFactory>();
            _apiDescriptionProvider = new Mock <IApiDescriptionProvider>();

            _repositoryClient = new ServiceRepositoryClient(_options, _httpClientFactory.Object, new Mock <ILogger <ServiceRepositoryClient> >().Object, _apiDescriptionProvider.Object);

            _httpClientTestingFactory = new HttpClientTestingFactory();
            _httpClientTestingFactory.HttpClient.BaseAddress = new Uri("http://repository.com");
            _httpClientFactory.Setup(f => f.CreateClient(ServiceRepositoryClient.HTTPCLIENT_NAME)).Returns(_httpClientTestingFactory.HttpClient);
        }
Пример #5
0
        public void Setup()
        {
            _options           = new AgentOptions();
            _httpClientFactory = new Mock <IHttpClientFactory>();
            _server            = new Mock <IServer>();
            var features = new FeatureCollection();

            _serverAddressesFeature = new ServerAddressesFeature();
            features.Set <IServerAddressesFeature>(_serverAddressesFeature);
            _server.SetupGet(s => s.Features).Returns(features);

            _registryClient = new RegistryClient(_options, _httpClientFactory.Object, _server.Object, new Mock <ILogger <RegistryClient> >().Object);

            _httpClientTestingFactory = new HttpClientTestingFactory();
            _httpClientTestingFactory.HttpClient.BaseAddress = new Uri("http://registry.com");
            _httpClientFactory.Setup(f => f.CreateClient(RegistryClient.HTTPCLIENT_NAME)).Returns(_httpClientTestingFactory.HttpClient);
        }
        public static async Task ValidateAsync_HttpError()
        {
            var authenticationSettings = new AuthenticationSettings();
            var options = Options.Create(authenticationSettings);

            using (var http = new HttpClientTestingFactory())
            {
                var handler    = new GoogleAssertionGrantHandler(options, http.HttpClient);
                var resultTask = handler.ValidateAsync(Assertion);

                http.Expect(ValidationEndpoint).Respond(HttpStatusCode.BadRequest);

                var result = await resultTask;
                Assert.NotNull(result);
                Assert.False(result.IsSuccessful);

                http.EnsureNoOutstandingRequests();
            }
        }
Пример #7
0
        public async Task PostDataTest()
        {
            using (var http = new HttpClientTestingFactory())
            {
                var worker = new Worker(http.HttpClient);

                // Make the call, but don't await the task
                var resultTask = worker.PostDataAsync();

                // Expect the request, validate it, and respond to it
                var request = http.Expect(HttpMethod.Post, "http://some-website.com/some-path");
                Assert.AreEqual("some data", await request.Request.Content.ReadAsStringAsync());
                request.Respond(HttpStatusCode.OK);

                // Let the call finish
                await resultTask;

                http.EnsureNoOutstandingRequests();
            }
        }
Пример #8
0
        public async Task FetchDataTest()
        {
            using (var http = new HttpClientTestingFactory())
            {
                var worker = new Worker(http.HttpClient);

                // Make the call, but don't await the task
                var resultTask = worker.FetchDataAsync();

                // Expect the request and respond to it
                var request = http.Expect("http://some-website.com/some-path");
                request.Respond(HttpStatusCode.OK, "123");

                // Await the result and assert on it
                var result = await resultTask;
                Assert.AreEqual(123, result);

                http.EnsureNoOutstandingRequests();
            }
        }
Пример #9
0
        public async Task FetchSequentialDataAsync()
        {
            using (var http = new HttpClientTestingFactory())
            {
                var worker = new Worker(http.HttpClient);

                // Make the call, but don't await the task
                var resultTask = worker.FetchSequentialDataAsync();

                // Expect the requests and respond to them
                http.Expect("http://some-website.com/items").Respond("1,2,3");
                http.Expect("http://some-website.com/items/3/subItems").Respond("4,5,6");
                http.Expect("http://some-website.com/items/3/subItems/6").Respond("item 3 subitem 6 data");

                // Await the result and assert on it
                var result = await resultTask;
                Assert.AreEqual("item 3 subitem 6 data", result);

                http.EnsureNoOutstandingRequests();
            }
        }
Пример #10
0
        public async Task FetchParallelDataTest()
        {
            using (var http = new HttpClientTestingFactory())
            {
                var worker = new Worker(http.HttpClient);

                // Make the call, but don't await the task
                var resultTask = worker.FetchParallelDataAsync();

                // Expect the requests and respond to them
                http.Expect("http://some-website.com/1").Respond("1");
                http.Expect("http://some-website.com/2").Respond("2");
                http.Expect("http://some-website.com/3").Respond("3");

                // Await the result and assert on it
                var result = await resultTask;
                Assert.AreEqual(6, result);

                http.EnsureNoOutstandingRequests();
            }
        }
Пример #11
0
        public async Task ValidateAsync_Success()
        {
            var authenticationSettings = new AuthenticationSettings
            {
                Microsoft = new MicrosoftAuthenticationSettings
                {
                    ClientId = ClientId,
                },
            };
            var options = Options.Create(authenticationSettings);

            var configuration = new OpenIdConnectConfiguration();

            configuration.JsonWebKeySet = new JsonWebKeySet();
            configuration.JsonWebKeySet.Keys.Add(jsonWebKey);

            using (var http = new HttpClientTestingFactory())
            {
                var tokenHandler = new JwtSecurityTokenHandler();
                var token        = new JwtSecurityToken(
                    audience: ClientId,
                    claims: new[] { new Claim("sub", ExternalUserId), new Claim("email", ExternalUserEmail) },
                    notBefore: DateTime.UtcNow,
                    expires: DateTime.UtcNow + TimeSpan.FromHours(1),
                    signingCredentials: new SigningCredentials(jsonWebKey, jsonWebKey.Alg));

                var handler    = new MicrosoftAssertionGrantHandler(options, http.HttpClient);
                var resultTask = handler.ValidateAsync(tokenHandler.WriteToken(token));

                http.Expect(ConfigurationEndpoint).Respond(OpenIdConnectConfiguration.Write(configuration));

                var result = await resultTask;
                Assert.NotNull(result);
                Assert.True(result.IsSuccessful);
                Assert.Equal(ExternalUserId, result.ExternalUserId);
                Assert.Equal(ExternalUserEmail, result.ExternalUserEmail);

                http.EnsureNoOutstandingRequests();
            }
        }
Пример #12
0
        public async Task ShouldMockMyHttpCall()
        {
            using (var httpFactory = new HttpClientTestingFactory())
            {
                var baseUrl = "http://some-website.com/some-path";
                var service = new MySuperService(httpFactory.HttpClient, baseUrl);

                var response = service.MakeAsyncCall();

                var request = httpFactory.Expect(baseUrl);

                Assert.AreEqual(request.Request.Method, HttpMethod.Get);

                Assert.AreEqual("Value", GetHeaderValueFromRequest(request, "CustomHeader"));

                request.Respond(HttpStatusCode.OK, "123");

                var result = await response;

                Assert.AreEqual("123", result);
            }
        }
 public void sendEmptyvariables()
 {
     using (var http = new HttpClientTestingFactory())
     {
     }
 }