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 70
|
package codegenerator_test
import (
"bytes"
"errors"
"io"
"strings"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/grpc-ecosystem/grpc-gateway/v2/internal/codegenerator"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/testing/protocmp"
"google.golang.org/protobuf/types/pluginpb"
)
var parseReqTests = []struct {
name string
in io.Reader
out *pluginpb.CodeGeneratorRequest
expectErr bool
}{
{
"Empty input should produce empty output",
mustGetReader(&pluginpb.CodeGeneratorRequest{}),
&pluginpb.CodeGeneratorRequest{},
false,
},
{
"Invalid reader should produce error",
&invalidReader{},
nil,
true,
},
{
"Invalid proto message should produce error",
strings.NewReader("{}"),
nil,
true,
},
}
func TestParseRequest(t *testing.T) {
for _, tt := range parseReqTests {
t.Run(tt.name, func(t *testing.T) {
out, err := codegenerator.ParseRequest(tt.in)
if tt.expectErr && err == nil {
t.Error("did not error as expected")
}
if diff := cmp.Diff(out, tt.out, protocmp.Transform()); diff != "" {
t.Errorf(diff)
}
})
}
}
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, errors.New("invalid reader")
}
|