forked from infobloxopen/atlas-app-toolkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
errors_test.go
167 lines (136 loc) · 4.92 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package gateway
import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"reflect"
"testing"
"github.com/infobloxopen/atlas-app-toolkit/errors"
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/status"
)
func TestProtoMessageErrorHandlerUnknownCode(t *testing.T) {
err := fmt.Errorf("simple text error")
v := &RestErrs{}
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.Error[0]["message"] != "simple text error" {
t.Errorf("invalid message: %s", v.Error[0]["message"])
}
}
func TestProtoMessageErrorHandlerWithDetails(t *testing.T) {
IncludeStatusDetails(true)
defer IncludeStatusDetails(false)
err := status.Error(codes.NotFound, "overridden error")
v := &RestErrs{}
b := &bytes.Buffer{}
enc := json.NewEncoder(b)
enc.Encode(map[string]interface{}{"bar": 2})
md := runtime.ServerMetadata{
HeaderMD: metadata.Pairs(
// "status-code", CodeName(codes.NotFound),
),
TrailerMD: metadata.Pairs(
"error", "message:err message",
"error", fmt.Sprintf("fields:%q", string(b.Bytes()))),
}
ctx := runtime.NewServerMetadataContext(context.Background(), md)
rw := httptest.NewRecorder()
ProtoMessageErrorHandler(ctx, 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.StatusNotFound {
t.Errorf("invalid http status code: %d - expected: %d", rw.Code, http.StatusNotFound)
}
if err := json.Unmarshal(rw.Body.Bytes(), v); err != nil {
t.Fatalf("failed to unmarshal response: %s", err)
}
if v.Error[0]["message"] != "err message" {
t.Errorf("invalid message: %s", v.Error[0]["message"])
}
if v.Error[0]["bar"].(float64) != 2 {
t.Errorf("invalid bar field: %d", v.Error[0]["bar"])
}
if v.Error[0]["status"] != CodeName(codes.NotFound) {
t.Errorf("invalid status name: %s - expected: %s", v.Error[0]["status"], CodeName(codes.NotFound))
}
if v.Error[0]["code"].(float64) != http.StatusNotFound {
t.Errorf("invalid status code: %d - expected: %d", v.Error[0]["code"], http.StatusNotFound)
}
}
func TestProtoMessageErrorHandlerUnimplementedCode(t *testing.T) {
err := status.Error(codes.Unimplemented, "service not implemented")
v := new(RestErrs)
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.Error[0]["message"] != "service not implemented" {
t.Errorf("invalid message: %s", v.Error[0]["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(RestErrs)
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.Error[0]["message"] != "Invalid 'x' value." {
t.Errorf("invalid message: %s", v.Error[0]["message"])
}
if len(v.Error[0]["details"].([]interface{})) != 2 {
t.Errorf("invalid details length: %d", len(v.Error[0]["details"].([]interface{})))
}
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.Error[0]["details"],
details,
) {
t.Errorf("invalid details value: %v", v.Error[0]["details"])
}
fields := map[string][]string{
"x": []string{"Check correct value of 'x'."}}
vMap := v.Error[0]["fields"].(map[string]interface{})
if vMap["x"].([]interface{})[0] != fields["x"][0] {
t.Errorf("invalid fields value: %v", v.Error[0]["fields"])
}
}