Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Star accept header (*/*) matches the first route #30

Merged
merged 3 commits into from
Jan 17, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 40 additions & 1 deletion test/phpunit/BaseRouterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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!");
Expand All @@ -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,
Expand Down