#Testing a path that is protected by session?

1 messages · Page 1 of 1 (latest)

random mural
#

Hello,

How I can test a path that requires authentication to get? And this authentication is a session in the cookies? (I can create the record of the session table, but how I should set the headers?)

Thanks,
Jaume.

dark terrace
#

You need to add a cookie to the request you're sending with the name vapor-session (unless you've changed it) and set the value to the session ID

random mural
#

hi, thanks, I just ended, calling to the login, and extracting the set-cookie I made a helper, and wala, there is my session with cookie, etc.

#

I should be able to change the test to request, right?

dark terrace
#

What do you mean change the test to request?

random mural
#

this ```
func loginWithUser(app: Application, user: String, password: String) async throws -> String {
let res = try app.sendRequest(.POST, "/login") { req in
try req.content.encode(["username": "[email protected]", "password": "admin"])
}
XCTAssertEqual(res.headers.contains(name: "set-cookie"), true)
let cookiesList: [String] = res.headers["set-cookie"]
let cookie : String = cookiesList[0]
XCTAssertContains(cookie, "vapor-session=")

return cookie

}