server_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. package gateway
  2. import (
  3. "context"
  4. "log"
  5. "net"
  6. "net/http"
  7. "testing"
  8. "time"
  9. "github.com/stretchr/testify/assert"
  10. "github.com/wuntsong-org/go-zero-plus/core/conf"
  11. "github.com/wuntsong-org/go-zero-plus/core/discov"
  12. "github.com/wuntsong-org/go-zero-plus/core/logx"
  13. "github.com/wuntsong-org/go-zero-plus/core/logx/logtest"
  14. "github.com/wuntsong-org/go-zero-plus/internal/mock"
  15. "github.com/wuntsong-org/go-zero-plus/rest/httpc"
  16. "github.com/wuntsong-org/go-zero-plus/zrpc"
  17. "google.golang.org/grpc"
  18. "google.golang.org/grpc/reflection"
  19. "google.golang.org/grpc/test/bufconn"
  20. )
  21. func init() {
  22. logx.Disable()
  23. }
  24. func dialer() func(context.Context, string) (net.Conn, error) {
  25. listener := bufconn.Listen(1024 * 1024)
  26. server := grpc.NewServer()
  27. mock.RegisterDepositServiceServer(server, &mock.DepositServer{})
  28. reflection.Register(server)
  29. go func() {
  30. if err := server.Serve(listener); err != nil {
  31. log.Fatal(err)
  32. }
  33. }()
  34. return func(context.Context, string) (net.Conn, error) {
  35. return listener.Dial()
  36. }
  37. }
  38. func TestMustNewServer(t *testing.T) {
  39. var c GatewayConf
  40. assert.NoError(t, conf.FillDefault(&c))
  41. // avoid popup alert on macos for asking permissions
  42. c.DevServer.Host = "localhost"
  43. c.Host = "localhost"
  44. c.Port = 18881
  45. s := MustNewServer(c, withDialer(func(conf zrpc.RpcClientConf) zrpc.Client {
  46. return zrpc.MustNewClient(conf, zrpc.WithDialOption(grpc.WithContextDialer(dialer())))
  47. }), WithHeaderProcessor(func(header http.Header) []string {
  48. return []string{"foo"}
  49. }))
  50. s.upstreams = []Upstream{
  51. {
  52. Mappings: []RouteMapping{
  53. {
  54. Method: "get",
  55. Path: "/deposit/:amount",
  56. RpcPath: "mock.DepositService/Deposit",
  57. },
  58. },
  59. Grpc: zrpc.RpcClientConf{
  60. Endpoints: []string{"foo"},
  61. Timeout: 1000,
  62. Middlewares: zrpc.ClientMiddlewaresConf{
  63. Trace: true,
  64. Duration: true,
  65. Prometheus: true,
  66. Breaker: true,
  67. Timeout: true,
  68. },
  69. },
  70. },
  71. }
  72. assert.NoError(t, s.build())
  73. go s.Server.Start()
  74. defer s.Stop()
  75. time.Sleep(time.Millisecond * 200)
  76. resp, err := httpc.Do(context.Background(), http.MethodGet, "http://localhost:18881/deposit/100", nil)
  77. assert.NoError(t, err)
  78. assert.Equal(t, http.StatusOK, resp.StatusCode)
  79. resp, err = httpc.Do(context.Background(), http.MethodGet, "http://localhost:18881/deposit_fail/100", nil)
  80. assert.NoError(t, err)
  81. assert.Equal(t, http.StatusNotFound, resp.StatusCode)
  82. }
  83. func TestServer_ensureUpstreamNames(t *testing.T) {
  84. var s = Server{
  85. upstreams: []Upstream{
  86. {
  87. Grpc: zrpc.RpcClientConf{
  88. Target: "target",
  89. },
  90. },
  91. },
  92. }
  93. assert.NoError(t, s.ensureUpstreamNames())
  94. assert.Equal(t, "target", s.upstreams[0].Name)
  95. }
  96. func TestServer_ensureUpstreamNames_badEtcd(t *testing.T) {
  97. var s = Server{
  98. upstreams: []Upstream{
  99. {
  100. Grpc: zrpc.RpcClientConf{
  101. Etcd: discov.EtcdConf{},
  102. },
  103. },
  104. },
  105. }
  106. logtest.PanicOnFatal(t)
  107. assert.Panics(t, func() {
  108. s.Start()
  109. })
  110. }