1717use Psr \Http \Message \ServerRequestInterface ;
1818use Psr \Http \Message \UriInterface ;
1919use Psr \Http \Server \RequestHandlerInterface ;
20+ use Psr \Log \LoggerInterface ;
2021use ReflectionClass ;
2122use Throwable ;
2223
@@ -34,6 +35,7 @@ class CspHeaderMiddlewareTest extends TestCase
3435 private readonly UriInterface &MockObject $ uriMock ;
3536 private readonly PolicyFactory &MockObject $ policyFactoryMock ;
3637 private readonly Policy &MockObject $ policyMock ;
38+ private readonly LoggerInterface &MockObject $ loggerMock ;
3739
3840 /**
3941 * @throws Throwable
@@ -51,6 +53,7 @@ protected function setUp(): void
5153 $ this ->uriMock = $ this ->createMock (UriInterface::class);
5254 $ this ->policyFactoryMock = $ this ->createMock (PolicyFactory::class);
5355 $ this ->policyMock = $ this ->createMock (Policy::class);
56+ $ this ->loggerMock = $ this ->createMock (LoggerInterface::class);
5457
5558 $ this ->middlewareReflection = new ReflectionClass ($ this ->middleware );
5659
@@ -75,6 +78,12 @@ protected function setUp(): void
7578 ['backend ' => ['matchUris ' => ['^/neos ' ]], 'custom-backend ' => ['matchUris ' => []]]
7679 );
7780
81+ $ reflectionProperty = $ this ->middlewareReflection ->getProperty ('throwInvalidDirectiveException ' );
82+ $ reflectionProperty ->setValue ($ this ->middleware , true );
83+
84+ $ reflectionProperty = $ this ->middlewareReflection ->getProperty ('logger ' );
85+ $ reflectionProperty ->setValue ($ this ->middleware , $ this ->loggerMock );
86+
7887 $ this ->requestHandlerMock ->expects ($ this ->once ())->method ('handle ' )->willReturn ($ this ->responseMock );
7988 }
8089
@@ -166,4 +175,42 @@ public function testProcessShouldNotMatchNeosWhenBackendMatchUrisOverridden(): v
166175
167176 $ this ->middleware ->process ($ this ->requestMock , $ this ->requestHandlerMock );
168177 }
178+
179+ public function testProcessThrowsOnInvalidMatchUriPattern (): void
180+ {
181+ $ reflectionProperty = $ this ->middlewareReflection ->getProperty ('policies ' );
182+ $ reflectionProperty ->setValue (
183+ $ this ->middleware ,
184+ ['backend ' => ['matchUris ' => ['^/neos( ' ]], 'custom-backend ' => ['matchUris ' => []]]
185+ );
186+
187+ $ this ->requestMock ->expects ($ this ->once ())->method ('getUri ' )->willReturn ($ this ->uriMock );
188+ $ this ->uriMock ->expects ($ this ->once ())->method ('getPath ' )->willReturn ('/neos ' );
189+
190+ $ this ->expectException (\InvalidArgumentException::class);
191+
192+ $ this ->middleware ->process ($ this ->requestMock , $ this ->requestHandlerMock );
193+ }
194+
195+ public function testProcessLogsInvalidMatchUriPatternInProduction (): void
196+ {
197+ $ reflectionProperty = $ this ->middlewareReflection ->getProperty ('throwInvalidDirectiveException ' );
198+ $ reflectionProperty ->setValue ($ this ->middleware , false );
199+
200+ $ reflectionProperty = $ this ->middlewareReflection ->getProperty ('policies ' );
201+ $ reflectionProperty ->setValue (
202+ $ this ->middleware ,
203+ ['backend ' => ['matchUris ' => ['^/neos( ' ]], 'custom-backend ' => ['matchUris ' => []]]
204+ );
205+
206+ $ this ->requestMock ->expects ($ this ->once ())->method ('getUri ' )->willReturn ($ this ->uriMock );
207+ $ this ->uriMock ->expects ($ this ->once ())->method ('getPath ' )->willReturn ('/neos ' );
208+
209+ $ this ->loggerMock ->expects ($ this ->once ())->method ('critical ' );
210+ $ this ->policyFactoryMock ->expects ($ this ->once ())->method ('create ' )->willReturn ($ this ->policyMock );
211+ $ this ->policyMock ->expects ($ this ->once ())->method ('hasNonceDirectiveValue ' )->willReturn (false );
212+ $ this ->responseMock ->expects ($ this ->once ())->method ('withAddedHeader ' )->willReturnSelf ();
213+
214+ $ this ->middleware ->process ($ this ->requestMock , $ this ->requestHandlerMock );
215+ }
169216}
0 commit comments