Skip to content

Commit

Permalink
fix: Call response modifier plugin when resp is not nil
Browse files Browse the repository at this point in the history
Allows merged response from multible backends to be modified if at least
one of the backends replied, resp is not nil and the error would be for
instance a mergeErr.

Signed-off-by: Daniel Ferreira <[email protected]>
  • Loading branch information
danielfpferreira committed Aug 10, 2022
1 parent 4b98222 commit 216025a
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 12 deletions.
25 changes: 13 additions & 12 deletions proxy/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,7 @@ func newPluginMiddleware(logger logging.Logger, tag, pattern string, cfg map[str

if totReqModifiers == 0 {
return func(ctx context.Context, r *Request) (*Response, error) {
resp, err := next[0](ctx, r)
if err != nil {
return resp, err
}

return executeResponseModifiers(respModifiers, resp)
return nextProxyWithResponseModifiers(ctx, r, next[0], respModifiers)
}
}

Expand All @@ -116,16 +111,22 @@ func newPluginMiddleware(logger logging.Logger, tag, pattern string, cfg map[str
return nil, err
}

resp, err := next[0](ctx, r)
if err != nil {
return resp, err
}

return executeResponseModifiers(respModifiers, resp)
return nextProxyWithResponseModifiers(ctx, r, next[0], respModifiers)
}
}
}

func nextProxyWithResponseModifiers(ctx context.Context, r *Request, next Proxy, respModifiers []func(interface{}) (interface{}, error)) (*Response, error) {
resp, err := next(ctx, r)
// merged responses of multiple backends will have a non nil resp if at least one of the backends responds
// successfully
if err != nil && resp == nil {
return resp, err
}

return executeResponseModifiers(respModifiers, resp)
}

func executeRequestModifiers(reqModifiers []func(interface{}) (interface{}, error), r *Request) (*Request, error) {
var tmp RequestWrapper
tmp = &requestWrapper{
Expand Down
53 changes: 53 additions & 0 deletions proxy/plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,59 @@ func TestNewPluginMiddleware_logger(t *testing.T) {
}
}

func TestNewPluginMiddleware_merge_error(t *testing.T) {
plugin.LoadWithLogger("./plugin/tests", ".so", plugin.RegisterModifier, logging.NoOp)

validator := func(ctx context.Context, r *Request) (*Response, error) {
return &Response{
Data: map[string]interface{}{"foo": "bar"},
IsComplete: true,
Metadata: Metadata{
Headers: map[string][]string{},
StatusCode: 0,
},
}, mergeError{errs: []error{fmt.Errorf("some backend error")}}
}

bknd := NewBackendPluginMiddleware(
logging.NoOp,
&config.Backend{
ExtraConfig: map[string]interface{}{
plugin.Namespace: map[string]interface{}{
"name": []interface{}{"lura-request-modifier-example-response"},
},
},
},
)(validator)

p := NewPluginMiddleware(
logging.NoOp,
&config.EndpointConfig{
ExtraConfig: map[string]interface{}{
plugin.Namespace: map[string]interface{}{
"name": []interface{}{
"lura-request-modifier-example-response",
},
},
},
},
)(bknd)

resp, err := p(context.Background(), &Request{Path: "/bar"})
if err != nil {
t.Error(err.Error())
}

if resp == nil {
t.Errorf("unexpected response: %v", resp)
return
}

if v, ok := resp.Data["foo"].(string); !ok || v != "bar" {
t.Errorf("unexpected foo value: %v", resp.Data["foo"])
}
}

func TestNewPluginMiddleware_error_request(t *testing.T) {
plugin.LoadWithLogger("./plugin/tests", ".so", plugin.RegisterModifier, logging.NoOp)

Expand Down

0 comments on commit 216025a

Please sign in to comment.