/**
  * @test
  */
 public function someWhiteListedSituationsFallbackToNextMiddleware()
 {
     $request = ServerRequestFactory::fromGlobals();
     $response = new Response();
     $isCalled = false;
     $this->assertFalse($isCalled);
     $this->middleware->__invoke($request, $response, function ($req, $resp) use(&$isCalled) {
         $isCalled = true;
     });
     $this->assertTrue($isCalled);
     $request = ServerRequestFactory::fromGlobals()->withAttribute(RouteResult::class, RouteResult::fromRouteFailure(['GET']));
     $response = new Response();
     $isCalled = false;
     $this->assertFalse($isCalled);
     $this->middleware->__invoke($request, $response, function ($req, $resp) use(&$isCalled) {
         $isCalled = true;
     });
     $this->assertTrue($isCalled);
     $request = ServerRequestFactory::fromGlobals()->withAttribute(RouteResult::class, RouteResult::fromRouteMatch('rest-authenticate', 'foo', []));
     $response = new Response();
     $isCalled = false;
     $this->assertFalse($isCalled);
     $this->middleware->__invoke($request, $response, function ($req, $resp) use(&$isCalled) {
         $isCalled = true;
     });
     $this->assertTrue($isCalled);
     $request = ServerRequestFactory::fromGlobals()->withAttribute(RouteResult::class, RouteResult::fromRouteMatch('bar', 'foo', []))->withMethod('OPTIONS');
     $response = new Response();
     $isCalled = false;
     $this->assertFalse($isCalled);
     $this->middleware->__invoke($request, $response, function ($req, $resp) use(&$isCalled) {
         $isCalled = true;
     });
     $this->assertTrue($isCalled);
 }
 /**
  * {@inheritdoc}
  */
 public function match(Request $request)
 {
     $this->router->handle($request->getUri()->getPath());
     if (!$this->router->wasMatched()) {
         // TODO Is it worth to validate, if route matches but the method is incompatible?
         return RouteResult::fromRouteFailure();
     }
     $matchedRoute = $this->router->getMatchedRoute();
     return RouteResult::fromRouteMatch($matchedRoute->getName(), $this->router->getNamespaceName(), $this->collectParams($matchedRoute));
 }
示例#3
0
 /**
  * Attempt to match an incoming request to a registered route.
  *
  * @param PsrRequest $request
  * @return RouteResult
  */
 public function match(PsrRequest $request)
 {
     $match = $this->zf2Router->match(Psr7ServerRequest::toZend($request, true));
     if (null === $match) {
         return RouteResult::fromRouteFailure();
     }
     $allowedMethods = $this->getAllowedMethods($match->getMatchedRouteName());
     if (!$this->methodIsAllowed($request->getMethod(), $allowedMethods)) {
         return RouteResult::fromRouteFailure($allowedMethods);
     }
     return $this->marshalSuccessResultFromRouteMatch($match);
 }
示例#4
0
 /**
  * {@inheritdoc}
  */
 public function match(ServerRequestInterface $request)
 {
     $this->injectRoutes();
     $matchResult = $this->router->match($request);
     if ($matchResult instanceof SuccessfulMatch) {
         return RouteResult::fromRouteMatch($matchResult->getRouteName(), $matchResult->getParam('middleware'), $matchResult->getParams());
     }
     if ($matchResult instanceof MethodNotAllowed) {
         return RouteResult::fromRouteFailure($matchResult->getAllowedMethods());
     }
     return RouteResult::fromRouteFailure();
 }
