summaryrefslogtreecommitdiffstats
path: root/test/fake-api.js
diff options
context:
space:
mode:
Diffstat (limited to 'test/fake-api.js')
-rw-r--r--test/fake-api.js44
1 files changed, 40 insertions, 4 deletions
diff --git a/test/fake-api.js b/test/fake-api.js
index 4b6ac39..d787bf1 100644
--- a/test/fake-api.js
+++ b/test/fake-api.js
@@ -43,6 +43,39 @@ const ADDRESSES = {
},
};
+/**
+ * The municipality a CEP that does not exist would have belonged to, keyed by
+ * the CEP that provokes it.
+ *
+ * `25845000` is the real-world shape of the whole problem: Areal/RJ answers
+ * from 25845-001 upwards and never at the `-000` a shopper reaches for. Any
+ * other missing CEP — `99999999` — belongs to no municipality at all, and the
+ * API then omits `faixa` entirely rather than sending it as null, which is what
+ * the plugin's fallback path has to keep working against.
+ */
+const RANGES = {
+ 25845000: {
+ localidade: "Areal",
+ uf: "RJ",
+ estado: "Rio de Janeiro",
+ regiao: "Sudeste",
+ ibge: "3300225",
+ cep_ini: "25845001",
+ cep_fim: "25849899",
+ ceps: 118,
+ },
+
+ // A faixa with no municipality in it. The real API does not send this, and
+ // the point of having it here is that the checkout must not care: an
+ // unusable faixa has to end in exactly the message the plugin has always
+ // shown, not in a broken sentence and not in a JavaScript error.
+ 25849000: {
+ uf: "RJ",
+ cep_ini: "25845001",
+ cep_fim: "25849899",
+ },
+};
+
const HEADERS = {
"Access-Control-Allow-Origin": "*",
"Content-Type": "application/json; charset=utf-8",
@@ -76,10 +109,13 @@ export function startFakeApi(port) {
const address = ADDRESSES[cep];
if (!address) {
- return new Response(
- JSON.stringify({ erro: "cep_nao_encontrado", mensagem: "CEP não existe na base do DNE" }),
- { status: 404, headers: HEADERS },
- );
+ const missing = { erro: "cep_nao_encontrado", mensagem: "CEP não existe na base do DNE" };
+
+ if (RANGES[cep]) {
+ missing.faixa = RANGES[cep];
+ }
+
+ return new Response(JSON.stringify(missing), { status: 404, headers: HEADERS });
}
return new Response(JSON.stringify(address), { status: 200, headers: HEADERS });