jeudi 8 janvier 2015

PHPUnit and CORS Headers Middleware

I am building an API with Laravel 5 and I have a CORS middleware that looks like this:



public function handle($request, Closure $next)
{
header('Access-Control-Allow-Origin: ' . env('CORS_ORIGIN', '*') );
header('Access-Control-Allow-Methods: GET, POST, OPTIONS, PUT, DELETE, PATCH');
header('Access-Control-Allow-Headers: Origin, Content-Type, Accept, Authorization, X-Request-With');
header('Access-Control-Allow-Credentials: true');

return $next($request);
}


It is registered here...



///app/Http/Kernel.php
protected $middleware = [
// ...
'Atiiv\Http\Middleware\EnableCORS',
];


And it woks just fine. My problem is when testing with PHPunit a call to a uri, since the test is triggered from the command line this headers aren't sent (I think) making the test to fail.


Just in case my test look like this:



/** @test */
public function it_fetches_users()
{
json_decode($this->call('GET', '/v1/users')->getContent());
$this->assertResponseOk();
}


Really simple stuff just to get me started.


Is it possible to set the CORS headers for every test or what is the best way to tackle this problem?


Really had no idea on what to do.


Thanks in advance.


Aucun commentaire:

Enregistrer un commentaire