PACT with Protobuf

1,884 views
Skip to first unread message

John Morgan

unread,
Aug 7, 2015, 6:28:13 PM8/7/15
to Pact
Is it possible to use PACT with protobuffer?  We are currently using PACT to verify REST and RabbitMQ contracts (that are producing JSON) but we are considering a switch from JSON to protobuffers and are concerned about the ramifications that will have on our PACT tests.

Ronald Holshausen

unread,
Aug 7, 2015, 8:26:16 PM8/7/15
to John Morgan, Pact
Hi John,

If you are going to switch to Protocol Buffers, then you probably don't need to use Pact. The .proto file becomes the contract. You just need to ensure that your consumers and providers use the same .proto file. The same thing holds for frameworks like Apache Avro, where the Avro schema defines the contract.

Regards,

Ronald

On 8 August 2015 at 08:28, John Morgan <john....@bettercloud.com> wrote:
Is it possible to use PACT with protobuffer?  We are currently using PACT to verify REST and RabbitMQ contracts (that are producing JSON) but we are considering a switch from JSON to protobuffers and are concerned about the ramifications that will have on our PACT tests.

--
You received this message because you are subscribed to the Google Groups "Pact" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pact-support...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Ronald Holshausen

DiUS Computing Pty Ltd

Level 10, 99 Queens Street
Melbourne, VIC 3000

Phone: +61 3 9008 5400
Mobile: +61 413 162 439

http://www.diuscomputing.com.au

John Morgan

unread,
Aug 7, 2015, 10:53:13 PM8/7/15
to Pact, john....@bettercloud.com
Sure, but we are using a micro services architecture and we are using PACT to know when a contract between various services is broken.  As for .proto providing the contract, I could use JSON Schema to do the achieve a similar goal but we are interested in implementing consumer-driven contracts.  Is there any way to use PACT and proto buffers together?

Ronald Holshausen

unread,
Aug 7, 2015, 11:05:39 PM8/7/15
to John Morgan, Pact
It is definitely doable, but may be quite a bit of work. I'm assuming proto buffers is a binary protocol using serialisation. The Pact mock servers would have to updated to understand how to read and generate the requests and responses and how to compare it. It may even require having a separate mock server implementation if there is not much in common with HTTP. But hopefully they use HTTP under the covers.

John Morgan

unread,
Aug 7, 2015, 11:15:32 PM8/7/15
to Pact, john....@bettercloud.com
Proto is binary over the wire and can easily be converted to JSON so it could still be compatible with PACT.  We are using the following gradle plugin to verify provider contracts: 

https://github.com/DiUS/pact-jvm/tree/master/pact-jvm-provider-gradle

I see that it has the plugin provides the ability to set a requestFilter to intercept a request and set HTTP headers or do something with the request before sending the request on.  I was wondering if there was something similar for the response so that I could intercept the response and convert the proto binary into JSON so that it would be compatible with PACT.  Do you know if this would be possible?

Ronald Holshausen

unread,
Aug 7, 2015, 11:29:40 PM8/7/15
to John Morgan, Pact
In that case, it might be easy to do. The responses are compared based on mime-type. If there is a distinct mime-type, we could add a handler that converts it and delegates to the JSON comparison.

John Morgan

unread,
Aug 7, 2015, 11:38:49 PM8/7/15
to Pact, john....@bettercloud.com
That sounds promising.  The MIME type for proto is: application/x-protobuf

Can you elaborate on how to add a handler that converts given MIME type to JSON and delegates to the JSON comparison?

Ronald Holshausen

unread,
Aug 7, 2015, 11:52:13 PM8/7/15
to John Morgan, Pact
The map from mime-type to matcher is here: https://github.com/DiUS/pact-jvm/blob/master/pact-jvm-matchers%2Fsrc%2Fmain%2Fscala%2Fau%2Fcom%2Fdius%2Fpact%2Fmodel%2FMatching.scala#L12

It should be easy to add a protobuf matcher that converts the response to JSON and then delegates to JsonBodyMatcher.
Reply all
Reply to author
Forward
0 new messages