Open ColmMcBarron opened 6 years ago
That is the expected correct behaviour. You are defining the endpoint, not including the GET parameters.
Your API call can append whatever GET parameters it wants and then you can verify you received them as follows:
<?php
use donatj\MockWebServer\MockWebServer;
use donatj\MockWebServer\RequestInfo;
use donatj\MockWebServer\Response;
require 'vendor/autoload.php';
$server = new MockWebServer();
$server->start();
$url = $server->setResponseOfPath("/api/endpoint", new Response('abc'));
$content = file_get_contents($url . '?with=parameter');
print_r($server->getLastRequest()[RequestInfo::JSON_KEY_GET]);
which returns
Array
(
[with] => parameter
)
ok, will have to use something else -- we have APIs with parameters and in our integration tests, multiple calls to the same API can be made.
Do you need to test multiple API calls at a time? If you're testing a single API call at a time, you can easily change the response of an endpoint.
Otherwise you can queue up responses, see:
https://github.com/donatj/mock-webserver#multiple-responses-to-the-same-endpoint
We have APIs we test internally that vary on GET parameters, as well but we are only testing a single call at a time, so we can easily do as follows:
<?php
use donatj\MockWebServer\MockWebServer;
use donatj\MockWebServer\Response;
require 'vendor/autoload.php';
$server = new MockWebServer();
$server->start();
// Psuedo API object
$api = new Api();
$url = $server->setResponseOfPath("/api/endpoint", new Response('first response'));
$api->makeCallOne();
// do validaitons
$url = $server->setResponseOfPath("/api/endpoint", new Response('more different response'));
$api->makeCallTwo();
// do validations
We use PHPUnit to run are tests and simply set the body for the endpoint at the top of each test case.
I am currently laying the groundwork to support varying by query string, and hope to have a solution in the next couple days. Hold tight.
Hi @donatj - I recently started using mock-webserver
and was wondering if there was an update to varying responses by query string? Right now I'm in a situation where I can get around it with a ResponseStack
, but soon will be at a point where using a query string would be useful.
This works:
While this does not:
Instead of returning def, it returns the information about the request.