/**
  * Test the Callback filter mode
  * @return void
  *
  * @expectedException \InvalidArgumentException
  */
 public function testMaintenanceModeCallbackException()
 {
     Configure::write('Wrench.enable', true);
     $request = ServerRequestFactory::fromGlobals(['HTTP_HOST' => 'localhost', 'REQUEST_URI' => '/']);
     $response = new Response();
     $next = function ($req, $res) {
         return $res;
     };
     $middleware = new MaintenanceMiddleware(['mode' => ['className' => 'Wrench\\Mode\\Callback', 'config' => ['callback' => 'wonkycallable']]]);
     $middleware($request, $response, $next);
 }
Beispiel #2
0
 /**
  * Test the Output filter mode with a wrong file path : it should throw an
  * exception
  * @return void
  *
  * @expectedException \LogicException
  */
 public function testOutputModeCustomParams()
 {
     Configure::write('Wrench.enable', true);
     $request = ServerRequestFactory::fromGlobals(['HTTP_HOST' => 'localhost', 'REQUEST_URI' => '/']);
     $response = new Response();
     $next = function ($req, $res) {
         return $res;
     };
     $middleware = new MaintenanceMiddleware(['mode' => ['className' => 'Wrench\\Mode\\Output', 'config' => ['path' => ROOT . DS . 'wonky.html']]]);
     $middleware($request, $response, $next);
 }
 /**
  * Test the Redirect filter mode with additional headers
  * @return void
  */
 public function testMaintenanceModeFilterRedirectHeaders()
 {
     Configure::write('Wrench.enable', true);
     $request = ServerRequestFactory::fromGlobals(['HTTP_HOST' => 'localhost', 'REQUEST_URI' => '/']);
     $response = new Response();
     $next = function ($req, $res) {
         return $res;
     };
     $middleware = new MaintenanceMiddleware(['mode' => ['className' => 'Wrench\\Mode\\Redirect', 'config' => ['code' => 503, 'url' => 'http://www.example.com/maintenance.html', 'headers' => ['someHeader' => 'someValue', 'additionalHeader' => 'additionalValue']]]]);
     $middlewareResponse = $middleware($request, $response, $next);
     $this->assertEquals(503, $middlewareResponse->getStatusCode());
     $this->assertEquals('http://www.example.com/maintenance.html', $middlewareResponse->getHeaderLine('location'));
     $this->assertEquals('someValue', $middlewareResponse->getHeaderLine('someHeader'));
     $this->assertEquals('additionalValue', $middlewareResponse->getHeaderLine('additionalHeader'));
 }
Beispiel #4
0
 /**
  * Run the request/response through the Application and its middleware.
  *
  * This will invoke the following methods:
  *
  * - App->bootstrap() - Perform any bootstrapping logic for your application here.
  * - App->middleware() - Attach any application middleware here.
  * - Trigger the 'Server.buildMiddleware' event. You can use this to modify the
  *   from event listeners.
  * - Run the middleware queue including the application.
  *
  * @param \Psr\Http\Message\ServerRequestInterface|null $request The request to use or null.
  * @param \Psr\Http\Message\ResponseInterface|null $response The response to use or null.
  * @return \Psr\Http\Message\ResponseInterface
  * @throws \RuntimeException When the application does not make a response.
  */
 public function run(ServerRequestInterface $request = null, ResponseInterface $response = null)
 {
     $this->app->bootstrap();
     $response = $response ?: new Response();
     try {
         $request = $request ?: ServerRequestFactory::fromGlobals();
     } catch (UnexpectedValueException $e) {
         $response->getBody()->write('Bad Request');
         return $response->withHeader('Content-Type', 'text/plain')->withStatus(400);
     }
     $middleware = $this->app->middleware(new MiddlewareQueue());
     if (!$middleware instanceof MiddlewareQueue) {
         throw new RuntimeException('The application `middleware` method did not return a middleware queue.');
     }
     $this->dispatchEvent('Server.buildMiddleware', ['middleware' => $middleware]);
     $middleware->add($this->app);
     $response = $this->runner->run($middleware, $request, $response);
     if (!$response instanceof ResponseInterface) {
         throw new RuntimeException(sprintf('Application did not create a response. Got "%s" instead.', is_object($response) ? get_class($response) : $response));
     }
     return $response;
 }
Beispiel #5
0
 /**
  * Test the View with custom params and plugins
  * @return void
  */
 public function testViewModeCustomParamsPlugin()
 {
     Configure::write('Wrench.enable', true);
     $request = ServerRequestFactory::fromGlobals(['HTTP_HOST' => 'localhost', 'REQUEST_URI' => '/']);
     $response = new Response();
     $next = function ($req, $res) {
         return $res;
     };
     $middleware = new MaintenanceMiddleware(['mode' => ['className' => 'Wrench\\Mode\\View', 'config' => ['code' => 404, 'view' => ['template' => 'maintenance', 'templatePath' => 'Maintenance', 'layout' => 'maintenance', 'plugin' => 'TestPlugin', 'layoutPath' => 'Maintenance']]]]);
     $middlewareResponse = $middleware($request, $response, $next);
     $expected = "Plugin Maintenance Header\nI'm in a plugin sub-directoryPlugin Maintenance Footer";
     $this->assertEquals($expected, (string) $middlewareResponse->getBody());
     $middleware = new MaintenanceMiddleware(['mode' => ['className' => 'Wrench\\Mode\\View', 'config' => ['code' => 404, 'view' => ['template' => 'maintenance', 'templatePath' => 'Maintenance', 'layout' => 'maintenance', 'theme' => 'TestPlugin', 'layoutPath' => 'Maintenance'], 'headers' => ['someHeader' => 'someValue', 'additionalHeader' => 'additionalValue']]]]);
     $middlewareResponse = $middleware($request, $response, $next);
     $expected = "Plugin Maintenance Header\nI'm in a plugin sub-directoryPlugin Maintenance Footer";
     $this->assertEquals($expected, (string) $middlewareResponse->getBody());
     $middleware = new MaintenanceMiddleware(['mode' => ['className' => 'Wrench\\Mode\\View', 'config' => ['code' => 404, 'view' => ['template' => 'TestPlugin.maintenance', 'templatePath' => 'Maintenance', 'layout' => 'TestPlugin.maintenance', 'layoutPath' => 'Maintenance'], 'headers' => ['someHeader' => 'someValue', 'additionalHeader' => 'additionalValue']]]]);
     $middlewareResponse = $middleware($request, $response, $next);
     $expected = "Plugin Maintenance Header\nI'm in a plugin sub-directoryPlugin Maintenance Footer";
     $this->assertEquals($expected, (string) $middlewareResponse->getBody());
     $this->assertEquals('someValue', $middlewareResponse->getHeaderLine('someHeader'));
     $this->assertEquals('additionalValue', $middlewareResponse->getHeaderLine('additionalHeader'));
 }
 /**
  * Create a PSR7 request from the request spec.
  *
  * @param array $spec The request spec.
  * @return Psr\Http\Message\RequestInterface
  */
 protected function _createRequest($spec)
 {
     if (isset($spec['input'])) {
         $spec['post'] = [];
     }
     $request = ServerRequestFactory::fromGlobals(array_merge($_SERVER, $spec['environment'], ['REQUEST_URI' => $spec['url']]), $spec['query'], $spec['post'], $spec['cookies']);
     $request = $request->withAttribute('session', $spec['session']);
     if (isset($spec['input'])) {
         $stream = new Stream('php://memory', 'rw');
         $stream->write($spec['input']);
         $stream->rewind();
         $request = $request->withBody($stream);
     }
     return $request;
 }