Compare commits

...

2 Commits
eas ... v1.4.3

Author SHA1 Message Date
Radosław Kapka
8bca66ac64 Do not require a handler function in the gateway (#9264)
* Do not require a handler function in the gateway

* test fix

Co-authored-by: Raul Jordan <raul@prysmaticlabs.com>
Co-authored-by: prylabs-bulldozer[bot] <58059840+prylabs-bulldozer[bot]@users.noreply.github.com>
(cherry picked from commit be82c8714f)
2021-07-30 13:44:40 -05:00
Nishant Das
b3650903ea Fix Parsing of Nested Subcommands (#9236)
(cherry picked from commit 2ffe8336fc)
2021-07-21 14:08:42 -05:00
5 changed files with 75 additions and 12 deletions

View File

@@ -1,8 +1,6 @@
package gateway
import (
"net/http"
gwruntime "github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
pbrpc "github.com/prysmaticlabs/prysm/proto/beacon/rpc/v1"
ethpbv1 "github.com/prysmaticlabs/prysm/proto/eth/v1"
@@ -65,9 +63,6 @@ func DefaultConfig(enableDebugRPCEndpoints bool) MuxConfig {
},
}),
)
muxHandler := func(h http.Handler, w http.ResponseWriter, req *http.Request) {
h.ServeHTTP(w, req)
}
v1Alpha1PbHandler := gateway.PbMux{
Registrations: v1Alpha1Registrations,
Patterns: []string{"/eth/v1alpha1/"},
@@ -80,7 +75,6 @@ func DefaultConfig(enableDebugRPCEndpoints bool) MuxConfig {
}
return MuxConfig{
Handler: muxHandler,
V1PbMux: v1PbHandler,
V1Alpha1PbMux: v1Alpha1PbHandler,
}

View File

@@ -10,7 +10,6 @@ import (
func TestDefaultConfig(t *testing.T) {
t.Run("Without debug endpoints", func(t *testing.T) {
cfg := DefaultConfig(false)
assert.NotNil(t, cfg.Handler)
assert.NotNil(t, cfg.V1PbMux.Mux)
require.Equal(t, 1, len(cfg.V1PbMux.Patterns))
assert.Equal(t, "/eth/v1/", cfg.V1PbMux.Patterns[0])
@@ -23,7 +22,6 @@ func TestDefaultConfig(t *testing.T) {
t.Run("With debug endpoints", func(t *testing.T) {
cfg := DefaultConfig(true)
assert.NotNil(t, cfg.Handler)
assert.NotNil(t, cfg.V1PbMux.Mux)
require.Equal(t, 1, len(cfg.V1PbMux.Patterns))
assert.Equal(t, "/eth/v1/", cfg.V1PbMux.Patterns[0])

View File

@@ -257,13 +257,28 @@ func LoadFlagsFromConfig(cliCtx *cli.Context, flags []cli.Flag) error {
// ValidateNoArgs insures that the application is not run with erroneous arguments or flags.
// This function should be used in the app.Before, whenever the application supports a default command.
func ValidateNoArgs(ctx *cli.Context) error {
commandList := ctx.App.Commands
for _, a := range ctx.Args().Slice() {
if strings.HasPrefix(a, "-") {
continue
}
if c := ctx.App.Command(a); c == nil {
c := checkCommandList(commandList, a)
if c == nil {
return fmt.Errorf("unrecognized argument: %s", a)
}
// Set the command list as the subcommand's
// from the current selected parent command.
commandList = c.Subcommands
}
return nil
}
// verifies that the provided command is in the command list.
func checkCommandList(commands []*cli.Command, name string) *cli.Command {
for _, c := range commands {
if c.Name == name {
return c
}
}
return nil
}

View File

@@ -55,6 +55,24 @@ func TestValidateNoArgs(t *testing.T) {
Commands: []*cli.Command{
{
Name: "bar",
Subcommands: []*cli.Command{
{
Name: "subComm1",
Subcommands: []*cli.Command{
{
Name: "subComm3",
},
},
},
{
Name: "subComm2",
Subcommands: []*cli.Command{
{
Name: "subComm4",
},
},
},
},
},
},
}
@@ -71,4 +89,39 @@ func TestValidateNoArgs(t *testing.T) {
// It should fail on unregistered flag (default logic in urfave/cli).
err = app.Run([]string{"command", "bar", "--baz"})
require.ErrorContains(t, "flag provided but not defined", err)
// Handle Nested Subcommands
err = app.Run([]string{"command", "bar", "subComm1"})
require.NoError(t, err)
err = app.Run([]string{"command", "bar", "subComm2"})
require.NoError(t, err)
// Should fail from unknown subcommands.
err = app.Run([]string{"command", "bar", "subComm3"})
require.ErrorContains(t, "unrecognized argument: subComm3", err)
err = app.Run([]string{"command", "bar", "subComm4"})
require.ErrorContains(t, "unrecognized argument: subComm4", err)
// Should fail with invalid double nested subcommands.
err = app.Run([]string{"command", "bar", "subComm1", "subComm2"})
require.ErrorContains(t, "unrecognized argument: subComm2", err)
err = app.Run([]string{"command", "bar", "subComm1", "subComm4"})
require.ErrorContains(t, "unrecognized argument: subComm4", err)
err = app.Run([]string{"command", "bar", "subComm2", "subComm1"})
require.ErrorContains(t, "unrecognized argument: subComm1", err)
err = app.Run([]string{"command", "bar", "subComm2", "subComm3"})
require.ErrorContains(t, "unrecognized argument: subComm3", err)
// Should pass with correct nested double subcommands.
err = app.Run([]string{"command", "bar", "subComm1", "subComm3"})
require.NoError(t, err)
err = app.Run([]string{"command", "bar", "subComm2", "subComm4"})
require.NoError(t, err)
}

View File

@@ -131,9 +131,12 @@ func (g *Gateway) Start() {
}
corsMux := g.corsMiddleware(g.mux)
g.mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
g.muxHandler(corsMux, w, r)
})
if g.muxHandler != nil {
g.mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
g.muxHandler(corsMux, w, r)
})
}
g.server = &http.Server{
Addr: g.gatewayAddr,