コード例 #1
0
        public async Task SendRequestCep_WhenCepIsNull_MustReturnCepRequiredError()
        {
            //Given
            IViaCepClient viaCepClient = CreateViaCepClientInstance();

            //When
            ResponseMessage <CepDetails> response = await viaCepClient.SendRequestAsync(null);

            //Then
            response.Should().NotBeNull();
            response.IsSuccessfulResponse.Should().BeFalse();
            response.Content.Should().BeNull();
            response.ErrorCode.Should().Be(ErrorCodes.CepRequired);
        }
コード例 #2
0
        public async Task SendRequestCep_WhenCepNotFound_MustReturnNotFoundError()
        {
            // Given
            IViaCepClient viaCepClient = CreateViaCepClientInstance();
            Cep           notFoundCep  = new Cep("00000000");

            // When
            ResponseMessage <CepDetails> response = await viaCepClient.SendRequestAsync(notFoundCep);

            // Then
            response.Should().NotBeNull();
            response.IsSuccessfulResponse.Should().BeFalse();
            response.Content.Should().BeNull();

            response.ErrorCode.Should().Be(ErrorCodes.ResourceNotFound);
        }
コード例 #3
0
ファイル: Program.cs プロジェクト: sidneyflima/viacepclient
        private static async Task CallViaCepClientFromDI(string cep)
        {
            using ServiceProvider serviceProvider = new ServiceCollection()
                                                    .AddViaCepClient(opt => opt.SetBaseAddress("http://viacep.com.br"))
                                                    .BuildServiceProvider();

            IViaCepClient viaCepClient = serviceProvider.GetRequiredService <IViaCepClient>();

            ResponseMessage <CepDetails> response = await viaCepClient.SendRequestAsync(new Cep(cep));

            CepDetails cepDetails = response.Content;

            string json = JsonSerializer.Serialize(cepDetails, new JsonSerializerOptions
            {
                WriteIndented = true,
                Encoder       = JavaScriptEncoder.UnsafeRelaxedJsonEscaping
            });

            Console.WriteLine(json);
        }
コード例 #4
0
        public async Task SendRequestCep_WhenCepFound_MustReturnCepDetails()
        {
            // Given
            IViaCepClient viaCepClient = CreateViaCepClientInstance();

            string cep            = "01001000";
            string address        = "Praça da Sé";
            string complement     = "lado ímpar";
            string neighbourhood  = "Sé";
            string city           = "São Paulo";
            string federativeUnit = "SP";
            string ibge           = "3550308";
            string gia            = "1004";
            string ddd            = "11";
            string siafi          = "7107";

            Cep validCep = new Cep(cep);

            // When
            ResponseMessage <CepDetails> response = await viaCepClient.SendRequestAsync(validCep);

            // Then
            response.Should().NotBeNull();
            response.IsSuccessfulResponse.Should().BeTrue();
            response.Content.Should().NotBeNull();

            response.ErrorCode.Should().BeNullOrEmpty();
            response.ErrorMessage.Should().BeNullOrEmpty();
            response.ErrorPropertyName.Should().BeNullOrEmpty();

            response.Content.Address.Should().Be(address);
            response.Content.Cep.Should().Be(cep);
            response.Content.City.Should().Be(city);
            response.Content.Complement.Should().Be(complement);
            response.Content.DDD.Should().Be(ddd);
            response.Content.FederativeUnit.Should().Be(federativeUnit);
            response.Content.GIA.Should().Be(gia);
            response.Content.IBGE.Should().Be(ibge);
            response.Content.Neighbourhood.Should().Be(neighbourhood);
            response.Content.Siafi.Should().Be(siafi);
        }
コード例 #5
0
        public async Task SendRequestCep_WhenCepIsInvalid_MustReturnValidationError(string cep)
        {
            //Given
            IViaCepClient viaCepClient = CreateViaCepClientInstance();

            Cep invalidCep = new Cep(cep);

            invalidCep.Validate();

            IError error = invalidCep.GetValidationErrors().First();

            //When
            ResponseMessage <CepDetails> response = await viaCepClient.SendRequestAsync(invalidCep);

            //Then
            response.Should().NotBeNull();
            response.IsSuccessfulResponse.Should().BeFalse();
            response.Content.Should().BeNull();

            response.ErrorCode.Should().Be(error.ErrorCode);
            response.ErrorMessage.Should().Be(error.ErrorMessage);
            response.ErrorPropertyName.Should().Be(error.PropertyName);
        }