Pact generator?

744 views
Skip to first unread message

Mani Venkatesan

unread,
Feb 5, 2016, 6:47:08 PM2/5/16
to Pact
I didn't find anything in the list and hence posting this.

I realize that a pact spec is typically generated using a client (JUnit for example). I didn't see any way to record live traffic (similar to Pacto - https://github.com/thoughtworks/pacto) to generate a sample pact which can be customized manually.

Is this possible? 

Ronald Holshausen

unread,
Feb 7, 2016, 10:00:35 PM2/7/16
to Mani Venkatesan, Pact
There is no implementation of a recording proxy as Pact has traditionally focused on the consumer creating the contract through defining their expectations in a test. The pact file is an artefact of this. Refer to https://github.com/realestate-com-au/pact/wiki/FAQ#how-does-pact-differ-from-pacto for a list of the differences from Pacto.

However, there is nothing stopping someone creating a proxy that records the interactions and saves them in a pact file.

--
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

Tom Davies

unread,
May 24, 2016, 7:43:49 PM5/24/16
to Pact, mavenk...@gmail.com
I would also like to do this -- we already have a large suite of integration tests which run against the real server, but we feel that pact would help isolate reasons for test failures and improve performance.

I'm using pact-jvm and I've written a filter which creates PactFragments:

@Singleton
@Provider
public class PactFilter implements ContainerResponseFilter {
@Override
public ContainerResponse filter(ContainerRequest req,
ContainerResponse res) {
ConsumerPactBuilder
.consumer("client")
.hasPactWith("server")
.uponReceiving("a request")
.path(req.getPath())
.method(req.getMethod())
.body(req.getEntity(String.class))
.willRespondWith()
.status(res.getStatus())
.body(res.getResponse().getEntity().toString())
.toFragment()
.runConsumer(null, new TestRun() {
@Override
public void run(MockProviderConfig mockProviderConfig) throws Throwable {
// do nothing
}
});
return res;
}
}

At present this is on the server -- I may put it in the client instead, and it will need to be enhanced to handle headers, cookies etc.

What I'm not clear on is how I ask Pact to write the fragments to a file after my tests have run?

Thanks,
  Tom

Ronald Holshausen

unread,
May 24, 2016, 8:08:05 PM5/24/16
to Tom Davies, Pact, Mani Venkatesan
Tom, that is a great idea. Just a few comments:  You don't need to call .runConsumer and you'll need to make sure the  .uponReceiving description is unique, because it is used as a key. Maybe use the method, path and query parameters to generate it, or use a random value.

Once you have the PactFragment, you can call .toPact() to get the actual Pact class. Then you have some choices on how to write the pact file. You can use the PactWriter class to write it out to a file, but that will overwrite the pact file after each request. There is also a PactGenerator class that can merge pacts together, and then write them all out afterwards. This is what the ConsumerPactRunner executes if everything is ok: PactGenerator.merge(pact).writeAllToFile(config).

If you want your filter to be stateless, you can first load the pact file with the PactReader class, then use the PactGenerator to merge it and write it out.

Regards,

Ron

--

Tom Davies

unread,
May 24, 2016, 8:12:27 PM5/24/16
to Pact
Thanks for the quick reply Ron!

Alfred E

unread,
Jan 27, 2017, 3:04:43 PM1/27/17
to Pact Support
Hi Tom,


Did you manage to implement it? Can you share the solution?

Thanks

Dean de Bree

unread,
May 2, 2017, 12:59:11 AM5/2/17
to Pact Support
Hi,

I wrote a proxy server implementation that does this: https://github.com/ddebree/pact-proxy
Its a simple java proxy server that writes out all the interactions as pact files. 

Matt Fellows

unread,
May 2, 2017, 3:26:41 AM5/2/17
to Dean de Bree, Pact Support
Cool! 

--
Pact-Support email group is being deprecated, please use StackOverflow instead to help make questions and answers more visible: http://stackoverflow.com/questions/tagged/pact
---
You received this message because you are subscribed to the Google Groups "Pact Support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pact-support+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--

Beth

unread,
May 3, 2017, 1:54:41 AM5/3/17
to Matt Fellows, Dean de Bree, Pact Support
Great stuff!
Reply all
Reply to author
Forward
0 new messages