diff options
| author | Murilo Ijanc <[email protected]> | 2026-07-26 16:41:48 -0300 |
|---|---|---|
| committer | Murilo Ijanc <[email protected]> | 2026-07-26 16:47:14 -0300 |
| commit | 535dbda6b48171f8115b455e54790de94d97d42a (patch) | |
| tree | 5c0410011e61a883bdf063ee612875a637fb1d22 /test/fake-api.js | |
| download | woocommerce-29190494c82855f0f0ce7fb16f831c8f971022c5.tar.gz | |
Initial importv0.1.0
Diffstat (limited to 'test/fake-api.js')
| -rw-r--r-- | test/fake-api.js | 104 |
1 files changed, 104 insertions, 0 deletions
diff --git a/test/fake-api.js b/test/fake-api.js new file mode 100644 index 0000000..4b6ac39 --- /dev/null +++ b/test/fake-api.js @@ -0,0 +1,104 @@ +/** + * A fake cep.is, so the test suite never touches the network. + * + * Same routes, bodies and status codes as the real API, plus a request log the + * tests assert on. It listens on every interface because two different clients + * have to reach it: the browser on the host, and PHP inside the WordPress + * container through the `fakecep` host alias. + * + * @module + */ + +const ADDRESSES = { + "01001000": { + cep: "01001000", + logradouro: "Praça da Sé", + complemento: "lado ímpar", + unidade: "", + bairro: "Sé", + localidade: "São Paulo", + uf: "SP", + estado: "São Paulo", + regiao: "Sudeste", + ibge: "3550308", + gia: "1004", + ddd: "11", + siafi: "7107", + geo: { lat: -23.567202, lon: -46.649659, raio_m: 1084, precisao: "logradouro" }, + }, + 20040020: { + cep: "20040020", + logradouro: "Avenida Rio Branco", + complemento: "até 0999 - lado ímpar", + unidade: "", + bairro: "Centro", + localidade: "Rio de Janeiro", + uf: "RJ", + estado: "Rio de Janeiro", + regiao: "Sudeste", + ibge: "3304557", + gia: "", + ddd: "21", + siafi: "6001", + }, +}; + +const HEADERS = { + "Access-Control-Allow-Origin": "*", + "Content-Type": "application/json; charset=utf-8", +}; + +/** + * Start the fake API. + * + * `state.requests` collects `{cep, userAgent}` for every lookup, which is how + * the proxy tests prove the request came from PHP rather than the browser. + * + * @param {number} port Port to listen on. + * @returns {{port: number, state: {requests: Array<{cep: string, userAgent: string}>}, reset: () => void, stop: () => void}} + */ +export function startFakeApi(port) { + const state = { requests: [] }; + + const server = Bun.serve({ + port, + hostname: "0.0.0.0", + fetch(request) { + const cep = new URL(request.url).pathname.slice(1); + state.requests.push({ cep, userAgent: request.headers.get("user-agent") || "" }); + + if (!/^\d{8}$/.test(cep)) { + return new Response( + JSON.stringify({ erro: "cep_invalido", mensagem: "CEP deve ter 8 dígitos" }), + { status: 400, headers: HEADERS }, + ); + } + + 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 }, + ); + } + + return new Response(JSON.stringify(address), { status: 200, headers: HEADERS }); + }, + }); + + return { + port: server.port, + state, + reset() { + state.requests.length = 0; + }, + stop() { + server.stop(true); + }, + }; +} + +if (import.meta.main) { + const api = startFakeApi(Number(process.argv[2] || 8899)); + console.log(`fake cep.is on :${api.port}`); +} |