spring-cloud / spring-cloud-gateway

An API Gateway built on Spring Framework and Spring Boot providing routing and more.
http://cloud.spring.io
Apache License 2.0
4.49k stars 3.3k forks source link

API gateway with mock capability #2147

Open vforvikash opened 3 years ago

vforvikash commented 3 years ago

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

qqeasonchen commented 3 years ago

looking forward to the feature too.

spencergibb commented 3 years ago

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

vforvikash commented 2 years ago

looking forward to the feature too.

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

qqeasonchen commented 2 years ago

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.

vforvikash commented 2 years ago

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

grabdoc commented 9 months ago

@vforvikash -

Controlix commented 6 months ago

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

vforvikash commented 6 months ago
  • 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.

kdhrubo commented 6 months ago

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.

spencergibb commented 6 months ago

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

vforvikash commented 6 months ago

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.

kdhrubo commented 6 months ago

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

vforvikash commented 6 months ago

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.

kdhrubo commented 6 months ago

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.

vforvikash commented 6 months ago

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

kdhrubo commented 6 months ago

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

Controlix commented 6 months ago

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

kdhrubo commented 6 months ago

Please check now. I have changed visibility to public.

kdhrubo commented 6 months ago

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

Please let me know.