Add values to Session during testing (FakeRequest, FakeApplication)

前端 未结 2 483
执念已碎
执念已碎 2021-01-19 01:00

I\'m trying to build some tests around a method that reads some data from the session.

I tried extending FakeRequest and overriding the session

相关标签:
2条回答
  • 2021-01-19 01:19

    I used some examples from the pull request to build this in without using an updated version of play:

    Create cookie:

    val sessionCookie = Session.encodeAsCookie(Session(Map("key" -> "value")))
    

    Create and execute fakeRequest:

    val Some(result) = routeAndCall(FakeRequest(GET,"/").withHeaders(play.api.http.HeaderNames.COOKIE -> Cookies.encode(Seq(sessionCookie))))
    

    Then to get stuff out I created the following:

    Given the existing style of test methods:

    status(result) must equalTo(OK)
    

    I made:

    def sessionCookie(result: Result): Option[Cookie] = cookies(result).get("PLAY_SESSION")
    def session(result: Result): Session = Session.decodeFromCookie(sessionCookie(result))
    

    Which you call:

      session(result).get("key") must be("value")
    

    Have to say this is not tested in anger. But see if it helps.

    0 讨论(0)
  • 2021-01-19 01:35

    There is a pull request about this functionality

    See https://github.com/playframework/Play20/pull/103

    0 讨论(0)
提交回复
热议问题