예제 #1
0
        public async Task GetSystems_WithValidResponse_ReturnsSystems()
        {
            // arrange
            var mockFactory            = new Mock <IHttpClientFactory>();
            var mockHttpMessageHandler = new Mock <HttpMessageHandler>();
            var mockContentResponse    = GetFakeCosyDeviceReponse();

            mockHttpMessageHandler.Protected()
            .Setup <Task <HttpResponseMessage> >(
                "SendAsync",
                ItExpr.IsAny <HttpRequestMessage>(),
                ItExpr.IsAny <CancellationToken>()).ReturnsAsync(
                new HttpResponseMessage
            {
                StatusCode = HttpStatusCode.OK,
                Content    = new StringContent(JsonConvert.SerializeObject(mockContentResponse), Encoding.UTF8, "application/json"),
            });


            var client = new HttpClient(mockHttpMessageHandler.Object);

            mockFactory.Setup(_ => _.CreateClient(It.IsAny <string>())).Returns(client);
            var geoCosyClient = new GeoCosyClient(mockFactory.Object);

            // act
            var response = await geoCosyClient.GetSystems("incorrect-token", false);

            // assert
            response.Data.ShouldNotBeNull();
            response.Data.SystemDetails[0].ShouldBeEquivalentTo(mockContentResponse.SystemDetails[0]);
            response.Data.SystemRoles[0].ShouldBeEquivalentTo(mockContentResponse.SystemRoles[0]);
        }
예제 #2
0
        public async Task GetSystems_WithInvalidToken_ReturnsErrorResponse()
        {
            // arrange
            var mockFactory            = new Mock <IHttpClientFactory>();
            var mockHttpMessageHandler = new Mock <HttpMessageHandler>();

            mockHttpMessageHandler.Protected()
            .Setup <Task <HttpResponseMessage> >(
                "SendAsync",
                ItExpr.IsAny <HttpRequestMessage>(),
                ItExpr.IsAny <CancellationToken>()).ReturnsAsync(
                new HttpResponseMessage
            {
                StatusCode = HttpStatusCode.Forbidden
            });


            var client = new HttpClient(mockHttpMessageHandler.Object);

            mockFactory.Setup(_ => _.CreateClient(It.IsAny <string>())).Returns(client);
            var geoCosyClient = new GeoCosyClient(mockFactory.Object);

            // act
            var response = await geoCosyClient.GetSystems("incorrect-token", false);

            // assert
            response.Data.ShouldBeNull();
            response.Errors.ShouldContain(x => x.ErrorCode == ErrorCode.NotAllowed);
        }