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