我正在爲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"}}"
我可以看到正確的輸出,但頭不正確的是我對此感到困惑。