Testing AJAX/XMLHttpRequest pages functionally in Symfony2

前端 未结 4 1230
迷失自我
迷失自我 2021-02-04 04:54

Is it possible to simulate/make an XMLHttpRequest request (ajax) in symfony2 tests?

4条回答
  •  失恋的感觉
    2021-02-04 05:23

    The Request#isXmlHttpRequest() method simply checks if the X-Requested-With header is equivalent to XMLHttpRequest. If that's the method you're using to determine if a request is an ajax call, then you can simulate the behavior in the test client by adding the appropriate header to the request:

    class FooFunctionalTest extends WebTestCase
    {
        $client = static::CreateClient();
        $crawler = $client->request('GET', '/foo/', array(), array(), array(
            'X-Requested-With' => 'XMLHttpRequest',
        ));
        // ...
    }
    

    More information can be found about the Request object in the source code.

提交回复
热议问题