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

Add response status code for the Whoops error handler #476

Merged
merged 3 commits into from
Mar 28, 2017
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions test/Middleware/WhoopsErrorResponseGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,8 @@ public function testJsonContentTypeResponseWithJsonResponseHandler()
$this->request->getParsedBody()->willReturn([]);

$this->response->withHeader('Content-Type', 'application/json')->will([$this->response, 'reveal']);
$this->response->withStatus(StatusCode::STATUS_INTERNAL_SERVER_ERROR)->will([$this->response, 'reveal']);
$this->response->getStatusCode()->willReturn(StatusCode::STATUS_INTERNAL_SERVER_ERROR);
$this->response->withStatus(StatusCode::STATUS_IM_A_TEAPOT)->will([$this->response, 'reveal']);
$this->response->getStatusCode()->willReturn(StatusCode::STATUS_IM_A_TEAPOT);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure I understand this change. $error here is a simple RuntimeException, so I'd expect an internal server error. Or are you redefining the $error somehow?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, in general. I think we need an additional test for when $e has an exception code that falls elsewhere in the 400-599 range, to validate that the response status is set accordingly.

I might have misunderstood that... or you wanted to see changes to the default RunTimeException error code.

$this->response->getBody()->will([$this->stream, 'reveal']);

$this->stream->write('error')->shouldBeCalled();
Expand All @@ -155,4 +155,25 @@ public function testJsonContentTypeResponseWithJsonResponseHandler()
$generator($error, $this->request->reveal(), $this->response->reveal())
);
}

public function testNonErrorStatusCodeIsSetTo500()
{
$error = new RuntimeException();

$this->whoops->getHandlers()->willReturn([]);
$this->whoops->handleException($error)->willReturn('WHOOPS');

$this->response->withStatus(StatusCode::STATUS_INTERNAL_SERVER_ERROR)->will([$this->response, 'reveal']);
$this->response->getBody()->will([$this->stream, 'reveal']);
$this->response->getStatusCode()->willReturn(StatusCode::STATUS_MOVED_PERMANENTLY);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wait... shouldn't this be StatusCode::STATUS_INTERNAL_SERVER_ERROR here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, it's deliberately an error code below 400 to make sure Utils::getStatusCode does its job and returns the default 500 code. However I don't know if it should be tested here and just rely on Stratigility tests.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Uhm, it doesn't make sense since the default error code is 0. So it's already converted and tested in the previous tests.


$this->stream->write('WHOOPS')->shouldBeCalled();

$generator = new WhoopsErrorResponseGenerator($this->whoops->reveal());

$this->assertSame(
$this->response->reveal(),
$generator($error, $this->request->reveal(), $this->response->reveal())
);
}
}