Can a bearer token be added to the provider verifi...
# pact-php
v
Can a bearer token be added to the provider verifier config like so:
Copy code
$config = new VerifierConfig();
$config
    ->header('headers' => ['Authorization' => 'Bearer <value>'])
    ->setRequestFilter(
        function (RequestInterface $r) {
            return $r->withHeader('Authorization', 'Bearer <value>');
        }
    );
m
I think so, is it not working?
v
Response said: "401 Token not found" using setRequestFilter or no header at all. The other 2 tryouts (headers, setAdditionalRequestHeaders) return php call errors. Following this repo guide: https://github.com/pact-foundation/pact-php#provider-verification.
Copy code
public function testPactVerifyConsumer() {
    $config = new VerifierConfig();
    $config
        ->setProviderName('Provider')
        ->setProviderVersion('1.0.0')
        ->setProviderBaseUrl(new Uri('<http://localhost:8081>'))
        ->setBrokerUri(new Uri('<http://localhost:9292>'))
        // not working ->header('headers' => ['Authorization' => 'Bearer <value>'])
        /* not working ->setRequestFilter(
            function (RequestInterface $r) {
                return $r->withHeader('Authorization', 'Bearer <value>');
            }
        );*/
    ;
    $verifier = new Verifier($config);
    // not working $verifier->setAdditionalRequestHeaders(['Authorization' => 'Bearer <value>'])
    $verifier->verifyFiles([__DIR__ . '/../../../pacts/*.json']);
    $this->assertTrue(true, 'Pact Verification has failed.');
}
ChatGPT recommended, but same response "401 Token not found":
Copy code
use GuzzleHttp\Psr7\Uri;
use PhpPact\Standalone\ProviderVerifier\Model\VerifierConfig;
use PhpPact\Standalone\ProviderVerifier\Verifier;
use PhpPact\Standalone\Runner\ProcessRunner;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\RequestInterface;

public function testPactVerifyConsumer() {
    $config = new VerifierConfig();
    $config
        ->setProviderName('Provider')
        ->setProviderVersion('1.0.0')
        ->setProviderBaseUrl(new Uri('<http://localhost:8081>'))
        ->setBrokerUri(new Uri('<http://localhost:9292>'))
        ->setRequestFilter(
            function (RequestInterface $r) {
                $authHeader = ['Authorization' => 'Bearer <value>'];
                return $r->withHeader($authHeader);
            }
        );
    ;
    $verifier = new Verifier($config);
    $verifier->verifyFiles([__DIR__ . '/../../../pacts/*.json']);
    $this->assertTrue(true, 'Pact Verification has failed.');
}
Just for info: I would need the token for every API call to verify the consumer, not to auth to the broker.
m
What does the output of the log file say when set to DEBUG/verbose - it should show the fully request/response
v
Copy code
I, [2023-04-21T09:14:13.465219 #48]  INFO -- : Running example 'Verifying a pact between Consumer and Provider A request for a clients list with GET /api/clients returns a response which has status code 200'
I, [2023-04-21T09:14:13.468601 #48]  INFO -- : Sending GET request to path: "/api/clients" with headers: {}, see debug logs for body
D, [2023-04-21T09:14:13.469643 #48] DEBUG -- : body :
I, [2023-04-21T09:14:15.490512 #48]  INFO -- : Received response with status: 401, headers: {"Content-Type"=>"application/json", "Connection"=>"keep-alive", "Www-Authenticate"=>"Bearer", "Cache-Control"=>"no-cache, private", "Date"=>"Fri, 21 Apr 2023 09:14:14 GMT", "Link"=>"<<http://localhost:8081/api/docs.jsonld>>; rel=\"<http://www.w3.org/ns/hydra/core#apiDocumentation>\"", "X-Debug-Token"=>"3afd26", "X-Debug-Token-Link"=>"<http://localhost:8081/api/_profiler/3afd26>", "X-Robots-Tag"=>"noindex", "Content-Length"=>"44"}, see debug logs for body
D, [2023-04-21T09:14:15.491504 #48] DEBUG -- : body: {"code":401,"message":"JWT Token not found"}
->setVerbose(true)
Copy code
same output
It works with:
->addCustomProviderHeader('Authorization', 'Bearer <value>')
Which I found here in line 41: https://github.com/pact-foundation/pact-php/blob/master/tests/PhpPact/Standalone/ProviderVerifier/VerifierTest.php.
2
m
Ah! yes, that makes sense now I look at it 😆
sorry for not picking that up