...
1 package codegenerator_test
2
3 import (
4 "bytes"
5 "fmt"
6 "io"
7 "reflect"
8 "strings"
9 "testing"
10
11 "github.com/golang/protobuf/proto"
12 plugin "github.com/golang/protobuf/protoc-gen-go/plugin"
13 "github.com/grpc-ecosystem/grpc-gateway/codegenerator"
14 )
15
16 var parseReqTests = []struct {
17 name string
18 in io.Reader
19 out *plugin.CodeGeneratorRequest
20 err error
21 }{
22 {
23 "Empty input should produce empty output",
24 mustGetReader(&plugin.CodeGeneratorRequest{}),
25 &plugin.CodeGeneratorRequest{},
26 nil,
27 },
28 {
29 "Invalid reader should produce error",
30 &invalidReader{},
31 nil,
32 fmt.Errorf("failed to read code generator request: invalid reader"),
33 },
34 {
35 "Invalid proto message should produce error",
36 strings.NewReader("{}"),
37 nil,
38 fmt.Errorf("failed to unmarshal code generator request: unexpected EOF"),
39 },
40 }
41
42 func TestParseRequest(t *testing.T) {
43 for _, tt := range parseReqTests {
44 t.Run(tt.name, func(t *testing.T) {
45 out, err := codegenerator.ParseRequest(tt.in)
46 if !reflect.DeepEqual(err, tt.err) {
47 t.Errorf("got %v, want %v", err, tt.err)
48 }
49 if err == nil && !reflect.DeepEqual(*out, *tt.out) {
50 t.Errorf("got %v, want %v", *out, *tt.out)
51 }
52 })
53 }
54 }
55
56 func mustGetReader(pb proto.Message) io.Reader {
57 b, err := proto.Marshal(pb)
58 if err != nil {
59 panic(err)
60 }
61 return bytes.NewBuffer(b)
62 }
63
64 type invalidReader struct {
65 }
66
67 func (*invalidReader) Read(p []byte) (int, error) {
68 return 0, fmt.Errorf("invalid reader")
69 }
70
View as plain text