Don't panic with undefined middleware
This commit is contained in:
parent
a917115a85
commit
10acbb8d92
4 changed files with 25 additions and 8 deletions
|
@ -23,6 +23,11 @@ func (f *Builder) Build(ctx context.Context, names []string) func(*http.Response
|
|||
|
||||
for _, middleName := range names {
|
||||
if conf, ok := f.configs[middleName]; ok {
|
||||
if conf == nil || conf.Middleware == nil {
|
||||
getLogger(ctx, middleName, "undefined").Error("Invalid Middleware configuration (ResponseModifier)")
|
||||
continue
|
||||
}
|
||||
|
||||
if conf.Headers != nil {
|
||||
getLogger(ctx, middleName, "Headers").Debug("Creating Middleware (ResponseModifier)")
|
||||
|
||||
|
|
|
@ -47,7 +47,7 @@ func TestBuilderBuild(t *testing.T) {
|
|||
assertResponse: func(t *testing.T, resp *http.Response) {
|
||||
t.Helper()
|
||||
|
||||
assert.Equal(t, resp.Header.Get("X-Foo"), "foo")
|
||||
assert.Equal(t, "foo", resp.Header.Get("X-Foo"))
|
||||
},
|
||||
},
|
||||
{
|
||||
|
@ -85,7 +85,7 @@ func TestBuilderBuild(t *testing.T) {
|
|||
assertResponse: func(t *testing.T, resp *http.Response) {
|
||||
t.Helper()
|
||||
|
||||
assert.Equal(t, resp.Header.Get("Referrer-Policy"), "no-referrer")
|
||||
assert.Equal(t, "no-referrer", resp.Header.Get("Referrer-Policy"))
|
||||
},
|
||||
},
|
||||
{
|
||||
|
@ -107,8 +107,8 @@ func TestBuilderBuild(t *testing.T) {
|
|||
assertResponse: func(t *testing.T, resp *http.Response) {
|
||||
t.Helper()
|
||||
|
||||
assert.Equal(t, resp.Header.Get("X-Foo"), "foo")
|
||||
assert.Equal(t, resp.Header.Get("X-Bar"), "bar")
|
||||
assert.Equal(t, "foo", resp.Header.Get("X-Foo"))
|
||||
assert.Equal(t, "bar", resp.Header.Get("X-Bar"))
|
||||
},
|
||||
},
|
||||
{
|
||||
|
@ -130,7 +130,7 @@ func TestBuilderBuild(t *testing.T) {
|
|||
assertResponse: func(t *testing.T, resp *http.Response) {
|
||||
t.Helper()
|
||||
|
||||
assert.Equal(t, resp.Header.Get("X-Foo"), "foo")
|
||||
assert.Equal(t, "foo", resp.Header.Get("X-Foo"))
|
||||
},
|
||||
},
|
||||
{
|
||||
|
@ -157,9 +157,18 @@ func TestBuilderBuild(t *testing.T) {
|
|||
assertResponse: func(t *testing.T, resp *http.Response) {
|
||||
t.Helper()
|
||||
|
||||
assert.Equal(t, resp.Header.Get("X-Foo"), "foo")
|
||||
assert.Equal(t, "foo", resp.Header.Get("X-Foo"))
|
||||
},
|
||||
},
|
||||
{
|
||||
desc: "nil middleware",
|
||||
middlewares: []string{"foo"},
|
||||
buildResponse: stubResponse,
|
||||
conf: map[string]*dynamic.Middleware{
|
||||
"foo": nil,
|
||||
},
|
||||
assertResponse: func(t *testing.T, resp *http.Response) {},
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range testCases {
|
||||
|
|
|
@ -102,6 +102,10 @@ func checkRecursion(ctx context.Context, middlewareName string) (context.Context
|
|||
// it is the responsibility of the caller to make sure that b.configs[middlewareName].Middleware exists
|
||||
func (b *Builder) buildConstructor(ctx context.Context, middlewareName string) (alice.Constructor, error) {
|
||||
config := b.configs[middlewareName]
|
||||
if config == nil || config.Middleware == nil {
|
||||
return nil, fmt.Errorf("invalid middleware %q configuration", middlewareName)
|
||||
}
|
||||
|
||||
var middleware alice.Constructor
|
||||
badConf := errors.New("cannot create middleware: multi-types middleware not supported, consider declaring two different pieces of middleware instead")
|
||||
|
||||
|
@ -314,7 +318,7 @@ func (b *Builder) buildConstructor(ctx context.Context, middlewareName string) (
|
|||
}
|
||||
|
||||
if middleware == nil {
|
||||
return nil, errors.New("middleware does not exist")
|
||||
return nil, fmt.Errorf("middleware %q does not exist", middlewareName)
|
||||
}
|
||||
|
||||
return tracing.Wrap(ctx, middleware), nil
|
||||
|
|
|
@ -369,7 +369,6 @@ func TestBuilder_buildConstructor(t *testing.T) {
|
|||
t.Parallel()
|
||||
|
||||
constructor, err := middlewaresBuilder.buildConstructor(context.Background(), test.middlewareID)
|
||||
|
||||
require.NoError(t, err)
|
||||
|
||||
middleware, err2 := constructor(http.HandlerFunc(func(_ http.ResponseWriter, _ *http.Request) {}))
|
||||
|
|
Loading…
Reference in a new issue