2021-05-04 15:20:58 +00:00
|
|
|
package gateway
|
|
|
|
|
|
|
|
import (
|
2021-06-18 13:35:13 +00:00
|
|
|
"context"
|
2021-05-04 15:20:58 +00:00
|
|
|
"flag"
|
|
|
|
"fmt"
|
2021-06-18 13:35:13 +00:00
|
|
|
"net/http"
|
2021-07-26 11:48:51 +00:00
|
|
|
"net/http/httptest"
|
|
|
|
"net/url"
|
2021-05-04 15:20:58 +00:00
|
|
|
"testing"
|
|
|
|
|
2021-09-21 19:20:57 +00:00
|
|
|
"github.com/gorilla/mux"
|
2024-02-15 05:46:47 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/v5/cmd/beacon-chain/flags"
|
|
|
|
"github.com/prysmaticlabs/prysm/v5/testing/assert"
|
|
|
|
"github.com/prysmaticlabs/prysm/v5/testing/require"
|
2021-05-04 15:20:58 +00:00
|
|
|
logTest "github.com/sirupsen/logrus/hooks/test"
|
|
|
|
"github.com/urfave/cli/v2"
|
|
|
|
)
|
|
|
|
|
2021-06-18 13:35:13 +00:00
|
|
|
func TestGateway_Customized(t *testing.T) {
|
2021-09-21 19:20:57 +00:00
|
|
|
r := mux.NewRouter()
|
2021-06-18 13:35:13 +00:00
|
|
|
cert := "cert"
|
|
|
|
origins := []string{"origin"}
|
|
|
|
size := uint64(100)
|
|
|
|
|
2021-12-07 20:26:21 +00:00
|
|
|
opts := []Option{
|
|
|
|
WithRouter(r),
|
|
|
|
WithRemoteCert(cert),
|
|
|
|
WithAllowedOrigins(origins),
|
|
|
|
WithMaxCallRecvMsgSize(size),
|
|
|
|
WithMuxHandler(func(
|
|
|
|
_ http.HandlerFunc,
|
|
|
|
_ http.ResponseWriter,
|
|
|
|
_ *http.Request,
|
|
|
|
) {
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
|
|
|
|
g, err := New(context.Background(), opts...)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
assert.Equal(t, r, g.cfg.router)
|
|
|
|
assert.Equal(t, cert, g.cfg.remoteCert)
|
|
|
|
require.Equal(t, 1, len(g.cfg.allowedOrigins))
|
|
|
|
assert.Equal(t, origins[0], g.cfg.allowedOrigins[0])
|
|
|
|
assert.Equal(t, size, g.cfg.maxCallRecvMsgSize)
|
2021-05-04 15:20:58 +00:00
|
|
|
}
|
|
|
|
|
2021-06-18 13:35:13 +00:00
|
|
|
func TestGateway_StartStop(t *testing.T) {
|
2021-05-04 15:20:58 +00:00
|
|
|
hook := logTest.NewGlobal()
|
|
|
|
|
|
|
|
app := cli.App{}
|
|
|
|
set := flag.NewFlagSet("test", 0)
|
|
|
|
ctx := cli.NewContext(&app, set, nil)
|
|
|
|
|
|
|
|
gatewayPort := ctx.Int(flags.GRPCGatewayPort.Name)
|
2021-06-18 13:35:13 +00:00
|
|
|
gatewayHost := ctx.String(flags.GRPCGatewayHost.Name)
|
2021-05-04 15:20:58 +00:00
|
|
|
rpcHost := ctx.String(flags.RPCHost.Name)
|
2021-06-18 13:35:13 +00:00
|
|
|
selfAddress := fmt.Sprintf("%s:%d", rpcHost, ctx.Int(flags.RPCPort.Name))
|
2021-05-04 15:20:58 +00:00
|
|
|
gatewayAddress := fmt.Sprintf("%s:%d", gatewayHost, gatewayPort)
|
|
|
|
|
2021-12-07 20:26:21 +00:00
|
|
|
opts := []Option{
|
|
|
|
WithGatewayAddr(gatewayAddress),
|
|
|
|
WithRemoteAddr(selfAddress),
|
|
|
|
WithMuxHandler(func(
|
|
|
|
_ http.HandlerFunc,
|
|
|
|
_ http.ResponseWriter,
|
|
|
|
_ *http.Request,
|
|
|
|
) {
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
|
|
|
|
g, err := New(context.Background(), opts...)
|
|
|
|
require.NoError(t, err)
|
2021-05-04 15:20:58 +00:00
|
|
|
|
2021-06-18 13:35:13 +00:00
|
|
|
g.Start()
|
2021-05-04 15:20:58 +00:00
|
|
|
go func() {
|
|
|
|
require.LogsContain(t, hook, "Starting gRPC gateway")
|
2021-06-18 00:38:15 +00:00
|
|
|
require.LogsDoNotContain(t, hook, "Starting API middleware")
|
2021-05-04 15:20:58 +00:00
|
|
|
}()
|
2021-12-07 20:26:21 +00:00
|
|
|
err = g.Stop()
|
2021-05-04 15:20:58 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
2021-07-26 11:48:51 +00:00
|
|
|
|
|
|
|
func TestGateway_NilHandler_NotFoundHandlerRegistered(t *testing.T) {
|
|
|
|
app := cli.App{}
|
|
|
|
set := flag.NewFlagSet("test", 0)
|
|
|
|
ctx := cli.NewContext(&app, set, nil)
|
|
|
|
|
|
|
|
gatewayPort := ctx.Int(flags.GRPCGatewayPort.Name)
|
|
|
|
gatewayHost := ctx.String(flags.GRPCGatewayHost.Name)
|
|
|
|
rpcHost := ctx.String(flags.RPCHost.Name)
|
|
|
|
selfAddress := fmt.Sprintf("%s:%d", rpcHost, ctx.Int(flags.RPCPort.Name))
|
|
|
|
gatewayAddress := fmt.Sprintf("%s:%d", gatewayHost, gatewayPort)
|
|
|
|
|
2021-12-07 20:26:21 +00:00
|
|
|
opts := []Option{
|
|
|
|
WithGatewayAddr(gatewayAddress),
|
|
|
|
WithRemoteAddr(selfAddress),
|
|
|
|
}
|
|
|
|
|
|
|
|
g, err := New(context.Background(), opts...)
|
|
|
|
require.NoError(t, err)
|
2021-07-26 11:48:51 +00:00
|
|
|
|
|
|
|
writer := httptest.NewRecorder()
|
2021-12-07 20:26:21 +00:00
|
|
|
g.cfg.router.ServeHTTP(writer, &http.Request{Method: "GET", Host: "localhost", URL: &url.URL{Path: "/foo"}})
|
2021-07-26 11:48:51 +00:00
|
|
|
assert.Equal(t, http.StatusNotFound, writer.Code)
|
|
|
|
}
|