Exemplo n.º 1
0
        private async void ShouldNotReturnTokenWhenAnyExceptionHappened()
        {
            var handlerMock   = new Mock <HttpMessageHandler>(MockBehavior.Strict);
            var httpClient    = new HttpClient(handlerMock.Object);
            var gatewayClient = new GatewayClient(httpClient, null);

            var result = await gatewayClient.Authenticate();

            result.HasValue.Should().BeFalse();
            handlerMock.Protected().Verify(
                "SendAsync",
                Times.Exactly(0),
                ItExpr.IsAny <HttpRequestMessage>(),
                ItExpr.IsAny <CancellationToken>());
        }
Exemplo n.º 2
0
        private async void ShouldReturnAccessToken()
        {
            var          handlerMock   = new Mock <HttpMessageHandler>(MockBehavior.Strict);
            var          httpClient    = new HttpClient(handlerMock.Object);
            const string rootRul       = "http://someUrl";
            var          expectedUri   = new Uri($"{rootRul}/{PATH_SESSIONS}");
            var          correlationId = Uuid.Generate().ToString();
            var          configuration = new GatewayConfiguration
            {
                Url          = rootRul,
                ClientId     = TestBuilder.RandomString(),
                ClientSecret = TestBuilder.RandomString()
            };
            var response = JsonConvert.SerializeObject(new { tokenType = "bearer", accessToken = "token" });

            handlerMock
            .Protected()
            .Setup <Task <HttpResponseMessage> >(
                "SendAsync",
                ItExpr.IsAny <HttpRequestMessage>(),
                ItExpr.IsAny <CancellationToken>())
            .ReturnsAsync(new HttpResponseMessage
            {
                StatusCode = HttpStatusCode.OK,
                Content    = new StringContent(response, Encoding.UTF8, MediaTypeNames.Application.Json)
            })
            .Verifiable();

            var client = new GatewayClient(httpClient, configuration);

            var result = await client.Authenticate(correlationId);

            result.HasValue.Should().BeTrue();
            result.MatchSome(token => token.Should().BeEquivalentTo("bearer token"));
            handlerMock.Protected().Verify(
                "SendAsync",
                Times.Exactly(1),
                ItExpr.Is <HttpRequestMessage>(message => message.Method == HttpMethod.Post &&
                                               message.RequestUri == expectedUri),
                ItExpr.IsAny <CancellationToken>());
        }
Exemplo n.º 3
0
        private async void ShouldNotReturnTokenWhenErrorResponse(HttpStatusCode statusCode)
        {
            var          handlerMock            = new Mock <HttpMessageHandler>(MockBehavior.Strict);
            var          httpClient             = new HttpClient(handlerMock.Object);
            const string centralRegistryRootUrl = "http://someUrl";
            var          expectedUri            = new Uri($"{centralRegistryRootUrl}/{PATH_SESSIONS}");
            var          configuration          = new GatewayConfiguration
            {
                Url          = centralRegistryRootUrl,
                ClientId     = TestBuilder.RandomString(),
                ClientSecret = TestBuilder.RandomString()
            };
            var response      = JsonConvert.SerializeObject(new { error = "some failure happened" });
            var correlationId = Uuid.Generate().ToString();

            handlerMock
            .Protected()
            .Setup <Task <HttpResponseMessage> >(
                "SendAsync",
                ItExpr.IsAny <HttpRequestMessage>(),
                ItExpr.IsAny <CancellationToken>())
            .ReturnsAsync(new HttpResponseMessage
            {
                StatusCode = statusCode,
                Content    = new StringContent(response, Encoding.UTF8, MediaTypeNames.Application.Json)
            })
            .Verifiable();

            var client = new GatewayClient(httpClient, configuration);

            var result = await client.Authenticate(correlationId);

            result.HasValue.Should().BeFalse();
            handlerMock.Protected().Verify(
                "SendAsync",
                Times.Exactly(1),
                ItExpr.Is <HttpRequestMessage>(message => message.Method == HttpMethod.Post &&
                                               message.RequestUri == expectedUri),
                ItExpr.IsAny <CancellationToken>());
        }