Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implementing Karate Generic Mock Server with Callbacks and Predicates. #2655

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from

Conversation

naveenchandra91
Copy link

@naveenchandra91 naveenchandra91 commented Feb 18, 2025

Description

This is an attempt to isolate Karate Mock Rule matching from the protocol implementation itself. Currently the Karate Mock Core handler has tight integration with http protocol. It certainly gives a lot of advantage for Http request mocking. So I didnt wanted to touch that as it works quite well.

But to work with different protocols, or to work with custom protocols, where Karate's powerful rule matching capabilities and Feature+Scenario based mock capabilities can be used with other protocols, we need to isolate the protocol implementation and how the Mock is invoked from the core. This is where I tried to use an interface called KarateMockCallback which will be implemented by the Handler, to receive the request for matching.

Advantage is that the Mock engine is isolated from protocol implementation. Can theoretically work with any protocol. Http/ Async/ MQ/ Fix Server (TCP+Wire). Mock server is invoked externally on the event where the mock should happen. Hence its to the protocol implementers to use this callback to define where the MockEvent should be called.

But to make it generic there should be also capability to test any MockEvent and a generic message passing capability. This needed 2 enhancements. A generic message which should define the most wire events there can be. For that I defined a class "KarateMessage" which has currently a very basic skeleton, Properties, Headers and a Generics Body. This should theoretically handle most of the wire events for Application Protocol implementation needs.

2nd Enhancement: Predicates. Predicate is a generic match function which takes another function as argument and tests whether the KarateMessage matches certain condition to invoke the rule. Because there needed to be a generic way to let the custom MockEvent to be matched.

This is currently just a POC. I have provided a sample test with MQ mock implementation of a TextMessage which gets mocked by the Karate matcher to respond as different text message. But I am open to inputs and enhancement.

Next Steps: Enhance with a Plugin architecture with SPI so that popular protocols like MQ can be defined as a mock plugins that defines the mock events+Predicates needed for the said protocol.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant