Pārlūkot izejas kodu

[Security] Fix http basic authentication entry point

Antoine Hérault 14 gadi atpakaļ
vecāks
revīzija
920a209bbc

+ 1 - 1
src/Symfony/Component/Security/Http/EntryPoint/BasicAuthenticationEntryPoint.php

@@ -34,7 +34,7 @@ class BasicAuthenticationEntryPoint implements AuthenticationEntryPointInterface
     {
         $response = new Response();
         $response->headers->set('WWW-Authenticate', sprintf('Basic realm="%s"', $this->realmName));
-        $response->setStatusCode(401, $authException->getMessage());
+        $response->setStatusCode(401, $authException ? $authException->getMessage() : null);
 
         return $response;
     }

+ 36 - 0
tests/Symfony/Tests/Component/Security/Http/EntryPoint/BasicAuthenticationEntryPointTest.php

@@ -0,0 +1,36 @@
+<?php
+
+namespace Symfony\Tests\Component\Security\Http\EntryPoint;
+
+use Symfony\Component\Security\Http\EntryPoint\BasicAuthenticationEntryPoint;
+use Symfony\Component\Security\Core\Exception\AuthenticationException;
+
+class BasicAuthenticationEntryPointTest extends \PHPUnit_Framework_TestCase
+{
+    public function testStart()
+    {
+        $request = $this->getMock('Symfony\Component\HttpFoundation\Request');
+
+        $authException = new AuthenticationException('The exception message');
+
+        $entryPoint = new BasicAuthenticationEntryPoint('TheRealmName');
+        $response = $entryPoint->start($request, $authException);
+
+        $this->assertEquals('Basic realm="TheRealmName"', $response->headers->get('WWW-Authenticate'));
+        $this->assertEquals(401, $response->getStatusCode());
+        $this->assertAttributeEquals('The exception message', 'statusText', $response);
+    }
+
+    public function testStartWithoutAuthException()
+    {
+        $request = $this->getMock('Symfony\Component\HttpFoundation\Request');
+
+        $entryPoint = new BasicAuthenticationEntryPoint('TheRealmName');
+
+        $response = $entryPoint->start($request);
+
+        $this->assertEquals('Basic realm="TheRealmName"', $response->headers->get('WWW-Authenticate'));
+        $this->assertEquals(401, $response->getStatusCode());
+        $this->assertAttributeEquals('Unauthorized', 'statusText', $response);
+    }
+}