Skip to content
This repository has been archived by the owner on Jan 29, 2020. It is now read-only.

Commit

Permalink
Merge branch 'hotfix/53' into develop
Browse files Browse the repository at this point in the history
Forward port #54
  • Loading branch information
weierophinney committed Mar 24, 2016
2 parents 2bd2653 + 6376fde commit d4d957c
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 3 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ details.

- Nothing.

## 1.2.1 - TBD
## 1.2.1 - 2016-03-24

### Added

Expand All @@ -43,6 +43,12 @@ details.
- [#52](https://github.com/zendframework/zend-stratigility/pull/52) fixes the
behavior of the `FinalHandler` with regards to exception handling, ensuring
that the reason phrase reported corresponds to the HTTP status code used.
- [#54](https://github.com/zendframework/zend-stratigility/pull/54) modifies the
behavior of the `FinalHandler` when creating an error or 404 response to call
`write()` instead of `end()` on the response object. This fixes a lingering
issue with emitting the `Content-Length` header from the `SapiEmitter`, as
well as prevents the `SapiEmitter` from raising exceptions when doing so
(which was happening starting with 1.2.0).

## 1.2.0 - 2016-03-17

Expand Down
4 changes: 2 additions & 2 deletions src/FinalHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -251,10 +251,10 @@ private function getUriFromRequest(RequestInterface $request)
private function completeResponse(ResponseInterface $response, $message)
{
if ($response instanceof Http\Response) {
return $response->end($message);
return $response->write($message);
}

$response = new Http\Response($response);
return $response->end($message);
return $response->write($message);
}
}
109 changes: 109 additions & 0 deletions test/FinalHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

use Exception;
use PHPUnit_Framework_TestCase as TestCase;
use Psr\Http\Message\StreamInterface;
use Zend\Diactoros\ServerRequest as PsrRequest;
use Zend\Diactoros\Response as PsrResponse;
use Zend\Diactoros\Uri;
Expand Down Expand Up @@ -237,4 +238,112 @@ public function testCanReplaceOriginalResponseAndBodySizeAfterConstruction()
$this->assertSame($originalResponse, $actualResponse);
$this->assertSame(3, $actualResponse->getBody()->getSize());
}

/**
* @group 53
*/
public function testShouldNotMarkStratigilityResponseAsCompleteWhenHandlingErrors()
{
$error = new Exception('Exception message', 501);

$response = $this->prophesize('Zend\Stratigility\Http\Response');
$response->getStatusCode()->willReturn(200);
$response->withStatus(501, '')->will(function () use ($response) {
return $response->reveal();
});
$response->getReasonPhrase()->willReturn('Not Implemented');
$response->write('Not Implemented')->will(function () use ($response) {
return $response->reveal();
});

$final = new FinalHandler([], new Response(new PsrResponse()));
$this->assertSame($response->reveal(), $final(
$this->prophesize('Zend\Stratigility\Http\Request')->reveal(),
$response->reveal(),
$error
));
}

/**
* @group 53
*/
public function testShouldNotDecoratePsrResponseAsStratigilityCompletedResponseWhenHandlingErrors()
{
$error = new Exception('Exception message', 501);

$response = (new PsrResponse())
->withStatus(200);

$final = new FinalHandler([], new Response(new PsrResponse()));
$test = $final(
$this->prophesize('Zend\Stratigility\Http\Request')->reveal(),
$response,
$error
);

$this->assertInstanceOf('Zend\Stratigility\Http\Response', $test);
$this->assertSame(501, $test->getStatusCode());
$this->assertSame('Not Implemented', $test->getReasonPhrase());

$body = $test->getBody();
$body->rewind();
$this->assertContains('Not Implemented', $body->getContents());
}

/**
* @group 53
*/
public function testShouldNotMarkStratigilityResponseAsCompleteWhenCreating404s()
{
$body = $this->prophesize('Psr\Http\Message\StreamInterface');
$body->getSize()->willReturn(0)->shouldBeCalledTimes(2);

$response = $this->prophesize('Zend\Stratigility\Http\Response');
$response->getBody()->will(function () use ($body) {
return $body->reveal();
});
$response->withStatus(404)->will(function () use ($response) {
return $response->reveal();
});
$response
->write("Cannot GET /foo\n")
->will(function () use ($response) {
return $response->reveal();
})
->shouldBeCalled();

$request = $this->prophesize('Zend\Diactoros\ServerRequest');
$request->getUri()->willReturn('/foo');
$request->getMethod()->willReturn('GET');

$final = new FinalHandler([], $response->reveal());
$this->assertSame($response->reveal(), $final(
$request->reveal(),
$response->reveal()
));
}

/**
* @group 53
*/
public function testShouldNotDecoratePsrResponseAsStratigilityCompletedResponseWhenCreating404s()
{
$response = new PsrResponse();

$request = $this->prophesize('Zend\Diactoros\ServerRequest');
$request->getUri()->willReturn('/foo');
$request->getMethod()->willReturn('GET');

$final = new FinalHandler([], $response);
$test = $final(
$request->reveal(),
$response
);
$this->assertInstanceOf('Zend\Stratigility\Http\Response', $test);
$this->assertSame(404, $test->getStatusCode());

$body = $test->getBody();
$body->rewind();
$this->assertContains('Cannot GET /foo', $body->getContents());
}
}

0 comments on commit d4d957c

Please sign in to comment.