public async Task GetMarketStackIndexes_HtttReponseNotContainsError_ReturnsValidContent() { Mock <IHttpClientHandler> _mockHttpClientHandler = new Mock <IHttpClientHandler>(); Mock <IStreamHandler> _mockStreamHandler = new Mock <IStreamHandler>(); _mockHttpClientHandler.Setup(httpClient => httpClient.GetAsync(It.IsAny <string>())) .ReturnsAsync(CreateHttpResponseMessage(HttpStatusCode.OK)); _mockStreamHandler.Setup(stream => stream.DeserializeJsonFromStreamAsync <SPIndexDTO>(It.IsAny <Stream>())) .ReturnsAsync(new SPIndexDTO() { Data = new List <SPIndexValueDTO>() { CreateMockedSPIndexValueDTO(3, 4), CreateMockedSPIndexValueDTO(6, 7) }, Pagination = CreateMockedSPIndexPaginationDTO(2, 100, 15) }); MarketStackService marketStackService = new MarketStackService(_mockHttpClientHandler.Object, _mockStreamHandler.Object); HttpHeaderSetting setting = CreateHttpHeaderSetting("https://marketstack.com/", "testKey", "json"); var url = "api/testURL"; var res = await marketStackService.GetMarketStackIndexes(url, setting); Assert.Equal(2, res.Data.Count); Assert.Equal(3, res.Data[0].Low); Assert.Equal(4, res.Data[0].High); Assert.Equal(6, res.Data[1].Low); Assert.Equal(7, res.Data[1].High); }
public async Task GetMarketStackIndexes_HtttReponseContainsError_ThrowsCustomException() { Mock <IHttpClientHandler> _mockHttpClientHandler = new Mock <IHttpClientHandler>(); Mock <IStreamHandler> _mockStreamHandler = new Mock <IStreamHandler>(); _mockHttpClientHandler.Setup(httpClient => httpClient.GetAsync(It.IsAny <string>())) .ReturnsAsync(CreateHttpResponseMessage(HttpStatusCode.InternalServerError)); MarketStackService marketStackService = new MarketStackService(_mockHttpClientHandler.Object, _mockStreamHandler.Object); HttpHeaderSetting setting = CreateHttpHeaderSetting("https://marketstack.com/", "testKey", "json"); var url = "api/testURL"; const int internalServerError = 500; Func <Task <SPIndexDTO> > deleg = () => marketStackService.GetMarketStackIndexes(url, setting); var exceptionDetails = await Assert.ThrowsAsync <ApiException>(deleg); Assert.Equal(exceptionDetails.StatusCode, internalServerError); }