-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
83 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
namespace Jsor\Stack\Hal; | ||
|
||
use Jsor\Stack\Hal\Negotiation\HalNegotiator; | ||
use Negotiation\Decoder\DecoderProviderInterface; | ||
use Negotiation\FormatNegotiatorInterface; | ||
use Negotiation\NegotiatorInterface; | ||
use Negotiation\Stack\Negotiation; | ||
use Symfony\Component\HttpKernel\HttpKernelInterface; | ||
|
||
class RequestFormatNegotiator extends Negotiation | ||
{ | ||
public function __construct( | ||
HttpKernelInterface $app, | ||
FormatNegotiatorInterface $formatNegotiator = null, | ||
NegotiatorInterface $languageNegotiator = null, | ||
DecoderProviderInterface $decoderProvider = null | ||
) { | ||
if (!$formatNegotiator) { | ||
$formatNegotiator = new HalNegotiator(); | ||
} | ||
|
||
parent::__construct($app, $formatNegotiator, $languageNegotiator, $decoderProvider); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?php | ||
|
||
namespace Jsor\Stack\Hal; | ||
|
||
use Nocarrier\Hal; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; | ||
use Symfony\Component\HttpKernel\HttpKernelInterface; | ||
|
||
class RequestFormatNegotiatorTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
/** | ||
* @test | ||
* @dataProvider provideAcceptHeaders | ||
*/ | ||
public function it_accepts_hal_headers($acceptHeader, $format) | ||
{ | ||
$kernel = $this->getMock('Symfony\Component\HttpKernel\HttpKernelInterface'); | ||
|
||
$kernel | ||
->expects($this->once()) | ||
->method('handle'); | ||
|
||
$app = new RequestFormatNegotiator($kernel); | ||
|
||
$request = new Request(); | ||
$request->headers->set('Accept', $acceptHeader); | ||
|
||
$app->handle($request); | ||
|
||
$this->assertEquals($format, $request->getRequestFormat()); | ||
$this->assertEquals($format, $request->attributes->get('_format')); | ||
} | ||
|
||
public static function provideAcceptHeaders() | ||
{ | ||
return array( | ||
array('application/hal+json,application/json;q=0.9,*/*;q=0.8', 'json'), | ||
array('application/json;q=0.9,*/*;q=0.8', 'json'), | ||
array('application/x-json;q=0.9,*/*;q=0.8', 'json'), | ||
|
||
array('application/hal+xml,text/xml;q=0.9,*/*;q=0.8', 'xml'), | ||
array('text/xml;q=0.9,*/*;q=0.8', 'xml'), | ||
array('application/xml;q=0.9,*/*;q=0.8', 'xml'), | ||
array('application/x-xml;q=0.9,*/*;q=0.8', 'xml'), | ||
|
||
array('text/html, application/json;q=0.8, text/csv;q=0.7', null), | ||
array('text/html', null), | ||
array('text/*, text/html, text/html;level=1, */*', null), | ||
array('text/html; q=0.0', null), | ||
); | ||
} | ||
} |