server_test.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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/zeromicro/go-zero/core/conf"
  11. "github.com/zeromicro/go-zero/core/logx"
  12. "github.com/zeromicro/go-zero/internal/mock"
  13. "github.com/zeromicro/go-zero/rest/httpc"
  14. "github.com/zeromicro/go-zero/zrpc"
  15. "google.golang.org/grpc"
  16. "google.golang.org/grpc/reflection"
  17. "google.golang.org/grpc/test/bufconn"
  18. )
  19. func init() {
  20. logx.Disable()
  21. }
  22. func dialer() func(context.Context, string) (net.Conn, error) {
  23. listener := bufconn.Listen(1024 * 1024)
  24. server := grpc.NewServer()
  25. mock.RegisterDepositServiceServer(server, &mock.DepositServer{})
  26. reflection.Register(server)
  27. go func() {
  28. if err := server.Serve(listener); err != nil {
  29. log.Fatal(err)
  30. }
  31. }()
  32. return func(context.Context, string) (net.Conn, error) {
  33. return listener.Dial()
  34. }
  35. }
  36. func TestMustNewServer(t *testing.T) {
  37. var c GatewayConf
  38. assert.NoError(t, conf.FillDefault(&c))
  39. // avoid popup alert on macos for asking permissions
  40. c.DevServer.Host = "localhost"
  41. c.Host = "localhost"
  42. c.Port = 18881
  43. s := MustNewServer(c, withDialer(func(conf zrpc.RpcClientConf) zrpc.Client {
  44. return zrpc.MustNewClient(conf, zrpc.WithDialOption(grpc.WithContextDialer(dialer())))
  45. }))
  46. s.upstreams = []Upstream{
  47. {
  48. Mappings: []RouteMapping{
  49. {
  50. Method: "get",
  51. Path: "/deposit/:amount",
  52. RpcPath: "mock.DepositService/Deposit",
  53. },
  54. },
  55. Grpc: zrpc.RpcClientConf{
  56. Endpoints: []string{"foo"},
  57. Timeout: 1000,
  58. Middlewares: zrpc.ClientMiddlewaresConf{
  59. Trace: true,
  60. Duration: true,
  61. Prometheus: true,
  62. Breaker: true,
  63. Timeout: true,
  64. },
  65. },
  66. },
  67. }
  68. assert.NoError(t, s.build())
  69. go s.Server.Start()
  70. time.Sleep(time.Millisecond * 200)
  71. resp, err := httpc.Do(context.Background(), http.MethodGet, "http://localhost:18881/deposit/100", nil)
  72. assert.NoError(t, err)
  73. assert.Equal(t, http.StatusOK, resp.StatusCode)
  74. resp, err = httpc.Do(context.Background(), http.MethodGet, "http://localhost:18881/deposit_fail/100", nil)
  75. assert.NoError(t, err)
  76. assert.Equal(t, http.StatusNotFound, resp.StatusCode)
  77. }
  78. func TestServer_ensureUpstreamNames(t *testing.T) {
  79. var s = Server{
  80. upstreams: []Upstream{
  81. {
  82. Grpc: zrpc.RpcClientConf{
  83. Target: "target",
  84. },
  85. },
  86. },
  87. }
  88. assert.NoError(t, s.ensureUpstreamNames())
  89. assert.Equal(t, "target", s.upstreams[0].Name)
  90. }