You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@openwhisk.apache.org by ra...@apache.org on 2017/08/17 02:12:31 UTC

[incubator-openwhisk] branch master updated: Fix illegal header warnings for web actions specifying content type (#2619)

This is an automated email from the ASF dual-hosted git repository.

rabbah pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-openwhisk.git


The following commit(s) were added to refs/heads/master by this push:
     new 4667e60  Fix illegal header warnings for web actions specifying content type (#2619)
4667e60 is described below

commit 4667e60ff7c3b263b2dd06a86158a37b10d81e5c
Author: James Dubee <jw...@us.ibm.com>
AuthorDate: Wed Aug 16 22:12:29 2017 -0400

    Fix illegal header warnings for web actions specifying content type (#2619)
---
 .../scala/whisk/core/controller/WebActions.scala   |  9 ++--
 tests/dat/actions/base64Web.js                     |  9 ++++
 .../whisk/core/cli/test/WskWebActionsTests.scala   | 63 ++++++++++++++--------
 .../core/controller/test/WebActionsApiTests.scala  |  1 -
 4 files changed, 55 insertions(+), 27 deletions(-)

diff --git a/core/controller/src/main/scala/whisk/core/controller/WebActions.scala b/core/controller/src/main/scala/whisk/core/controller/WebActions.scala
index c26d32b..4d5e006 100644
--- a/core/controller/src/main/scala/whisk/core/controller/WebActions.scala
+++ b/core/controller/src/main/scala/whisk/core/controller/WebActions.scala
@@ -243,7 +243,7 @@ protected[core] object WhiskWebActionsApi extends Directives {
                 case JsString(str) => interpretHttpResponse(code, headers, str, transid)
                 case js            => interpretHttpResponseAsJson(code, headers, js, transid)
             } getOrElse {
-                respondWithHeaders(headers) {
+                respondWithHeaders(removeContentTypeHeader(headers)) {
                     // note that if header defined a content-type, it will be ignored
                     // since the type must be compatible with the data response
                     complete(code, HttpEntity.Empty)
@@ -287,7 +287,7 @@ protected[core] object WhiskWebActionsApi extends Directives {
     private def interpretHttpResponseAsJson(code: StatusCode, headers: List[RawHeader], js: JsValue, transid: TransactionId) = {
         findContentTypeInHeader(headers, transid, `application/json`) match {
             case Success(mediaType) if (mediaType == `application/json`) =>
-                respondWithHeaders(headers) {
+                respondWithHeaders(removeContentTypeHeader(headers)) {
                     complete(code, js)
                 }
 
@@ -306,7 +306,7 @@ protected[core] object WhiskWebActionsApi extends Directives {
             }
         } match {
             case Success((mediaType, data: String)) =>
-                respondWithHeaders(headers) {
+                respondWithHeaders(removeContentTypeHeader(headers)) {
                     complete(code, HttpEntity(ContentType(MediaType.customWithFixedCharset(mediaType.mainType, mediaType.subType, HttpCharsets.`UTF-8`)), data))
                 }
 
@@ -317,6 +317,9 @@ protected[core] object WhiskWebActionsApi extends Directives {
                 terminate(BadRequest, Messages.httpContentTypeError)(transid)
         }
     }
+
+    private def removeContentTypeHeader(headers: List[RawHeader]) =
+        headers.filter(_.lowercaseName != `Content-Type`.lowercaseName)
 }
 
 trait WhiskWebActionsApi
diff --git a/tests/dat/actions/base64Web.js b/tests/dat/actions/base64Web.js
new file mode 100644
index 0000000..3e4773e
--- /dev/null
+++ b/tests/dat/actions/base64Web.js
@@ -0,0 +1,9 @@
+function main() {
+    return {
+        headers: {
+            "content-type": "application/json"
+        },
+        statusCode: 200,
+        body: new Buffer(JSON.stringify({'status': 'success'})).toString('base64')
+    }
+}
diff --git a/tests/src/test/scala/whisk/core/cli/test/WskWebActionsTests.scala b/tests/src/test/scala/whisk/core/cli/test/WskWebActionsTests.scala
index d6712d5..1c23f0a 100644
--- a/tests/src/test/scala/whisk/core/cli/test/WskWebActionsTests.scala
+++ b/tests/src/test/scala/whisk/core/cli/test/WskWebActionsTests.scala
@@ -86,8 +86,8 @@ class WskWebActionsTestsV2 extends WskWebActionsTests with BeforeAndAfterAll {
     "vanity subdomain" should "access a web action via namespace subdomain" in withAssetCleaner(wskPropsForSubdomainTest) {
         (wp, assetHelper) =>
             val actionName = "webaction"
-
             val file = Some(TestUtils.getTestActionFilename("echo.js"))
+
             assetHelper.withCleaner(wsk.action, actionName) {
                 (action, _) => action.create(actionName, file, web = Some(true.toString))(wp)
             }
@@ -145,14 +145,14 @@ trait WskWebActionsTests
         (wp, assetHelper) =>
             val name = "webaction"
             val file = Some(TestUtils.getTestActionFilename("echo.js"))
+            val host = getServiceURL()
+            val requestPath = host + s"$testRoutePath/$namespace/default/$name.text/a?a="
+            val padAmount = MAX_URL_LENGTH - requestPath.length
 
             assetHelper.withCleaner(wsk.action, name) {
                 (action, _) => action.create(name, file, web = Some("true"))
             }
 
-            val host = getServiceURL()
-            val requestPath = host + s"$testRoutePath/$namespace/default/webaction.text/a?a="
-            val padAmount = MAX_URL_LENGTH - requestPath.length
             Seq(("A", 200),
                 ("A" * padAmount, 200),
                 // ideally the bad case is just +1 but there's some differences
@@ -183,19 +183,18 @@ trait WskWebActionsTests
         (wp, assetHelper) =>
             val name = "webaction"
             val file = Some(TestUtils.getTestActionFilename("echo.js"))
+            val host = getServiceURL()
+            val url = if (testRoutePath == "/api/v1/experimental/web") {
+                s"$host$testRoutePath/$namespace/default/$name.text/__ow_meta_namespace"
+            } else {
+                s"$host$testRoutePath/$namespace/default/$name.text/__ow_user"
+            }
 
             assetHelper.withCleaner(wsk.action, name) {
                 (action, _) =>
                     action.create(name, file, web = Some("true"), annotations = Map("require-whisk-auth" -> true.toJson))
             }
 
-            val host = getServiceURL()
-            val url = if (testRoutePath == "/api/v1/experimental/web") {
-                s"$host$testRoutePath/$namespace/default/webaction.text/__ow_meta_namespace"
-            } else {
-                s"$host$testRoutePath/$namespace/default/webaction.text/__ow_user"
-            }
-
             val unauthorizedResponse = RestAssured.given().config(sslconfig).get(url)
             unauthorizedResponse.statusCode shouldBe 401
 
@@ -214,7 +213,7 @@ trait WskWebActionsTests
             val name = "webaction"
             val file = Some(TestUtils.getTestActionFilename("corsHeaderMod.js"))
             val host = getServiceURL()
-            val url = host + s"$testRoutePath/$namespace/default/webaction.http"
+            val url = host + s"$testRoutePath/$namespace/default/$name.http"
 
             assetHelper.withCleaner(wsk.action, name) {
                 (action, _) =>
@@ -236,7 +235,7 @@ trait WskWebActionsTests
             val name = "webaction"
             val file = Some(TestUtils.getTestActionFilename("corsHeaderMod.js"))
             val host = getServiceURL()
-            val url = host + s"$testRoutePath/$namespace/default/webaction"
+            val url = host + s"$testRoutePath/$namespace/default/$name"
 
             assetHelper.withCleaner(wsk.action, name) {
                 (action, _) => action.create(name, file, web = Some("true"))
@@ -261,16 +260,15 @@ trait WskWebActionsTests
             val name = "webaction"
             val file = Some(TestUtils.getTestActionFilename("echo.js"))
             val bodyContent = "This is the body"
-
-            assetHelper.withCleaner(wsk.action, name) {
-                (action, _) => action.create(name, file, web = Some("true"))
-            }
-
             val host = getServiceURL()
             val url = if (testRoutePath == "/api/v1/experimental/web") {
-                s"$host$testRoutePath/$namespace/default/webaction.text/__ow_meta_body"
+                s"$host$testRoutePath/$namespace/default/$name.text/__ow_meta_body"
             } else {
-                s"$host$testRoutePath/$namespace/default/webaction.text/__ow_body"
+                s"$host$testRoutePath/$namespace/default/$name.text/__ow_body"
+            }
+
+            assetHelper.withCleaner(wsk.action, name) {
+                (action, _) => action.create(name, file, web = Some("true"))
             }
 
             val paramRes = RestAssured.given().contentType("text/html").param("key", "value").config(sslconfig).post(url)
@@ -286,13 +284,13 @@ trait WskWebActionsTests
         (wp, assetHelper) =>
             val name = "webaction"
             val file = Some(TestUtils.getTestActionFilename("textBody.js"))
+            val host = getServiceURL()
+            val url = host + s"$testRoutePath/$namespace/default/$name.http"
 
             assetHelper.withCleaner(wsk.action, name) {
                 (action, _) => action.create(name, file, web = Some("true"))
             }
 
-            val host = getServiceURL()
-            val url = host + s"$testRoutePath/$namespace/default/webaction.http"
             val response = RestAssured.given().header("accept", "application/json").config(sslconfig).get(url)
             response.statusCode shouldBe 406
             response.body.asString should include("Resource representation is only available with these types:\\ntext/html")
@@ -303,7 +301,7 @@ trait WskWebActionsTests
             val name = "webaction"
             val file = Some(TestUtils.getTestActionFilename("multipleHeaders.js"))
             val host = getServiceURL()
-            val url = host + s"$testRoutePath/$namespace/default/webaction.http"
+            val url = host + s"$testRoutePath/$namespace/default/$name.http"
 
             assetHelper.withCleaner(wsk.action, name) {
                 (action, _) =>
@@ -319,4 +317,23 @@ trait WskWebActionsTests
                 new Header("Set-Cookie", "c=d")
             )
     }
+
+    it should "handle http web action with base64 encoded response" in withAssetCleaner(wskprops) {
+        (wp, assetHelper) =>
+            val name = "base64Web"
+            val file = Some(TestUtils.getTestActionFilename("base64Web.js"))
+            val host = getServiceURL
+            val url = host + s"$testRoutePath/$namespace/default/$name.http"
+
+            assetHelper.withCleaner(wsk.action, name) {
+                (action, _) =>
+                    action.create(name, file, web = Some("raw"))
+            }
+
+            val response = RestAssured.given().config(sslconfig).get(url)
+
+            response.statusCode shouldBe 200
+            response.header("Content-type") shouldBe "application/json"
+            response.body.asString.parseJson.asJsObject shouldBe JsObject("status" -> "success".toJson)
+    }
 }
diff --git a/tests/src/test/scala/whisk/core/controller/test/WebActionsApiTests.scala b/tests/src/test/scala/whisk/core/controller/test/WebActionsApiTests.scala
index 6cd92e4..d23ebc1 100644
--- a/tests/src/test/scala/whisk/core/controller/test/WebActionsApiTests.scala
+++ b/tests/src/test/scala/whisk/core/controller/test/WebActionsApiTests.scala
@@ -788,7 +788,6 @@ trait WebActionsApiTests extends ControllerTestCommon with BeforeAndAfterEach wi
 
                         m(s"$testRoutePath/$path") ~> Route.seal(routes(creds)) ~> check {
                             status should be(OK)
-                            header("content-type").get.toString shouldBe "content-type: application/json"
                             responseAs[JsObject] shouldBe JsObject("field" -> "value".toJson)
                         }
                     }

-- 
To stop receiving notification emails like this one, please contact
['"commits@openwhisk.apache.org" <co...@openwhisk.apache.org>'].