Skip to content

Commit

Permalink
Fix broken error handler example (see tuupola#218) (tuupola#219)
Browse files Browse the repository at this point in the history
* Add some headers to error handler tests
* Fix broken example
  • Loading branch information
tuupola committed Mar 27, 2022
1 parent d9ed8bc commit 9777345
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 10 deletions.
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -221,9 +221,12 @@ $app->add(new Tuupola\Middleware\JwtAuthentication([
"error" => function ($response, $arguments) {
$data["status"] = "error";
$data["message"] = $arguments["message"];
return $response
->withHeader("Content-Type", "application/json")
->getBody()->write(json_encode($data, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT));

$response->getBody()->write(
json_encode($data, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT)
);

return $response->withHeader("Content-Type", "application/json")
}
]));
```
Expand Down
12 changes: 7 additions & 5 deletions tests/JwtAuthenticationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -751,8 +751,6 @@ public function testShouldCallAnonymousErrorFunction()
$request = (new ServerRequestFactory)
->createServerRequest("GET", "https://example.com/api");

$dummy = null;

$default = function (ServerRequestInterface $request) {
$response = (new ResponseFactory)->createResponse();
$response->getBody()->write("Success");
Expand All @@ -763,16 +761,18 @@ public function testShouldCallAnonymousErrorFunction()
new JwtAuthentication([
"secret" => "supersecretkeyyoushouldnotcommit",
"error" => function (ResponseInterface $response, $arguments) use (&$dummy) {
$dummy = true;
$response->getBody()->write("error");
return $response
->withHeader("X-Electrolytes", "Plants");
}
])
]);

$response = $collection->dispatch($request, $default);

$this->assertEquals(401, $response->getStatusCode());
$this->assertEquals("", $response->getBody());
$this->assertTrue($dummy);
$this->assertEquals("Plants", $response->getHeaderLine("X-Electrolytes"));
$this->assertEquals("error", $response->getBody());
}

public function testShouldCallInvokableErrorClass()
Expand All @@ -798,6 +798,7 @@ public function testShouldCallInvokableErrorClass()
$response = $collection->dispatch($request, $default);

$this->assertEquals(402, $response->getStatusCode());
$this->assertEquals("Bar", $response->getHeaderLine("X-Foo"));
$this->assertEquals(TestErrorHandler::class, $response->getBody());
}

Expand All @@ -824,6 +825,7 @@ public function testShouldCallArrayNotationError()
$response = $collection->dispatch($request, $default);

$this->assertEquals(418, $response->getStatusCode());
$this->assertEquals("Foo", $response->getHeaderLine("X-Bar"));
$this->assertEquals(TestErrorHandler::class, $response->getBody());
}

Expand Down
8 changes: 6 additions & 2 deletions tests/TestErrorHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,18 @@ public function __invoke(
array $arguments
) {
$response->getBody()->write(self::class);
return $response->withStatus(402);
return $response
->withStatus(402)
->withHeader("X-Foo", "Bar");
}

public static function error(
ResponseInterface $response,
array $arguments
) {
$response->getBody()->write(self::class);
return $response->withStatus(418);
return $response
->withStatus(418)
->withHeader("X-Bar", "Foo");
}
}

0 comments on commit 9777345

Please sign in to comment.