github.com/renbou/grpcbridge@v0.0.2-0.20240416012907-bcbd8b12648a/webbridge/webbridge_test.go (about) 1 package webbridge 2 3 import ( 4 "testing" 5 6 "github.com/renbou/grpcbridge/internal/bridgetest" 7 "github.com/renbou/grpcbridge/internal/bridgetest/testpb" 8 "github.com/renbou/grpcbridge/routing" 9 "github.com/renbou/grpcbridge/transcoding" 10 "google.golang.org/grpc" 11 "google.golang.org/protobuf/proto" 12 ) 13 14 const ctJson = "application/json" 15 16 func mustTranscodedTestSvc(t *testing.T) (*testpb.TestService, *routing.PatternRouter, *transcoding.StandardTranscoder) { 17 testsvc := new(testpb.TestService) 18 19 server, pool, _ := bridgetest.MustGRPCServer(t, func(s *grpc.Server) { 20 testpb.RegisterTestServiceServer(s, testsvc) 21 }) 22 t.Cleanup(server.Stop) 23 24 transcoder := transcoding.NewStandardTranscoder(transcoding.StandardTranscoderOpts{}) 25 router := routing.NewPatternRouter(pool, routing.PatternRouterOpts{}) 26 27 routerWatcher, err := router.Watch(bridgetest.TestTargetName) 28 if err != nil { 29 t.Fatalf("failed to create router watcher to write state: %s", err) 30 } 31 32 routerWatcher.UpdateDesc(testpb.TestServiceDesc) 33 t.Cleanup(routerWatcher.Close) 34 35 return testsvc, router, transcoder 36 } 37 38 func unmarshalJSON(b []byte, msg proto.Message) error { 39 return transcoding.DefaultJSONMarshaler.Unmarshal(testpb.TestServiceTypesResolver, b, msg.ProtoReflect(), nil) 40 }