Domanda

I have a simple REST API in a bundle and I want to have some tests that can consistently test the results from this API.

The first test I implement simply calls a GET on a URL:

namespace Me\Bundle\ApiBundle\Tests\Controller;

use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

class SomeControllerTest extends WebTestCase {

    public function testGetSomething()
    {
        $client = static::createClient();

        $crawler = $client->request('GET', 'http://local.api.example.com/app_dev.php/v2.0/something');

            // Purely here for testing purposes!
        print_r( $client->getResponse()->getContent() );
    }

}

However, when I call my URL like this I get an empty content body and I test the HTTP status code in the response I get a 403:

$client->getResponse()->getStatusCode();

But when I place the exact same URL in a browser or a cURL command I get the correct output.

What is the obvious thing I'm doing wrong?

PS: The 403 comes from a catch all route I have set up in the API but in reality it shouldn't get there since the route exists.

È stato utile?

Soluzione

Standard client doesnot actually make http request to server, its just make a call to symfony kernel. So, possible solution is to change your url

$crawler = $client->request('GET', '/v2.0/something');
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top