For Laravel API I have write PHPUnit test but whenever I will run this I received a 500 error as below
1) Tests\Feature\CompanyTest::orgTest
Expected status code 200 but received 500.
Failed asserting that 200 is identical to 500.
Actually the issue is I have created 2 separate subdomains in my local for running the web (web.local) and API (api.local) environment
. Whenever calling the API from web domain it returns a Access-Control-Allow-Origin
hence I have added a cors middleware in laravel. Because of this cors middleware whenever I run the PHP unit test received below error in laravel log,
local.ERROR: Cannot modify header information - headers already sent by (output started at /vendor/phpunit/phpunit/src/Util/Printer.php:119) {"exception":"[object] (ErrorException(code: 0): Cannot modify header information - headers already sent by (output started at /vendor/phpunit/phpunit/src/Util/Printer.php:119) at /app/Http/Middleware/Cors.php:18)
My Cors Middleware code as below
Middlaware/cors.php
public function handle($request, Closure $next)
{
header('Access-Control-Allow-Origin: http://web.local');
header('Access-Control-Allow-Headers: Content-Type, X-Auth-Token, Authorization, Origin');
header('Access-Control-Allow-Methods: POST, PUT');
return $next($request);
}
Kernel.php
$middleware array
\App\Http\Middleware\Cors::class
$routeMiddleware array
'cors' => \App\Http\Middleware\Cors::class,
I have also tried with Laravel 7 CORS support By adding below code in Config\cors.php file but no success.
Config\cors.php code
return [
'paths' => ['api/*'],
'allowed_methods' => ['POST, PUT'],
'allowed_origins' => ['http://web.local'],
'allowed_origins_patterns' => [],
'allowed_headers' => ['Content-Type, X-Auth-Token, Authorization, Origin'],
'exposed_headers' => [],
'max_age' => 0,
'supports_credentials' => false,
];
I have googled the error and tried many solutions but unable to succeed. Anyone, please let me know whats the issue. Thanks!