Exemplo n.º 1
0
        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 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(
            PostcodeLocationApiResponse 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.PostcodeUrl, query)));
            var client             = new HttpClient(httpMessageHandler.Object);
            var postcodeService    = new PostcodeApiService(client);

            //Act
            var actual = await postcodeService.GetPostcodeData(query);

            //Assert
            actual.Should().Be(null);
        }
        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());
        }
Exemplo n.º 5
0
        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));
        }