diff --git a/pkg/apigw/filter/processer.go b/pkg/apigw/filter/processer.go index 00446bb3f..bb7c3bdd7 100644 --- a/pkg/apigw/filter/processer.go +++ b/pkg/apigw/filter/processer.go @@ -3,7 +3,6 @@ package filter import ( "bytes" "context" - "encoding/base64" "encoding/json" "errors" "fmt" @@ -208,14 +207,6 @@ func (h *processerPayload) Merge(params []byte) (types.Handler, error) { return nil, err } - fn, err := base64.StdEncoding.DecodeString(h.params.Func) - - if err != nil { - return nil, fmt.Errorf("could not decode js func: %s", err) - } - - h.params.Func = string(fn) - if h.params.Func == "" { return nil, errors.New("could not register function, body empty") } diff --git a/pkg/apigw/filter/processer_test.go b/pkg/apigw/filter/processer_test.go index ec86d77e9..2b3f73925 100644 --- a/pkg/apigw/filter/processer_test.go +++ b/pkg/apigw/filter/processer_test.go @@ -2,8 +2,7 @@ package filter import ( "context" - "encoding/base64" - "fmt" + "encoding/json" "io/ioutil" "net/http" "net/http/httptest" @@ -38,7 +37,7 @@ func Test_processerPayload(t *testing.T) { Body: ioutil.NopCloser(strings.NewReader(`[1,2,3]`)), }, exp: "2\n", - params: prepareFuncPayload(` + params: prepareFuncPayload(t, ` var b = JSON.parse(readRequestBody(input.Get('request').Body)); return b[1]; `), @@ -50,7 +49,7 @@ func Test_processerPayload(t *testing.T) { Body: ioutil.NopCloser(strings.NewReader(`[{"name":"johnny", "surname":"mnemonic"},{"name":"johnny", "surname":"knoxville"}]`)), }, exp: "{\"count\":2,\"results\":[{\"fullname\":\"Johnny Mnemonic\"},{\"fullname\":\"Johnny Knoxville\"}]}\n", - params: prepareFuncPayload(` + params: prepareFuncPayload(t, ` var b = JSON.parse(readRequestBody(input.Get('request').Body)); return { @@ -70,7 +69,7 @@ func Test_processerPayload(t *testing.T) { Method: "POST", Body: ioutil.NopCloser(strings.NewReader(`[{"name":"johnny", "surname":"mnemonic"},{"name":"johnny", "surname":"knoxville"}]`)), }, - params: prepareFuncPayload(``), + params: prepareFuncPayload(t, ``), errv: `could not register function, body empty`, }, } @@ -112,6 +111,10 @@ func Test_processerPayload(t *testing.T) { } } -func prepareFuncPayload(s string) string { - return fmt.Sprintf(`{"jsfunc": "%s"}`, base64.StdEncoding.EncodeToString([]byte(s))) +func prepareFuncPayload(t *testing.T, s string) string { + aux, err := json.Marshal(map[string]string{"jsfunc": s}) + if err != nil { + t.Error(err) + } + return string(aux) } diff --git a/tests/apigw/processing_payload_simple_test.go b/tests/apigw/processing_payload_simple_test.go new file mode 100644 index 000000000..eca09ea04 --- /dev/null +++ b/tests/apigw/processing_payload_simple_test.go @@ -0,0 +1,21 @@ +package apigw + +import ( + "net/http" + "testing" +) + +func Test_processor_payload_simple(t *testing.T) { + var ( + _, h, _ = setupScenario(t) + ) + + h.apiInit(). + Get("/test"). + Header("Accept", "application/json"). + Expect(t). + Status(http.StatusOK). + Body("60"). + End() + +} diff --git a/tests/apigw/testdata/processor_payload_simple/def.yaml b/tests/apigw/testdata/processor_payload_simple/def.yaml new file mode 100644 index 000000000..a89e7c4e5 --- /dev/null +++ b/tests/apigw/testdata/processor_payload_simple/def.yaml @@ -0,0 +1,14 @@ +apigateway: + - endpoint: /test + method: GET + enabled: true + filters: + - ref: "payload" + kind: "processer" + params: + jsfunc: | + const x = 10; + const y = 20; + const z = x + y; + + return x + y + z