Skip to content

Commit

Permalink
Add test for connectrpc
Browse files Browse the repository at this point in the history
  • Loading branch information
k1LoW committed May 20, 2024
1 parent 9821116 commit c850225
Show file tree
Hide file tree
Showing 7 changed files with 173 additions and 2 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ testclient:
mkdir -p testdata/hello
cd testdata/ && protoc --go_out=routeguide --go_opt=paths=source_relative --go-grpc_out=routeguide --go-grpc_opt=paths=source_relative route_guide.proto
cd testdata/ && protoc --go_out=hello --go_opt=paths=source_relative --go-grpc_out=hello --go-grpc_opt=paths=source_relative hello.proto
cd testdata/bsr/protobuf && buf mod update && buf generate

cert:
rm -f testdata/*.pem testdata/*.srl
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ module github.com/k1LoW/grpcstub
go 1.22.2

require (
buf.build/gen/go/bufbuild/protovalidate/protocolbuffers/go v1.34.1-20240508200655-46a4cf4ba109.1
connectrpc.com/connect v1.16.2
github.com/bmatcuk/doublestar/v4 v4.6.1
github.com/bufbuild/protocompile v0.13.0
github.com/google/go-cmp v0.6.0
Expand Down
8 changes: 8 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
buf.build/gen/go/bufbuild/protovalidate/protocolbuffers/go v1.34.1-20240508200655-46a4cf4ba109.1 h1:LEXWFH/xZ5oOWrC3oOtHbUyBdzRWMCPpAQmKC9v05mA=
buf.build/gen/go/bufbuild/protovalidate/protocolbuffers/go v1.34.1-20240508200655-46a4cf4ba109.1/go.mod h1:XF+P8+RmfdufmIYpGUC+6bF7S+IlmHDEnCrO3OXaUAQ=
connectrpc.com/connect v1.16.2 h1:ybd6y+ls7GOlb7Bh5C8+ghA6SvCBajHwxssO2CGFjqE=
connectrpc.com/connect v1.16.2/go.mod h1:n2kgwskMHXC+lVqb18wngEpF95ldBHXjZYJussz5FRc=
github.com/bmatcuk/doublestar/v4 v4.6.1 h1:FH9SifrbvJhnlQpztAx++wlkk70QBf0iBWDwNy7PA4I=
github.com/bmatcuk/doublestar/v4 v4.6.1/go.mod h1:xBQ8jztBU6kakFMg+8WGxn0c6z1fTSPVIjEY1Wr7jzc=
github.com/bufbuild/protocompile v0.13.0 h1:6cwUB0Y2tSvmNxsbunwzmIto3xOlJOV7ALALuVOs92M=
github.com/bufbuild/protocompile v0.13.0/go.mod h1:dr++fGGeMPWHv7jPeT06ZKukm45NJscd7rUxQVzEKRk=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/jaswdr/faker v1.19.1 h1:xBoz8/O6r0QAR8eEvKJZMdofxiRH+F0M/7MU9eNKhsM=
Expand Down Expand Up @@ -61,11 +67,13 @@ golang.org/x/tools v0.20.0 h1:hz/CVckiOxybQvFw6h7b/q80NTr9IUQb4s1IIzW7KNY=
golang.org/x/tools v0.20.0/go.mod h1:WvitBU7JJf6A4jOdg4S1tviW9bhUxkgeCui/0JHctQg=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/genproto/googleapis/rpc v0.0.0-20240415180920-8c6c420018be h1:LG9vZxsWGOmUKieR8wPAUR3u3MpnYFQZROPIMaXh7/A=
google.golang.org/genproto/googleapis/rpc v0.0.0-20240415180920-8c6c420018be/go.mod h1:WtryC6hu0hhx87FDGxWCDptyssuo68sk10vYjF+T9fY=
google.golang.org/grpc v1.64.0 h1:KH3VH9y/MgNQg1dE7b3XfVK0GsPSIzJwdF617gUSbvY=
google.golang.org/grpc v1.64.0/go.mod h1:oxjF8E3FBnjp+/gVFYdWacaLDx9na1aqy9oovLpxQYg=
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
google.golang.org/protobuf v1.34.1 h1:9ddQBjfCyZPOHPUiPxpYESBLc+T8P3E+Vo4IbKZgFWg=
google.golang.org/protobuf v1.34.1/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
Expand Down
45 changes: 45 additions & 0 deletions grpcstub_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,23 @@ package grpcstub

import (
"context"
"crypto/tls"
"fmt"
"net/http"
"os"
"strings"
"testing"
"time"

"connectrpc.com/connect"
"github.com/google/go-cmp/cmp"
"github.com/jhump/protoreflect/v2/grpcreflect"
"github.com/k1LoW/grpcstub/testdata/bsr/protobuf/gen/go/pinger"
"github.com/k1LoW/grpcstub/testdata/bsr/protobuf/gen/go/pinger/pingerconnect"
"github.com/k1LoW/grpcstub/testdata/hello"
"github.com/k1LoW/grpcstub/testdata/routeguide"
"github.com/tenntenn/golden"
"golang.org/x/net/http2"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
healthpb "google.golang.org/grpc/health/grpc_health_v1"
Expand Down Expand Up @@ -734,3 +740,42 @@ func TestBufProtoRegistry(t *testing.T) {
})
})
}

func TestWithConnectClient(t *testing.T) {
ctx := context.Background()
cacert, err := os.ReadFile("testdata/cacert.pem")
if err != nil {
t.Fatal(err)
}
cert, err := os.ReadFile("testdata/cert.pem")
if err != nil {
t.Fatal(err)
}
key, err := os.ReadFile("testdata/key.pem")
if err != nil {
t.Fatal(err)
}
ts := NewTLSServer(t, "testdata/bsr/protobuf", cacert, cert, key)
ts.Service("pinger.PingerService").Method("Ping").Response(&pinger.PingResponse{
Message: "hello",
})
t.Cleanup(func() {
ts.Close()
})
u := fmt.Sprintf("https://%s", ts.Addr())
httpClient := &http.Client{
Transport: &http2.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, //nolint: gosec
},
}
client := pingerconnect.NewPingerServiceClient(httpClient, u, connect.WithGRPC())
res, err := client.Ping(ctx, connect.NewRequest(&pinger.PingRequest{
Message: "hello",
}))
if err != nil {
t.Fatal(err)
}
if want := "hello"; res.Msg.GetMessage() != want {
t.Errorf("got %v\nwant %v", res.Msg.GetMessage(), want)
}
}
3 changes: 3 additions & 0 deletions testdata/bsr/protobuf/buf.gen.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,6 @@ plugins:
- name: go-grpc
out: gen/go
opt: paths=source_relative
- name: connect-go
out: gen/go
opt: paths=source_relative
4 changes: 2 additions & 2 deletions testdata/bsr/protobuf/buf.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ deps:
- remote: buf.build
owner: bufbuild
repository: protovalidate
commit: b983156c5e994cc9892e0ce3e64e17e0
digest: shake256:fb47a62989d38c2529bcc5cd86ded43d800eb84cee82b42b9e8a9e815d4ee8134a0fb9d0ce8299b27c2d2bbb7d6ade0c4ad5a8a4d467e1e2c7ca619ae9f634e2
commit: 46a4cf4ba1094a34bcd89a6c67163b4b
digest: shake256:436ce453801917c11bc7b21d66bcfae87da2aceb804a041487be1e51dc9fbc219e61ea6a552db7a7aa6d63bb5efd0f3ed5fe3d4c42d4f750d0eb35f14144e3b6
112 changes: 112 additions & 0 deletions testdata/bsr/protobuf/gen/go/pinger/pingerconnect/pinger.connect.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit c850225

Please sign in to comment.