示例#5
0
 /**
  * Match a request against the known routes.
  *
  * Implementations will aggregate required information from the provided
  * request instance, and pass them to the underlying router implementation;
  * when done, they will then marshal a `RouteResult` instance indicating
  * the results of the matching operation and return it to the caller.
  *
  * @param  Request $request
  * @return RouteResult
  */
 public function match(Request $request)
 {
     $this->_forbidRouteAdd = true;
     $params = $request->getServerParams();
     $path = $params['PATH_INFO'];
     // get the matched route
     $matchedRoute = $this->_matchPathToRoutePath($path);
     // check if route exists
     if ($matchedRoute instanceof Route) {
         return RouteResult::fromRouteMatch($matchedRoute->getName(), $matchedRoute->getMiddleware(), $params);
     }
     return RouteResult::fromRouteFailure();
 }
 /**
  * @param  Request $request
  * @return RouteResult
  */
 public function match(Request $request)
 {
     $matchedRoutes = $this->router->getMatchedRoutes($request->getMethod(), $request->getUri()->getPath());
     if (count($matchedRoutes) === 0) {
         return RouteResult::fromRouteFailure();
     }
     /** @var \Slim\Route $matchedRoute */
     $matchedRoute = array_shift($matchedRoutes);
     $params = $matchedRoute->getParams();
     // Get the middleware from the route params and remove it
     $middleware = $params['middleware'];
     unset($params['middleware']);
     return RouteResult::fromRouteMatch($matchedRoute->getName(), $middleware, $params);
 }
 public function testGeneralRoutingFailureCallsNextWithSameRequestAndResponse()
 {
     $request = new ServerRequest();
     $response = new Response();
     $result = RouteResult::fromRouteFailure();
     $this->router->match($request)->willReturn($result);
     $called = false;
     $next = function ($req, $res) use(&$called, $request, $response) {
         $this->assertSame($request, $req);
         $this->assertSame($response, $res);
         $called = true;
     };
     $app = $this->getApplication();
     $app->routeMiddleware($request, $response, $next);
     $this->assertTrue($called);
 }
 public function testComposedEmitterIsCalledByRun()
 {
     $routeResult = RouteResult::fromRouteFailure();
     $this->router->match()->willReturn($routeResult);
     $finalResponse = $this->prophesize('Psr\\Http\\Message\\ResponseInterface')->reveal();
     $finalHandler = function ($req, $res, $err = null) use($finalResponse) {
         return $finalResponse;
     };
     $emitter = $this->prophesize('Zend\\Diactoros\\Response\\EmitterInterface');
     $emitter->emit(Argument::type('Psr\\Http\\Message\\ResponseInterface'))->shouldBeCalled();
     $app = new Application($this->router->reveal(), null, $finalHandler, $emitter->reveal());
     $request = new Request([], [], 'http://example.com/');
     $response = $this->prophesize('Psr\\Http\\Message\\ResponseInterface');
     $app->run($request, $response->reveal());
 }
 public function testInvokeRedirectResponseDisallowNotRoutedUrlAndRouteMatchIsFailure()
 {
     $config = ['allow_not_routed_url' => false, 'default_url' => '/default'];
     $router = $this->prophesize(RouterInterface::class);
     $middleware = new RedirectHandlerAction($config, $router->reveal());
     $request = $this->prophesize(ServerRequest::class);
     $uri = $this->prophesize(Uri::class);
     $uri->getPath()->willReturn('/')->shouldBeCalled();
     $request->getUri()->willReturn($uri)->shouldBeCalled();
     $request->withUri(Argument::type(Uri::class))->willReturn($request);
     $routeResult = RouteResult::fromRouteFailure();
     $router->match($request)->willReturn($routeResult);
     $response = new RedirectResponse('/foo?query');
     $next = function ($req, $res, $err = null) use($response) {
         return $response;
     };
     $this->assertInstanceOf(RedirectResponse::class, $response);
     $response = $middleware->__invoke($request->reveal(), $response, $next);
     $this->assertInstanceOf(Response::class, $response);
 }
 /**
  * Marshals a route result based on the results of matching and the current HTTP method.
  *
  * @param array $result
  * @param string $method
  * @return RouteResult
  */
 private function marshalMatchedRoute(array $result, $method)
 {
     $path = $result[1];
     $route = array_reduce($this->routes, function ($matched, $route) use($path, $method) {
         if ($matched) {
             return $matched;
         }
         if ($path !== $route->getPath()) {
             return $matched;
         }
         if (!$route->allowsMethod($method)) {
             return $matched;
         }
         return $route;
     }, false);
     if (false === $route) {
         // This likely should never occur, but is present for completeness.
         return RouteResult::fromRouteFailure();
     }
     return RouteResult::fromRouteMatch($route->getName(), $route->getMiddleware(), $result[2]);
 }
 /**
  * Marshal a routing failure result.
  *
  * If the failure was due to the HTTP method, passes the allowed HTTP
  * methods to the factory.
  *
  * @return RouteResult
  */
 private function marshalFailedRoute(array $result)
 {
     if ($result[0] === Dispatcher::METHOD_NOT_ALLOWED) {
         return RouteResult::fromRouteFailure($result[1]);
     }
     return RouteResult::fromRouteFailure();
 }
示例#12
0
 /**
  * Marshal a RouteResult representing a route failure.
  *
  * If the route failure is due to the HTTP method, passes the allowed
  * methods when creating the result.
  *
  * @param Request $request
  * @return RouteResult
  */
 private function marshalFailedRoute(Request $request)
 {
     $failedRoute = $this->router->getFailedRoute();
     // Evidently, getFailedRoute() can sometimes return null; these are 404 conditions.
     if (null === $failedRoute) {
         return RouteResult::fromRouteFailure();
     }
     if ($failedRoute->failedMethod()) {
         return RouteResult::fromRouteFailure($failedRoute->method);
     }
     // Check to see if the route regex matched; if so, and we have an entry
     // for the path, register a 405.
     list($path) = explode('^', $failedRoute->name);
     if (isset($failedRoute->failed) && $failedRoute->failed !== AuraRoute::FAILED_REGEX && array_key_exists($path, $this->routes)) {
         return RouteResult::fromRouteFailure($this->routes[$path]);
     }
     return RouteResult::fromRouteFailure();
 }
示例#13
0
 /**
  * Create a successful RouteResult from the given RouteMatch.
  *
  * @param RouteMatch $match
  * @return RouteResult
  */
 private function marshalSuccessResultFromRouteMatch(RouteMatch $match)
 {
     $params = $match->getParams();
     if (array_key_exists(self::METHOD_NOT_ALLOWED_ROUTE, $params)) {
         return RouteResult::fromRouteFailure($this->allowedMethodsByPath[$params[self::METHOD_NOT_ALLOWED_ROUTE]]);
     }
     return RouteResult::fromRouteMatch($this->getMatchedRouteName($match->getMatchedRouteName()), $params['middleware'], $params);
 }
示例#14
0
 /**
  * Marshal a RouteResult representing a route failure.
  *
  * If the route failure is due to the HTTP method, passes the allowed
  * methods when creating the result.
  *
  * @return RouteResult
  */
 private function marshalFailedRoute()
 {
     $failedRoute = $this->router->getFailedRoute();
     if (!$failedRoute->failedMethod()) {
         return RouteResult::fromRouteFailure();
     }
     return RouteResult::fromRouteFailure($failedRoute->method);
 }