public async Task Then_The_Endpoint_Is_Called_And_CourseDirectory_Data_Returned( string apiKey, string coursesUrl, List <Provider> importProviders) { //Arrange coursesUrl = $"https://test.local/{coursesUrl}"; var configuration = new Mock <IOptions <CourseDirectoryConfiguration> >(); configuration.Setup(x => x.Value.Key).Returns(apiKey); configuration.Setup(x => x.Value.Url).Returns(coursesUrl); var response = new HttpResponseMessage { Content = new StringContent(JsonConvert.SerializeObject(importProviders)), StatusCode = HttpStatusCode.Accepted }; var httpMessageHandler = MessageHandler.SetupMessageHandlerMock(response, new Uri(coursesUrl), HttpMethod.Get); var client = new HttpClient(httpMessageHandler.Object); var apprenticeshipService = new CourseDirectoryService(client, configuration.Object); //Act var providers = await apprenticeshipService.GetProviderCourseInformation(); //Assert providers.Should().BeEquivalentTo(importProviders); }
public async Task GetAllProvidersData_UnsuccessfulResponse_ReturnsJsonContent() { var mockMessageHandler = new Mock <HttpMessageHandler>(); mockMessageHandler.Protected() .Setup <Task <HttpResponseMessage> >("SendAsync", ItExpr.IsAny <HttpRequestMessage>(), ItExpr.IsAny <CancellationToken>()) .ReturnsAsync(new HttpResponseMessage { StatusCode = HttpStatusCode.BadRequest, RequestMessage = new HttpRequestMessage() }); HttpClient httpClient = new HttpClient(mockMessageHandler.Object); httpClient.BaseAddress = new Uri("https://test"); var sut = new CourseDirectoryService(httpClient, Mock.Of <ILogger <CourseDirectoryService> >()); Func <Task> action = () => sut.GetAllProvidersData(); await action.Should().ThrowAsync <HttpRequestException>(); }
public void Then_If_It_Is_Not_Successful_An_Exception_Is_Thrown( string apiKey, string coursesUrl) { //Arrange coursesUrl = $"https://test.local/{coursesUrl}"; var configuration = new Mock <IOptions <CourseDirectoryConfiguration> >(); configuration.Setup(x => x.Value.Key).Returns(apiKey); configuration.Setup(x => x.Value.Url).Returns(coursesUrl); var response = new HttpResponseMessage { Content = new StringContent(""), StatusCode = HttpStatusCode.BadRequest }; var httpMessageHandler = MessageHandler.SetupMessageHandlerMock(response, new Uri(coursesUrl), HttpMethod.Get); var client = new HttpClient(httpMessageHandler.Object); var apprenticeshipService = new CourseDirectoryService(client, configuration.Object); //Act Assert Assert.ThrowsAsync <HttpRequestException>(() => apprenticeshipService.GetProviderCourseInformation()); }
public async Task GetAllProvidersData_OkResponse_ReturnsJsonContent() { var content = @"[{ 'id': 1, 'title': 'Cool post!'}, { 'id': 100, 'title': 'Some title'}]"; var mockMessageHandler = new Mock <HttpMessageHandler>(); mockMessageHandler.Protected() .Setup <Task <HttpResponseMessage> >("SendAsync", ItExpr.IsAny <HttpRequestMessage>(), ItExpr.IsAny <CancellationToken>()) .ReturnsAsync(new HttpResponseMessage { StatusCode = HttpStatusCode.OK, Content = new StringContent(content), RequestMessage = new HttpRequestMessage() }); HttpClient httpClient = new HttpClient(mockMessageHandler.Object); httpClient.BaseAddress = new Uri("https://test"); var sut = new CourseDirectoryService(httpClient, Mock.Of <ILogger <CourseDirectoryService> >()); var result = await sut.GetAllProvidersData(); result.Should().Be(content); }