spring-cloud-gateway icon indicating copy to clipboard operation
spring-cloud-gateway copied to clipboard

API gateway with mock capability

Open vforvikash opened this issue 4 years ago • 19 comments

Is your feature request related to a problem? Please describe. Needed a mock service based on proxy capability. The required mock service must be able to intercept incoming requests and return mock response if configured or forwards to existing live service.

Describe the solution you'd like I have developed a framework on top of Spring Cloud Gateway to enable mock configuration based on properties/config service. This framework allows to add mock configuration for a given API endpoint. This mock API either produces mock response or proxies the requests to live the service. The mock can be turned on/off for a given endpoint. It can also help to simulate difficult cases like timeouts for outbound calls by adding delays to mock response. Also, the mock configuration is property based which doesn’t require any modification.

Describe alternatives you've considered There are few json-server which can be quickly configured and started. But it does not have a proxy capability.

Additional context image

vforvikash avatar Feb 15 '21 20:02 vforvikash

looking forward to the feature too.

qqeasonchen avatar Feb 19 '21 02:02 qqeasonchen

we have support for wiremock in spring-cloud-contract. This is outside the scope of spring cloud gateway.

spencergibb avatar Mar 31 '21 20:03 spencergibb

looking forward to the feature too.

@qqeasonchen here if you are still interested. https://github.com/ExpressScripts/sample-gateway-mock-apis

vforvikash avatar Nov 15 '21 18:11 vforvikash

looking forward to the feature too.

@qqeasonchen here if you are still interested. https://github.com/ExpressScripts/sample-gateway-mock-apis

@vforvikash thanks, let me try it.

qqeasonchen avatar Nov 16 '21 01:11 qqeasonchen

@spencergibb Please guide if i can contribute towards this feature based-of above mentioned repository.

vforvikash avatar Dec 17 '21 00:12 vforvikash

@vforvikash -

  • idea 1 - why not use spring cloud wire mock .
  • idea 2 - why not keep the same route configuration as the gateway and use a mock filter that routes to mock://

grabdoc avatar Nov 03 '23 13:11 grabdoc

@grabdoc I do like the idea of having a route to mock://. Can you elaborate on that please? Do you have an implementation that provides this route?

Controlix avatar Feb 10 '24 09:02 Controlix

  • why not use spring cloud wire mock .

yes wire mock is good choice. I didnt wanted to add mock code to actual code and with gateway mechanism i could do mock for multiple REST apis/application.

i'm not sure about your second suggestion. @grabdoc.

vforvikash avatar Feb 12 '24 15:02 vforvikash

You are not going to add mock code.. It's another filter that routes request to wiremock instead of actual backend. When your backend is ready you can just switch over.

kdhrubo avatar Feb 12 '24 15:02 kdhrubo

I don't thing a uri of mock:// makes much sense since you wouldn't be testing any real routes. Maybe something along the lines of boots "slice" testing, with an annotation @GatewayTest(mock=true) or something along those lines that replaces normal routes with mocked ones. Or combined with openapi to generate respones

spencergibb avatar Feb 12 '24 15:02 spencergibb

it wasnt just limited to my backend ready! using this feature testing team can change and add cases which are difficult to produce normally. This is all runtime changes with the gateway capability.

vforvikash avatar Feb 12 '24 15:02 vforvikash

What are these abnormal cases ? The filter with wiremock should be able to handle that ? Do you mind sharing couple of examples

kdhrubo avatar Feb 12 '24 15:02 kdhrubo

like one of our endpoint calls other services and in case if few services fails to respond or responds with unexpected messages or cases which are hard to setup....in such cases the testing team can write mock responses and use gateway capability to produce such cases and test the same in integration environment. Time to time this feature has helped as its easy to change and maintain by QAs.

vforvikash avatar Feb 12 '24 15:02 vforvikash

Services always fail and return unexpected messages. This can be handled with a mock route filter that routes to wiremock. Wiremock being standard and well documented for mocking - QAs will find it easier to write wiremock files and commit them to GIT assuming QAs are technical as well.

kdhrubo avatar Feb 12 '24 15:02 kdhrubo

OK. Sounds good. Well i loved the feature with gateway. Thanks for your suggestion.

vforvikash avatar Feb 12 '24 15:02 vforvikash

@Controlix Hi - Here is a skeleton implementation of the idea with mock://

https://github.com/kdhrubo/scg-wiremock

I have created the filter and configuration. I have checked the requests are picked up by the mock filter.

I have not implemented the loading of the wiremock response files. I am sure you will get the idea.

Bit busy with my other project.

kdhrubo avatar Feb 13 '24 02:02 kdhrubo

@kdhrubo thank you, but I get a 404 when navigating to your github repo. Maybe it's not public?

Controlix avatar Feb 13 '24 08:02 Controlix

Please check now. I have changed visibility to public.

kdhrubo avatar Feb 13 '24 13:02 kdhrubo

@Controlix did you get a chance to review ? Is this something you need ?

Please let me know.

kdhrubo avatar Feb 16 '24 13:02 kdhrubo