From 42e586658b4bc39542a5311842f0040f7d29cb2b Mon Sep 17 00:00:00 2001 From: Murilo Ijanc Date: Mon, 27 Jul 2026 20:31:13 -0300 Subject: Name the municipality behind a CEP that does not exist MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The API now hangs an optional faixa block off the 404 of GET /{cep}, and this exposes it as CepNotFoundException.Faixa. A CEP that fails is rarely random: it is usually someone who knows the city and guessed the number, most often the -000 that 957 municipalities never received. Knowing where the guess landed turns a dead-end "CEP not found" into a question worth asking the user. It stays an error. The block names the municipality the number would belong to, never an address, and the lookup still throws. CepIni and CepFim are the smallest and largest CEP that actually exist there, not the range the Correios publish — the published start for Areal is 25845000, exactly the CEP that fails — and the interval between them is sparse, which is what Ceps is for. Parsing is deliberately toothless: absent, misshapen or nameless, the block becomes null and the caller gets the same clean not-found it got before the field existed. A courtesy on top of a 404 must never be able to turn it into a parse failure. --- tests/Cepis.Tests/LookupTests.cs | 99 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 99 insertions(+) (limited to 'tests/Cepis.Tests/LookupTests.cs') diff --git a/tests/Cepis.Tests/LookupTests.cs b/tests/Cepis.Tests/LookupTests.cs index ad3e2bf..91f1c03 100644 --- a/tests/Cepis.Tests/LookupTests.cs +++ b/tests/Cepis.Tests/LookupTests.cs @@ -1,3 +1,5 @@ +using System.Globalization; + namespace Cepis.Tests; /// A single lookup: the happy path, the coordinate, and the refusals. @@ -119,6 +121,103 @@ public sealed class LookupTests : HttpTestBase Assert.Equal(1, Server.Count("/99999999")); } + [Fact] + public async Task AMissingCepNamesTheMunicipalityItWouldBeIn() + { + Server.Respond("/25845000", Reply.Of(404, Fixtures.NotFoundWithFaixa)); + + var failure = await Assert.ThrowsAsync( + () => Client.LookupAsync("25845000")); + + var faixa = failure.Faixa; + Assert.NotNull(faixa); + Assert.Equal("Areal", faixa.Localidade); + Assert.Equal("RJ", faixa.UF); + Assert.Equal("Rio de Janeiro", faixa.Estado); + Assert.Equal("Sudeste", faixa.Regiao); + Assert.Equal("3300225", faixa.Ibge); + Assert.Equal("25845001", faixa.CepIni); + Assert.Equal("25849899", faixa.CepFim); + Assert.Equal(118, faixa.Ceps); + } + + /// + /// The CEP asked for, 25845000, is the DNE's own start for Areal. The range + /// reports what exists, so it starts one above — and holds 118 CEPs across + /// the ~4,900 numbers it spans, which is why the count travels with it. + /// + [Fact] + public async Task TheRangeIsWhatExistsAndIsSparse() + { + Server.Respond("/25845000", Reply.Of(404, Fixtures.NotFoundWithFaixa)); + + var failure = await Assert.ThrowsAsync( + () => Client.LookupAsync("25845000")); + + var faixa = failure.Faixa; + Assert.NotNull(faixa); + Assert.NotEqual("25845000", faixa.CepIni); + Assert.True(faixa.Ceps < int.Parse(faixa.CepFim, CultureInfo.InvariantCulture) + - int.Parse(faixa.CepIni, CultureInfo.InvariantCulture)); + } + + [Fact] + public async Task TheMessageMentionsTheMunicipality() + { + Server.Respond("/25845000", Reply.Of(404, Fixtures.NotFoundWithFaixa)); + + var failure = await Assert.ThrowsAsync( + () => Client.LookupAsync("25845000")); + + Assert.Contains("25845000", failure.Message, StringComparison.Ordinal); + Assert.Contains("Areal/RJ", failure.Message, StringComparison.Ordinal); + } + + [Fact] + public async Task AMissingCepInNobodysRangeHasNoMunicipality() + { + Server.Respond("/99999999", Reply.Of(404, Fixtures.NotFound)); + + var failure = await Assert.ThrowsAsync( + () => Client.LookupAsync("99999999")); + + Assert.Null(failure.Faixa); + Assert.Equal("CEP 99999999 does not exist in the DNE database", failure.Message); + } + + [Fact] + public async Task ARangeOfTheWrongShapeIsDropped() + { + Server.Respond("/25845000", Reply.Of(404, Fixtures.NotFoundBrokenFaixa)); + + var failure = await Assert.ThrowsAsync( + () => Client.LookupAsync("25845000")); + + Assert.Null(failure.Faixa); + } + + [Fact] + public async Task ARangeNamingNoMunicipalityIsDropped() + { + Server.Respond("/25845000", Reply.Of(404, Fixtures.NotFoundNamelessFaixa)); + + var failure = await Assert.ThrowsAsync( + () => Client.LookupAsync("25845000")); + + Assert.Null(failure.Faixa); + } + + [Fact] + public async Task AnUnreadable404BodyIsStillJustANotFound() + { + Server.Respond("/25845000", Reply.Of(404, "gateway")); + + var failure = await Assert.ThrowsAsync( + () => Client.LookupAsync("25845000")); + + Assert.Null(failure.Faixa); + } + [Fact] public async Task A400CarriesTheApiMessage() { -- cgit v1.3