1
votes

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!

2
Laravel 7 supports CORS out of the box, hence, you don't need your own middleware for that laravel.com/docs/7.x/releases#laravel-7Tony
@Tony I have tried the same but no success. I have updated that code in my questionNarayan

2 Answers

0
votes

I've encountered this problem just now, and I solved it by installing php-sqlite, depends on your php version mine was:

sudo apt-get install php7.2-sqlite3

hope it helps

0
votes

I ended up here because of another cors problem :D but the answer to your question is that you cannot add header unless it is nothing printed to stdout, not even a new line. So after the first output, PHP will never accept any new header because it will be already processed and it is not possible to modify them, that is why you are receiving that error.

in order to add these headers you may add them to the response object:

public function handle($request, Closure $next)
    {
        $response = $next($request);

        $response->headers->set('Access-Control-Allow-Origin', 'http://web.local');
        $response->headers->set('Access-Control-Allow-Headers', 'Content-Type, X-Auth-Token, Authorization, Origin');
        $response->headers->set('Access-Control-Allow-Methods', 'POST, PUT');

       return $response;
    }

I hope that helps you or anyone comes here from google :D