https://github.com/soroushj/grpcmock
Mock gRPC servers dynamically in Go
https://github.com/soroushj/grpcmock
go-module golang-module grpc grpc-go mock testing
Last synced: 12 days ago
JSON representation
Mock gRPC servers dynamically in Go
- Host: GitHub
- URL: https://github.com/soroushj/grpcmock
- Owner: soroushj
- License: mit
- Created: 2023-02-03T20:01:28.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2024-03-23T09:15:45.000Z (about 2 years ago)
- Last Synced: 2024-12-04T04:18:43.539Z (over 1 year ago)
- Topics: go-module, golang-module, grpc, grpc-go, mock, testing
- Language: Go
- Homepage:
- Size: 17.6 KB
- Stars: 9
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# grpcmock: Mock gRPC servers dynamically in Go
[](https://pkg.go.dev/github.com/soroushj/grpcmock)
[](https://github.com/soroushj/grpcmock/actions/workflows/ci.yml)
[](https://goreportcard.com/report/github.com/soroushj/grpcmock)
See [docs and examples on pkg.go.dev](https://pkg.go.dev/github.com/soroushj/grpcmock).
See the [example](./example/) directory for a complete example.
## Usage
```go
import (
"context"
"log"
"net"
"github.com/soroushj/grpcmock"
"github.com/soroushj/grpcmock/example/notes"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
func Example() {
// Create a gRPC server using a grpcmock interceptor
mock := grpcmock.New()
server := grpc.NewServer(grpc.UnaryInterceptor(mock.UnaryServerInterceptor()))
defer server.Stop()
// Register an implementation of your server; the example Notes server in this case.
// This typically should be the generated Unimplemented implementation.
notes.RegisterNotesServer(server, ¬es.UnimplementedNotesServer{})
// Run the server on an available port
lis, err := net.Listen("tcp", ":0")
if err != nil {
log.Fatalf("listen: %v", err)
}
go func() {
if err := server.Serve(lis); err != nil {
log.Fatalf("serve at %v: %v", lis.Addr(), err)
}
}()
// At this point, all methods on the server will return an Unimplemented error.
// Let's change this behavior for the GetNote method of this server.
// This is how you can set a mock response for a method; an error in this case.
// After this, the GetNote method will return a NotFound error.
mock.SetResponse("GetNote", &grpcmock.UnaryResponse{
Err: status.Error(codes.NotFound, "note not found"),
})
// Similarly, you can set a non-error response.
// After this, the GetNote method will return the response below.
mock.SetResponse("GetNote", &grpcmock.UnaryResponse{
Resp: ¬es.GetNoteResponse{
Note: ¬es.Note{
Id: "1",
Text: "a",
},
},
})
// If you need something more than a simple response, you can set a handler.
// After this, any call to GetNote will be handled using the function below.
mock.SetHandler("GetNote", func(ctx context.Context, req any) (any, error) {
r := req.(*notes.GetNoteRequest)
if r.Id == "1" {
return ¬es.GetNoteResponse{
Note: ¬es.Note{
Id: "1",
Text: "a",
},
}, nil
}
return nil, status.Error(codes.NotFound, "note not found")
})
// You can remove any previously-set response or handler for a method.
// After this, the GetNote method will return an Unimplemented error.
mock.Unset("GetNote")
// You can also remove any response or handler for all methods
mock.Clear()
}
```