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();
            }
        }
Exemplo n.º 2
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();
            }
        }
Exemplo n.º 4
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();
            }
        }
Exemplo n.º 5
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();
            }
        }
        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();
            }
        }
Exemplo n.º 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();
            }
        }
Exemplo n.º 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();
            }
        }
Exemplo n.º 9
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();
            }
        }
Exemplo n.º 10
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);
            }
        }