Skip to content

Commit

Permalink
Merge pull request #514 from luraproject/fix_static_nil_data
Browse files Browse the repository at this point in the history
check if the data field is null before adding the static values
  • Loading branch information
kpacha committed Jul 20, 2021
2 parents 4022a00 + aa946d7 commit 4717518
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
2 changes: 2 additions & 0 deletions proxy/static.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ func NewStaticMiddleware(endpointConfig *config.EndpointConfig) Middleware {

if result == nil {
result = &Response{Data: map[string]interface{}{}}
} else if result.Data == nil {
result.Data = map[string]interface{}{}
}

for k, v := range cfg.Data {
Expand Down
41 changes: 40 additions & 1 deletion proxy/static_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package proxy
import (
"context"
"errors"
"reflect"
"testing"

"github.com/luraproject/lura/config"
Expand Down Expand Up @@ -103,6 +104,38 @@ type staticMatcherTestCase struct {
expected bool
}

func TestNewStaticMiddleware(t *testing.T) {
data := map[string]interface{}{
"new-1": true,
"new-2": map[string]interface{}{"k1": 42},
"new-3": "42",
}
extra := config.ExtraConfig{
Namespace: map[string]interface{}{
staticKey: map[string]interface{}{
"data": data,
"strategy": staticIfCompleteStrategy,
},
},
}

mw := NewStaticMiddleware(&config.EndpointConfig{ExtraConfig: extra})

p := mw(func(_ context.Context, r *Request) (*Response, error) {
return &Response{IsComplete: true}, nil
})

resp, err := p(context.Background(), nil)
if err != nil {
t.Error(err)
return
}

if !reflect.DeepEqual(data, resp.Data) {
t.Errorf("unexpected data: %+v", resp.Data)
}
}

func Test_staticAlwaysMatch(t *testing.T) {
extra := config.ExtraConfig{
Namespace: map[string]interface{}{
Expand Down Expand Up @@ -311,7 +344,13 @@ func Test_staticIfIncompleteMatch(t *testing.T) {

func testStaticMatcher(t *testing.T, marcher func(*Response, error) bool, testCase staticMatcherTestCase) {
if marcher(testCase.response, testCase.err) != testCase.expected {
t.Errorf("[%s] unexepecting match result (%v) with: %v, %v", testCase.name, testCase.expected, testCase.response, testCase.err)
t.Errorf(
"[%s] unexepecting match result (%v) with: %v, %v",
testCase.name,
testCase.expected,
testCase.response,
testCase.err,
)
}
}

Expand Down

0 comments on commit 4717518

Please sign in to comment.