diff options
| author | Murilo Ijanc <[email protected]> | 2026-07-26 14:58:55 -0300 |
|---|---|---|
| committer | Murilo Ijanc <[email protected]> | 2026-07-26 14:58:55 -0300 |
| commit | 64764fb9fac0227f8e0443e7c7519d73aec275b9 (patch) | |
| tree | cb4fc56eeb6ecce53854f9c530609cf01ab3349f /tests/server | |
| download | php-0.1.0.tar.gz | |
Initial importv0.1.0
Diffstat (limited to 'tests/server')
| -rw-r--r-- | tests/server/router.php | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/tests/server/router.php b/tests/server/router.php new file mode 100644 index 0000000..e082027 --- /dev/null +++ b/tests/server/router.php @@ -0,0 +1,53 @@ +<?php + +declare(strict_types=1); + +$state = getenv('CEPIS_TEST_STATE'); +if ($state === false) { + http_response_code(500); + echo 'CEPIS_TEST_STATE is not set'; + + return true; +} + +$uri = $_SERVER['REQUEST_URI'] ?? '/'; +file_put_contents($state . '/calls.log', $uri . "\n", FILE_APPEND | LOCK_EX); + +$routes = json_decode((string) file_get_contents($state . '/routes.json'), true); +$routes = is_array($routes) ? $routes : []; + +$specs = $routes[$uri] ?? $routes['*'] ?? null; +if (!is_array($specs) || $specs === []) { + header('Content-Type: application/json; charset=utf-8'); + http_response_code(404); + echo json_encode(['erro' => 'cep_nao_encontrado', 'mensagem' => 'no route registered']); + + return true; +} + +$calls = array_filter(explode("\n", (string) file_get_contents($state . '/calls.log')), 'strlen'); +$seen = count(array_filter($calls, static fn (string $line): bool => $line === $uri)); +$spec = $specs[min(max($seen - 1, 0), count($specs) - 1)]; + +if (($spec['kind'] ?? 'static') === 'csv_echo') { + parse_str((string) parse_url($uri, PHP_URL_QUERY), $query); + $ceps = array_filter(explode(',', (string) ($query['ceps'] ?? '')), 'strlen'); + $rows = ['cep,logradouro,complemento,unidade,bairro,localidade,uf,estado,regiao,ibge,gia,ddd,siafi']; + foreach ($ceps as $cep) { + $rows[] = $cep . ',Rua Teste,,,Centro,Cidade,SP,São Paulo,Sudeste,1,,11,1'; + } + header('Content-Type: text/csv; charset=utf-8'); + http_response_code(200); + echo implode("\n", $rows) . "\n"; + + return true; +} + +foreach (($spec['headers'] ?? []) as $name => $value) { + header($name . ': ' . $value); +} +header('Content-Type: ' . ($spec['content_type'] ?? 'text/plain; charset=utf-8')); +http_response_code((int) ($spec['status'] ?? 200)); +echo (string) ($spec['body'] ?? ''); + +return true; |