Skip to content

Commit 500268a

Browse files
committed
Add a non regression test
1 parent 5e8a571 commit 500268a

File tree

4 files changed

+71
-3
lines changed

4 files changed

+71
-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: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace ApiPlatform\Core\Tests\Fixtures\TestBundle\Security;
15+
16+
use Symfony\Component\HttpFoundation\JsonResponse;
17+
use Symfony\Component\HttpFoundation\RedirectResponse;
18+
use Symfony\Component\HttpFoundation\Request;
19+
use Symfony\Component\HttpFoundation\Response;
20+
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
21+
use Symfony\Component\Routing\RouterInterface;
22+
use Symfony\Component\Security\Core\Exception\AuthenticationException;
23+
use Symfony\Component\Security\Http\EntryPoint\AuthenticationEntryPointInterface;
24+
25+
final class AuthenticationEntryPoint implements AuthenticationEntryPointInterface
26+
{
27+
private $router;
28+
29+
public function __construct(RouterInterface $router)
30+
{
31+
$this->router = $router;
32+
}
33+
34+
public function start(Request $request, AuthenticationException $authException = null): Response
35+
{
36+
if ('html' === $request->getRequestFormat()) {
37+
return new RedirectResponse($this->router->generate('api_doc', [], UrlGeneratorInterface::ABSOLUTE_URL));
38+
}
39+
if ('json' === $request->getRequestFormat()) {
40+
return new JsonResponse(
41+
['message' => 'Authentication Required'],
42+
Response::HTTP_UNAUTHORIZED,
43+
['WWW-Authenticate' => 'Bearer realm="example"']
44+
);
45+
}
46+
47+
return new Response('', Response::HTTP_UNAUTHORIZED);
48+
}
49+
}

tests/Fixtures/app/AppKernel.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ protected function configureContainer(ContainerBuilder $c, LoaderInterface $load
168168
'http_basic' => null,
169169
'anonymous' => null,
170170
'stateless' => true,
171+
'entry_point' => 'app.security.authentication_entrypoint',
171172
],
172173
],
173174
'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)