Tien Vo
03/23/2023, 10:59 AMverifying service method interaction with a gRPC server
https://github.com/pactflow/pact-protobuf-plugin#service-method-provider
I choose to write unit test to verify instead of using pact_verifier_cli
.
This part of the unit test:
public function testPactVerifyConsumer()
{
$config = new VerifierConfig();
$config->getProviderInfo()
->setName('protobufProvider')
->setHost('127.0.0.1')
->setScheme('tcp')
->setPort(9001);
$providerTransport = new ProviderTransport();
$providerTransport
->setProtocol('grpc')
->setScheme('tcp')
->setPort(9001)
->setPath('/')
;
$config->addProviderTransport($providerTransport);
if ($logLevel = \getenv('PACT_LOGLEVEL')) {
$config->setLogLevel($logLevel);
}
$verifier = new Verifier($config);
$verifier->addDirectory(__DIR__.'/../../../broker/pacts');
$this->assertTrue($verifier->verify());
}
This is the output:
Verifying a pact between protobufConsumer and protobufProvider
request for calculate shape area (0s loading, 2ms verification)
Request Failed - builder error for url (<tcp://127.0.0.1:9001>): URL scheme is not allowed
Failures:
1) Verifying a pact between protobufConsumer and protobufProvider - request for calculate shape area - builder error for url (<tcp://127.0.0.1:9001>): URL scheme is not allowed
This is part of the log I think it's useful:
2023-03-23T10:56:35.997068Z INFO ThreadId(01) verify_interaction{interaction="request for calculate shape area"}: pact_verifier: Running provider verification for 'request for calculate shape area'
2023-03-23T10:56:35.997099Z DEBUG ThreadId(01) verify_interaction{interaction="request for calculate shape area"}: pact_verifier: Verifying a synchronous message (request/response)
2023-03-23T10:56:35.997212Z INFO ThreadId(01) verify_interaction{interaction="request for calculate shape area"}: pact_verifier::provider_client: Sending request to provider at <tcp://127.0.0.1:9001/>
2023-03-23T10:56:35.997230Z DEBUG ThreadId(01) verify_interaction{interaction="request for calculate shape area"}: pact_verifier::provider_client: Provider details = ProviderInfo { name: "protobufProvider", protocol: "tcp", host: "127.0.0.1", port: Some(9001), path: "/", transports: [ProviderTransport { transport: "tcp", port: Some(9001), path: Some("/"), scheme: None }, ProviderTransport { transport: "grpc", port: Some(9001), path: Some("/"), scheme: Some("tcp") }] }
2023-03-23T10:56:35.997244Z INFO ThreadId(01) verify_interaction{interaction="request for calculate shape area"}: pact_verifier::provider_client: Sending request HTTP Request ( method: POST, path: /, query: None, headers: Some({"Content-Type": ["application/json"]}), body: Present(447 bytes, application/json) )
2023-03-23T10:56:35.997257Z DEBUG ThreadId(01) verify_interaction{interaction="request for calculate shape area"}: pact_verifier::provider_client: body:
{"description":"request for calculate shape area","request":{"contents":{"content":"EgoNAABAQBUAAIBA","contentType":"application/protobuf;message=ShapeMessage","contentTypeHint":"BINARY","encoded":"base64"},"matchingRules":{"body":{"$.rectangle.length":{"combine":"AND","matchers":[{"match":"number"}]},"$.rectangle.width":{"combine":"AND","matchers":[{"match":"number"}]}}},"metadata":{"contentType":"application/protobuf;message=ShapeMessage"}}}
2023-03-23T10:56:35.997298Z DEBUG ThreadId(01) verify_interaction{interaction="request for calculate shape area"}: pact_verifier::provider_client: Native request builder: RequestBuilder { method: POST, url: Url { scheme: "tcp", cannot_be_a_base: false, username: "", password: None, host: Some(Domain("127.0.0.1")), port: Some(9001), path: "", query: None, fragment: None }, headers: {"content-type": "application/json"}
Do you know how to solve that error URL scheme is not allowed
?Tien Vo
03/23/2023, 12:19 PMpact_ffi
, I see the real issue: transport
is missing from pact file:
{
"consumer": {
"name": "protobufConsumer"
},
"interactions": [
{
.................
"transport": "grpc"
}
],
"provider": {
"name": "protobufProvider"
}
}
How can I add this value to pact file?Yousaf Nabi (pactflow.io)
pactffi_verifier_set_provider_info
&
pactffi_verifier_add_provider_transport
I jotted some comments and experiments when trying to verify both a plugin pact (grpc) and http pact and trying to set the correct transports for both
// 💡
// This would be my preferrred option
// Set the provider name (which should be used by anything using the verifier_handle, and filter sourced pacts that don't contain name)
// add multiple transports.
// note pactffi_verifier_set_provider_name does not exist
// update_provider_info might work
// <https://github.com/pact-foundation/pact-reference/blob/cfb2c03f87b3f67464291dd936d0aac555c42c91/rust/pact_ffi/src/verifier/handle.rs#L89>
// but is marked as deprecated.
//
// also worthy of note, if pactffi_verifier_set_provider_info didn't mix with the information used in pactffi_verifier_add_provider_transport
// this probably wouldn't be neccessary.
$ffi->pactffi_verifier_set_provider_name($handle, 'http-provider'); // note this function doesn't exist (wishlist)
$ffi->pactffi_verifier_add_provider_transport($handle, 'http',8000,'/','http');
$ffi->pactffi_verifier_add_provider_transport($handle, 'protobuf',37757,'/','tcp');
Yousaf Nabi (pactflow.io)
Tien Vo
03/23/2023, 1:25 PMTien Vo
03/23/2023, 2:33 PMpactffi_pact_handle_write_file
by pactffi_write_pact_file
.
I think because mock server know what transport it's using (by calling pactffi_create_mock_server_for_transport
), asking it to write pact file will have additional transport
information.
I think this useful information should be mentioned somewhere , at least in the pact.h
file?Yousaf Nabi (pactflow.io)
Yousaf Nabi (pactflow.io)
uglyog
setScheme('tcp')
, it should be grpc.Tien Vo
03/23/2023, 11:51 PMtcp is not a valid schemeI assume this note is for providerInfo part only. I removed it once I found solution above., it should be grpcsetScheme('tcp')
Tien Vo
03/24/2023, 12:08 AMpactffi_write_pact_file
into the file consumer-plugin.php, then create a PR to merge branch php_plugins
into master
is another option, but it's less obvious, and only for people who interest in reading PHP code only