From e00c1e0b42e781d3f0865cc488baee9c15b8c323 Mon Sep 17 00:00:00 2001 From: Greg Bowler Date: Tue, 17 Jan 2023 13:45:49 +0000 Subject: [PATCH] Star accept header (*/*) matches the first route (#30) * feature: deeper dynamic pages closes #4 * feature: test matching star accept header closes #19 --- test/phpunit/BaseRouterTest.php | 41 ++++++++++++++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/test/phpunit/BaseRouterTest.php b/test/phpunit/BaseRouterTest.php index 53c39f8..b33492c 100644 --- a/test/phpunit/BaseRouterTest.php +++ b/test/phpunit/BaseRouterTest.php @@ -4,7 +4,6 @@ use Exception; use Gt\Config\ConfigSection; use Gt\Http\Request; -use Gt\Http\ResponseStatusException\AbstractResponseStatusException; use Gt\Http\ResponseStatusException\ClientError\HttpNotAcceptable; use Gt\Http\ResponseStatusException\ClientError\HttpNotFound; use Gt\Http\ResponseStatusException\Redirection\HttpFound; @@ -234,6 +233,7 @@ public function thisShouldNotMatchBecauseItsNothing():void { throw new HttpNotFound(); } + /** @noinspection PhpUnused */ #[Any(path: "/something", accept: "application/json,application/xml")] public function thisShouldMatch():void { throw new Exception("Match!"); @@ -249,6 +249,45 @@ public function thisShouldNotMatchBecauseLessQuality():void { $sut->route($request); } + public function testRoute_matchesFirstRouteIfStarAccept():void { + $uri = self::createMock(Uri::class); + $uri->method("getPath")->willReturn("/something"); + $request = self::createMock(Request::class); + $request->method("getUri")->willReturn($uri); + $request->method("getMethod")->willReturn("GET"); + $request->method("getHeaderLine") + ->with("accept") + ->willReturn("*/*"); + + $sut = new class extends BaseRouter { + /** @noinspection PhpUnused */ + #[Any(path: "/something", accept: "text/html")] + public function thisShouldNotMatch():void { + throw new Exception("Route 1"); + } + + /** @noinspection PhpUnused */ + #[Any(path: "/something", accept: "fake/nothing")] + public function thisShouldNotMatchBecauseItsNothing():void { + throw new Exception("Route 2"); + } + + /** @noinspection PhpUnused */ + #[Any(path: "/something", accept: "application/json,application/xml")] + public function thisShouldMatch():void { + throw new Exception("Route 3"); + } + + /** @noinspection PhpUnused */ + #[Any(path: "/something", accept: "application/xhtml+xml,application/xml;q=0.8")] + public function thisShouldNotMatchBecauseLessQuality():void { + throw new Exception("Route 4"); + } + }; + self::expectExceptionMessage("Route 1"); + $sut->route($request); + } + /** @dataProvider data_redirectCode */ public function testHandleRedirects_responseCodeFromConfig( int $code,