-
Notifications
You must be signed in to change notification settings - Fork 12
Managing Stubs Using the REST API
The gRPC mock server is capable of returning any success or error response. Ir order for it to know what response to send for a specific request it is necessary to create stubs and it is done through its REST API. When the gRPC server starts, by default it is listening on two different ports: 1068 and 10010. Port 1068 is the REST API endpoint that we will use to manage the stubs.
The API consists of two endpoints:
The stubs stored in the server only live while the server is running. There is no provision to add support for persistent stubs storage. However, there is an open issue to add support to load stubs from the filesystem at start Issue #1.
Endpoint: /stubs
Exposes a CRUD interface for stub management.
Method: POST
Body: Stub
Method: GET
method OPTIONAL
If provided returns the only the stubs added for the gRPC method. Otherwise returns all stubs.
Method: PUT
Body: Stub
Method: DELETE
Deletes all stubs.
Optionally a method name can be provided as a query parameter, in that case all stubs stored for the provided method will be deleted.
Optionally a stub can be provided in the body, in that case only that stub will be deleted.
Body: Stub OPTIONAL
method OPTIONAL
If provided all the existing stubs for the provided method will be deleted.
Endpoint: examples
Returns auto-generated stub examples for all supported methods in the server.
Method: GET
A stub has this basic structure:
{
"fullMethod": "/carvalhorr.greeter.Greeter/Hello",
"request": {
"match": "exact | partial",
"content": {},
"metadata": {}
},
"response": {
"type": "success | error",
"content": {},
"error": {
"code": 0,
"message": "",
"details": {}
}
}
}
The fun gRPC method name. The method names supported by the server are logged when the server starts up.
The request definition to be matched against incoming gRPC requests.
Determine how the stub will be matched against an incoming gRPC request. If it matches then the response is returned for the incoming request.
Possible values: exact or partial
If the stub defines the exact match mode then all the fields in an incoming request must match all the fields defined in the stub`s stub.request.content.
If the stub defines the partialmatch mode then only the fields defined in stub.request.content must match to the fields in an incoming request.
The JSON definition of the request to be matched against incoming requests.
Optionally, metadata can be used to determine if an incoming request matches a stub. If provided, the fields added to the metadata must match to the metadata in an incoming request.
Each metadata field is of type string. To provide multiple values, separate them by a comma.
The definition of the response to return in case an incoming request matches the stub.
Defines the type of response to return: success or error.
The response content to return in case the field stub.response.type is success. This is the JSON that will be unmarshaled to the response type defined in the API.
The error to be returned in case the request matches the stub and the field stub.response.type is error. Errors are described by status.Status.
The error code. See Code. In addition to the defined error codes, any unsigned int 32 can be used.
The error message.
See Advanced error mocking to learn how to add details to the error response.