forked from grpc-ecosystem/grpc-gateway
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproto_error_test.go
222 lines (188 loc) · 5.58 KB
/
proto_error_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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
package integration_test
import (
"context"
"fmt"
"io/ioutil"
"net/http"
"strings"
"testing"
"time"
"github.com/golang/protobuf/jsonpb"
"github.com/grpc-ecosystem/grpc-gateway/runtime"
spb "google.golang.org/genproto/googleapis/rpc/status"
"google.golang.org/grpc/codes"
)
func runServer(ctx context.Context, t *testing.T, port uint16) {
opt := runtime.WithProtoErrorHandler(runtime.DefaultHTTPProtoErrorHandler)
if err := runGateway(ctx, fmt.Sprintf(":%d", port), opt); err != nil {
t.Errorf("runGateway() failed with %v; want success", err)
}
}
func TestWithProtoErrorHandler(t *testing.T) {
if testing.Short() {
t.Skip()
return
}
ctx := context.Background()
ctx, cancel := context.WithCancel(ctx)
defer cancel()
const port = 8082
go runServer(ctx, t, port)
if err := waitForGateway(ctx, 8082); err != nil {
t.Errorf("waitForGateway(ctx, 8082) failed with %v; want success", err)
}
testEcho(t, port, "application/json")
testEchoBody(t, port)
}
func TestABEWithProtoErrorHandler(t *testing.T) {
if testing.Short() {
t.Skip()
return
}
ctx := context.Background()
ctx, cancel := context.WithCancel(ctx)
defer cancel()
const port = 8083
go runServer(ctx, t, port)
if err := waitForGateway(ctx, 8083); err != nil {
t.Errorf("waitForGateway(ctx, 8083) failed with %v; want success", err)
}
testABECreate(t, port)
testABECreateBody(t, port)
testABEBulkCreate(t, port)
testABELookup(t, port)
testABELookupNotFoundWithProtoError(t, port)
testABEList(t, port)
testABEBulkEcho(t, port)
testABEBulkEchoZeroLength(t, port)
testAdditionalBindings(t, port)
}
func testABELookupNotFoundWithProtoError(t *testing.T, port uint16) {
url := fmt.Sprintf("http://localhost:%d/v1/example/a_bit_of_everything", port)
uuid := "not_exist"
url = fmt.Sprintf("%s/%s", url, 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.StatusNotFound; 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.NotFound); got != want {
t.Errorf("msg.Code = %d; want %d", got, want)
return
}
if got, want := msg.Message, "not found"; got != want {
t.Errorf("msg.Message = %s; want %s", got, want)
return
}
if got, want := resp.Header.Get("Grpc-Metadata-Uuid"), uuid; got != want {
t.Errorf("Grpc-Metadata-Uuid was %s, wanted %s", got, want)
}
if got, want := resp.Trailer.Get("Grpc-Trailer-Foo"), "foo2"; got != want {
t.Errorf("Grpc-Trailer-Foo was %q, wanted %q", got, want)
}
if got, want := resp.Trailer.Get("Grpc-Trailer-Bar"), "bar2"; got != want {
t.Errorf("Grpc-Trailer-Bar was %q, wanted %q", got, want)
}
}
func TestUnknownPathWithProtoError(t *testing.T) {
if testing.Short() {
t.Skip()
return
}
ctx := context.Background()
ctx, cancel := context.WithCancel(ctx)
defer cancel()
const port = 8084
go runServer(ctx, t, port)
if err := waitForGateway(ctx, 8084); err != nil {
t.Errorf("waitForGateway(ctx, 8084) failed with %v; want success", err)
}
url := fmt.Sprintf("http://localhost:%d", port)
resp, err := http.Post(url, "application/json", strings.NewReader("{}"))
if err != nil {
t.Errorf("http.Post(%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.StatusNotImplemented; got != want {
t.Errorf("resp.StatusCode = %d; want %d", got, want)
t.Logf("%s", buf)
}
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.Unimplemented); got != want {
t.Errorf("msg.Code = %d; want %d", got, want)
return
}
if msg.Message == "" {
t.Errorf("msg.Message should not be empty")
return
}
}
func TestMethodNotAllowedWithProtoError(t *testing.T) {
if testing.Short() {
t.Skip()
return
}
ctx := context.Background()
ctx, cancel := context.WithCancel(ctx)
defer cancel()
const port = 8085
go runServer(ctx, t, port)
// Waiting for the server's getting available.
// TODO(yugui) find a better way to wait
time.Sleep(100 * time.Millisecond)
url := fmt.Sprintf("http://localhost:%d/v1/example/echo/myid", port)
resp, err := http.Get(url)
if err != nil {
t.Errorf("http.Post(%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.StatusNotImplemented; got != want {
t.Errorf("resp.StatusCode = %d; want %d", got, want)
t.Logf("%s", buf)
}
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.Unimplemented); got != want {
t.Errorf("msg.Code = %d; want %d", got, want)
return
}
if msg.Message == "" {
t.Errorf("msg.Message should not be empty")
return
}
}