Fixing typo (copy/paste error) and adding raw test case (#230)
* fixing typo and refactoring __ow_body
* adding test
* fixing space issues
diff --git a/knative-build/runtimes/javascript/platform/platform.js b/knative-build/runtimes/javascript/platform/platform.js
index 9307c84..acc73bb 100644
--- a/knative-build/runtimes/javascript/platform/platform.js
+++ b/knative-build/runtimes/javascript/platform/platform.js
@@ -34,7 +34,7 @@
var binary = (typeof env.__OW_ACTION_BINARY === 'undefined') ? false : env.__OW_ACTION_BINARY.toLowerCase() === "true";
// TODO: deault to empty?
var actionName = (typeof env.__OW_ACTION_NAME === 'undefined') ? "" : env.__OW_ACTION_NAME;
- var raw = (typeof env.__OW_ACTION_RAW === 'undefined') ? false : env.__OW_ACTION_BINARY.toLowerCase() === "true";
+ var raw = (typeof env.__OW_ACTION_RAW === 'undefined') ? false : env.__OW_ACTION_RAW.toLowerCase() === "true";
DEBUG.dumpObject(actionName, "Action name");
DEBUG.dumpObject(main, "Action main");
@@ -59,7 +59,7 @@
}
if (initdata.raw && typeof initdata.raw === 'boolean') {
// TODO: Throw error if RAW is not 'true' or 'false'
- binary = initdata.raw;
+ raw = initdata.raw;
}
}
@@ -130,10 +130,15 @@
try {
if (valueData.raw) {
- if (typeof body === "string" && body !== undefined) {
- valueData.__ow_body = body;
+ if (typeof req.body.value === "string" && req.body.value !== undefined) {
+ valueData.__ow_body = req.body.value;
} else {
- // TODO: delete main, binary, raw, and code from the body before sending it as action argument
+ const body = Object.assign({}, req.body.value);
+ // delete main, binary, raw, and code from the body before sending it as an action argument
+ delete body.main;
+ delete body.code;
+ delete body.binary;
+ delete body.raw;
var bodyStr = JSON.stringify(body);
var bodyBase64 = Buffer.from(bodyStr).toString("base64");
valueData.__ow_body = bodyBase64;
@@ -287,30 +292,30 @@
};
this.registerHandlers = function(app, platform) {
- var httpMethods = process.env.__OW_HTTP_METHODS;
- // default to "[post]" HTTP method if not defined
- if (typeof httpMethods === "undefined" || !Array.isArray(httpMethods)) {
- console.error("__OW_HTTP_METHODS is undefined; defaulting to '[post]' ...");
- httpMethods = [http_method.post];
+ var httpMethods = process.env.__OW_HTTP_METHODS;
+ // default to "[post]" HTTP method if not defined
+ if (typeof httpMethods === "undefined" || !Array.isArray(httpMethods)) {
+ console.error("__OW_HTTP_METHODS is undefined; defaulting to '[post]' ...");
+ httpMethods = [http_method.post];
+ }
+ httpMethods.forEach(function (method) {
+ switch (method.toUpperCase()) {
+ case http_method.get:
+ app.get('/', platform.run);
+ break;
+ case http_method.post:
+ app.post('/', platform.run);
+ break;
+ case http_method.put:
+ app.put('/', platform.run);
+ break;
+ case http_method.delete:
+ app.delete('/', platform.run);
+ break;
+ default:
+ console.error("Environment variable '__OW_HTTP_METHODS' has an unrecognized value (" + method + ").");
}
- httpMethods.forEach(function (method) {
- switch (method.toUpperCase()) {
- case http_method.get:
- app.get('/', platform.run);
- break;
- case http_method.post:
- app.post('/', platform.run);
- break;
- case http_method.put:
- app.put('/', platform.run);
- break;
- case http_method.delete:
- app.delete('/', platform.run);
- break;
- default:
- console.error("Environment variable '__OW_HTTP_METHODS' has an unrecognized value (" + method + ").");
- }
- });
+ });
}
};
diff --git a/knative-build/runtimes/javascript/tests/webactionraw/README.md b/knative-build/runtimes/javascript/tests/webactionraw/README.md
new file mode 100644
index 0000000..dbe1d95
--- /dev/null
+++ b/knative-build/runtimes/javascript/tests/webactionraw/README.md
@@ -0,0 +1,76 @@
+<!--
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements. See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+-->
+
+# Raw Web Action Test for OpenWhisk NodeJS Runtime using Knative
+
+## Running the test using the `curl` command
+
+Depending on the value you set in [buildtemplate.yaml](../../buildtemplate.yaml) for the ```OW_RUNTIME_PLATFORM``` parameter, you will need to invoke different endpoints to execute the test.
+
+### Running with OW_RUNTIME_PLATFORM set to "knative"
+
+#### Invoke / endpoint on the Service
+
+```
+curl -H "Host: nodejs-web-action-raw.default.example.com" -X POST http://localhost/
+{
+ "response":{
+ "__ow_body":"eyJuYW1lIjoiSm9lIn0=",
+ "__ow_query":{
+ },
+ "__ow_user":"",
+ "__ow_method":"POST",
+ "__ow_headers":{
+ "host":"localhost",
+ "user-agent":"curl/7.54.0",
+ "accept":"*/*",
+ "content-type":"application/json",
+ "content-length":"394"
+ },
+ "__ow_path":""
+ }
+}
+```
+
+### Running with OW_RUNTIME_PLATFORM set to "openwhisk"
+
+#### Initialize the runtime
+
+Initialize the runtime with the function and other configuration data using the ```/init``` endpoint.
+
+```
+curl -H "Host: nodejs-web-aciton-raw.default.example.com" -d "@data-init.json" -H "Content-Type: application/json" http://localhost/init
+
+{"OK":true}
+```
+
+#### Run the function
+
+Execute the function using the ```/run``` endpoint.
+
+```
+curl -H "Host: nodejs-web-action-raw.default.example.com" -d "@data-run.json" -H "Content-Type: application/json" -X POST http://localhost/run
+{
+ "response":{
+ "name":"Joe"
+ }
+}
+```
+
+**Note**: OpenWhisk controller plays an important role in handling web actions and that's why lacking `__ow_*` parameters from the response.
diff --git a/knative-build/runtimes/javascript/tests/webactionraw/build.yaml.tmpl b/knative-build/runtimes/javascript/tests/webactionraw/build.yaml.tmpl
new file mode 100644
index 0000000..b850d96
--- /dev/null
+++ b/knative-build/runtimes/javascript/tests/webactionraw/build.yaml.tmpl
@@ -0,0 +1,26 @@
+apiVersion: build.knative.dev/v1alpha1
+kind: Build
+metadata:
+ name: nodejs-10-web-action-raw
+spec:
+ serviceAccountName: openwhisk-runtime-builder
+ source:
+ git:
+ url: "https://github.com/apache/incubator-openwhisk-devtools.git"
+ revision: "master"
+ template:
+ name: openwhisk-nodejs-runtime
+ arguments:
+ - name: TARGET_IMAGE_NAME
+ value: "docker.io/${DOCKER_USERNAME}/nodejs-10-web-action-raw"
+ - name: DOCKERFILE
+ value: "./knative-build/runtimes/javascript/Dockerfile"
+ - name: OW_RUNTIME_DEBUG
+ value: "true"
+ - name: OW_ACTION_NAME
+ value: "nodejs-web-action-raw"
+ - name: OW_ACTION_CODE
+ value : "function main(params) { return { response: params }; }"
+ - name: OW_ACTION_RAW
+ value: "true"
+
diff --git a/knative-build/runtimes/javascript/tests/webactionraw/data-init-run.json b/knative-build/runtimes/javascript/tests/webactionraw/data-init-run.json
new file mode 100644
index 0000000..87082e5
--- /dev/null
+++ b/knative-build/runtimes/javascript/tests/webactionraw/data-init-run.json
@@ -0,0 +1,20 @@
+{
+ "init": {
+ "name" : "nodejs-web-action-raw",
+ "main" : "main",
+ "binary": false,
+ "code" : "function main(params) { return { response: params }; }",
+ "raw": true
+ },
+ "activation": {
+ "namespace": "default",
+ "action_name": "nodejs-web-action-raw",
+ "api_host": "",
+ "api_key": "",
+ "activation_id": "",
+ "deadline": "4102498800000"
+ },
+ "value": {
+ "name": "Joe"
+ }
+}
diff --git a/knative-build/runtimes/javascript/tests/webactionraw/data-init.json b/knative-build/runtimes/javascript/tests/webactionraw/data-init.json
new file mode 100644
index 0000000..09fcbd6
--- /dev/null
+++ b/knative-build/runtimes/javascript/tests/webactionraw/data-init.json
@@ -0,0 +1,8 @@
+{
+ "value": {
+ "name" : "nodejs-web-action-raw",
+ "main" : "main",
+ "binary": false,
+ "code" : "function main(params) { return { response: params }; }"
+ }
+}
diff --git a/knative-build/runtimes/javascript/tests/webactionraw/data-run.json b/knative-build/runtimes/javascript/tests/webactionraw/data-run.json
new file mode 100644
index 0000000..f020547
--- /dev/null
+++ b/knative-build/runtimes/javascript/tests/webactionraw/data-run.json
@@ -0,0 +1,11 @@
+{
+ "value": {
+ "name": "Joe"
+ },
+ "namespace": "default",
+ "action_name": "nodejs-web-action-raw",
+ "api_host": "",
+ "api_key": "",
+ "activation_id": "",
+ "deadline": "4102498800000"
+}
diff --git a/knative-build/runtimes/javascript/tests/webactionraw/payload-knative-init-run.http b/knative-build/runtimes/javascript/tests/webactionraw/payload-knative-init-run.http
new file mode 100644
index 0000000..b8c3bb7
--- /dev/null
+++ b/knative-build/runtimes/javascript/tests/webactionraw/payload-knative-init-run.http
@@ -0,0 +1,25 @@
+POST http://localhost:8080/ HTTP/1.1
+content-type: application/json
+
+{
+ "init": {
+ "name" : "nodejs-web-action-raw",
+ "main" : "main",
+ "binary": false,
+ "code" : "function main(params) { return { response: params }; }",
+ "raw": true
+ },
+ "activation": {
+ "namespace": "default",
+ "action_name": "nodejs-web-action-raw",
+ "api_host": "",
+ "api_key": "",
+ "activation_id": "",
+ "deadline": "4102498800000"
+ },
+ "value": {
+ "name": "Joe"
+ }
+}
+
+###
diff --git a/knative-build/runtimes/javascript/tests/webactionraw/payload-openwhisk-init.http b/knative-build/runtimes/javascript/tests/webactionraw/payload-openwhisk-init.http
new file mode 100644
index 0000000..bc64d95
--- /dev/null
+++ b/knative-build/runtimes/javascript/tests/webactionraw/payload-openwhisk-init.http
@@ -0,0 +1,13 @@
+POST http://localhost:8080/init HTTP/1.1
+content-type: application/json
+
+{
+ "value": {
+ "name" : "nodejs-web-action-raw",
+ "main" : "main",
+ "binary": false,
+ "code" : "function main(params) { return { response: params }; }"
+ }
+}
+
+###
diff --git a/knative-build/runtimes/javascript/tests/webactionraw/payload-openwhisk-run.http b/knative-build/runtimes/javascript/tests/webactionraw/payload-openwhisk-run.http
new file mode 100644
index 0000000..4baff6b
--- /dev/null
+++ b/knative-build/runtimes/javascript/tests/webactionraw/payload-openwhisk-run.http
@@ -0,0 +1,16 @@
+POST http://localhost:8080/run HTTP/1.1
+content-type: application/json
+
+{
+ "value": {
+ "name" : "Joe"
+ },
+ "namespace": "default",
+ "action_name": "nodejs-web-action-raw",
+ "api_host": "",
+ "api_key": "",
+ "activation_id": "",
+ "deadline": "4102498800000"
+}
+
+###
diff --git a/knative-build/runtimes/javascript/tests/webactionraw/service.yaml.tmpl b/knative-build/runtimes/javascript/tests/webactionraw/service.yaml.tmpl
new file mode 100644
index 0000000..73c9ffb
--- /dev/null
+++ b/knative-build/runtimes/javascript/tests/webactionraw/service.yaml.tmpl
@@ -0,0 +1,12 @@
+apiVersion: serving.knative.dev/v1alpha1
+kind: Service
+metadata:
+ name: nodejs-web-action-raw
+ namespace: default
+spec:
+ runLatest:
+ configuration:
+ revisionTemplate:
+ spec:
+ container:
+ image: docker.io/${DOCKER_USERNAME}/nodejs-10-web-action-raw