public async Task Then_The_Endpoint_Is_Called_And_Data_Returned() { //Arrange var fixture = new Fixture().Customize(new AutoMoqCustomization { ConfigureMembers = true }); var postcodeLookup = fixture.Create <PostcodeLookup>(); var postcodes = fixture.Create <PostcodeLookupRequest>(); var response = new HttpResponseMessage { Content = new StringContent(JsonConvert.SerializeObject(postcodeLookup)), StatusCode = HttpStatusCode.OK }; var uri = new Uri(ConfigurationConstants.PostcodeLookupUrl + "postcodes"); var httpMessageHandler = MessageHandler.SetupMessageHandlerMock(response, uri, HttpMethod.Post); var client = new HttpClient(httpMessageHandler.Object); var apiService = new PostcodeApiService(client); //Act var postcodeData = await apiService.GetPostcodeData(postcodes); //Assert httpMessageHandler.Protected() .Verify <Task <HttpResponseMessage> >( "SendAsync", Times.Once(), ItExpr.Is <HttpRequestMessage>(c => c.Method.Equals(HttpMethod.Post) && c.RequestUri.Equals(uri)), ItExpr.IsAny <CancellationToken>() ); postcodeData.Should().BeEquivalentTo(postcodeLookup); }
public async Task Then_The_Endpoint_Is_Called_And_Outcode_Data_Returned( PostcodesLocationApiResponse postcodeResponse, string query, int count) { foreach (var postcode in postcodeResponse.Result) { postcode.Country = "England"; } //Arrange var response = new HttpResponseMessage { Content = new StringContent(JsonConvert.SerializeObject(postcodeResponse)), StatusCode = System.Net.HttpStatusCode.Accepted }; var httpMessageHandler = MessageHandler.SetupMessageHandlerMock(response, new Uri(string.Format(Constants.PostcodesUrl, query, count))); var client = new HttpClient(httpMessageHandler.Object); var postcodeService = new PostcodeApiService(client); //Act var actual = await postcodeService.GetAllStartingWithOutcode(query, count); //Assert actual.Should().BeEquivalentTo(postcodeResponse.Result, options => options.Excluding(c => c.AdminDistrict)); }
public async Task Then_The_Endpoint_Is_Called_And_District_Data_Returned_For_English_Postcodes( PostcodeLocationDistrictApiResponse postcodeResponse, string query) { //Arrange postcodeResponse.Result.Country = postcodeResponse.Result.Country.Select(c => { c = "England"; return(c); }).ToArray(); var response = new HttpResponseMessage { Content = new StringContent(JsonConvert.SerializeObject(postcodeResponse)), StatusCode = HttpStatusCode.Accepted }; var httpMessageHandler = MessageHandler.SetupMessageHandlerMock(response, new Uri(string.Format(Constants.DistrictNameUrl, query))); var client = new HttpClient(httpMessageHandler.Object); var postcodeService = new PostcodeApiService(client); //Act var actual = await postcodeService.GetDistrictData(query); //Assert actual.Should().BeEquivalentTo(postcodeResponse.Result, options => options .Excluding(c => c.Country) .Excluding(c => c.AdminDistrict) ); actual.Country.Should().Be(postcodeResponse.Result.Country.First()); actual.AdminDistrict.Should().Be(postcodeResponse.Result.AdminDistrict.First()); }
public void Then_If_It_Is_Not_Successful_An_Exception_Is_Thrown(string query) { //Arrange var response = new HttpResponseMessage { Content = new StringContent(""), StatusCode = HttpStatusCode.BadRequest }; var httpMessageHandler = MessageHandler.SetupMessageHandlerMock(response, new Uri(string.Format(Constants.PostcodeUrl, query))); var client = new HttpClient(httpMessageHandler.Object); var postcodeService = new PostcodeApiService(client); //Act Assert Assert.ThrowsAsync <HttpRequestException>(() => postcodeService.GetPostcodeData(query)); }
public async Task Then_If_NotFound_Result_Then_Service_Returns_Null( PostcodesLocationApiResponse postcodeResponse, string query) { var response = new HttpResponseMessage { Content = new StringContent(JsonConvert.SerializeObject(postcodeResponse)), StatusCode = HttpStatusCode.NotFound, }; var httpMessageHandler = MessageHandler.SetupMessageHandlerMock(response, new Uri(string.Format(Constants.PostcodesUrl, query, 10))); var client = new HttpClient(httpMessageHandler.Object); var postcodeService = new PostcodeApiService(client); //Act var actual = await postcodeService.GetAllStartingWithOutcode(query); //Assert actual.Should().BeNull(); }
public async Task Then_If_Postcode_Is_Not_English_Returns_Null( PostcodeLocationApiResponse postcodeResponse, string query) { postcodeResponse.Result.Country = "Scotland"; var response = new HttpResponseMessage { Content = new StringContent(JsonConvert.SerializeObject(postcodeResponse)), StatusCode = HttpStatusCode.Accepted, }; var httpMessageHandler = MessageHandler.SetupMessageHandlerMock(response, new Uri(string.Format(Constants.PostcodeUrl, query))); var client = new HttpClient(httpMessageHandler.Object); var postcodeService = new PostcodeApiService(client); //Act var actual = await postcodeService.GetPostcodeData(query); //Assert actual.Should().BeNull(); }
public async Task Then_The_Endpoint_Is_Called_And_Postcode_Data_Returned( PostcodeLocationApiResponse postcodeResponse, string query) { postcodeResponse.Result.Country = "England"; var response = new HttpResponseMessage { Content = new StringContent(JsonConvert.SerializeObject(postcodeResponse)), StatusCode = HttpStatusCode.Accepted, }; var httpMessageHandler = MessageHandler.SetupMessageHandlerMock(response, new Uri(string.Format(Constants.PostcodeUrl, query))); var client = new HttpClient(httpMessageHandler.Object); var postcodeService = new PostcodeApiService(client); //Act var actual = await postcodeService.GetPostcodeData(query); //Assert actual.Should().BeEquivalentTo(postcodeResponse.Result, options => options.ExcludingMissingMembers()); }
public void Then_If_It_Is_Not_Successful_An_Exception_Is_Thrown() { //Arrange var fixture = new Fixture().Customize(new AutoMoqCustomization { ConfigureMembers = true }); var postcodes = fixture.Create <PostcodeLookupRequest>(); var response = new HttpResponseMessage { Content = new StringContent(""), StatusCode = HttpStatusCode.BadRequest }; var uri = new Uri(ConfigurationConstants.PostcodeLookupUrl + "postcodes"); var httpMessageHandler = MessageHandler.SetupMessageHandlerMock(response, uri, HttpMethod.Post); var client = new HttpClient(httpMessageHandler.Object); var apiService = new PostcodeApiService(client); //Act Assert Assert.ThrowsAsync <HttpRequestException>(() => apiService.GetPostcodeData(postcodes)); }
public async Task Then_Only_Returns_English_Postcodes( PostcodesLocationApiResponse postcodeResponse, string query, int count) { postcodeResponse.Result[0].Country = "England"; //Arrange var response = new HttpResponseMessage { Content = new StringContent(JsonConvert.SerializeObject(postcodeResponse)), StatusCode = HttpStatusCode.Accepted }; var httpMessageHandler = MessageHandler.SetupMessageHandlerMock(response, new Uri(string.Format(Constants.PostcodesUrl, query, count))); var client = new HttpClient(httpMessageHandler.Object); var postcodeService = new PostcodeApiService(client); //Act var actual = await postcodeService.GetAllStartingWithOutcode(query, count); //Assert actual.Should().BeEquivalentTo(postcodeResponse.Result.Where(c => c.Country == "England"), options => options.Excluding(c => c.AdminDistrict)); actual.Count().Should().Be(1); }