1 package gengateway
2
3 import (
4 "errors"
5 "path/filepath"
6 "strings"
7 "testing"
8
9 "github.com/golang/protobuf/proto"
10 protodescriptor "github.com/golang/protobuf/protoc-gen-go/descriptor"
11 "github.com/grpc-ecosystem/grpc-gateway/protoc-gen-grpc-gateway/descriptor"
12 )
13
14 func newExampleFileDescriptor() *descriptor.File {
15 return newExampleFileDescriptorWithGoPkg(
16 &descriptor.GoPackage{
17 Path: "example.com/path/to/example/example.pb",
18 Name: "example_pb",
19 },
20 )
21 }
22
23 func newExampleFileDescriptorWithGoPkg(gp *descriptor.GoPackage) *descriptor.File {
24 msgdesc := &protodescriptor.DescriptorProto{
25 Name: proto.String("ExampleMessage"),
26 }
27 msg := &descriptor.Message{
28 DescriptorProto: msgdesc,
29 }
30 msg1 := &descriptor.Message{
31 DescriptorProto: msgdesc,
32 File: &descriptor.File{
33 GoPkg: descriptor.GoPackage{
34 Path: "github.com/golang/protobuf/ptypes/empty",
35 Name: "empty",
36 },
37 },
38 }
39 meth := &protodescriptor.MethodDescriptorProto{
40 Name: proto.String("Example"),
41 InputType: proto.String("ExampleMessage"),
42 OutputType: proto.String("ExampleMessage"),
43 }
44 meth1 := &protodescriptor.MethodDescriptorProto{
45 Name: proto.String("ExampleWithoutBindings"),
46 InputType: proto.String("empty.Empty"),
47 OutputType: proto.String("empty.Empty"),
48 }
49 svc := &protodescriptor.ServiceDescriptorProto{
50 Name: proto.String("ExampleService"),
51 Method: []*protodescriptor.MethodDescriptorProto{meth, meth1},
52 }
53 return &descriptor.File{
54 FileDescriptorProto: &protodescriptor.FileDescriptorProto{
55 Name: proto.String("example.proto"),
56 Package: proto.String("example"),
57 Dependency: []string{"a.example/b/c.proto", "a.example/d/e.proto"},
58 MessageType: []*protodescriptor.DescriptorProto{msgdesc},
59 Service: []*protodescriptor.ServiceDescriptorProto{svc},
60 },
61 GoPkg: *gp,
62 Messages: []*descriptor.Message{msg},
63 Services: []*descriptor.Service{
64 {
65 ServiceDescriptorProto: svc,
66 Methods: []*descriptor.Method{
67 {
68 MethodDescriptorProto: meth,
69 RequestType: msg,
70 ResponseType: msg,
71 Bindings: []*descriptor.Binding{
72 {
73 HTTPMethod: "GET",
74 Body: &descriptor.Body{FieldPath: nil},
75 },
76 },
77 },
78 {
79 MethodDescriptorProto: meth1,
80 RequestType: msg1,
81 ResponseType: msg1,
82 },
83 },
84 },
85 },
86 }
87 }
88
89 func TestGenerateServiceWithoutBindings(t *testing.T) {
90 file := newExampleFileDescriptor()
91 g := &generator{}
92 got, err := g.generate(crossLinkFixture(file))
93 if err != nil {
94 t.Errorf("generate(%#v) failed with %v; want success", file, err)
95 return
96 }
97 if notwanted := `"github.com/golang/protobuf/ptypes/empty"`; strings.Contains(got, notwanted) {
98 t.Errorf("generate(%#v) = %s; does not want to contain %s", file, got, notwanted)
99 }
100 }
101
102 func TestGenerateOutputPath(t *testing.T) {
103 cases := []struct {
104 file *descriptor.File
105 pathType pathType
106 modulePath string
107 expected string
108 expectedError error
109 }{
110 {
111 file: newExampleFileDescriptorWithGoPkg(
112 &descriptor.GoPackage{
113 Path: "example.com/path/to/example",
114 Name: "example_pb",
115 },
116 ),
117 expected: "example.com/path/to/example",
118 },
119 {
120 file: newExampleFileDescriptorWithGoPkg(
121 &descriptor.GoPackage{
122 Path: "example",
123 Name: "example_pb",
124 },
125 ),
126 expected: "example",
127 },
128 {
129 file: newExampleFileDescriptorWithGoPkg(
130 &descriptor.GoPackage{
131 Path: "example.com/path/to/example",
132 Name: "example_pb",
133 },
134 ),
135 pathType: pathTypeSourceRelative,
136 expected: ".",
137 },
138 {
139 file: newExampleFileDescriptorWithGoPkg(
140 &descriptor.GoPackage{
141 Path: "example",
142 Name: "example_pb",
143 },
144 ),
145 pathType: pathTypeSourceRelative,
146 expected: ".",
147 },
148 {
149 file: newExampleFileDescriptorWithGoPkg(
150 &descriptor.GoPackage{
151 Path: "example.com/path/root",
152 Name: "example_pb",
153 },
154 ),
155 modulePath: "example.com/path/root",
156 expected: ".",
157 },
158 {
159 file: newExampleFileDescriptorWithGoPkg(
160 &descriptor.GoPackage{
161 Path: "example.com/path/to/example",
162 Name: "example_pb",
163 },
164 ),
165 modulePath: "example.com/path/to",
166 expected: "example",
167 },
168 {
169 file: newExampleFileDescriptorWithGoPkg(
170 &descriptor.GoPackage{
171 Path: "example.com/path/to/example/with/many/nested/paths",
172 Name: "example_pb",
173 },
174 ),
175 modulePath: "example.com/path/to",
176 expected: "example/with/many/nested/paths",
177 },
178
179
180 {
181 file: newExampleFileDescriptorWithGoPkg(
182 &descriptor.GoPackage{
183 Path: "example.com/path/root",
184 Name: "example_pb",
185 },
186 ),
187 modulePath: "example.com/path/root",
188 pathType: pathTypeSourceRelative,
189 expectedError: errors.New("cannot use module= with paths="),
190 },
191 {
192 file: newExampleFileDescriptorWithGoPkg(
193 &descriptor.GoPackage{
194 Path: "example.com/path/rootextra",
195 Name: "example_pb",
196 },
197 ),
198 modulePath: "example.com/path/root",
199 expectedError: errors.New("example.com/path/rootextra: file go path does not match module prefix: example.com/path/root/"),
200 },
201 }
202
203 for _, c := range cases {
204 g := &generator{
205 pathType: c.pathType,
206 modulePath: c.modulePath,
207 }
208
209 file := c.file
210 gots, err := g.Generate([]*descriptor.File{crossLinkFixture(file)})
211
212
213 if c.expectedError != nil {
214 if err == nil || err.Error() != c.expectedError.Error() {
215 t.Errorf("Generate(%#v) failed with %v; wants error of: %v", file, err, c.expectedError)
216 }
217 return
218 }
219
220
221 if err != nil {
222 t.Errorf("Generate(%#v) failed with %v; wants success", file, err)
223 return
224 }
225
226 if len(gots) != 1 {
227 t.Errorf("Generate(%#v) failed; expects on result got %d", file, len(gots))
228 return
229 }
230
231 got := gots[0]
232 if got.Name == nil {
233 t.Errorf("Generate(%#v) failed; expects non-nil Name(%v)", file, got.Name)
234 return
235 }
236
237 gotPath := filepath.Dir(*got.Name)
238 expectedPath := c.expected
239 if gotPath != expectedPath {
240 t.Errorf("Generate(%#v) failed; got path: %s expected path: %s", file, gotPath, expectedPath)
241 return
242 }
243 }
244 }
245
View as plain text