added formatter and tests
This commit is contained in:
parent
8e2cf9b3c2
commit
c35039ecde
|
|
@ -109,6 +109,8 @@ var (
|
|||
will accept. If not specified, defaults to 4,194,304 (4 megabytes).`))
|
||||
emitDefaults = flags.Bool("emit-defaults", false, prettify(`
|
||||
Emit default values for JSON-encoded responses.`))
|
||||
jsonError = flags.Bool("json-error", false, prettify(`
|
||||
Emit error response as JSON.`))
|
||||
protosetOut = flags.String("protoset-out", "", prettify(`
|
||||
The name of a file to be written that will contain a FileDescriptorSet
|
||||
proto. With the list and describe verbs, the listed or described
|
||||
|
|
@ -652,7 +654,11 @@ func main() {
|
|||
fmt.Printf("Sent %d request%s and received %d response%s\n", reqCount, reqSuffix, h.NumResponses, respSuffix)
|
||||
}
|
||||
if h.Status.Code() != codes.OK {
|
||||
grpcurl.PrintStatus(os.Stderr, h.Status, formatter)
|
||||
if *jsonError {
|
||||
grpcurl.PrintJSONStatus(os.Stderr, h.Status)
|
||||
} else {
|
||||
grpcurl.PrintStatus(os.Stderr, h.Status, formatter)
|
||||
}
|
||||
exit(1)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -467,3 +467,12 @@ func PrintStatus(w io.Writer, stat *status.Status, formatter Formatter) {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
// PrintJSONStatus returns the grpc status response as a JSON object
|
||||
func PrintJSONStatus(w io.Writer, stat *status.Status) {
|
||||
jsonStatus, err := (&jsonpb.Marshaler{}).MarshalToString(stat.Proto())
|
||||
if err != nil {
|
||||
fmt.Fprintf(w, "ERROR: %v", err.Error())
|
||||
}
|
||||
fmt.Fprint(w, jsonStatus)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -11,7 +11,9 @@ import (
|
|||
"github.com/golang/protobuf/proto"
|
||||
"github.com/golang/protobuf/ptypes/struct"
|
||||
"github.com/jhump/protoreflect/desc"
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/metadata"
|
||||
"google.golang.org/grpc/status"
|
||||
)
|
||||
|
||||
func TestRequestParser(t *testing.T) {
|
||||
|
|
@ -173,6 +175,25 @@ func TestHandler(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestPrintJSONStatus(t *testing.T) {
|
||||
testCases := []struct {
|
||||
input *status.Status
|
||||
expectedOutput string
|
||||
}{{
|
||||
input: status.New(codes.InvalidArgument, "Missing Argument"),
|
||||
expectedOutput: statusAsJSON,
|
||||
}}
|
||||
|
||||
for _, tc := range testCases {
|
||||
var b bytes.Buffer
|
||||
PrintJSONStatus(&b, tc.input)
|
||||
got := b.String()
|
||||
if !compare(tc.expectedOutput, got) {
|
||||
t.Errorf("Incorrect output. Expected:\n%s\nGot:\n%s", tc.expectedOutput, got)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// compare checks that actual and expected are equal, returning true if so.
|
||||
// A simple equality check (==) does not suffice because jsonpb formats
|
||||
// structpb.Value strangely. So if that formatting gets fixed, we don't
|
||||
|
|
@ -244,6 +265,7 @@ Response contents:
|
|||
"null": null
|
||||
}
|
||||
`
|
||||
statusAsJSON = `{"code":3,"message":"Missing Argument"} `
|
||||
messageAsText = `struct_value: <
|
||||
fields: <
|
||||
key: "bar"
|
||||
|
|
|
|||
Loading…
Reference in New Issue