2016-11-23 13 views
0

デフォルトでLaravel disablesVerifyCsrfToken実行中のテストのミドルウェア。その結果、私はAPIルートが成功するためにはcsrfトークンが必要であることに気付かなかった。いくつかのテストでそれを有効にする方法はありますか?いくつかのテストでVerifyCsrfTokenを有効にするにはどうすればよいですか?

function withVerifyCsrfTokenMiddleware() 
{ 
    $this->app... // here we're passing something to the app 
} 

そしてVerifyCsrfTokenhandle方法で変更を行うように。カスタムで。これは、フレームワークのメソッドをオーバーライドします。しかしそれは私の考えです。もっと良いものは?

+0

? – tomirons

+0

@tomirons Laravel 5.2。 –

答えて

0

さて、ここで私がやったことだ:

app/Http/Middleware/VerifyCsrfToken.php

function handle($request, Closure $next) 
{ 
    $dontSkipCsrfVerificationWhenRunningTests 
     = $this->app->bound('dontSkipCsrfVerificationWhenRunningTests') 
      && $this->app->make('dontSkipCsrfVerificationWhenRunningTests'); 
    if (
     $this->isReading($request) || 
     ! $dontSkipCsrfVerificationWhenRunningTests && $this->runningUnitTests() || 
     $this->shouldPassThrough($request) || 
     $this->tokensMatch($request) 
    ) { 
     return $this->addCookieToResponse($request, $next($request)); 
    } 

    throw new TokenMismatchException; 
} 

tests/TestCase.php:あなたが使用しているlaravelのどのバージョン

function withVerifyCsrfTokenMiddleware() 
{ 
    $this->app->instance('dontSkipCsrfVerificationWhenRunningTests', TRUE); 
} 
関連する問題