示例#1
0
 public function testComplexExternalUrlMapping()
 {
     $routePatten = '/test/local/$testvar/$why/$also';
     $requestUri = '/test/local/bla/whynot/hi';
     $targetUri = 'https://www.google.com/search?q=$testvar&why=$why&also=$also';
     $route = new Route($routePatten, function () {
         // ...
     });
     $request = new Request();
     $request->setRequestUri($requestUri);
     $this->assertTrue($route->matches($request));
     $variableSet = VariableUrl::extractUrlVariables($requestUri, $routePatten);
     $this->assertEquals(['testvar' => 'bla', 'why' => 'whynot', 'also' => 'hi'], $variableSet);
     $mappedOutput = VariableUrl::applyUrlVariables($targetUri, $variableSet);
     $this->assertEquals('https://www.google.com/search?q=bla&why=whynot&also=hi', $mappedOutput);
 }
示例#2
0
 /**
  * Based on the current configuration, begins handling the incoming request.
  * This function should result in data being output.
  *
  * @return Response The response that was sent.
  */
 public function start()
 {
     $this->beforeStart();
     // Begin output buffering and begin building the HTTP response
     $this->isBuffering = true;
     ob_start();
     $this->response = new Response();
     $this->context->registerInstance($this->response);
     try {
         // Dispatch the request to the router
         if (!$this->filters->trigger(Filters::BEFORE_ROUTE, $this->context)) {
             return $this->response;
         }
         $routingResult = $this->router->route($this->request);
         if ($routingResult != null) {
             $this->context->registerInstance($routingResult);
             $this->response->setResponseCode(ResponseCode::HTTP_OK);
             $returnValue = $this->dispatch($routingResult);
             // If a Response object was returned by the target code, use that object from now on
             if ($returnValue instanceof Response) {
                 if ($this->isBuffering) {
                     ob_clean();
                 }
                 $this->response = $returnValue;
             }
         } else {
             $anyOptionsAvailable = $this->router->getOptions($this->request);
             if (!$anyOptionsAvailable) {
                 // No options at all for this route, this is a true 404
                 $this->prepareNotFoundResponse();
             } else {
                 if ($this->request->getMethod() == RequestMethod::OPTIONS) {
                     // Options available, no custom OPTIONS handler, generate an OPTIONS ("Allow") response.
                     $this->prepareOptionsResponse();
                 } else {
                     // There are options, but no match, this is a 405
                     $this->prepareNotAllowedResponse();
                 }
             }
         }
         $this->filters->trigger(Filters::AFTER_ROUTE, $this->context);
     } catch (\Exception $ex) {
         $this->prepareErrorResponse($ex);
     } finally {
         $this->sendResponse();
     }
     return $this->response;
 }
示例#3
0
 public function testBeforeFilterCanPreventContinue()
 {
     $route = new Route('/bla.html', function () {
         echo 'hello!';
     });
     $route->before(function () {
         // Oh no you don't!
         return false;
     });
     $request = new Request();
     $request->setRequestUri('/bla.html');
     $this->assertEquals(null, $route->action());
     $this->expectOutputString('', 'Filter function should break execution; no output is expected');
 }
示例#4
0
 /**
  * Matches a request against the pattern configured on this route.
  *
  * @param Request $request
  * @return bool
  */
 public function matchesPattern(Request $request)
 {
     if (preg_match($this->regexPattern, $request->getRequestUri()) <= 0) {
         return false;
     }
     return true;
 }
示例#5
0
 public function testCreateRedirect()
 {
     // Prepare: Prepare environment to capture response
     $response = new Response();
     $request = new Request();
     $request->setRequestUri('/redirect/bla');
     $context = new Context();
     $context->registerInstance($response);
     $context->registerInstance($request);
     $router = new Router();
     $router->setContext($context);
     $route = $router->createRedirect('/redirect/$testVar', '/target/$testVar', true);
     $this->assertEquals('/redirect/$testVar', $route->getPattern());
     $routeResult = $router->route($request);
     $this->assertEquals($route, $routeResult);
     $router->dispatch($routeResult, $request);
     $this->assertEquals(ResponseCode::HTTP_MOVED_PERMANENTLY, $response->getResponseCode());
     $this->assertEquals('/target/bla', $response->getHeader('Location'));
 }
示例#6
0
 public function testGetUrl()
 {
     $request = new Request();
     $request->setRequestUri('/web/page.html?action=eat&what=pie');
     $request->setEnvironmentData(['HTTPS' => 'On', 'SERVER_PORT' => 1337, 'HTTP_HOST' => 'web.com']);
     $expectedFullUrl = 'https://web.com:1337/web/page.html';
     $expectedFullUrlWithParams = $expectedFullUrl . '?action=eat&what=pie';
     $this->assertEquals($expectedFullUrl, $request->getUrl(false));
     $this->assertEquals($expectedFullUrlWithParams, $request->getUrl(true));
     $this->assertEquals($expectedFullUrlWithParams, $request->getUrl(), 'Params should be included by default');
     $request->setEnvironmentData(['HTTPS' => 'On', 'SERVER_PORT' => 443, 'HTTP_HOST' => 'web.com']);
     $expectedFullUrl = 'https://web.com/web/page.html';
     $this->assertEquals($expectedFullUrl, $request->getUrl(false), 'Regular port numbers should be hidden');
 }
示例#7
0
 /**
  * @runInSeparateProcess
  */
 public function testNotAllowedResponse()
 {
     $app = new Enlighten();
     $app->get('/sample', function () {
         echo 'hi!!!';
     });
     $optionsRequest = new Request();
     $optionsRequest->setRequestUri('/sample');
     $optionsRequest->setMethod(RequestMethod::POST);
     $app->setRequest($optionsRequest);
     $response = $app->start();
     $this->assertEquals(ResponseCode::HTTP_METHOD_NOT_ALLOWED, $response->getResponseCode());
 }
示例#8
0
 public function sampleFunction($nullMeBro, $bogusParam = 'abc', Request $request = null)
 {
     if ($request == null) {
         return null;
     }
     return $request->getRequestUri();
 }