Rework API GW js func handling

Remove base64 encode/decede as it was not consistent.
Using built-in JSON marshling was the better solution.
This commit is contained in:
Tomaž Jerman
2021-10-01 10:08:58 +02:00
committed by Denis Arh
parent b342ec91d8
commit 2936fda461
4 changed files with 45 additions and 16 deletions
-9
View File
@@ -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")
}
+10 -7
View File
@@ -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)
}
@@ -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()
}
+14
View File
@@ -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