/// <summary> /// Determines whether the specified value matches the pattern of a valid CEP. /// </summary> /// <param name="value">The value to validate.</param> /// <returns>true if the specified value is valid or null; otherwise, false.</returns> public override bool IsValid(object value) { var valueAsString = value as string; if (string.IsNullOrWhiteSpace(valueAsString)) { return(true); } var isValid = Cep.Validate(valueAsString, this.punctuation); return(isValid); }
/// <summary> /// Get cep details information from cep /// </summary> public Task <ResponseMessage <CepDetails> > SendRequestAsync(Cep cep) { if (cep == null) { return(Task.FromResult(new ResponseMessage <CepDetails>(ErrorCodes.CepRequired, "Cep is required"))); } cep.Validate(); if (cep.IsInvalid()) { return(Task.FromResult(new ResponseMessage <CepDetails>(cep.GetValidationErrors().First()))); } return(SendRequestAndParseResultAsync(cep)); }
private Task ProcessCepRequest(HttpContext context) { Cep cep = new Cep(context.Request.RouteValues["cep"].ToString()); cep.Validate(); if (cep.IsInvalid()) { context.Response.StatusCode = (int)HttpStatusCode.BadRequest; context.Response.ContentType = "text/html"; return(context.Response.WriteAsync("<html><head></head><body>400 - Bad Request</body></html>")); } if (cep.Value == "00000000") { context.Response.StatusCode = (int)HttpStatusCode.OK; context.Response.ContentType = "application/json"; return(context.Response.WriteAsync(@"{""erro"":true}")); } StringBuilder builder = new StringBuilder() .Append("{") .Append($@"""cep"":""{cep.Value}"",") .Append($@"""logradouro"":""Praça da Sé"",") .Append($@"""complemento"":""lado ímpar"",") .Append($@"""bairro"":""Sé"",") .Append($@"""localidade"":""São Paulo"",") .Append($@"""uf"":""SP"",") .Append($@"""ibge"":""3550308"",") .Append($@"""gia"":""1004"",") .Append($@"""ddd"":""11"",") .Append($@"""siafi"":""7107""") .Append("}"); context.Response.StatusCode = (int)HttpStatusCode.OK; context.Response.ContentType = "application/json"; return(context.Response.WriteAsync(builder.ToString())); }
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); }
public void ValidateReturnsFalseIfCepIsLooseAndInvalid() { var actual = Cep.Validate("123-5678"); Assert.False(actual); }
public void ValidateReturnsFalseIfCepIsLooseAndGreaterThanEightCaracters() { var actual = Cep.Validate("1234567890"); Assert.False(actual); }
public void ValidateReturnsFalseIfCepIsLooseAndInvalidAndIncomplete() { var actual = Cep.Validate("12345"); Assert.False(actual); }
public void ValidateReturnsTrueIfCepIsLooseAndValid() { var actual = Cep.Validate("12345678"); Assert.True(actual); }
public void ValidateReturnsTrueIfCepIsEmpty() { var actual = Cep.Validate(string.Empty); Assert.False(actual); }
public void ValidateReturnsFalseIfCepIsValidButNotStrict() { var actual = Cep.Validate("12345678", CepPunctuation.Strict); Assert.False(actual); }
public void ValidateReturnsFalseIfCepContainsInvalidChars() { var actual = Cep.Validate("013s3-9b1"); Assert.False(actual); }
public void ValidateReturnsFalseIfCepIsStrictAndInvalid() { var actual = Cep.Validate("32341-3d3", CepPunctuation.Strict); Assert.False(actual); }
public void ValidateReturnsTrueIfCepIsStrictAndValid() { var actual = Cep.Validate("12345-678", CepPunctuation.Strict); Assert.True(actual); }
public void StrictValidateReturnsFalseIfCepIsNull() { var actual = Cep.Validate(null, CepPunctuation.Strict); Assert.False(actual); }
public void ValidateReturnsFalseIfCepIsNull() { var actual = Cep.Validate(null); Assert.False(actual); }