Skip to content

Commit 10e65b6

Browse files
committed
Add a non regression test
1 parent 2e02825 commit 10e65b6

File tree

4 files changed

+63
-3
lines changed

4 files changed

+63
-3
lines changed

features/main/content_negotiation.feature

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ Feature: Content Negotiation support
2222
<response><description/><dummy/><dummyBoolean/><dummyDate/><dummyFloat/><dummyPrice/><relatedDummy/><relatedDummies/><jsonData/><arrayData/><name_converted/><relatedOwnedDummy/><relatedOwningDummy/><id>1</id><name>XML!</name><alias/><foo/></response>
2323
"""
2424

25-
Scenario: Retrieve a collection in XML
25+
Scenario: Retrieve a collection in XML
2626
When I add "Accept" header equal to "text/xml"
2727
And I send a "GET" request to "/dummies"
2828
Then the response status code should be 200
@@ -34,7 +34,7 @@ Feature: Content Negotiation support
3434
<response><item key="0"><description/><dummy/><dummyBoolean/><dummyDate/><dummyFloat/><dummyPrice/><relatedDummy/><relatedDummies/><jsonData/><arrayData/><name_converted/><relatedOwnedDummy/><relatedOwningDummy/><id>1</id><name>XML!</name><alias/><foo/></item></response>
3535
"""
3636

37-
Scenario: Retrieve a collection in XML using the .xml URL
37+
Scenario: Retrieve a collection in XML using the .xml URL
3838
When I send a "GET" request to "/dummies.xml"
3939
Then the response status code should be 200
4040
And the header "Content-Type" should be equal to "application/xml; charset=utf-8"
@@ -45,7 +45,7 @@ Feature: Content Negotiation support
4545
<response><item key="0"><description/><dummy/><dummyBoolean/><dummyDate/><dummyFloat/><dummyPrice/><relatedDummy/><relatedDummies/><jsonData/><arrayData/><name_converted/><relatedOwnedDummy/><relatedOwningDummy/><id>1</id><name>XML!</name><alias/><foo/></item></response>
4646
"""
4747

48-
Scenario: Retrieve a collection in JSON
48+
Scenario: Retrieve a collection in JSON
4949
When I add "Accept" header equal to "application/json"
5050
And I send a "GET" request to "/dummies"
5151
Then the response status code should be 200
@@ -155,3 +155,17 @@ Feature: Content Negotiation support
155155
id,name
156156
1,Kevin
157157
"""
158+
159+
Scenario: Get a security response in JSON
160+
Given there are 1 SecuredDummy objects
161+
And I add "Accept" header equal to "application/json"
162+
When I send a "GET" request to "/secured_dummies"
163+
Then the response status code should be 401
164+
And the header "Content-Type" should be equal to "application/json"
165+
And the response should be in JSON
166+
And the JSON should be equal to:
167+
"""
168+
{
169+
"message": "Authentication Required"
170+
}
171+
"""
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace ApiPlatform\Core\Tests\Fixtures\TestBundle\Security;
6+
7+
use Symfony\Component\HttpFoundation\JsonResponse;
8+
use Symfony\Component\HttpFoundation\RedirectResponse;
9+
use Symfony\Component\HttpFoundation\Request;
10+
use Symfony\Component\HttpFoundation\Response;
11+
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
12+
use Symfony\Component\Routing\RouterInterface;
13+
use Symfony\Component\Security\Core\Exception\AuthenticationException;
14+
use Symfony\Component\Security\Http\EntryPoint\AuthenticationEntryPointInterface;
15+
16+
final class AuthenticationEntryPoint implements AuthenticationEntryPointInterface
17+
{
18+
private $router;
19+
20+
public function __construct(RouterInterface $router)
21+
{
22+
$this->router = $router;
23+
}
24+
25+
public function start(Request $request, AuthenticationException $authException = null): Response
26+
{
27+
if ('html' === $request->getRequestFormat()) {
28+
return new RedirectResponse($this->router->generate('api_doc', [], UrlGeneratorInterface::ABSOLUTE_URL));
29+
}
30+
if ('json' === $request->getRequestFormat()) {
31+
return new JsonResponse(
32+
['message' => 'Authentication Required'],
33+
Response::HTTP_UNAUTHORIZED,
34+
['WWW-Authenticate' => 'Bearer realm="example"']
35+
);
36+
}
37+
38+
return new Response('', Response::HTTP_UNAUTHORIZED);
39+
}
40+
}

tests/Fixtures/app/AppKernel.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use ApiPlatform\Core\Bridge\Symfony\Bundle\ApiPlatformBundle;
1515
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Document\User as UserDocument;
1616
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\User;
17+
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Security\AuthenticationEntryPoint;
1718
use ApiPlatform\Core\Tests\Fixtures\TestBundle\TestBundle;
1819
use Doctrine\Bundle\DoctrineBundle\DoctrineBundle;
1920
use Doctrine\Bundle\MongoDBBundle\DoctrineMongoDBBundle;
@@ -168,6 +169,7 @@ protected function configureContainer(ContainerBuilder $c, LoaderInterface $load
168169
'http_basic' => null,
169170
'anonymous' => null,
170171
'stateless' => true,
172+
'entry_point' => 'app.security.authentication_entrypoint',
171173
],
172174
],
173175
'access_control' => [

tests/Fixtures/app/config/config_common.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,3 +359,7 @@ services:
359359
tags:
360360
- { name: 'api_platform.data_transformer' }
361361

362+
app.security.authentication_entrypoint:
363+
class: 'ApiPlatform\Core\Tests\Fixtures\TestBundle\Security\AuthenticationEntryPoint'
364+
arguments:
365+
$router: '@router'

0 commit comments

Comments
 (0)