Search code examples
scalascalatestscalatra

Getting Session Value from ScalatraTest-ScalaTest


I am currently writing a number of Scalatra tests using the ScalaTest framework and the ScalatraSuite class.

  test("when i try to go to the base url it shold redirect me "){
    get("/") {
      status should be(302) 
    }
  }

The next step requires me to check the existance of some session values but it is unclear how to do this? Can anyone advise? I am creating a SessionAccess trait that, for the purposes of the test, overriding witha simple trait the stores session in an accessible HashMap but I am certain there is a simpler way?


Solution

  • I had a look at the code of ScalatraSuite and it looks like there is no way to retrieve the session object itself. You can however execute multiple calls inside of a session to check for the expected behaviour.

    If you had these calls:

    post("/start") {
      session("foo") = params("foo")
      // ...
    }
    
    get("/do_something") {
      session.get("foo")
    }
    

    you could test it like this:

    test("Whatever inside of a session") {
      session {
        post("/start", "foo" -> "bar") {
          // assert...
        }
        get("/do_something") {
          body should equal ("bar")
        }
      }
    }
    

    Hope that helps.