mock-webserver icon indicating copy to clipboard operation
mock-webserver copied to clipboard

setResponseOfPath not working with query parameters

Open ColmMcBarron opened this issue 7 years ago • 6 comments

This works:

$url = $this->server->setResponseOfPath("/api/endpoint", new Response('abc' ));
$content = file_get_contents($url);
print_r($content);

While this does not:

$url = $this->server->setResponseOfPath("/api/endpoint?with=parameter", new Response('def'));
$content = file_get_contents($url);
print_r($content);

Instead of returning def, it returns the information about the request.

ColmMcBarron avatar Feb 16 '18 11:02 ColmMcBarron

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
)

donatj avatar Feb 16 '18 15:02 donatj

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.

ColmMcBarron avatar Feb 19 '18 11:02 ColmMcBarron

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

donatj avatar Feb 19 '18 15:02 donatj

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.

donatj avatar Feb 19 '18 15:02 donatj

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.

donatj avatar Mar 12 '18 18:03 donatj

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.

avehlies avatar May 24 '22 23:05 avehlies