dimanche 29 mars 2015

Validate route guzzle request

I'm doing some unit testing with guzzle and I have a question.


I have created an API which consume an external Web Service, so is a Web Service where I do not have control, I have been thinking how to test properly something like that as they could change for example the structure of the JSON of the response.


To put you in context in my API if you hit /Times/Departure/10/ABC this will return a JSON that comes from the external Web Service that I'm using(no control on it)


So I can mock the response like this:



$mockResponse = new Response(200);
$mockResponseBody = EntityBody::factory(fopen('test.json', 'r'));


Headers



$mockResponse->setHeaders(array(
"Host" => "host.com",
"User-Agent" => "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36",
"Accept" => "application/json",
"Content-Type" => "application/json"
));


$mockPlugin = new MockPlugin();
$mockPlugin->addResponse($mockResponse);
$httpClient = new HttpClient();
$httpClient->addSubscriber($mockPlugin);


Request



$request = $httpClient->get('/Times/Departure/10/ABC');
$response = $request->send();


$response contains the JSON located in test.json


This will work, but will work even if I pass "hello/world" as a URL of the request, which is wrong because hello/world is not a route in my routing.yml



$request = $httpClient->get('hello/world');
$response = $request->send();


Should I take care about the route in other test as the purpose of this one is only validate the response?


Do you think this is a good approach to test something where I don't have control?


Thank you!


Aucun commentaire:

Enregistrer un commentaire