forked from infobloxopen/atlas-app-toolkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherrors_test.go
143 lines (112 loc) · 3.83 KB
/
errors_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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package gateway
import (
"context"
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"reflect"
"testing"
"github.com/infobloxopen/atlas-app-toolkit/errors"
"github.com/grpc-ecosystem/grpc-gateway/runtime"
"google.golang.org/genproto/googleapis/rpc/code"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
func TestProtoMessageErrorHandlerUnknownCode(t *testing.T) {
err := fmt.Errorf("simple text error")
v := new(RestError)
rw := httptest.NewRecorder()
ProtoMessageErrorHandler(context.Background(), nil, &runtime.JSONBuiltin{}, rw, nil, err)
if ct := rw.Header().Get("Content-Type"); ct != "application/json" {
t.Errorf("invalid content-type: %s - expected: %s", ct, "application/json")
}
if rw.Code != http.StatusInternalServerError {
t.Errorf("invalid http status code: %d - expected: %d", rw.Code, http.StatusInternalServerError)
}
if err := json.Unmarshal(rw.Body.Bytes(), v); err != nil {
t.Fatalf("failed to unmarshal response: %s", err)
}
if v.Status.HTTPStatus != http.StatusInternalServerError {
t.Errorf("invalid http status: %d", v.Status.HTTPStatus)
}
if v.Status.Code != code.Code_UNKNOWN.String() {
t.Errorf("invalid code: %s", v.Status.Code)
}
if v.Status.Message != "simple text error" {
t.Errorf("invalid message: %s", v.Status.Message)
}
}
func TestProtoMessageErrorHandlerUnimplementedCode(t *testing.T) {
err := status.Error(codes.Unimplemented, "service not implemented")
v := new(RestError)
rw := httptest.NewRecorder()
ProtoMessageErrorHandler(context.Background(), nil, &runtime.JSONBuiltin{}, rw, nil, err)
if ct := rw.Header().Get("Content-Type"); ct != "application/json" {
t.Errorf("invalid content-type: %s - expected: %s", ct, "application/json")
}
if rw.Code != http.StatusNotImplemented {
t.Errorf("invalid status code: %d - expected: %d", rw.Code, http.StatusNotImplemented)
}
if err := json.Unmarshal(rw.Body.Bytes(), v); err != nil {
t.Fatalf("failed to unmarshal response: %s", err)
}
if v.Status.HTTPStatus != http.StatusNotImplemented {
t.Errorf("invalid http status: %d", v.Status.HTTPStatus)
}
if v.Status.Code != "NOT_IMPLEMENTED" {
t.Errorf("invalid code: %s", v.Status.Code)
}
if v.Status.Message != "service not implemented" {
t.Errorf("invalid message: %s", v.Status.Message)
}
}
func TestWriteErrorContainer(t *testing.T) {
err := errors.
NewContainer(codes.InvalidArgument, "Invalid 'x' value.").
WithDetail(codes.InvalidArgument, "resource", "x could be one of.").
WithDetail(codes.AlreadyExists, "resource", "x btw already exists.").
WithField("x", "Check correct value of 'x'.")
v := new(RestError)
rw := httptest.NewRecorder()
ProtoMessageErrorHandler(context.Background(), nil, &runtime.JSONBuiltin{}, rw, nil, err)
if err := json.Unmarshal(rw.Body.Bytes(), v); err != nil {
t.Fatalf("failed to unmarshal response: %s", err)
}
if v.Status.HTTPStatus != http.StatusBadRequest {
t.Errorf("invalid http status: %d", v.Status.HTTPStatus)
}
if v.Status.Code != "INVALID_ARGUMENT" {
t.Errorf("invalid code: %s", v.Status.Code)
}
if v.Status.Message != "Invalid 'x' value." {
t.Errorf("invalid message: %s", v.Status.Message)
}
if len(v.Details) != 2 {
t.Errorf("invalid details length: %d", len(v.Details))
}
details := []interface{}{
map[string]interface{}{
"code": "INVALID_ARGUMENT",
"target": "resource",
"message": "x could be one of.",
},
map[string]interface{}{
"code": "ALREADY_EXISTS",
"target": "resource",
"message": "x btw already exists.",
},
}
if !reflect.DeepEqual(
v.Details,
details,
) {
t.Errorf("invalid details value: %v", v.Details)
}
fields := map[string][]string{
"x": []string{"Check correct value of 'x'."}}
vMap := v.Fields.(map[string]interface{})
if vMap["x"].([]interface{})[0] != fields["x"][0] {
t.Errorf("invalid fields value: %v", v.Fields)
}
}