Mock Response Plugin
The mock response plugin allows you to return a fixed response without making a request to the backend service.
Use cases:
- Mocking interfaces for client debugging.
- Returning a fixed response when an interface is offline to prevent errors in older client versions.
- Providing a configuration management interface.
Technical Solution
Intercept the request and return a pre-configured fixed response.
Plugin Usage
Import the Plugin in the Gateway Project's main.go
import (
_ "trpc.group/trpc-go/trpc-gateway/plugin/mocking"
)
- Configure the tRPC framework to enable the mocking interceptor.
Note: Make sure to register it in server.service.filter and not in server.filter.
global:
server:
filter:
service:
- name: trpc.inews.trpc.gateway
filter:
- mocking
plugins:
log:
gateway:
mocking:
Configure the Plugin in the Gateway's router.yaml File
Different levels of plugins are executed only once, with the priority order: router plugin > service plugin > global plugin.
router:
- method: /v1/user/info
id: "xxxxxx"
target_service:
- service: trpc.user.service
plugins:
- name: mocking
props:
response_example: '{"code":0,"data":{}}'
content_type: ""
delay: 0
response_status: 200
with_mock_header: true
scale: true
hash_key: suid
client:
- name: trpc.user.service
plugins:
- name: mocking
props:
response_example: '{"code":0,"data":{}}'
plugins:
- name: mocking
props:
response_example: '{"code":0,"data":{}}'