forked from connectrpc/connect-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
interceptor_ext_test.go
243 lines (222 loc) · 7.56 KB
/
interceptor_ext_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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
// Copyright 2021-2022 Buf Technologies, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package connect_test
import (
"context"
"net/http"
"net/http/httptest"
"testing"
"github.com/bufbuild/connect-go"
"github.com/bufbuild/connect-go/internal/assert"
pingv1 "github.com/bufbuild/connect-go/internal/gen/connect/ping/v1"
"github.com/bufbuild/connect-go/internal/gen/connect/ping/v1/pingv1connect"
)
func TestOnionOrderingEndToEnd(t *testing.T) {
t.Parallel()
// Helper function: returns a function that asserts that there's some value
// set for header "expect", and adds a value for header "add".
newInspector := func(expect, add string) func(connect.Spec, http.Header) {
return func(spec connect.Spec, header http.Header) {
if expect != "" {
assert.NotZero(
t,
header.Get(expect),
assert.Sprintf(
"%s (IsClient %v): header %q missing: %v",
spec.Procedure,
spec.IsClient,
expect,
header,
),
)
}
header.Set(add, "v")
}
}
// Helper function: asserts that there's a value present for header keys
// "one", "two", "three", and "four".
assertAllPresent := func(spec connect.Spec, header http.Header) {
for _, key := range []string{"one", "two", "three", "four"} {
assert.NotZero(
t,
header.Get(key),
assert.Sprintf(
"%s (IsClient %v): checking all headers, %q missing: %v",
spec.Procedure,
spec.IsClient,
key,
header,
),
)
}
}
// The client and handler interceptor onions are the meat of the test. The
// order of interceptor execution must be the same for unary and streaming
// procedures.
//
// Requests should fall through the client onion from top to bottom, traverse
// the network, and then fall through the handler onion from top to bottom.
// Responses should climb up the handler onion, traverse the network, and
// then climb up the client onion.
//
// The request and response sides of this onion are numbered to make the
// intended order clear.
clientOnion := connect.WithInterceptors(
newHeaderInterceptor(
// 1 (start). request: should see protocol-related headers
func(_ connect.Spec, h http.Header) {
assert.NotZero(t, h.Get("Content-Type"))
},
// 12 (end). response: check "one"-"four"
assertAllPresent,
),
newHeaderInterceptor(
newInspector("", "one"), // 2. request: add header "one"
newInspector("three", "four"), // 11. response: check "three", add "four"
),
newHeaderInterceptor(
newInspector("one", "two"), // 3. request: check "one", add "two"
newInspector("two", "three"), // 10. response: check "two", add "three"
),
)
handlerOnion := connect.WithInterceptors(
newHeaderInterceptor(
newInspector("two", "three"), // 4. request: check "two", add "three"
newInspector("one", "two"), // 9. response: check "one", add "two"
),
newHeaderInterceptor(
newInspector("three", "four"), // 5. request: check "three", add "four"
newInspector("", "one"), // 8. response: add "one"
),
newHeaderInterceptor(
assertAllPresent, // 6. request: check "one"-"four"
nil, // 7. response: no-op
),
)
mux := http.NewServeMux()
mux.Handle(
pingv1connect.NewPingServiceHandler(
pingServer{},
handlerOnion,
),
)
server := httptest.NewServer(mux)
defer server.Close()
client := pingv1connect.NewPingServiceClient(
server.Client(),
server.URL,
clientOnion,
)
_, err := client.Ping(context.Background(), connect.NewRequest(&pingv1.PingRequest{Number: 10}))
assert.Nil(t, err)
responses, err := client.CountUp(context.Background(), connect.NewRequest(&pingv1.CountUpRequest{Number: 10}))
assert.Nil(t, err)
var sum int64
for responses.Receive() {
sum += responses.Msg().Number
}
assert.Equal(t, sum, 55)
assert.Nil(t, responses.Close())
}
// headerInterceptor makes it easier to write interceptors that inspect or
// mutate HTTP headers. It applies the same logic to unary and streaming
// procedures, wrapping the send or receive side of the stream as appropriate.
//
// It's useful as a testing harness to make sure that we're chaining
// interceptors in the correct order.
type headerInterceptor struct {
inspectRequestHeader func(connect.Spec, http.Header)
inspectResponseHeader func(connect.Spec, http.Header)
}
// newHeaderInterceptor constructs a headerInterceptor. Nil function pointers
// are treated as no-ops.
func newHeaderInterceptor(
inspectRequestHeader func(connect.Spec, http.Header),
inspectResponseHeader func(connect.Spec, http.Header),
) *headerInterceptor {
interceptor := headerInterceptor{
inspectRequestHeader: inspectRequestHeader,
inspectResponseHeader: inspectResponseHeader,
}
if interceptor.inspectRequestHeader == nil {
interceptor.inspectRequestHeader = func(_ connect.Spec, _ http.Header) {}
}
if interceptor.inspectResponseHeader == nil {
interceptor.inspectResponseHeader = func(_ connect.Spec, _ http.Header) {}
}
return &interceptor
}
func (h *headerInterceptor) WrapUnary(next connect.UnaryFunc) connect.UnaryFunc {
return func(ctx context.Context, req connect.AnyRequest) (connect.AnyResponse, error) {
h.inspectRequestHeader(req.Spec(), req.Header())
res, err := next(ctx, req)
if err != nil {
return nil, err
}
h.inspectResponseHeader(req.Spec(), res.Header())
return res, nil
}
}
func (h *headerInterceptor) WrapStreamingClient(next connect.StreamingClientFunc) connect.StreamingClientFunc {
return func(ctx context.Context, spec connect.Spec) connect.StreamingClientConn {
return &headerInspectingClientConn{
StreamingClientConn: next(ctx, spec),
inspectRequestHeader: h.inspectRequestHeader,
inspectResponseHeader: h.inspectResponseHeader,
}
}
}
func (h *headerInterceptor) WrapStreamingHandler(next connect.StreamingHandlerFunc) connect.StreamingHandlerFunc {
return func(ctx context.Context, conn connect.StreamingHandlerConn) error {
h.inspectRequestHeader(conn.Spec(), conn.RequestHeader())
return next(ctx, &headerInspectingHandlerConn{
StreamingHandlerConn: conn,
inspectResponseHeader: h.inspectResponseHeader,
})
}
}
type headerInspectingHandlerConn struct {
connect.StreamingHandlerConn
inspectedResponse bool
inspectResponseHeader func(connect.Spec, http.Header)
}
func (hc *headerInspectingHandlerConn) Send(msg any) error {
if !hc.inspectedResponse {
hc.inspectResponseHeader(hc.Spec(), hc.ResponseHeader())
hc.inspectedResponse = true
}
return hc.StreamingHandlerConn.Send(msg)
}
type headerInspectingClientConn struct {
connect.StreamingClientConn
inspectedRequest bool
inspectRequestHeader func(connect.Spec, http.Header)
inspectedResponse bool
inspectResponseHeader func(connect.Spec, http.Header)
}
func (cc *headerInspectingClientConn) Send(msg any) error {
if !cc.inspectedRequest {
cc.inspectRequestHeader(cc.Spec(), cc.RequestHeader())
cc.inspectedRequest = true
}
return cc.StreamingClientConn.Send(msg)
}
func (cc *headerInspectingClientConn) Receive(msg any) error {
err := cc.StreamingClientConn.Receive(msg)
if !cc.inspectedResponse {
cc.inspectResponseHeader(cc.Spec(), cc.ResponseHeader())
cc.inspectedResponse = true
}
return err
}