Skip to content

Commit

Permalink
Add RequestFormatNegotiator
Browse files Browse the repository at this point in the history
  • Loading branch information
jsor committed Oct 17, 2014
1 parent f666747 commit b648bb0
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 2 deletions.
6 changes: 4 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
"suggest": {
"symfony/form": "For using FormErrorException",
"symfony/validator": "For using ValidationErrorException",
"willdurand/negotiation": "For using HalNegotiator"
"willdurand/negotiation": "For using HalNegotiator",
"willdurand/stack-negotiation": "For using RequestFormatNegotiator"
},
"require-dev": {
"symfony/config": "~2.0",
Expand All @@ -29,7 +30,8 @@
"symfony/form": "~2.1",
"symfony/validator": "~2.1",

"willdurand/negotiation": "~1.3"
"willdurand/negotiation": "~1.3",
"willdurand/stack-negotiation": "~0.1"
},
"autoload": {
"psr-4": {
Expand Down
26 changes: 26 additions & 0 deletions src/RequestFormatNegotiator.php
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);
}
}
53 changes: 53 additions & 0 deletions tests/RequestFormatNegotiatorTest.php
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),
);
}
}

0 comments on commit b648bb0

Please sign in to comment.