public function testSchemeFailure() { $absoluteUri = $this->prophesize(UriInterface::class)->reveal(); $matchResult = MatchResult::fromSchemeFailure($absoluteUri); $this->assertFalse($matchResult->isSuccess()); $this->assertFalse($matchResult->isMethodFailure()); $this->assertTrue($matchResult->isSchemeFailure()); $this->assertSame($absoluteUri, $matchResult->getAbsoluteUri()); try { $matchResult->getRouteName(); $this->fail('An expected DomainException was not raised'); } catch (DomainException $e) { $this->assertSame('Route name is only available on successful match', $e->getMessage()); } try { $matchResult->getParams(); $this->fail('An expected DomainException was not raised'); } catch (DomainException $e) { $this->assertSame('Params are only available on successful match', $e->getMessage()); } try { $matchResult->getAllowedMethods(); $this->fail('An expected DomainException was not raised'); } catch (DomainException $e) { $this->assertSame('Allowed methods are only available on method failure', $e->getMessage()); } }
public function testSchemeNotAllowedResultTakesPrecedence() { $expectedMatchResult = MatchResult::fromSchemeFailure($this->prophesize(UriInterface::class)->reveal()); $matchResult = RouteCollectionMatcher::matchRouteCollection($this->buildRouteCollection([$expectedMatchResult, MatchResult::fromMethodFailure([])]), $this->prophesize(ServerRequestInterface::class)->reveal(), 0, []); $this->assertSame($expectedMatchResult, $matchResult); }
/** * {@inheritdoc} * * @throws Exception\UnexpectedValueException */ public function match(ServerRequestInterface $request, $pathOffset) { $uri = $request->getUri(); $params = $this->defaults; // Verify scheme first, if set. if (true === $this->secure && 'https' !== $uri->getScheme()) { return MatchResult::fromSchemeFailure($uri->withScheme('https')); } elseif (false === $this->secure && 'http' !== $uri->getScheme()) { return MatchResult::fromSchemeFailure($uri->withScheme('http')); } // Then match hostname, if parser is set. if (null !== $this->hostnameParser) { $hostnameResult = $this->hostnameParser->parse($uri->getHost(), 0); if (null === $hostnameResult || strlen($uri->getHost()) !== $hostnameResult->getMatchLength()) { return null; } $params = $hostnameResult->getParams() + $params; } // Then match port, if set. if (null !== $this->port) { $port = $uri->getPort() ?: ('http' === $uri->getScheme() ? 80 : 443); if ($port !== $this->port) { return null; } } // Next match the path. $completePathMatched = false; if (null !== $this->pathParser) { if (null === ($pathResult = $this->pathParser->parse($uri->getPath(), $pathOffset))) { return null; } $pathOffset += $pathResult->getMatchLength(); $completePathMatched = $pathOffset === strlen($uri->getPath()); $params = $pathResult->getParams() + $params; } if ($completePathMatched) { if (null === $this->methods || in_array($request->getMethod(), $this->methods)) { return MatchResult::fromSuccess($params); } if (empty($this->methods)) { // Special case: when no methods are defined at all, this route may simply not terminate. return null; } return MatchResult::fromMethodFailure($this->methods); } // The path was not completely matched yet, so we check the children. if (null === $this->children) { return null; } return RouteCollectionMatcher::matchRouteCollection($this->children, $request, $pathOffset, $params); }