Ejemplo n.º 1
0
        public async Task EmptyResult_Serializes_Correctly(string json)
        {
            // Arrange
            var httpHandlerMock = new Mock <HttpMessageHandler>();
            var httpResponse    = new HttpResponseMessage
            {
                StatusCode = HttpStatusCode.OK,
                Content    = new StringContent(json),
            };

            httpResponse.Content.Headers.Remove("Content-Type");
            httpResponse.Content.Headers.Add("Content-Type", "application/json");
            httpHandlerMock.Protected()
            .Setup <Task <HttpResponseMessage> >(
                "SendAsync",
                ItExpr.IsAny <HttpRequestMessage>(),
                ItExpr.IsAny <CancellationToken>())
            .Returns(Task.FromResult(httpResponse));

            var httpClient = new HttpClient(httpHandlerMock.Object)
            {
                BaseAddress = new Uri("https://test.com")
            };

            // Act
            var sut      = new ApiPersonService(httpClient);
            var response = await sut.GetAll();

            // Assert
            Assert.Empty(response);
        }
Ejemplo n.º 2
0
        public async Task Forbidden_Throws_Exception()
        {
            // Arrange
            var httpHandlerMock = new Mock <HttpMessageHandler>();
            var httpResponse    = new HttpResponseMessage
            {
                StatusCode = HttpStatusCode.Forbidden
            };

            httpHandlerMock.Protected()
            .Setup <Task <HttpResponseMessage> >(
                "SendAsync",
                ItExpr.IsAny <HttpRequestMessage>(),
                ItExpr.IsAny <CancellationToken>())
            .Returns(Task.FromResult(httpResponse));

            var httpClient = new HttpClient(httpHandlerMock.Object)
            {
                BaseAddress = new Uri("https://test.com")
            };

            // Act
            var sut = new ApiPersonService(httpClient);

            // Assert
            await Assert.ThrowsAsync <HttpRequestException>(() => sut.GetAll());
        }