Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

setResponseOfPath not working with query parameters #6

Open
ColmMcBarron opened this issue Feb 16, 2018 · 6 comments
Open

setResponseOfPath not working with query parameters #6

ColmMcBarron opened this issue Feb 16, 2018 · 6 comments

Comments

@ColmMcBarron
Copy link

ColmMcBarron commented Feb 16, 2018

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
Copy link
Owner

donatj commented Feb 16, 2018

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
Copy link
Author

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
Copy link
Owner

donatj commented Feb 19, 2018

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
Copy link
Owner

donatj commented Feb 19, 2018

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
Copy link
Owner

donatj commented Mar 12, 2018

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
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants