donatj / mock-webserver

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

Problem with child paths not working #69

Closed esteban-serfe closed 4 months ago

esteban-serfe commented 4 months ago

I'm trying to use the url in this following way:

POST /api/properties/XXXXXXXX
PUT /api/properties/XXXXXXXX/sellers

While doing this, the first url works but the second one gives me:

Client error: `PUT http://api-cli:8086/api/properties/XXXXXX/sellers` resulted in a `404 Not Found` response:
VND.DonatStudios.MockWebServer: Resource '/api/propiedades/XXXXXXX/sellers' not found!

Following the examples on the documentation I'm using:

$server = new MockWebServer(8086);
$server->setDefaultResponse(new NotFoundResponse);
$response = new ResponseByMethod([
            ResponseByMethod::METHOD_POST => new Response(
                '{ "id": ".$id." }',
                ['Cache-Control' => 'no-cache', 'Content-Type' => 'application/json'],
                201
            ),
            ResponseByMethod::METHOD_PUT => new Response(
                    '{ "id": '.$id_mu.' }',
                    ['Cache-Control' => 'no-cache', 'Content-Type' => 'application/json'],
                    201
                ),
        ]);
$server->setResponseOfPath(
            '/api/properties/'.$id.'/sellers',
            $response
);
 $server->setResponseOfPath(
            '/api/properties/'.$id,
            $response
);
donatj commented 4 months ago

Sorry for the late response - I had gone on a trip for the solar eclipse and just got home.

Does your code happen to start the server with $server->start(); before making requests against it? It's not included in your example.

Probably unrelated small bug, you have '{ "id": ".$id." }' and '{ "id": '.$id_mu.' }'. The prior comes out to the string { "id": ".$id." } which I presume is unintentional. I've fixed it in the following example.

I took your code above and wrapped it in the necessary scaffolding to run it

<?php

use donatj\MockWebServer\MockWebServer;
use donatj\MockWebServer\Response;
use donatj\MockWebServer\ResponseByMethod;
use donatj\MockWebServer\Responses\NotFoundResponse;

require 'vendor/autoload.php';

$id    = '123abc';
$id_mu = '123abc_mu';

$server = new MockWebServer(8086);
$server->setDefaultResponse(new NotFoundResponse);
$response = new ResponseByMethod([
            ResponseByMethod::METHOD_POST => new Response(
                '{ "id": '.$id.' }',
                ['Cache-Control' => 'no-cache', 'Content-Type' => 'application/json'],
                201
            ),
            ResponseByMethod::METHOD_PUT => new Response(
                    '{ "id": '.$id_mu.' }',
                    ['Cache-Control' => 'no-cache', 'Content-Type' => 'application/json'],
                    201
                ),
        ]);
$server->setResponseOfPath(
            '/api/properties/'.$id.'/sellers',
            $response
);
$server->setResponseOfPath(
            '/api/properties/'.$id,
            $response
);

$server->start();

echo "\n\nPOST\n\n";

$ctx = stream_context_create(['http' => ['method' => 'POST']]);
echo file_get_contents($server->getServerRoot().'/api/properties/'.$id, false, $ctx);

echo "\n\nPUT\n\n";

$ctx = stream_context_create(['http' => ['method' => 'PUT']]);
echo file_get_contents($server->getServerRoot().'/api/properties/'.$id, false, $ctx);

echo "\n\nPOST\n\n";

$ctx = stream_context_create(['http' => ['method' => 'POST']]);
echo file_get_contents($server->getServerRoot().'/api/properties/'.$id.'/sellers', false, $ctx);

echo "\n\nPUT\n\n";

$ctx = stream_context_create(['http' => ['method' => 'PUT']]);
echo file_get_contents($server->getServerRoot().'/api/properties/'.$id.'/sellers', false, $ctx);

running that gives me the following which seems correct to me

image

esteban-serfe commented 4 months ago

Hi @donatj Thanks for looking this up. I was able to have this working in order. I run this inside a Codeception Helper and was having trouble with the correct initialization on the docker setup we have for the project. So is not related to the code issue.

As you mentioned, this is working as expected. We can close this.

I'll open another request separately for an improvement on the debug process later today.