Skip to content

How to test 'Internal Server Error' from a LocalWebTestCase #34

@Starli0n

Description

@Starli0n

Hi,

I would like to test an 'Internal Server Error' from a LocalWebTestCase but I do not know how to do it.

First I added this error handler in SlimApp:

// Error handler
$container['errorHandler'] = function ($c) {
    return function ($request, $response, $exception) use ($c) {
        $c->logger->error('Internal Server Error');
        $data = array('message' => 'Internal Server Error');
        return $c['response']->withJson($data, 500);
    };
};

I added a specific route only for testing purpose:

// Route for testing server internal error (ie: $container['errorHandler'])
$app->get('/internalerror', function ($request, $response, $args) {
    $this->logger->info("Route '/' internalerror");
    throw new \Exception('Testing /internalerror.');
    return $response;
});

In a test case I do:

public function testInternalError()
{
    try {
        $this->client->get('/internalerror');
    } catch (\Exception $e) {
    }
    $response = $this->client->response;
    $this->assertEquals(500, $response->getStatusCode());
    $this->assertEquals('Internal Server Error', $response->getReasonPhrase());
    $this->assertEquals('application/json;charset=utf-8', $response->getHeader('Content-Type')[0]);
    $data = json_decode($response->getBody());
    $this->assertEquals('Internal Server Error', $data->message);
}

But $this->client->response return null.

My aim would be to test the message sent by the server defined in the error handler.

In a production environment, this route is working fine and I have the message 'Internal Server Error' in json.

http://localhost:8080/internalerror

{"message":"Internal Server Error"}

Thanks for your help!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions