Skip to content
This repository has been archived by the owner on Feb 15, 2019. It is now read-only.

Commit

Permalink
Fixes playframework#4719 - Content-type incorrectly parsed by play.ap…
Browse files Browse the repository at this point in the history
…i.test.ResultExtractors
  • Loading branch information
Guido committed Jun 20, 2015
1 parent 16c7db3 commit 3679c39
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 3679c39

Please sign in to comment.