2017-04-02 101 views
0

我正在爲laravel 5.4編寫一個json路由的測試,如果GET請求不是ajax,代碼在瀏覽器上正常工作chrome,但是當我測試它不起作用時,我應該重定向如預期。我嘗試了不同的變體來將標題添加到沒有工作的請求中。Laravel 5.4 HTTP測試

當我將請求更改爲POST時,它的工作方式爲例外,我不確定我缺少什麼。這裏是代碼

routes.php

// Redirect all none-ajax routes to the main edit test route 
if (!Request::ajax()) { 
    Route::get('{vue?}', ['as'=>'vue_redirect', 'uses' => '[email protected]'])->where('vue', '[\/\w\.-]*'); 
} 
    /* 
    * General route to the test instructions 
    */ 
    Route::get('getInstructions', ['as' => 'getInstructions', 'uses' => '[email protected]']); 

這是測試代碼

public function testgetTest() 
{ 

    $x = $this->actingAs($this->customer)->json('GET', '/test/edit/' . $this->test->id . '/getInstructions'); 
    var_dump($x->getContent()); 


} 

我試着像

$this->actingAs($this->customer)->call('GET', '/test/edit/' . $this->test->id . '/getInstructions', [], [], ['X-Requested-With' => 'XMLHttpRequest']

放還沒有工作的變化。

當我更改爲POST和打印出來的頭,這是我所得到的

if (!Request::ajax()) { 
     var_dump(Request::header()); 
     Route::get('{vue?}', ['as'=>'vue_redirect', 'uses' => '[email protected]'])->where('vue', '[\/\w\.-]*'); 
    } 

    /* 
    * General route to the test instructions 
    */ 
    Route::post('getInstructions', ['as' => 'getInstructions', 'uses' => '[email protected]']); 

輸出爲POST工作

array(5) { 
    ["host"]=> 
    array(1) { 
    [0]=> 
    string(7) "app.dev" 
    } 
    ["user-agent"]=> 
    array(1) { 
    [0]=> 
    string(11) "Symfony/3.X" 
    } 
    ["accept"]=> 
    array(1) { 
    [0]=> 
    string(63) "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8" 
    } 
    ["accept-language"]=> 
    array(1) { 
    [0]=> 
    string(14) "en-us,en;q=0.5" 
    } 
    ["accept-charset"]=> 
    array(1) { 
    [0]=> 
    string(30) "ISO-8859-1,utf-8;q=0.7,*;q=0.7" 
    } 
} 
string(373) "{"success":true,"message":{"id":1,"type_id":"2","user_id":"3","test_name":"Test-MEAjT","test_url":"http:\/\/www.kuphal.com\/eveniet-distinctio-voluptas-sint","test_instructions":"Accusamus laboriosam alias magni ea. Et libero totam sunt ut.","status":"draft","completion_date":null,"is_onboarding":"0","created_at":"2017-04-02 19:50:00","updated_at":"2017-04-02 19:50:00"}}" 

我可以看到正確的輸出,但頭不正確的是我對此感到困惑。

回答

0

我設法根據@sisve@lagbox的指導解決了這個問題我對代碼進行了以下更新。

我刪除了檢查ajax到中間件的條件,然後我將路由vue_redirect的位置移到了匹配的底部,以免它與其他路由衝突。 (這是一個將所有流量重定向到錯誤路由的錯誤)。

Routes.php

Route::group(['middleware' => ['VerifyAjaxRequest']], function(){ 
      /* 
      * General route to the test instructions 
      */ 
      Route::get('getInstructions', ['as' => 'getInstructions', 'uses' => '[email protected]']); 

}); 

VerifyAjaxRequest.php中間件

public function handle($request, Closure $next) 
{ 

    // Redirect all none-ajax routes to the main edit test route 
    if (!$request->ajax()) { 
    return redirect(route('vue_redirect')); 
    } 

    return $next($request); 
} 

Test.php

public function testgetTest() 
{ 
    $response = $this->actingAs($this->customer)->json('get', '/test/edit/' . $this->test->id . '/getInstructions', [], ['X-Requested-With'=>'XMLHttpRequest']); 
    var_dump($response->getContent()); 
} 

現在,它的工作原理爲例外,包括頭和正確的響應