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/09/18 21:32:17 UTC
[incubator-openwhisk] branch master updated: Display content-type
in __ow_headers for web actions (#2741)
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 93081fa Display content-type in __ow_headers for web actions (#2741)
93081fa is described below
commit 93081fa376ec4a0427c59648887fe53cf0051492
Author: James Dubee <jw...@us.ibm.com>
AuthorDate: Mon Sep 18 16:32:14 2017 -0500
Display content-type in __ow_headers for web actions (#2741)
---
.../scala/whisk/core/controller/WebActions.scala | 7 ++++-
.../whisk/core/cli/test/WskWebActionsTests.scala | 31 ++++++++++++++++++++++
.../core/controller/test/WebActionsApiTests.scala | 30 ++++++++++++---------
3 files changed, 55 insertions(+), 13 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 9544ef0..2e2ba84 100644
--- a/core/controller/src/main/scala/whisk/core/controller/WebActions.scala
+++ b/core/controller/src/main/scala/whisk/core/controller/WebActions.scala
@@ -371,13 +371,18 @@ trait WhiskWebActionsApi extends Directives with ValidateRequestSize with PostAc
`Access-Control-Allow-Methods`(OPTIONS, GET, DELETE, POST, PUT, HEAD, PATCH),
`Access-Control-Allow-Headers`(`Authorization`.name, `Content-Type`.name))
+ private def contentTypeFromEntity(entity: HttpEntity) = entity.contentType match {
+ case ct if ct == ContentTypes.NoContentType => None
+ case ct => Some(RawHeader(`Content-Type`.lowercaseName, ct.toString))
+ }
+
/** Extracts the HTTP method, headers, query params and unmatched (remaining) path. */
private val requestMethodParamsAndPath = {
extract { ctx =>
val method = ctx.request.method
val query = ctx.request.uri.query()
val path = ctx.unmatchedPath.toString
- val headers = ctx.request.headers
+ val headers = ctx.request.headers ++ contentTypeFromEntity(ctx.request.entity)
Context(webApiDirectives, method, headers, path, query)
}
}
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 3640ffb..5ff0c98 100644
--- a/tests/src/test/scala/whisk/core/cli/test/WskWebActionsTests.scala
+++ b/tests/src/test/scala/whisk/core/cli/test/WskWebActionsTests.scala
@@ -59,6 +59,37 @@ class WskWebActionsTests extends TestHelpers with WskTestHelpers with RestUtil w
behavior of "Wsk Web Actions"
+ it should "ensure __ow_headers contains the proper content-type" in withAssetCleaner(wskprops) { (wp, assetHelper) =>
+ val name = "webContenttype"
+ val file = Some(TestUtils.getTestActionFilename("echo.js"))
+ val bodyContent = JsObject("key" -> "value".toJson)
+ val host = getServiceURL()
+ val url = s"$host$testRoutePath/$namespace/default/$name.json"
+
+ assetHelper.withCleaner(wsk.action, name) { (action, _) =>
+ action.create(name, file, web = Some("true"))
+ }
+
+ val resWithContentType =
+ RestAssured.given().contentType("application/json").body(bodyContent.compactPrint).config(sslconfig).post(url)
+
+ resWithContentType.statusCode shouldBe 200
+ resWithContentType.header("Content-type") shouldBe "application/json"
+ resWithContentType.body.asString.parseJson.asJsObject
+ .fields("__ow_headers")
+ .asJsObject
+ .fields("content-type") shouldBe "application/json".toJson
+
+ val resWithoutContentType =
+ RestAssured.given().config(sslconfig).get(url)
+
+ resWithoutContentType.statusCode shouldBe 200
+ resWithoutContentType.header("Content-type") shouldBe "application/json"
+ resWithoutContentType.body.asString.parseJson.asJsObject
+ .fields("__ow_headers")
+ .toString should not include ("content-type")
+ }
+
/**
* Tests web actions, plus max url limit.
*/
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 250ccb9..d84e12c 100644
--- a/tests/src/test/scala/whisk/core/controller/test/WebActionsApiTests.scala
+++ b/tests/src/test/scala/whisk/core/controller/test/WebActionsApiTests.scala
@@ -482,7 +482,8 @@ trait WebActionsApiTests extends ControllerTestCommon with BeforeAndAfterEach wi
creds,
body = Some(content),
path = p,
- pkgName = "proxy"))
+ pkgName = "proxy",
+ headers = List(`Content-Type`(ContentTypes.`application/json`))))
}
}
}
@@ -505,7 +506,8 @@ trait WebActionsApiTests extends ControllerTestCommon with BeforeAndAfterEach wi
m.method.name.toLowerCase,
if (arg.nonEmpty && arg != "{}") JsObject(webApiDirectives.body -> arg.parseJson) else JsObject(),
creds,
- pkgName = "proxy"))
+ pkgName = "proxy",
+ headers = List(`Content-Type`(ContentTypes.`application/json`))))
}
}
}
@@ -531,7 +533,8 @@ trait WebActionsApiTests extends ControllerTestCommon with BeforeAndAfterEach wi
Map("a" -> "b", "c" -> "d").toJson.asJsObject,
creds,
body = Some(content),
- pkgName = "proxy"))
+ pkgName = "proxy",
+ headers = List(`Content-Type`(ContentTypes.`application/json`))))
}
}
}
@@ -1377,9 +1380,8 @@ trait WebActionsApiTests extends ControllerTestCommon with BeforeAndAfterEach wi
*
*/
- Post(s"$testRoutePath/$systemId/proxy/export_c.json", str) ~> addHeader(
- "Content-type",
- ContentTypes.`text/html(UTF-8)`.value) ~> Route.seal(routes(creds)) ~> check {
+ Post(s"$testRoutePath/$systemId/proxy/export_c.json", HttpEntity(ContentTypes.`text/html(UTF-8)`, str)) ~> Route
+ .seal(routes(creds)) ~> check {
status should be(OK)
val response = responseAs[JsObject]
response shouldBe JsObject(
@@ -1416,7 +1418,8 @@ trait WebActionsApiTests extends ControllerTestCommon with BeforeAndAfterEach wi
Post.method.name.toLowerCase,
Map("a" -> "b", "c" -> "d").toJson.asJsObject,
creds,
- pkgName = "proxy"))
+ pkgName = "proxy",
+ headers = List(`Content-Type`(ContentTypes.`application/json`))))
}
}
@@ -1552,7 +1555,8 @@ trait WebActionsApiTests extends ControllerTestCommon with BeforeAndAfterEach wi
JsObject("x" -> JsString("overriden"), "key2" -> JsString("value2")).compactPrint.getBytes
}.toJson).toJson.asJsObject,
creds,
- pkgName = "proxy"))
+ pkgName = "proxy",
+ headers = List(`Content-Type`(ContentTypes.`application/json`))))
}
}
@@ -1562,9 +1566,9 @@ trait WebActionsApiTests extends ControllerTestCommon with BeforeAndAfterEach wi
invocationsAllowed = 1
val queryString = "key1=value1&key2=value2"
- Post(s"$testRoutePath/$systemId/proxy/raw_export_c.json?$queryString", str) ~> addHeader(
- "Content-type",
- MediaTypes.`application/json`.value) ~> Route.seal(routes(creds)) ~> check {
+ Post(
+ s"$testRoutePath/$systemId/proxy/raw_export_c.json?$queryString",
+ HttpEntity(ContentTypes.`application/json`, str)) ~> Route.seal(routes(creds)) ~> check {
status should be(OK)
val response = responseAs[JsObject]
response shouldBe JsObject(
@@ -1572,7 +1576,9 @@ trait WebActionsApiTests extends ControllerTestCommon with BeforeAndAfterEach wi
"action" -> "raw_export_c".toJson,
"content" -> metaPayload(
Post.method.name.toLowerCase,
- Map(webApiDirectives.body -> str.toJson, webApiDirectives.query -> queryString.toJson).toJson.asJsObject,
+ Map(webApiDirectives.body -> Base64.getEncoder.encodeToString {
+ str.getBytes
+ }.toJson, webApiDirectives.query -> queryString.toJson).toJson.asJsObject,
creds,
pkgName = "proxy",
headers = List(`Content-Type`(ContentTypes.`application/json`))))
--
To stop receiving notification emails like this one, please contact
['"commits@openwhisk.apache.org" <co...@openwhisk.apache.org>'].