Documentation
¶
Index ¶
- func GetStubHash(jsonBytes []byte) (string, error)
- func RequestAndResponseToFile(requestMessage proto.Message, responseMessage proto.Message, errorCode *int, ...) errordeprecated
- func WriteStubFile(requestMessage proto.Message, responseMessage proto.Message, st *status.Status, ...) error
- func WriteToFileAndSync(filePath string, jsonBytes []byte, syncFileMode os.FileMode) error
- type Middleware
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func GetStubHash ¶
func RequestAndResponseToFile
deprecated
func RequestAndResponseToFile(requestMessage proto.Message, responseMessage proto.Message, errorCode *int, errorMessage *string, requestDirectory string) error
Deprecated: Prefer WriteStubFile, which accepts a *status.Status (carrying code, message, and rich error details) and writes the canonical on-disk format consumed by Middleware.Stubbed().
RequestAndResponseToFile has several known limitations:
- It always writes a `response` field, even when errorCode is set. The runtime middleware ignores `response` whenever errorCode is present, so that data is dead bytes on disk.
- It does not support errorDetails (the gRPC richer error model).
- It builds JSON via string concatenation and does not safely escape the errorMessage value, which can produce invalid JSON for messages containing quotes, backslashes, or newlines.
This function is retained for backward compatibility.
func WriteStubFile ¶ added in v0.4.2
func WriteStubFile( requestMessage proto.Message, responseMessage proto.Message, st *status.Status, requestDirectory string, ) error
WriteStubFile writes a stub fixture for the given request to <requestDirectory>/<md5(canonicalized request)>.json.
The fixture format depends on st:
If st is nil or st.Code() == codes.OK, a success fixture is written containing `request` and `response`. responseMessage must be non-nil.
If st is non-nil and st.Code() != codes.OK, an error fixture is written containing `request`, `errorCode`, `errorMessage`, and (if any are present on the status) `errorDetails`. The responseMessage parameter is IGNORED in this case, matching the runtime behavior of Middleware.Stubbed(): when errorCode is set on disk, the response field is never read.
The on-disk format is the canonical format consumed by Middleware.Stubbed().
Types ¶
type Middleware ¶
type Middleware struct {
// contains filtered or unexported fields
}
func NewMiddleware ¶
func NewMiddleware(methodsToProtos map[string]func() proto.Message, dataDir string) *Middleware
func NewMiddlewareWithErrorProtos ¶ added in v0.4.2
func NewMiddlewareWithErrorProtos(methodsToProtos map[string]func() proto.Message, methodToErrorProtos map[string][]func() proto.Message, dataDir string) *Middleware
NewMiddlewareWithErrorProtos creates a Middleware that additionally honors errorDetails entries in stub fixtures.
methodToErrorProtos maps a full gRPC method path (e.g., "/users.v1.UsersService/GetUser") to a list of zero-arg constructors for the proto types that method may return as rich error details. At read time, each errorDetails JSON entry is unmarshaled (strict, DiscardUnknown: false) against the registered candidates in order; the first successful match is attached to the returned status via WithDetails. Methods absent from the map have their errorDetails silently dropped.
func (*Middleware) Stubbed ¶
func (m *Middleware) Stubbed() grpc.UnaryServerInterceptor