|
| 1 | +package gengateway |
| 2 | + |
| 3 | +import ( |
| 4 | + "strings" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/golang/protobuf/proto" |
| 8 | + protodescriptor "github.com/golang/protobuf/protoc-gen-go/descriptor" |
| 9 | + "github.com/grpc-ecosystem/grpc-gateway/protoc-gen-grpc-gateway/descriptor" |
| 10 | +) |
| 11 | + |
| 12 | +func TestGenerateServiceWithoutBindings(t *testing.T) { |
| 13 | + msgdesc := &protodescriptor.DescriptorProto{ |
| 14 | + Name: proto.String("ExampleMessage"), |
| 15 | + } |
| 16 | + msg := &descriptor.Message{ |
| 17 | + DescriptorProto: msgdesc, |
| 18 | + } |
| 19 | + msg1 := &descriptor.Message{ |
| 20 | + DescriptorProto: msgdesc, |
| 21 | + File: &descriptor.File{ |
| 22 | + GoPkg: descriptor.GoPackage{ |
| 23 | + Path: "github.com/golang/protobuf/ptypes/empty", |
| 24 | + Name: "empty", |
| 25 | + }, |
| 26 | + }, |
| 27 | + } |
| 28 | + meth := &protodescriptor.MethodDescriptorProto{ |
| 29 | + Name: proto.String("Example"), |
| 30 | + InputType: proto.String("ExampleMessage"), |
| 31 | + OutputType: proto.String("ExampleMessage"), |
| 32 | + } |
| 33 | + meth1 := &protodescriptor.MethodDescriptorProto{ |
| 34 | + Name: proto.String("ExampleWithoutBindings"), |
| 35 | + InputType: proto.String("empty.Empty"), |
| 36 | + OutputType: proto.String("empty.Empty"), |
| 37 | + } |
| 38 | + svc := &protodescriptor.ServiceDescriptorProto{ |
| 39 | + Name: proto.String("ExampleService"), |
| 40 | + Method: []*protodescriptor.MethodDescriptorProto{meth, meth1}, |
| 41 | + } |
| 42 | + file := descriptor.File{ |
| 43 | + FileDescriptorProto: &protodescriptor.FileDescriptorProto{ |
| 44 | + Name: proto.String("example.proto"), |
| 45 | + Package: proto.String("example"), |
| 46 | + Dependency: []string{"a.example/b/c.proto", "a.example/d/e.proto"}, |
| 47 | + MessageType: []*protodescriptor.DescriptorProto{msgdesc}, |
| 48 | + Service: []*protodescriptor.ServiceDescriptorProto{svc}, |
| 49 | + }, |
| 50 | + GoPkg: descriptor.GoPackage{ |
| 51 | + Path: "example.com/path/to/example/example.pb", |
| 52 | + Name: "example_pb", |
| 53 | + }, |
| 54 | + Messages: []*descriptor.Message{msg}, |
| 55 | + Services: []*descriptor.Service{ |
| 56 | + { |
| 57 | + ServiceDescriptorProto: svc, |
| 58 | + Methods: []*descriptor.Method{ |
| 59 | + { |
| 60 | + MethodDescriptorProto: meth, |
| 61 | + RequestType: msg, |
| 62 | + ResponseType: msg, |
| 63 | + Bindings: []*descriptor.Binding{ |
| 64 | + { |
| 65 | + HTTPMethod: "GET", |
| 66 | + Body: &descriptor.Body{FieldPath: nil}, |
| 67 | + }, |
| 68 | + }, |
| 69 | + }, |
| 70 | + { |
| 71 | + MethodDescriptorProto: meth1, |
| 72 | + RequestType: msg1, |
| 73 | + ResponseType: msg1, |
| 74 | + }, |
| 75 | + }, |
| 76 | + }, |
| 77 | + }, |
| 78 | + } |
| 79 | + g := &generator{} |
| 80 | + got, err := g.generate(crossLinkFixture(&file)) |
| 81 | + if err != nil { |
| 82 | + t.Errorf("generate(%#v) failed with %v; want success", file, err) |
| 83 | + return |
| 84 | + } |
| 85 | + if notwanted := `"github.com/golang/protobuf/ptypes/empty"`; strings.Contains(got, notwanted) { |
| 86 | + t.Errorf("generate(%#v) = %s; does not want to contain %s", file, got, notwanted) |
| 87 | + } |
| 88 | +} |
0 commit comments