Skip to content

Commit

Permalink
integration_test: port error status test to proto_error_test.go
Browse files Browse the repository at this point in the history
Signed-off-by: Stephan Renatus <srenatus@chef.io>
  • Loading branch information
srenatus committed Apr 28, 2020
1 parent 1f689cb commit 2aaabe5
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 74 deletions.
2 changes: 2 additions & 0 deletions examples/internal/integration/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ go_test(
"@com_github_golang_protobuf//descriptor:go_default_library_gen",
"@com_github_golang_protobuf//jsonpb:go_default_library_gen",
"@com_github_golang_protobuf//proto:go_default_library",
"@com_github_golang_protobuf//ptypes:go_default_library_gen",
"@com_github_google_go_cmp//cmp:go_default_library",
"@go_googleapis//google/rpc:errdetails_go_proto",
"@go_googleapis//google/rpc:status_go_proto",
"@io_bazel_rules_go//proto/wkt:empty_go_proto",
"@io_bazel_rules_go//proto/wkt:field_mask_go_proto",
Expand Down
78 changes: 4 additions & 74 deletions examples/internal/integration/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,12 @@ import (
"github.com/grpc-ecosystem/grpc-gateway/v2/examples/internal/proto/pathenum"
"github.com/grpc-ecosystem/grpc-gateway/v2/examples/internal/proto/sub"
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
spb "google.golang.org/genproto/googleapis/rpc/status"
"google.golang.org/genproto/protobuf/field_mask"
"google.golang.org/grpc/codes"
"google.golang.org/protobuf/testing/protocmp"
)

type errorBody struct {
Message string `json:"message"`
Code int `json:"code"`
Details []interface{} `json:"details"`
}

func TestEcho(t *testing.T) {
if testing.Short() {
t.Skip()
Expand Down Expand Up @@ -616,7 +611,7 @@ func testABEBulkCreateWithError(t *testing.T, port int) {
t.Logf("%s", buf)
}

var msg errorBody
var msg spb.Status
if err := json.Unmarshal(buf, &msg); err != nil {
t.Fatalf("json.Unmarshal(%s, &msg) failed with %v; want success", buf, err)
}
Expand Down Expand Up @@ -907,13 +902,13 @@ func testABELookupNotFound(t *testing.T, port int) {
return
}

var msg errorBody
var msg spb.Status
if err := json.Unmarshal(buf, &msg); err != nil {
t.Errorf("json.Unmarshal(%s, &msg) failed with %v; want success", buf, err)
return
}

if got, want := msg.Code, int(codes.NotFound); got != want {
if got, want := msg.Code, int32(codes.NotFound); got != want {
t.Errorf("msg.Code = %d; want %d", got, want)
return
}
Expand Down Expand Up @@ -1310,71 +1305,6 @@ func TestTimeout(t *testing.T) {
}
}

func TestErrorWithDetails(t *testing.T) {
if testing.Short() {
t.Skip()
return
}

apiURL := "http://localhost:8088/v2/example/errorwithdetails"
resp, err := http.Get(apiURL)
if err != nil {
t.Errorf("http.Get(%q) failed with %v; want success", apiURL, err)
return
}
defer resp.Body.Close()

buf, err := ioutil.ReadAll(resp.Body)
if err != nil {
t.Fatalf("ioutil.ReadAll(resp.Body) failed with %v; want success", err)
}

if got, want := resp.StatusCode, http.StatusInternalServerError; got != want {
t.Errorf("resp.StatusCode = %d; want %d", got, want)
}

var msg errorBody
if err := json.Unmarshal(buf, &msg); err != nil {
t.Fatalf("json.Unmarshal(%s, &msg) failed with %v; want success", buf, err)
}

if got, want := msg.Code, int(codes.Unknown); got != want {
t.Errorf("msg.Code = %d; want %d", got, want)
}
if got, want := msg.Message, "with details"; got != want {
t.Errorf("msg.Message = %s; want %s", got, want)
}
if got, want := len(msg.Details), 1; got != want {
t.Fatalf("len(msg.Details) = %q; want %q", got, want)
}

details, ok := msg.Details[0].(map[string]interface{})
if got, want := ok, true; got != want {
t.Fatalf("msg.Details[0] got type: %T, want %T", msg.Details[0], map[string]interface{}{})
}
typ, ok := details["@type"].(string)
if got, want := ok, true; got != want {
t.Fatalf("msg.Details[0][\"@type\"] got type: %T, want %T", typ, "")
}
if got, want := details["@type"], "type.googleapis.com/google.rpc.DebugInfo"; got != want {
t.Errorf("msg.Details[\"@type\"] = %q; want %q", got, want)
}
if got, want := details["detail"], "error debug details"; got != want {
t.Errorf("msg.Details[\"detail\"] = %q; want %q", got, want)
}
entries, ok := details["stack_entries"].([]interface{})
if got, want := ok, true; got != want {
t.Fatalf("msg.Details[0][\"stack_entries\"] got type: %T, want %T", entries, []string{})
}
entry, ok := entries[0].(string)
if got, want := ok, true; got != want {
t.Fatalf("msg.Details[0][\"stack_entries\"][0] got type: %T, want %T", entry, "")
}
if got, want := entries[0], "foo:1"; got != want {
t.Errorf("msg.Details[\"stack_entries\"][0] = %q; want %q", got, want)
}
}

func TestPostWithEmptyBody(t *testing.T) {
if testing.Short() {
t.Skip()
Expand Down
59 changes: 59 additions & 0 deletions examples/internal/integration/proto_error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ import (
"time"

"github.com/golang/protobuf/jsonpb"
"github.com/golang/protobuf/ptypes"
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
"google.golang.org/genproto/googleapis/rpc/errdetails"
spb "google.golang.org/genproto/googleapis/rpc/status"
"google.golang.org/grpc/codes"
)
Expand Down Expand Up @@ -62,6 +64,7 @@ func TestABEWithProtoErrorHandler(t *testing.T) {
testABEBulkCreate(t, port)
testABELookup(t, port)
testABELookupNotFoundWithProtoError(t, port)
testABELookupNotFoundWithProtoErrorIncludingDetails(t, port)
testABEList(t, port)
testABEBulkEcho(t, port)
testABEBulkEchoZeroLength(t, port)
Expand Down Expand Up @@ -118,6 +121,62 @@ func testABELookupNotFoundWithProtoError(t *testing.T, port uint16) {
}
}

func testABELookupNotFoundWithProtoErrorIncludingDetails(t *testing.T, port uint16) {
uuid := "errorwithdetails"
url := fmt.Sprintf("http://localhost:%d/v2/example/%s", port, uuid)
resp, err := http.Get(url)
if err != nil {
t.Errorf("http.Get(%q) failed with %v; want success", url, err)
return
}
defer resp.Body.Close()

buf, err := ioutil.ReadAll(resp.Body)
if err != nil {
t.Errorf("ioutil.ReadAll(resp.Body) failed with %v; want success", err)
return
}

if got, want := resp.StatusCode, http.StatusInternalServerError; got != want {
t.Errorf("resp.StatusCode = %d; want %d", got, want)
t.Logf("%s", buf)
return
}

var msg spb.Status
if err := jsonpb.UnmarshalString(string(buf), &msg); err != nil {
t.Errorf("jsonpb.UnmarshalString(%s, &msg) failed with %v; want success", buf, err)
return
}

if got, want := msg.Code, int32(codes.Unknown); got != want {
t.Errorf("msg.Code = %d; want %d", got, want)
return
}

if got, want := msg.Message, "with details"; got != want {
t.Errorf("msg.Message = %s; want %s", got, want)
return
}

details := msg.Details
if got, want := len(details), 1; got != want {
t.Fatalf("got %q details, wanted %q", got, want)
}

detail := errdetails.DebugInfo{}
if got, want := ptypes.UnmarshalAny(msg.Details[0], &detail), error(nil); got != want {
t.Errorf("unmarshaling any: got %q, wanted %q", got, want)
}

if got, want := len(detail.StackEntries), 1; got != want {
t.Fatalf("got %d stack entries, expected %d", got, want)
}
if got, want := detail.StackEntries[0], "foo:1"; got != want {
t.Errorf("StackEntries[0]: got %q; want %q", got, want)
}
}

func TestUnknownPathWithProtoError(t *testing.T) {
if testing.Short() {
t.Skip()
Expand Down

0 comments on commit 2aaabe5

Please sign in to comment.