forked from connectrpc/connect-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client_ext_test.go
233 lines (210 loc) · 7.03 KB
/
client_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
// Copyright 2021-2023 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"
"errors"
"net/http"
"net/http/httptest"
"strings"
"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 TestNewClient_InitFailure(t *testing.T) {
t.Parallel()
client := pingv1connect.NewPingServiceClient(
http.DefaultClient,
"http://127.0.0.1:8080",
// This triggers an error during initialization, so each call will short circuit returning an error.
connect.WithSendCompression("invalid"),
)
validateExpectedError := func(t *testing.T, err error) {
t.Helper()
assert.NotNil(t, err)
var connectErr *connect.Error
assert.True(t, errors.As(err, &connectErr))
assert.Equal(t, connectErr.Message(), `unknown compression "invalid"`)
}
t.Run("unary", func(t *testing.T) {
t.Parallel()
_, err := client.Ping(context.Background(), connect.NewRequest(&pingv1.PingRequest{}))
validateExpectedError(t, err)
})
t.Run("bidi", func(t *testing.T) {
t.Parallel()
bidiStream := client.CumSum(context.Background())
err := bidiStream.Send(&pingv1.CumSumRequest{})
validateExpectedError(t, err)
})
t.Run("client_stream", func(t *testing.T) {
t.Parallel()
clientStream := client.Sum(context.Background())
err := clientStream.Send(&pingv1.SumRequest{})
validateExpectedError(t, err)
})
t.Run("server_stream", func(t *testing.T) {
t.Parallel()
_, err := client.CountUp(context.Background(), connect.NewRequest(&pingv1.CountUpRequest{Number: 3}))
validateExpectedError(t, err)
})
}
func TestClientPeer(t *testing.T) {
t.Parallel()
mux := http.NewServeMux()
mux.Handle(pingv1connect.NewPingServiceHandler(pingServer{}))
server := httptest.NewUnstartedServer(mux)
server.EnableHTTP2 = true
server.StartTLS()
t.Cleanup(server.Close)
run := func(t *testing.T, opts ...connect.ClientOption) {
t.Helper()
client := pingv1connect.NewPingServiceClient(
server.Client(),
server.URL,
connect.WithClientOptions(opts...),
connect.WithInterceptors(&assertPeerInterceptor{t}),
)
ctx := context.Background()
// unary
_, err := client.Ping(ctx, connect.NewRequest[pingv1.PingRequest](nil))
assert.Nil(t, err)
text := strings.Repeat(".", 256)
r, err := client.Ping(ctx, connect.NewRequest(&pingv1.PingRequest{Text: text}))
assert.Nil(t, err)
assert.Equal(t, r.Msg.Text, text)
// client streaming
clientStream := client.Sum(ctx)
t.Cleanup(func() {
_, closeErr := clientStream.CloseAndReceive()
assert.Nil(t, closeErr)
})
assert.NotZero(t, clientStream.Peer().Addr)
assert.NotZero(t, clientStream.Peer().Protocol)
err = clientStream.Send(&pingv1.SumRequest{})
assert.Nil(t, err)
// server streaming
serverStream, err := client.CountUp(ctx, connect.NewRequest(&pingv1.CountUpRequest{}))
t.Cleanup(func() {
assert.Nil(t, serverStream.Close())
})
assert.Nil(t, err)
// bidi streaming
bidiStream := client.CumSum(ctx)
t.Cleanup(func() {
assert.Nil(t, bidiStream.CloseRequest())
assert.Nil(t, bidiStream.CloseResponse())
})
assert.NotZero(t, bidiStream.Peer().Addr)
assert.NotZero(t, bidiStream.Peer().Protocol)
err = bidiStream.Send(&pingv1.CumSumRequest{})
assert.Nil(t, err)
}
t.Run("connect", func(t *testing.T) {
t.Parallel()
run(t)
})
t.Run("connect get", func(t *testing.T) {
t.Parallel()
run(t,
connect.WithHTTPGet(),
connect.WithSendGzip(),
)
})
t.Run("grpc", func(t *testing.T) {
t.Parallel()
run(t, connect.WithGRPC())
})
t.Run("grpcweb", func(t *testing.T) {
t.Parallel()
run(t, connect.WithGRPCWeb())
})
}
func TestGetNotModified(t *testing.T) {
t.Parallel()
const etag = "some-etag"
// Handlers should automatically set Vary to include request headers that are
// part of the RPC protocol.
expectVary := []string{"Accept-Encoding"}
mux := http.NewServeMux()
mux.Handle(pingv1connect.NewPingServiceHandler(¬ModifiedPingServer{etag: etag}))
server := httptest.NewUnstartedServer(mux)
server.EnableHTTP2 = true
server.StartTLS()
t.Cleanup(server.Close)
client := pingv1connect.NewPingServiceClient(
server.Client(),
server.URL,
connect.WithHTTPGet(),
)
ctx := context.Background()
// unconditional request
res, err := client.Ping(ctx, connect.NewRequest(&pingv1.PingRequest{}))
assert.Nil(t, err)
assert.Equal(t, res.Header().Get("Etag"), etag)
assert.Equal(t, res.Header().Values("Vary"), expectVary)
conditional := connect.NewRequest(&pingv1.PingRequest{})
conditional.Header().Set("If-None-Match", etag)
_, err = client.Ping(ctx, conditional)
assert.NotNil(t, err)
assert.Equal(t, connect.CodeOf(err), connect.CodeUnknown)
assert.True(t, connect.IsNotModifiedError(err))
var connectErr *connect.Error
assert.True(t, errors.As(err, &connectErr))
assert.Equal(t, connectErr.Meta().Get("Etag"), etag)
assert.Equal(t, connectErr.Meta().Values("Vary"), expectVary)
}
type notModifiedPingServer struct {
pingv1connect.UnimplementedPingServiceHandler
etag string
}
func (s *notModifiedPingServer) Ping(
_ context.Context,
req *connect.Request[pingv1.PingRequest]) (*connect.Response[pingv1.PingResponse], error) {
if len(req.Peer().Query) > 0 && req.Header().Get("If-None-Match") == s.etag {
return nil, connect.NewNotModifiedError(http.Header{"Etag": []string{s.etag}})
}
resp := connect.NewResponse(&pingv1.PingResponse{})
resp.Header().Set("Etag", s.etag)
return resp, nil
}
type assertPeerInterceptor struct {
tb testing.TB
}
func (a *assertPeerInterceptor) WrapUnary(next connect.UnaryFunc) connect.UnaryFunc {
return func(ctx context.Context, req connect.AnyRequest) (connect.AnyResponse, error) {
assert.NotZero(a.tb, req.Peer().Addr)
assert.NotZero(a.tb, req.Peer().Protocol)
return next(ctx, req)
}
}
func (a *assertPeerInterceptor) WrapStreamingClient(next connect.StreamingClientFunc) connect.StreamingClientFunc {
return func(ctx context.Context, spec connect.Spec) connect.StreamingClientConn {
conn := next(ctx, spec)
assert.NotZero(a.tb, conn.Peer().Addr)
assert.NotZero(a.tb, conn.Peer().Protocol)
assert.NotZero(a.tb, conn.Spec())
return conn
}
}
func (a *assertPeerInterceptor) WrapStreamingHandler(next connect.StreamingHandlerFunc) connect.StreamingHandlerFunc {
return func(ctx context.Context, conn connect.StreamingHandlerConn) error {
assert.NotZero(a.tb, conn.Peer().Addr)
assert.NotZero(a.tb, conn.Peer().Protocol)
assert.NotZero(a.tb, conn.Spec())
return next(ctx, conn)
}
}