diff options
Diffstat (limited to 'internal/app/fakes_test.go')
| -rw-r--r-- | internal/app/fakes_test.go | 20 |
1 files changed, 18 insertions, 2 deletions
diff --git a/internal/app/fakes_test.go b/internal/app/fakes_test.go index 011d4af..9281a53 100644 --- a/internal/app/fakes_test.go +++ b/internal/app/fakes_test.go @@ -182,12 +182,16 @@ func (f *fakeTelegram) pendingCount() int { // fakeCEPIS is a real HTTP server speaking the cep.is native contract, with // enough of it to exercise every branch the bot has: an address with a street -// coordinate, one that is a whole municipality, a 404, a 400 and a 500. +// coordinate, one that is a whole municipality, a 404 that names the +// municipality the CEP would fall in, a 404 that does not, a 400 and a 500. type fakeCEPIS struct { server *httptest.Server - mu sync.Mutex + mu sync.Mutex + // bodies are the 200s, missing are the 404s that carry a faixa, and + // statuses forces a status for a CEP regardless of the other two. bodies map[string]string + missing map[string]string statuses map[string]int requests int } @@ -208,6 +212,14 @@ func newFakeCEPIS(t *testing.T) *fakeCEPIS { `"unidade":"","bairro":"Centro","localidade":"Linhares","uf":"ES","estado":"Espírito Santo",` + `"regiao":"Sudeste","ibge":"3203205","gia":"","ddd":"27","siafi":"5665"}`, }, + // 25845000 is the CEP the "-000" guess produces for Areal/RJ, which + // is coded street by street and never received that number: the + // municipality exists, the CEP does not. + missing: map[string]string{ + "25845000": `{"erro":"cep_nao_encontrado","mensagem":"CEP não existe na base do DNE",` + + `"faixa":{"localidade":"Areal","uf":"RJ","estado":"Rio de Janeiro","regiao":"Sudeste",` + + `"ibge":"3300225","cep_ini":"25845001","cep_fim":"25849899","ceps":118}}`, + }, statuses: map[string]int{ "50000000": http.StatusInternalServerError, }, @@ -225,6 +237,7 @@ func (f *fakeCEPIS) handle(w http.ResponseWriter, r *http.Request) { cep := strings.TrimPrefix(r.URL.Path, "/") status, forced := f.statuses[cep] body, known := f.bodies[cep] + absent, attributed := f.missing[cep] f.mu.Unlock() w.Header().Set("Content-Type", "application/json; charset=utf-8") @@ -235,6 +248,9 @@ func (f *fakeCEPIS) handle(w http.ResponseWriter, r *http.Request) { case known: w.WriteHeader(http.StatusOK) w.Write([]byte(body)) + case attributed: + w.WriteHeader(http.StatusNotFound) + w.Write([]byte(absent)) case len(cep) != 8: w.WriteHeader(http.StatusBadRequest) w.Write([]byte(`{"erro":"cep_invalido","mensagem":"CEP deve ter 8 dígitos"}`)) |