Skip to content

Commit

Permalink
Merge pull request #4723 from Oduig/fix-content-type-parsing
Browse files Browse the repository at this point in the history
Fixes #4719 - Content-type incorrectly parsed by play.api.test.ResultExtractors
  • Loading branch information
jroper committed Jun 22, 2015
2 parents 16c7db3 + 28c48ad commit 433593c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
Expand Up @@ -336,9 +336,11 @@ trait ResultExtractors {
/**
* Extracts the Charset of this Result value.
*/
def charset(of: Future[Result])(implicit timeout: Timeout): Option[String] = header(CONTENT_TYPE, of) match {
case Some(s) if s.contains("charset=") => Some(s.split("; charset=").drop(1).mkString.trim)
case _ => None
def charset(of: Future[Result])(implicit timeout: Timeout): Option[String] = {
val matcher = ".*;\\s*charset=(.+)$".r
header(CONTENT_TYPE, of) collect {
case matcher(charsetValue) => charsetValue
}
}

/**
Expand Down
Expand Up @@ -33,6 +33,22 @@ class HelpersSpec extends Specification {
}
}
}

"charset" should {

"extract a charset without whitespace from a Result as Some[String]" in {
charset(Future.successful(Ok.withHeaders(CONTENT_TYPE -> "text/html;charset=utf-8"))) must beSome("utf-8")
}

"extract a charset with whitespace from a Result as Some[String]" in {
charset(Future.successful(Ok.withHeaders(CONTENT_TYPE -> "text/html;\t charset=utf-8"))) must beSome("utf-8")
}

"extract a missing charset from a Result as None" in {
charset(Future.successful(Ok.withHeaders(CONTENT_TYPE -> "text/html"))) must beNone
}

}

"contentAsString" should {

Expand Down

0 comments on commit 433593c

Please sign in to comment.