Skip to content
This repository has been archived by the owner on Sep 12, 2021. It is now read-only.

Commit

Permalink
Merge pull request #244 from akkie/master
Browse files Browse the repository at this point in the history
AnyContent extractors doesn't work as expected
  • Loading branch information
akkie committed Jan 4, 2015
2 parents efca960 + 8edc6be commit e96251e
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 56 deletions.
43 changes: 18 additions & 25 deletions app/com/mohiva/play/silhouette/api/util/RequestExtractor.scala
Original file line number Diff line number Diff line change
Expand Up @@ -98,29 +98,22 @@ trait LowPriorityRequestExtractors {
trait DefaultRequestExtractors extends LowPriorityRequestExtractors {

/**
* Tries to extract the value from query string and then from form url encoded body.
*/
implicit val anyContentAsFormUrlEncodedExtractor = new RequestExtractor[AnyContentAsFormUrlEncoded] {
def extractString(name: String)(implicit request: Request[AnyContentAsFormUrlEncoded]) = {
fromQueryString(name).orElse(fromFormUrlEncoded(name, request.body.data))
}
}

/**
* Tries to extract the value from query string and then from Json body.
*/
implicit val anyContentAsJsonExtractor = new RequestExtractor[AnyContentAsJson] {
def extractString(name: String)(implicit request: Request[AnyContentAsJson]) = {
fromQueryString(name).orElse(fromJson(name, request.body.json))
}
}

/**
* Tries to extract the value from query string and then from Xml body.
*/
implicit val anyContentAsXmlExtractor = new RequestExtractor[AnyContentAsXml] {
def extractString(name: String)(implicit request: Request[AnyContentAsXml]) = {
fromQueryString(name).orElse(fromXml(name, request.body.xml))
* Tries to extract the value from query string and then it tries to extract the value from any
* content.
*/
implicit val anyContentExtractor = new RequestExtractor[AnyContent] {
def extractString(name: String)(implicit request: Request[AnyContent]) = {
fromQueryString(name).orElse {
if (request.body.asFormUrlEncoded.isDefined) {
fromFormUrlEncoded(name, request.body.asFormUrlEncoded.get)
} else if (request.body.asJson.isDefined) {
fromJson(name, request.body.asJson.get)
} else if (request.body.asXml.isDefined) {
fromXml(name, request.body.asXml.get)
} else {
None
}
}
}
}

Expand Down Expand Up @@ -189,7 +182,7 @@ object ExtractableRequest {
}

/**
* Converts a `Request` to a `ExtractableRequest` instance.
* Converts a `Request` to an `ExtractableRequest` instance.
*
* @param request The request to convert.
* @param extractor The extractor to extract the value.
Expand All @@ -201,7 +194,7 @@ object ExtractableRequest {
}

/**
* Converts an implicit `Request` to a `ExtractableRequest` instance.
* Converts an implicit `Request` to an `ExtractableRequest` instance.
*
* @param request The request to convert.
* @param extractor The extractor to extract the value.
Expand Down
64 changes: 33 additions & 31 deletions test/com/mohiva/play/silhouette/api/util/RequestExtractorSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class RequestExtractorSpec extends PlaySpecification {
}
}

"The `anyContentAsEmptyExtractor`" should {
"The `anyContent`" should {
"extract a value from query string" in new Context {
implicit val request = FakeRequest("GET", "?code=value")

Expand All @@ -37,62 +37,38 @@ class RequestExtractorSpec extends PlaySpecification {

extract("code") must beNone
}
}

"The `anyContentAsFormUrlEncodedExtractor`" should {
"extract a value from query string" in new Context {
implicit val request = FakeRequest("GET", "?code=value").withFormUrlEncodedBody(("none", "value"))

extract("code") must beSome("value")
}

"extract a value from body" in new Context {
"extract a value from URL encoded body" in new Context {
implicit val request = FakeRequest().withFormUrlEncodedBody(("code", "value"))

extract("code") must beSome("value")
}

"return None if no value could be found in query string or body" in new Context {
"return None if no value could be found in query string or URL encoded body" in new Context {
implicit val request = FakeRequest().withFormUrlEncodedBody(("none", "value"))

extract("code") must beNone
}
}

"The `anyContentAsJsonExtractor`" should {
"extract a value from query string" in new Context {
implicit val request = FakeRequest("GET", "?code=value").withJsonBody(Json.obj("none" -> "value"))

extract("code") must beSome("value")
}

"extract a value from body" in new Context {
"extract a value from Json body" in new Context {
implicit val request = FakeRequest().withJsonBody(Json.obj("code" -> "value"))

extract("code") must beSome("value")
}

"return None if no value could be found in query string or body" in new Context {
"return None if no value could be found in query string or Json body" in new Context {
implicit val request = FakeRequest().withJsonBody(Json.obj("none" -> "value"))

extract("code") must beNone
}
}

"The `anyContentAsXmlExtractor`" should {
"extract a value from query string" in new Context {
implicit val request = FakeRequest("GET", "?code=value").withXmlBody(<none>value</none>)

extract("code") must beSome("value")
}

"extract a value from body" in new Context {
"extract a value from XML body" in new Context {
implicit val request = FakeRequest().withXmlBody(<code>value</code>)

extract("code") must beSome("value")
}

"return None if no value could be found in query string or body" in new Context {
"return None if no value could be found in query string or XML body" in new Context {
implicit val request = FakeRequest().withXmlBody(<none>value</none>)

extract("code") must beNone
Expand Down Expand Up @@ -159,6 +135,14 @@ class RequestExtractorSpec extends PlaySpecification {
}
}

"The `anyExtractor`" should {
"be executed for not supported body types" in new Context {
implicit val request = FakeRequest().withBody("text")

extract("code") must beNone
}
}

"An extractor" should {
"be overridden" in new Context {
import extractors._
Expand All @@ -169,6 +153,24 @@ class RequestExtractorSpec extends PlaySpecification {
}
}

"The `ExtractableRequest`" should {
"be converted from explicit request" in new Context {
def test[B](request: ExtractableRequest[B]) = request

val request = FakeRequest().withBody(<none>value</none>)

test(request) must beAnInstanceOf[ExtractableRequest[_]]
}

"be converted from implicit request" in new Context {
def test[B](implicit request: ExtractableRequest[B]) = request

implicit val request = FakeRequest().withBody(<none>value</none>)

test must beAnInstanceOf[ExtractableRequest[_]]
}
}

/**
* The context.
*/
Expand Down

0 comments on commit e96251e

Please sign in to comment.