Files
FOSSBilling/tests/Unit/FOSSBilling/RouteMatcherTest.php
T
Adam Daley d7f0ed20a5 Refactor HTTP helpers and centralize response handling (#3877)
* Refactor API request/response HTTP helpers

* Centralize exception and response handling

* Use ResponseEmitter in installer entrypoints

* Add HTTP ResponseFactory and refactor Box_App

* Extract route matching into HTTP matcher

* Fix route param parsing and remove UrlHelper

* Fix exception status code crash and clean up refactor leftovers

ApiResponseFactory::getStatusCode() required an int code, but
Throwable::getCode() can return a string (e.g. PDOException's SQLSTATE),
so any non-FOSSBilling exception with a non-integer code crashed API
error handling with a TypeError instead of returning a JSON error.
Numeric-string codes are normalized to int to preserve prior loose-
comparison behavior; non-numeric codes fall back to the default status.

Also removes now-dead ErrorPage::generatePage(), redundant manual
requires of ResponseEmitter (already covered by composer's classmap
autoload), and duplicate emitResponse() calls in exceptionHandler().
2026-07-02 22:20:10 +01:00

57 lines
2.1 KiB
PHP

<?php
declare(strict_types=1);
use FOSSBilling\Http\RouteMatcher;
test('route matcher matches exact routes for the same HTTP method', function (): void {
$match = (new RouteMatcher())->match('get', '/invoice', [], '/invoice', 'GET');
expect($match->matched)->toBeTrue()
->and($match->params)->toBe([]);
});
test('route matcher rejects different HTTP methods', function (): void {
$match = (new RouteMatcher())->match('post', '/invoice', [], '/invoice', 'GET');
expect($match->matched)->toBeFalse();
});
test('route matcher treats HEAD requests as GET requests', function (): void {
$match = (new RouteMatcher())->match('get', '/invoice/:hash', ['hash' => '[a-z0-9]+'], '/invoice/abc123', 'HEAD');
expect($match->matched)->toBeTrue()
->and($match->params)->toBe(['hash' => 'abc123']);
});
test('route matcher extracts default route parameters', function (): void {
$match = (new RouteMatcher())->match('get', '/kb/:slug', [], '/kb/getting-started_1', 'GET');
expect($match->matched)->toBeTrue()
->and($match->params)->toBe(['slug' => 'getting-started_1']);
});
test('route matcher applies custom route parameter conditions', function (): void {
$matcher = new RouteMatcher();
$matched = $matcher->match('get', '/invoice/:hash', ['hash' => '[a-z0-9]+'], '/invoice/abc123', 'GET');
$rejected = $matcher->match('get', '/invoice/:hash', ['hash' => '[a-z0-9]+'], '/invoice/ABC123', 'GET');
expect($matched->matched)->toBeTrue()
->and($matched->params)->toBe(['hash' => 'abc123'])
->and($rejected->matched)->toBeFalse();
});
test('route matcher accepts null conditions for routes without constrained parameters', function (): void {
$match = (new RouteMatcher())->match('get', '/servicehosting', null, '/servicehosting', 'GET');
expect($match->matched)->toBeTrue();
});
test('route matcher extracts full placeholder names', function (): void {
$match = (new RouteMatcher())->match('get', '/item/:item_id', ['item_id' => '[0-9]+'], '/item/42', 'GET');
expect($match->matched)->toBeTrue()
->and($match->params)->toBe(['item_id' => '42']);
});