mocking

package module
v0.0.0-...-2c47b87 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 2, 2024 License: Apache-2.0, BSD-3-Clause, MIT Imports: 14 Imported by: 0

README

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
  • Add the import statement
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:                             # Global configuration
server: # Server configuration
  filter:                                          # Interceptor list for all service handler functions
  service: # Business services provided, can have multiple
    - name: trpc.inews.trpc.gateway      # Route name of the service
      filter:
        - mocking # Gateway plugin registered in the service filter, so that it can be dynamically loaded in router.yaml
plugins: # Plugin configuration
  log:                                            # Log configuration
  gateway: # Plugin type is gateway
    mocking:  # Mock response plugin
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 # Router-level plugin
        props:
          response_example: '{"code":0,"data":{}}' # Mock response body
          content_type: "" # Content-Type header of the response, default: application/json
          delay: 0 # Delay in milliseconds before returning the response, default: 0
          response_status: 200 # HTTP status code of the response, default: 200
          with_mock_header: true # When set to true, adds the response header "x-mock-by: tRPC-Gateway". When set to false, the header is not added.
          scale: true # Mock traffic ratio, in percentage. For example, if it is one in ten thousand, fill in: 0.01. The default is full-scale mock.
          hash_key: suid # Hash key for mocking traffic, providing the ability to perform grayscale testing based on request parameters.
client:
  - name: trpc.user.service
    plugins:
      - name: mocking # Service-level configuration
        props:
          response_example: '{"code":0,"data":{}}' # Mock response body
plugins:
  - name: mocking # Global configuration
    props:
      response_example: '{"code":0,"data":{}}' # Mock response body

Documentation

Overview

Package mocking provides mock responses

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ServerFilter

func ServerFilter(ctx context.Context, req interface{}, handler filter.ServerHandleFunc) (interface{}, error)

ServerFilter is the server interceptor

Types

type Options

type Options struct {
	// Delay in milliseconds before returning the response, default is 0
	Delay int `yaml:"delay"`
	// HTTP status code of the response, default is 200
	ResponseStatus int `yaml:"response_status"`
	// Content-Type header of the response, default is "application/json"
	ContentType string `yaml:"content_type"`
	// Body of the response
	ResponseExample string `yaml:"response_example"`
	// When set to true, adds the response header "x-mock-by: tRPC-Gateway". When set to false, the header is not added.
	WithMockHeader bool `yaml:"with_mock_header"`
	// Mock traffic ratio, in percentage. For example, if it is one in ten thousand, fill in: 0.01. The default is
	// full mock.
	Scale float64 `yaml:"scale" json:"scale"`
	// Hash key for mock traffic, providing the ability to perform grayscale based on request parameters.
	HashKey string `yaml:"hash_key" json:"hash_key"`
}

Options represents the plugin configuration

type Plugin

type Plugin struct {
}

Plugin defines the plugin

func (*Plugin) CheckConfig

func (p *Plugin) CheckConfig(_ string, decoder plugin.Decoder) error

CheckConfig validates the plugin configuration and returns the parsed configuration object. Used in the ServerFilter method for parsing.

func (*Plugin) Setup

func (p *Plugin) Setup(string, plugin.Decoder) error

Setup initializes the plugin

func (*Plugin) Type

func (p *Plugin) Type() string

Type returns the plugin type

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL