protoc-gen-mock's People
Forkers
vitaneyeyu deeminc harker2015 michielboekhoff toyozaki sradevski isgasho khorevaa jbendotnet peterlgh7protoc-gen-mock's Issues
Add support for google.protobuf.Any
cannot use new(Request) (type *Request) as type protoreflect.ProtoMessage
I am having issues running tests/compiling the code:
cannot use new(Request) (type *Request) as type protoreflect.ProtoMessage in argument to stub.CreateStubExample:
*Request does not implement protoreflect.ProtoMessage (missing ProtoReflect method)
I believe the main issue is proto.MessageV2 vs proto.MessageV1.
go version go1.14.4 linux/amd64
I also ran it in docker using go 1.13:
go version go1.13.12 linux/amd64
Steps to reproduce:
git checkout V1.1.11
mkdir greeter-service
protoc --plugin `which protoc-gen-mock` --go_out=plugins=grpc:greeter-service --mock_out=greeter-service greeter.proto
docker run -v $PWD:/mock -it --rm golang:1.13-alpine sh -c "cd /mock; CGO_ENABLED=0 go test ./..."
Output:
# github.com/carvalhorr/protoc-gen-mock/grpchandler [github.com/carvalhorr/protoc-gen-mock/grpchandler.test]
grpchandler/handler_test.go:46:4: cannot use stub.StubResponse literal (type stub.StubResponse) as type *stub.StubResponse in field value
grpchandler/handler_test.go:51:48: cannot use mockStubsMatcher (type *MockStubsMatcher) as type stub.StubsMatcher in argument to MockHandler:
*MockStubsMatcher does not implement stub.StubsMatcher (wrong type for Match method)
have Match(string, string) *stub.Stub
want Match(context.Context, string, string) *stub.Stub
grpchandler/handler_test.go:63:4: cannot use stub.StubResponse literal (type stub.StubResponse) as type *stub.StubResponse in field value
grpchandler/handler_test.go:66:14: cannot use "return an error" (type string) as type *stub.ErrorResponse in field value
grpchandler/handler_test.go:70:42: cannot use mockStubsMatcher (type *MockStubsMatcher) as type stub.StubsMatcher in argument to MockHandler:
*MockStubsMatcher does not implement stub.StubsMatcher (wrong type for Match method)
have Match(string, string) *stub.Stub
want Match(context.Context, string, string) *stub.Stub
grpchandler/handler_test.go:82:42: cannot use mockStubsMatcher (type *MockStubsMatcher) as type stub.StubsMatcher in argument to MockHandler:
*MockStubsMatcher does not implement stub.StubsMatcher (wrong type for Match method)
have Match(string, string) *stub.Stub
want Match(context.Context, string, string) *stub.Stub
grpchandler/handler_test.go:94:4: cannot use stub.StubResponse literal (type stub.StubResponse) as type *stub.StubResponse in field value
grpchandler/handler_test.go:99:42: cannot use mockStubsMatcher (type *MockStubsMatcher) as type stub.StubsMatcher in argument to MockHandler:
*MockStubsMatcher does not implement stub.StubsMatcher (wrong type for Match method)
have Match(string, string) *stub.Stub
want Match(context.Context, string, string) *stub.Stub
grpchandler/handler_test.go:109:42: cannot use mockStubsMatcher (type *MockStubsMatcher) as type stub.StubsMatcher in argument to MockHandler:
*MockStubsMatcher does not implement stub.StubsMatcher (wrong type for Match method)
have Match(string, string) *stub.Stub
want Match(context.Context, string, string) *stub.Stub
# github.com/carvalhorr/protoc-gen-mock/greeter-service
greeter-service/greeter.mock.pb.go:41:57: cannot use new(Request) (type *Request) as type protoreflect.ProtoMessage in argument to stub.CreateStubExample:
*Request does not implement protoreflect.ProtoMessage (missing ProtoReflect method)
greeter-service/greeter.mock.pb.go:46:56: cannot use new(Response) (type *Response) as type protoreflect.ProtoMessage in argument to stub.CreateStubExample:
*Response does not implement protoreflect.ProtoMessage (missing ProtoReflect method)
# github.com/carvalhorr/protoc-gen-mock/restcontrollers [github.com/carvalhorr/protoc-gen-mock/restcontrollers.test]
restcontrollers/examples_test.go:32:5: cannot use stub.StubRequest literal (type stub.StubRequest) as type *stub.StubRequest in field value
restcontrollers/examples_test.go:35:6: cannot use map[string]interface {} literal (type map[string]interface {}) as type map[string][]string in field value
restcontrollers/examples_test.go:37:5: cannot use stub.StubResponse literal (type stub.StubResponse) as type *stub.StubResponse in field value
restcontrollers/examples_test.go:40:15: cannot use "error1" (type string) as type *stub.ErrorResponse in field value
restcontrollers/examples_test.go:50:68: response.Header().Values undefined (type http.Header has no field or method Values)
restcontrollers/stubs_add_test.go:16:3: unknown field 'SupportedMethods' in struct literal of type StubsController
restcontrollers/stubs_add_test.go:50:3: unknown field 'SupportedMethods' in struct literal of type StubsController
restcontrollers/stubs_delete_test.go:16:3: cannot use stub.StubRequest literal (type stub.StubRequest) as type *stub.StubRequest in field value
restcontrollers/stubs_delete_test.go:20:3: cannot use stub.StubResponse literal (type stub.StubResponse) as type *stub.StubResponse in field value
restcontrollers/stubs_delete_test.go:23:13: cannot use "error1" (type string) as type *stub.ErrorResponse in field value
restcontrollers/stubs_delete_test.go:20:3: too many errors
? github.com/carvalhorr/protoc-gen-mock/bootstrap [no test files]
FAIL github.com/carvalhorr/protoc-gen-mock/grpchandler [build failed]
FAIL github.com/carvalhorr/protoc-gen-mock/restcontrollers [build failed]
--- FAIL: TestJsonString_Matches_TwoEqualJsonStrings (0.00s)
panic: runtime error: comparing uncomparable type map[string]interface {} [recovered]
panic: runtime error: comparing uncomparable type map[string]interface {}
goroutine 19 [running]:
testing.tRunner.func1(0xc000152100)
/usr/local/go/src/testing/testing.go:874 +0x3a3
panic(0x85a540, 0xc00008f3d0)
/usr/local/go/src/runtime/panic.go:679 +0x1b2
github.com/carvalhorr/protoc-gen-mock/stub.jsonStringMatches(0xc000093710, 0xc000093770, 0x0, 0x8274e0)
/mock/stub/model.go:162 +0x2db
github.com/carvalhorr/protoc-gen-mock/stub.(*JsonString).Matches(0xc000046f50, 0x8e9601, 0x32, 0x5efaed9d)
/mock/stub/model.go:95 +0x12c
github.com/carvalhorr/protoc-gen-mock/stub.TestJsonString_Matches_TwoEqualJsonStrings(0xc000152100)
/mock/stub/model_test.go:11 +0x4e
testing.tRunner(0xc000152100, 0x8f2db0)
/usr/local/go/src/testing/testing.go:909 +0xc9
created by testing.(*T).Run
/usr/local/go/src/testing/testing.go:960 +0x350
FAIL github.com/carvalhorr/protoc-gen-mock/stub 0.005s
Stub an empty request
Is it possible to stub the following rpc with an empty request? If so how?
service Greeter {
rpc Hello(Request) returns (Response) {}
}
message Request {}
message Response {
string greeting = 1;
}
Thank you
Improve stub.request.metadata multiple values
Currently, multiple values are for a metadata field are added by separating them by comma. It is proposed to use a list so that each individual value can also contain commas.
requests with slices
It panics with runtime error: comparing uncomparable type
if the request has a slice. Example
cannot use make(map[string]string, 0) (type map[string]string) as type map[string]string in field value
Error occurs on Mac, go 1.14, grpc-gen-mock 1.0.1
Error is in the generated initialization of the example payload in GetPayloadExamples() on the field Stub.Request.Metadata
Generate Dockerfile
Add functionality to generate a Dockerfile that will start a mock service from the generated code.
Automate testing
multiple grpc services
Is it possible to mock multiple services with a single server? It looks like bootstrap used to accept a slice of services to mock, but that was changed in this commit. Did this functionality move somewhere else? This was one reason I really like this module.
The readme example still has the slice version of the API.
Thanks
Using enum value in stub prevents the http server to persist the stub
Since enum is encoded as string in json, the element will be a string.
Direct conversion between EnumValueDescriptor and String is not sufficient,so this requires extracting the enum value descriptor's Name() and unboxing the custom name
type back to string.
https://github.com/carvalhorr/protoc-gen-mock/blob/master/stub/validation.go#L93
if string(value.Name()) == element {
forget to import "fmt"
eg:
func _MemberDataAPI_MultiUserNumbersByMobiles_MockHandler(srv interface{}, stream grpc.ServerStream) error {
// Mock not implemented for streaming
return fmt.Errorf("mock not implemented for streaming")
}
the generated mock.pb.go forget to import "fmt"
Augment error spec with version
The import will retrieve the latest version of the api
Feature Request - Optionally generate random responses for requests without existing mocks
I would like the generated server to optionally generate a random response for requests which don't match a stub. Ideally it would also write out the stub with the request/response information. This would be very helpful for initially creating the stubs. It might also be useful in and of itself for testing. Great go package. Thank you very much
Implement partial matching of stubs
stub.request.match
supports two values: partial
or exact
. At the moment only exact match is implemented.
We would like to check if a request matches any existing stubs using only some of the fields.
Add validation error details during stub creation
Check if the error details can be marshaled to the error type defined in the specification.
Errors are already validated, except error details.
no response found when the request contains enum values
request contains []int will panic
request like this:
"request": { "match": "partial", "content": { "numbers": [1001, 1002, 1101] } }
numbers type is []int64, and then call with grpc mock server will panic.
Not allow to add stub with more than one of the fields in a oneof set
ex.:
service Greeter {
rpc Hello(Request) returns (Response) {}
}
message Request {
string name = 1;
repeated Request req = 2;
oneof optional {
string option1 = 3;
string option2 = 4;
}
}
message Response {
string greeting = 1;
}
It is allowing to add a stub with both option1
and option2
set.
EXPECTED:
It should return a validation error when adding a stub with both fields set.
Add a Client to the REST API
Add a client to manage stubs without interacting with the REST API directly. The client will expose an interface to create stubs.
m := greetservice.NewGreeterRemoteMockClient("127.0.0.1", 1068)
md := metadata.Pairs("key1", "value1", "key2", "value2")
ctx := metadata.NewOutgoingContext(context.Background(), md)
m.OnHello(ctx, &greetservice.Request{Name: "Rodrigo"}).
Return(&greetservice.Response{Greeting: "Hello, Rodrigo."})
m.OnHello(ctx, &greetservice.Request{Name: "Carvalho"}).
Error(codes.NotFound, "Not found")
m.Clear()
Clean up tmp folder on shutdown
Add support for `proto3` optional
Currently the experimental optional
keyword is not supported, it would be awesome if it was handled, even if it is behind an experimental flag. Other than that, great tool!
Add support for mocking streaming requests/responses
stack overflow by recursive struct
eg:
message Stage {
int64 id = 1;
int64 pid = 2;
repeated Stage children = 3;
}
Unable to stub method calls with messages containing repeated fields
When trying to mock a call that includes repeated fields, the stub validation is failing and causing a silent failure. Outwardly, the call to add stub appears to have succeeded, however the mocked service will not return stub when called.
Actual behavior:
- Call to create stub appears to complete successfully,
- When mocked service is called for a matched stub, the stub isn't found.
Expected behavior:
- Stub is created successfully
- When mocked service is called for a matched stub, the stub is properly returned.
We have a PR ready for your review that addresses this issue.
issue with repeated
Hi ,
I've tried your mock with a repeated field:
syntax = "proto3";
package carvalhorr.greeter;
option go_package = ".;greeter_service";
service Greeter {
rpc Hello(Request) returns (Response) {}
}
message Request {
string name = 1;
repeated ABC abc = 2;
message ABC {
string key = 1;
}
}
message Response {
string greeting = 1;
}
But I get the following error:
panic: runtime error: comparing uncomparable type []interface {}
goroutine 68 [running]:
github.com/carvalhorr/protoc-gen-mock/stub.jsonStringMatches(0xc000255020, 0xc000255080, 0x1, 0x459ad00)
/Users/jfgosse/golang/src/github.com/carvalhorr/protoc-gen-mock/stub/model.go:123 +0x287
github.com/carvalhorr/protoc-gen-mock/stub.(*JsonString).Equals(0xc000207930, 0xc00002e1b0, 0x28, 0xc000207700)
/Users/jfgosse/golang/src/github.com/carvalhorr/protoc-gen-mock/stub/model.go:103 +0x12c
github.com/carvalhorr/protoc-gen-mock/stub.(*stubsMatcher).Match(0xc0001906b0, 0x4780840, 0xc000254f00, 0x46b3597, 0x21, 0xc00002e1b0, 0x28, 0x0)
/Users/jfgosse/golang/src/github.com/carvalhorr/protoc-gen-mock/stub/matcher.go:35 +0x1c1
github.com/carvalhorr/protoc-gen-mock/grpchandler.glob..func1(0x4780840, 0xc000254f00, 0x47708c0, 0xc0001906b0, 0x46b3597, 0x21, 0x463aac0, 0xc00026ebe0, 0x4627680, 0xc00007c580, ...)
/Users/jfgosse/golang/src/github.com/carvalhorr/protoc-gen-mock/grpchandler/handler.go:19 +0xcc
greeter-service._Greeter_Hello_MockHandler(0x46458c0, 0xc0000b5200, 0x4780840, 0xc000254f00, 0xc0000324e0, 0x0, 0x4780840, 0xc000254f00, 0xc00002a490, 0x10)
/Users/jfgosse/golang/src/greeter-service/greeter.mock.pb.go:102 +0x10e
google.golang.org/grpc.(*Server).processUnaryRPC(0xc0001fc1c0, 0x4787e40, 0xc000272900, 0xc0002a2100, 0xc0001613e0, 0x4b742e0, 0x0, 0x0, 0x0)
/Users/jfgosse/golang/src/google.golang.org/grpc/server.go:1171 +0x4fd
google.golang.org/grpc.(*Server).handleStream(0xc0001fc1c0, 0x4787e40, 0xc000272900, 0xc0002a2100, 0x0)
/Users/jfgosse/golang/src/google.golang.org/grpc/server.go:1494 +0xd25
google.golang.org/grpc.(*Server).serveStreams.func1.2(0xc00002a384, 0xc0001fc1c0, 0x4787e40, 0xc000272900, 0xc0002a2100)
/Users/jfgosse/golang/src/google.golang.org/grpc/server.go:834 +0xbb
created by google.golang.org/grpc.(*Server).serveStreams.func1
/Users/jfgosse/golang/src/google.golang.org/grpc/server.go:832 +0x204
oneof field is ignored
I did a test with the following proto:
service Greeter {
rpc Hello(Request) returns(Response) {}
}
message Request {
string name = 1;
oneof input {
string first = 2;
string last = 3;
}
}
message Response { string greeting = 1; }
If I push the following stubs:
{
"fullMethod": "/carvalhorr.greeter.Greeter/Hello",
"request": {
"match": "partial",
"content": {
"last": "Smith"
}
},
"response": {
"type": "success",
"content": {
"greeting": "Hello, John"
}
}
}
I get this error:
"errors": [
"Field 'request.content.last' does not exist"
],
I think you skip the oneof when you parse the proto.
protoc-gen-mock/stub/validation.go
Line 64 in 791aff6
Thanks
Add support for recording of gRPC communication
The idea is to set up the server as a proxy and record the communication that passes through it and use the recordings later as stubs.
Unit test
Initial Target: 90% code coverage
Allow recordings of duplicate stubs
Currently recordings and stubs stores share the same underlying implementation. Since the store was designed to support storing stubs it does not allow duplicate stubs (a stub is a duplicate if it has the same request and metadata).
When storing recordings it is needed to store duplicates.
Investigate if it is possible to record typed error responses
Investigate how to record typed error responses.
- Are all error responses of type google.rpc.Status?
- How to find out the type of
google.rpc.Status.Details
?
Augment error mocking with custom error definitions from local file system
Augment error spec with replace
Exact matching stubs take precedence over partial matching stubs
When there are partial matching stubs and exact matching stubs, the exact match should take precedence. Currently the first match in the order they are added is returned.
Add ability to load stubs from file at server startup
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. ๐๐๐
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google โค๏ธ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.