score:11
Update : Like haui said in his comment :
Seems like they added something similar in play version 2.6. There you can use
import play.api.test.CSRFTokenHelper._ FakeRequest().withCSRFToken
(Scala) andCSRFTokenHelper.addCSRFToken(requestBuilder)
(Java) as explained in the Migration guide
For people who are still in 2.5.6, my answer still apply :
So, after looking in Play-scala classes for a certain time, I finally found a way to adapt this answer : https://stackoverflow.com/a/19849420/4496364 to Play 2.5.6
I even made a trait, so if someone need it one day, here it is :
import play.api.Application
import play.api.test.FakeRequest
import play.filters.csrf.CSRF.Token
import play.filters.csrf.{CSRFConfigProvider, CSRFFilter}
import scala.language.postfixOps
trait CSRFTest {
def addToken[T](fakeRequest: FakeRequest[T])(implicit app: Application) = {
val csrfConfig = app.injector.instanceOf[CSRFConfigProvider].get
val csrfFilter = app.injector.instanceOf[CSRFFilter]
val token = csrfFilter.tokenProvider.generateToken
fakeRequest.copyFakeRequest(tags = fakeRequest.tags ++ Map(
Token.NameRequestTag -> csrfConfig.tokenName,
Token.RequestTag -> token
)).withHeaders((csrfConfig.headerName, token))
}
}
To use it, simply extend your test class with it, like this :
class LoginSpec extends PlaySpec with OneAppPerSuite /* or whatever OneApp */ with CSRFTest
then, instead of calling
val fakeRequest = FakeRequest(/* params */)
simply call
val fakeRequest = addToken(FakeRequest(/* params */))
I tried to make it look like the addToken{} in the Controller :)
Source: stackoverflow.com
Related Query
- Testing request with CSRF Token in Play framework 2.5 (Scala)
- Testing scala Play (2.2.1) controllers with CSRF protection
- Scala : Template testing with Play framework
- Scala Play framework rest api add token from request to response
- Send POST request with a body using scala and play framework
- How to use IntelliJ with Play Framework and Scala
- Which is best data access options available for Play framework with Scala and PostgreSQL?
- Dynamic SQL Parameters with Anorm and Scala Play Framework
- How do I get Intellij IDEA 12.0 to work with Play Framework 2.1.0 app and Scala 2.10.0?
- Debug not working with play framework activator, scala and eclipse
- Composing `Future` result in Play Framework with Scala
- Play Framework 2.4 Scala missing parameter type for implicit request in Action after ActionRefiner
- Implicit Messages Provider instance was not found in Play framework with scala
- Chunked Response from an Iterator with Play Framework in Scala
- play framework 2: test a request with a json string as body
- Sending POST Request to Twitter API with Play Framework 2.0
- Unit testing file upload in a controller with Java Play Framework 2.3.x
- Is there a quick built in way to forward a request in the scala Play framework
- Scala unused import statement with play framework in IntelliJ
- Play Framework 2 Scala WS Doing a Sync Request
- Unit testing controllers in Play framework with SecureSocial
- How to create an instance of a model with the ebean framework and scala in Play 2.2
- Scala Play Framework Anorm SQL.on disable wrapping replacements with ' '
- Scala ActiveRecord performance issue when used with play framework
- Play framework with scala and GAE
- Play framework with Scala 3
- With Scala 2.10.2, SBT 0.13.0, Specs2 & Play Framework 2.2.1 how can I control logging whilst running tests?
- Does Play Framework with the Scala plugin support MVC and the Java API?
- configuring Play framework with Scala to use Neo4j graph db
- How do I set up Play Framework 2.0 in Netbeans with Scala IDE
More Query from same tag
- Play Framework: could not bind from form
- Make objects with different methods in Factory Pattern
- jar does not generate correct manifest file
- Can Flink be used with Kotlin?
- Idiomatic method to convert Array into ListBuffer
- play 2.3.6, java 1.8 how to pass session to scala templates
- A type constructor IS a monad or HAS a monad?
- Scala won't show up in new project - Intellig IDE
- ConcurrentModificationException when using Spark collectionAccumulator
- Spark select column based on row values
- Print gatling session map values on console
- How do you write to and read from an external process using scalaz streams
- How to compare the performance of two spark streaming jobs?
- How to filter array of objects by timestamp attribute (code alternative to SQL lag over partition by - command)
- Setup logging/config for production with deb file and SBT
- Calling R function with parameters from Scala using JVMR
- How do I extract values from a kafka row via spark under structured streaming?
- typed actors vs futures - how are they different?
- ForAll in scala check skips some input and do not respect containers size
- How to set Spark configuration properties using Apache Livy?
- wholeTextFiles Method is failing with ExitCode 52 java.lang.OutOfMemoryError
- Spark : ClassCastException while converting string into date
- Removing spaces from data in a column of dataframe in scala spark
- How to set up a Redshift connection with Slick?
- Spark: Rows to Columns (like transpose or pivot)
- What is the best way to perform OAuth2 authentication using akka-http?
- How to get the class of Option[_] on a None with Reflection
- How to free Redis Scala client allocated by RedisClientPool?
- Scala function that return another function which can internally hold data
- com.arangodb.velocypack.exception.VPackParserException: java.lang.InstantiationException