Esempio n. 1
0
        public async void ErrorResponseShouldThrow()
        {
            // Arrange
            const string otherComponentBaseUrl = "https://api.othercomponent.com/";
            var          serviceCollection     = new ServiceCollection();

            serviceCollection.Configure <AppSettings>(settings =>
            {
                settings.OtherComponentBaseUrl = otherComponentBaseUrl;
            });

            var httpMessageHandler = new TestHttpMessageHandler();

            httpMessageHandler.PushGetResponse(new Uri(new Uri(otherComponentBaseUrl), "/secret"), HttpStatusCode.BadRequest, "You are saying it wrong!");
            var fixture = new AuthorizedTestFixture <DemoStartup>(httpMessageHandler: httpMessageHandler, serviceCollection: serviceCollection);

            // Act
            var request = new HttpRequestMessage(HttpMethod.Get, ApiEndpointGetFromOtherComponent);

            request.Headers.Add("Authorization", "Bearer " + fixture.TokenService.GetToken());

            // Assert
            var exception = await Assert.ThrowsAsync <Exception>(() => fixture.HttpClient.SendAsync(request));

            Assert.Equal("Could not get secret from other!", exception.Message);
        }
Esempio n. 2
0
        public async void AddedGetResponseShouldBeReturned()
        {
            // Arrange
            const string otherComponentBaseUrl = "https://api.othercomponent.com/";
            var          serviceCollection     = new ServiceCollection();

            serviceCollection.Configure <AppSettings>(settings =>
            {
                settings.OtherComponentBaseUrl = otherComponentBaseUrl;
            });

            var httpMessageHandler = new TestHttpMessageHandler();

            httpMessageHandler.PushGetResponse(new Uri(new Uri(otherComponentBaseUrl), "/secret"), HttpStatusCode.OK, "Mellon!");
            var fixture = new AuthorizedTestFixture <DemoStartup>(httpMessageHandler: httpMessageHandler, serviceCollection: serviceCollection);

            // Act
            var request = new HttpRequestMessage(HttpMethod.Get, ApiEndpointGetFromOtherComponent);

            request.Headers.Add("Authorization", "Bearer " + fixture.TokenService.GetToken());
            var response = await fixture.HttpClient.SendAsync(request);

            // Assert
            Assert.True(response.IsSuccessStatusCode);
            Assert.Equal(HttpStatusCode.OK, response.StatusCode);
        }
Esempio n. 3
0
        public static void ConfigureFakeAuth0Authority(
            this TestHttpMessageHandler testHttpMessageHandler,
            Auth0Settings appSettings,
            TestTokenService tokenService)
        {
            var jwksUri = new Uri(new Uri(appSettings.Auth0Domain), WellKnownJwks);

            testHttpMessageHandler.PushGetResponse(
                jwksUri,
                HttpStatusCode.OK,
                new Jwks
            {
                Keys = new List <Key>
                {
                    new Key
                    {
                        Alg = "RS256",
                        Kty = "RSA",
                        Use = "sig",
                        X5C = new List <string>
                        {
                            tokenService.GetSigningKey()
                        }
                    }
                }
            },
                true
                );

            testHttpMessageHandler.PushGetResponse(
                new Uri(new Uri(appSettings.Auth0Domain), WellKnownOpenIdConfiguartion),
                HttpStatusCode.OK,
                new OpenIdConfiguration
            {
                Issuer  = appSettings.Auth0Domain,
                JwksUri = jwksUri,
                IdTokenSigningAlgValuesSupported = new List <string>
                {
                    "RS256"
                }
            },
                true
                );
        }