Exemplo n.º 1
0
 /**
  * Test the parseOrigin with values
  * Uses reflection as this is a protected method.
  *
  * @test
  * @covers \Bairwell\MiddlewareCors\Traits\Parse::parseOrigin
  */
 public function testParseOriginInvalidString()
 {
     $sut = new MiddlewareCors();
     // setup the logger
     $this->logger = new Logger('test');
     $this->testLogger = new TestHandler();
     $this->logger->pushHandler($this->testLogger);
     $sut->setLogger($this->logger);
     //
     $reflection = new \ReflectionClass(get_class($sut));
     $settingsProperty = $reflection->getProperty('settings');
     $settingsProperty->setAccessible(true);
     $method = $reflection->getMethod('parseOrigin');
     $method->setAccessible(true);
     $request = $this->getMockBuilder('Psr\\Http\\Message\\ServerRequestInterface')->disableOriginalConstructor()->getMock();
     $request->expects($this->once())->method('getHeaderLine')->with('origin')->willReturn(123);
     $result = $method->invokeArgs($sut, [$request]);
     $this->assertSame('', $result);
     // check the logger
     $this->assertTrue($this->testLogger->hasDebugThatContains('Origin is empty or is not a string'));
 }
Exemplo n.º 2
0
 /**
  * Get the CORs system integrated with Slim and FastRoute.
  *
  * @param ContainerInterface              $container The Slim Container.
  *
  * @return MiddlewareCors
  */
 protected function getCors(ContainerInterface $container) : MiddlewareCors
 {
     // set our allowed methods callback to integrate with Slim
     $corsAllowedMethods = function (ServerRequestInterface $request) use($container) : array {
         // if this closure is called, make sure it has the route available in the container.
         /* @var \Slim\Interfaces\RouterInterface $router */
         $router = $container->get('router');
         $routeInfo = $router->dispatch($request);
         $methods = [];
         // was the method called allowed?
         if ($routeInfo[0] === Dispatcher::METHOD_NOT_ALLOWED) {
             $methods = $routeInfo[1];
         } else {
             // if it was, see if we can get the routes and then the methods from it.
             // @var \Slim\Route $route
             $route = $request->getAttribute('route');
             // has the request get a route defined? is so use that
             if (null !== $route) {
                 $methods = $route->getMethods();
             }
         }
         // if we have methods, let's list them removing the OPTIONs one.
         if (false === empty($methods)) {
             // find the OPTIONs method
             $key = array_search('OPTIONS', $methods);
             // and remove it if set.
             if (false !== $key) {
                 unset($methods[$key]);
                 $methods = array_values($methods);
             }
         }
         return $methods;
     };
     // setup CORs
     $cors = new MiddlewareCors(['origin' => $this->allowedHosts, 'exposeHeaders' => '', 'maxAge' => 60, 'allowCredentials' => true, 'allowMethods' => $corsAllowedMethods, 'allowHeaders' => ['Accept-Language', 'Authorization', 'Content-type']]);
     // setup the logger
     $this->logger = new Logger('test');
     $this->testLogger = new TestHandler();
     $this->logger->pushHandler($this->testLogger);
     $cors->setLogger($this->logger);
     return $cors;
 }
 /**
  * Checks the settings will allow random stuff to be set.
  *
  * @test
  * @covers \Bairwell\MiddlewareCors::__construct
  * @covers \Bairwell\MiddlewareCors::getSettings
  */
 public function testCheckChangedSettingsViaSetterRandomSettings()
 {
     $sut = new MiddlewareCors();
     $sut->setSettings(['maxAge' => 123, 'allowCredentials' => true, 'random' => '123']);
     $expected = $this->defaults;
     $expected['maxAge'] = 123;
     $expected['allowCredentials'] = true;
     $expected['random'] = '123';
     $settings = $sut->getSettings();
     $this->arraysAreSimilar($expected, $settings);
 }