pact-foundation / pact-php

PHP version of Pact. Enables consumer driven contract testing, providing a mock service and DSL for the consumer project, and interaction playback and verification for the service provider project
Apache License 2.0
271 stars 92 forks source link

Client error: `POST http://host.docker.internal:8080/interactions` resulted in a `404 Not Found #560

Closed aformuli closed 4 months ago

aformuli commented 4 months ago

Hi, I am using symfony framework with Docker. I have added a contract test with below code but I get No route found for POST http://host.docker.internal:8080/interactions; (404 Not Found).

    $request = new ConsumerRequest();
    $request->setMethod('GET')
        ->setPath('/api/user/<id>')
        ->addHeader('Accept', 'application/json')
        ->addHeader('Content-Type', 'application/json');

    $response = new ProviderResponse();
    $response->setStatus(200)
        ->addHeader('Content-Type', 'application/json')
        ->setBody([
            "id" => "<id>",
            "contact_id" => "<id>"
        ]);

    $config  = new MockServerConfig();
    $config->setHost('host.docker.internal');
    $config->setPort(8080);

    $builder = new InteractionBuilder($config);
    $builder->given('a user exists')
        ->uponReceiving('Get user for id /api/user/{id}')
        ->with($request)
        ->willRespondWith($response); -> **It fails here**

    $client = new Client(['base_uri' => $config->getBaseUri()]);

    $controller = new UserController($client, $config->getBaseUri()->getHost());
    $response = $controller->fetch(new Request([], [], ['id' => '<id>']));
    $builder->verify();
    $this->assertSame(200, $response->getStatusCode());

I don't get where the /interactions route is set or if I am missing config for this path.

Thanks

mefellows commented 4 months ago

You may need to explain your setup here, it's not clear why docker is involved and how it is configured to work with Pact.

Specifically, what is this doing:

    $config  = new MockServerConfig();
    $config->setHost('host.docker.internal');
    $config->setPort(8080);

I'm guessing maybe this is where your provider API is running? If so, that's not how Pact is intended to work. The mock server is that - a mock - and is setup by Pact automatically based on the $builder definition. This could explain the 404 - your API is not the Pact mock server and doesn't know how to respond to the administration calls.

aformuli commented 4 months ago

Thanks Matt,

That is what I was confused of. I changed the the mock server config and it works now.

mefellows commented 4 months ago

Great, no worries :)