donatj / mock-webserver

Simple mock web server in PHP for unit testing.
MIT License
132 stars 21 forks source link

setResponseOfPath not working with query parameters #6

Open ColmMcBarron opened 6 years ago

ColmMcBarron commented 6 years ago

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.

donatj commented 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
)
ColmMcBarron commented 6 years ago

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.

donatj commented 6 years ago

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 commented 6 years ago

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 commented 6 years ago

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.

avehlies commented 2 years ago

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.