GithubHelp home page GithubHelp logo

protoc-gen-mock's People

Contributors

carvalhorr avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

protoc-gen-mock's Issues

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

requests with slices

It panics with runtime error: comparing uncomparable type if the request has a slice. Example

Generate Dockerfile

Add functionality to generate a Dockerfile that will start a mock service from the generated code.

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

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"

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.

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()

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!

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.

jsonTag := field.Tag.Get("json")

Thanks

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.