Add HeadPrep
This commit is contained in:
parent
e1058a4132
commit
559044a82f
|
|
@ -0,0 +1,24 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace WellRESTed\Routing\ResponsePrep;
|
||||||
|
|
||||||
|
use Psr\Http\Message\ResponseInterface;
|
||||||
|
use Psr\Http\Message\ServerRequestInterface;
|
||||||
|
use WellRESTed\Message\NullStream;
|
||||||
|
use WellRESTed\Routing\MiddlewareInterface;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Removes the body of a response to a HEAD request.
|
||||||
|
*/
|
||||||
|
class HeadPrep implements MiddlewareInterface
|
||||||
|
{
|
||||||
|
public function dispatch(ServerRequestInterface $request, ResponseInterface &$response)
|
||||||
|
{
|
||||||
|
$method = strtoupper($request->getMethod());
|
||||||
|
if ($method === "HEAD") {
|
||||||
|
if ($response->getBody()->getSize() !== 0) {
|
||||||
|
$response = $response->withBody(new NullStream());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,51 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace WellRESTed\Test\Unit\Routing;
|
||||||
|
|
||||||
|
use Prophecy\Argument;
|
||||||
|
use WellRESTed\Routing\ResponsePrep\HeadPrep;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers WellRESTed\Routing\ResponsePrep\HeadPrep
|
||||||
|
* @uses WellRESTed\Message\NullStream
|
||||||
|
*/
|
||||||
|
class HeadPrepTest extends \PHPUnit_Framework_TestCase
|
||||||
|
{
|
||||||
|
private $request;
|
||||||
|
private $response;
|
||||||
|
private $body;
|
||||||
|
|
||||||
|
public function setUp()
|
||||||
|
{
|
||||||
|
parent::setUp();
|
||||||
|
$this->body = $this->prophesize('Psr\Http\Message\StreamInterface');
|
||||||
|
$this->body->getSize()->willReturn(1024);
|
||||||
|
$this->request = $this->prophesize('Psr\Http\Message\ServerRequestInterface');
|
||||||
|
$this->response = $this->prophesize('Psr\Http\Message\ResponseInterface');
|
||||||
|
$this->response->getBody()->willReturn($this->body->reveal());
|
||||||
|
$this->response->withBody(Argument::any())->will(function ($args) {
|
||||||
|
$this->getBody()->willReturn($args[0]);
|
||||||
|
return $this;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testReplacesBodyForHeadRequest()
|
||||||
|
{
|
||||||
|
$this->request->getMethod()->willReturn("HEAD");
|
||||||
|
$request = $this->request->reveal();
|
||||||
|
$response = $this->response->reveal();
|
||||||
|
$prep = new HeadPrep();
|
||||||
|
$prep->dispatch($request, $response);
|
||||||
|
$this->assertSame(0, $response->getBody()->getSize());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testDoesNotReplaceBodyForNonHeadRequests()
|
||||||
|
{
|
||||||
|
$this->request->getMethod()->willReturn("GET");
|
||||||
|
$request = $this->request->reveal();
|
||||||
|
$response = $this->response->reveal();
|
||||||
|
$prep = new HeadPrep();
|
||||||
|
$prep->dispatch($request, $response);
|
||||||
|
$this->response->withBody(Argument::any())->shouldNotHaveBeenCalled();
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue